import {
    createStoreBindings
} from 'mobx-miniprogram-bindings'
import {
    store
} from '~/store/index'
import {
    setUserInfo,
    exchangePhone,
    getMyInfo,
    bindPhone
} from '~/api/user'
import event from '~/mixins/event'
let storeBindings
Page({
    behaviors: [event],
    data: {
        gradeIndex: 0,
        gradeArray: [{
                value: 'PRESCHOOL',
                key: '学前班'
            }, {
                value: 'PRIMARY_FIRST_GRADE',
                key: '一年级'
            },
            {
                value: 'PRIMARY_SECOND_GRADE',
                key: '二年级'
            },
            {
                value: 'PRIMARY_THREE_GRADE',
                key: '三年级'
            },
            {
                value: 'PRIMARY_SENIOR_GRADE',
                key: '四年级'
            }, {
                value: 'PRIMARY_FIVE_GRADE',
                key: '五年级'
            }, {
                value: 'PRIMARY_SIX_GRADE',
                key: '六年级'
            },
        ],
    },
    onLoad(options) {
        // 手工绑定 
        this.storeBindings = createStoreBindings(this, {
            store,
            fields: {
                userInfo: 'userInfo'
            },
            actions: {
                setUser: 'setUser'
            }
        })
        // 立刻更新
        this.storeBindings.updateStoreBindings()
        let {
            grade
        } = this.data.userInfo
        let gradeIndex = this.data.gradeArray.findIndex(item => {
            return item.value == grade
        })
        this.setData({
            gradeIndex
        })

    },
    // 调用清理函数
    onUnload() {
        this.storeBindings.destroyStoreBindings()
    },
    changeAvatar(e) {
        const {
            avatarUrl
        } = e.detail
        wx.uploadFile({
            url: 'https://reader-api.ai160.com/file/upload',
            filePath: avatarUrl,
            name: '头像',
            header: {
                uid: wx.getStorageSync('uid')
            },
            success: (res) => {
                const newAvatar = JSON.parse(res.data).data;
                const str = 'userInfo.avatar'
                this.setData({
                    [str]: newAvatar
                })
                this.setUserInfo({
                    avatar: newAvatar
                })
            }
        })
    },
    saveNickName(e) {
        let nickName = e.detail.value;
        if (nickName == this.data.userInfo.nickName) {
            return
        } else if (!nickName) {
            return this.setData({
                ['userInfo.nickName']: this.data.userInfo.nickName
            })
        }
        this.setUserInfo({
            nickName
        })
    },
    selectProfession() {
        wx.showActionSheet({
            itemList: ['学生', '家长', '老师'],
            success: (res) => {
                if (['学生', '家长', '老师'][res.tapIndex] == this.data.userInfo.profession) {
                    return
                }
                this.setUserInfo({
                    profession: ['学生', '家长', '老师'][res.tapIndex]
                })
            },
        })
    },
    selectGender() {
        wx.showActionSheet({
            itemList: ['女', '男'],
            success: (res) => {
                if (res.tapIndex == this.data.userInfo.gender) {
                    return
                }
                this.setUserInfo({
                    gender: res.tapIndex
                })
            },
        })
    },
    bindDateChange(e) {
        this.setUserInfo({
            birthday: e.detail.value
        })
    },
    bindGradeChange(e) {
        let grade = this.data.gradeArray[e.detail.value].value
        this.setUserInfo({
            grade
        })
    },
    saveSchool(e) {
        let schoolName = e.detail.value;
        if (schoolName == this.data.userInfo.schoolName) {
            return
        }
        this.setUserInfo({
            schoolName
        })
    },
    async setUserInfo(data) {
        wx.showLoading({
            title: '提交中',
        })
        let res = await setUserInfo(data, 'put').finally(() => {
            wx.hideLoading()
        })
        this.setUser(res)
    },
    async getPhoneNumber({
        detail
    }) {
        let mobile = await exchangePhone({
            code: detail.code
        })
        await bindPhone({
            mobile
        })
        let userInfo = await getMyInfo()
        this.setUser(userInfo.user)
        wx.showToast({
            title: '绑定成功！',
            icon: "none",
            duration: 4000
        })
    },
})