import {
    findAuth,
    cheakValue,
    insure
} from '@/api/auth.js'
import {
    getStore,
    setStore
} from '@/untils/store.js'
import {
    realName
} from '@/api/auth.js'

const convert = (v) => {
    return v === '1'
}

const auth = {
    state: {
        laborState: getStore({
            name: 'laborState'
        }) || false,
        realNameState: getStore({
            name: 'realNameState'
        }) || false,
        bankCardState: getStore({
            name: 'bankCardState'
        }) || false,
        insureState: getStore({
            name: 'insureState'
        }) || false,
        authInfo: getStore({
            name: 'authInfo'
        }) || [],
        loginType: 0, // 0
    },
    actions: {
        //刷新token
        refreshAuthState({
            commit,
            dispatch
        }) {
            // authValue 身份信息|实名认证|银行卡认证|社保信息
            return new Promise((resolve, reject) => {
                findAuth().then((res) => {
                    const data = res.data.data;
                    const authValue = data.authValue
                    commit('SET_AUTH_LABOR', convert(authValue[0]))
                    commit('SET_AUTH_REALNAME', convert(authValue[1]))
                    commit('SET_AUTH_BANKCARD', convert(authValue[2]))
                    commit('SET_AUTH_INSURE', convert(authValue[3]))
                    commit('SET_AUTH_INFO', data)
                    // dispatch('getVipCode')

                    resolve()
                }).catch(reject)
            })
        },
        authRealName({
            commit,
            dispatch
        }, {
            name,
            idNumber
        }) {
            return new Promise((resolve, reject) => {
                realName(name, idNumber).then(() => {
                    commit('SET_AUTH_REALNAME', true)
                    commit('UPDATE_AUTH_REALNAME', {
                        realName: name,
                        idNumber: idNumber
                    })
                    // dispatch('getVipCode')
                    resolve()
                }).catch(reject)
            })
        },
        authLabor({
            commit,
            dispatch
        }) {
            return new Promise((resolve, reject) => {
                cheakValue(1).then(resp => {
                    commit('SET_AUTH_LABOR', true)
                    resolve()
                }).catch(reject)
            })
        },
        authInsure({
            commit,
            dispatch
        }, data) {
            return new Promise((resolve, reject) => {
                insure(data).then(resp => {
                    commit('SET_AUTH_INSURE', true)
                    commit('UPDATE_AUTH_INSURE_TYPE', data)
                    resolve()
                }).catch(reject)
            })
        },
        clearAuthState({
            commit
        }) {
            commit('SET_AUTH_LABOR', '')
            commit('SET_AUTH_REALNAME', '')
            commit('SET_AUTH_BANKCARD', '')
            commit('SET_AUTH_INSURE', '')
            commit('SET_AUTH_INFO', '')
        }
    },
    mutations: {
        changeLoginType(state, payload) {
            state.loginType = payload
        },
        SET_AUTH_LABOR: (state, status) => {
            state.laborState = status
            setStore({
                name: 'laborState',
                content: state.laborState
            })
        },
        SET_AUTH_REALNAME: (state, status) => {
            state.realNameState = status
            setStore({
                name: 'realNameState',
                content: state.realNameState
            })
        },
        SET_AUTH_BANKCARD: (state, status) => {
            state.bankCardState = status
            setStore({
                name: 'bankCardState',
                content: state.bankCardState
            })
        },
        SET_AUTH_INSURE: (state, status) => {
            state.insureState = status
            setStore({
                name: 'insureState',
                content: state.insureState
            })
        },
        SET_AUTH_INFO: (state, data) => {
            state.authInfo = data
            setStore({
                name: 'authInfo',
                content: state.authInfo
            })
        },
        UPDATE_AUTH_INSURE_TYPE: (state, type) => {
            state.authInfo.bakValue = type
            setStore({
                name: 'authInfo',
                content: state.authInfo
            })
        },
        UPDATE_AUTH_REALNAME: (state, {
            realName,
            idNumber
        }) => {
            state.authInfo.realName = realName
            state.authInfo.idNumber = idNumber
            setStore({
                name: 'authInfo',
                content: state.authInfo
            })
        }
    }
}
export default auth