import {login, logout, getInfo} from '@/api/login'
import {getToken, setToken, removeToken} from '@/utils/auth'

const user = {
    state: {
        token: getToken(),
        studentName: '',
        studentId: '',
        idCard: '',
        phoneNumber: '',
        photo: '',
        photoData: '',
        temPhotoData: '',
        active: 0,
    },

    mutations: {
        SET_TOKEN: (state, token) => {
            state.token = token
        },
        SET_NAME: (state, studentName) => {
            state.studentName = studentName
        },
        SET_ID: (state, studentId) => {
            state.studentId = studentId
        },
        SET_IDCARD: (state, idCard) => {
            state.idCard = idCard
        },
        SET_PHONENUMBER: (state, phoneNumber) => {
            state.phoneNumber = phoneNumber
        },
        SET_PHOTO: (state, photo) => {
            state.photo = photo
        },
        SET_PHOTODATA: (state, photoData) => {
            state.photoData = photoData
        },
        SET_TEMPHOTODATA: (state, temPhotoData) => {
            state.temPhotoData = temPhotoData
        },
        SET_ACTIVE: (state, active) => {
            state.active = Number(active)
            console.log('SET_ACTIVE', state.active)
        },

    },

    actions: {

        // 登录
        Login({commit}, userInfo) {
            const idCard = userInfo.idCard
            const phoneNumber = userInfo.phoneNumber
            const password = userInfo.password
            return new Promise((resolve, reject) => {
                login(idCard, phoneNumber, password).then(res => {
                    setToken(res.token)
                    commit('SET_TOKEN', res.token)
                    resolve()
                }).catch(error => {
                    reject(error)
                })
            })
        },

        // 获取用户信息
        GetInfo({commit}, state) {
            return new Promise((resolve, reject) => {
                getInfo().then(res => {
                    // console.log(getInfo)
                    const user = res.info
                    commit('SET_NAME', user.studentName)
                    commit('SET_IDCARD', user.idCard)
                    commit('SET_PHONENUMBER', user.phoneNumber)
                    commit('SET_PHOTO', user.photo)
                    commit('SET_PHOTODATA', user.photoData)
                    commit('SET_ID', user.id)
                    resolve(res)
                }).catch(error => {
                    reject(error)
                })
            })
        },

        // 退出系统
        LogOut({commit}, state) {
            return new Promise((resolve, reject) => {
                logout().then(() => {
                    commit('SET_TOKEN', '')
                    commit('SET_NAME', '')
                    commit('studentId', '')
                    commit('idCard', '')
                    commit('phoneNumber', '')
                    commit('photo', '')
                    commit('photoData', '')
                    removeToken()
                    resolve()
                }).catch(error => {
                    removeToken()
                    reject(error)
                })
            })
        },

        // 前端 登出
        FedLogOut({commit}) {
            return new Promise(resolve => {
                commit('SET_TOKEN', '')
                removeToken()
                resolve()
            })
        }
    }
}

export default user
