// pages/index/mineinfo.js
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        sourceUrl: app.baseUrls.testUrl + app.baseUrls.baseImgUrl,
        personInfo: {
            // birthDate: '2021-01-01',
            gender: '46bec29f-2368-44f7-98d0-b33246e5e16d',
            cardType: 'b7d74045-d984-4822-b0f3-711b15b933ca',
            fullName: '',
            idCardNumber: '',
            education: '',
            telephone: '',
            userPhotoId: '',
            idCardFront: '',
            idCardBack: '',
            educationPhoto: ''
        },
        sexArray: ['男','女'],
        sexIdx: 0,
        cultrueAll: [],
        cultrueList: [],
        cultrueIdx: 0,
        cultrueId: '',
        avatar: '',
        bindPhone: '', // 输入绑定的手机号
        isBindPhone: false, // 是否绑定了手机号
    },
    // 获取个人信息
    getPersonInfo: function () {
        var self = this
        app.restAjax.get(app.restAjax.path(app.apis.gerPersonInfo, [app.baseUrls.testUrl]), {}, {
            headers: {
                token: wx.getStorageSync('token')
            }
        },function (code, data) {
            console.log(data)
            var sex
            if (data.gender == '46bec29f-2368-44f7-98d0-b33246e5e16d') {
                sex = 0
            } else {
                sex = 1
            }
            self.setData({
                personInfo: data,
                sexIdx: sex
            })
            for (let i = 0; i < self.data.cultrueAll.length; i++) {
                if (self.data.cultrueAll[i].dataId == data.education) {
                    self.setData({
                        cultrueIdx: i
                    })
                }
            }
        },
        function (code, err) {
            console.log(err)
        })
    },
    // 选择生日
    bindDateChange: function (e) {
        var self = this
        self.setData({
            ['personInfo.birthDate']: e.detail.value
        })
    },
    // 选择性别
    bindSexPickerChange: function (e) {
        var self = this
        var sexId
        if (e.detail.value == '0') {
            sexId = '46bec29f-2368-44f7-98d0-b33246e5e16d'
        } else {
            sexId = '4a5f549a-d339-4403-8d26-9058e80499b1'
        }
        self.setData({
            sexIdx: e.detail.value,
            ['personInfo.gender']: sexId
        })
    },
    // 获取文化程度列表
    getCulture: function () {
        var self = this
        app.restAjax.get(app.restAjax.path(app.apis.getCultrue, [app.baseUrls.testUrl]), {}, {
          headers: {
            token: wx.getStorageSync('token')
          }
        },
        function (code, data) {
          var result = ['请选择文化程度']
          for (let i = 0; i < data.length; i++) {
            result.push(data[i].dataName)
          }
          self.setData({
            cultrueAll: data,
            cultrueList: result
          })
          self.getPersonInfo()
        },
        function (code, err) {
          console.log(err)
        })
    },
    // 选择文化程度
    bindCultruePickerChange: function (e) {
        var self = this
        self.setData({
          cultrueIdx: e.detail.value,
          cultrueId: self.data.cultrueAll[e.detail.value].dataId,
          ['personInfo.education']: self.data.cultrueAll[e.detail.value].dataId
        })
    },
    // 上传图片
    uploadImg: function (e) {
        var self = this
        var from = e.currentTarget.dataset.type
        wx.chooseImage({
            count: 1,
            sizeType: ['original', 'compressed'],
            sourceType: ['album', 'camera'],
            success (res) {
                app.restAjax.file(app.restAjax.path(app.apis.doUploadImg, [app.baseUrls.testUrl]), res.tempFilePaths[0],'image', {
                    headers: {
                      token: wx.getStorageSync('token')
                    }
                  },
                  function (code, data) {
                    var result = JSON.parse(data).data
                    if (from == 'avatar') {
                        self.setData({
                            ['personInfo.userPhotoId']: result
                        })
                    } else if (from == 'front') {
                        self.setData({
                            ['personInfo.idCardFront']: result
                        })
                    } else if (from == 'back') {
                        self.setData({
                            ['personInfo.idCardBack']: result
                        })
                    } else {
                        self.setData({
                            ['personInfo.educationPhoto']: result
                        })
                    }
                  },
                  function (code, err) {
                    console.log(err)
                  })
            }
        })
    },
    // 保存个人信息
    savePersonInfo: function () {
        var self = this
        console.log(self.data.personInfo)
        if (!self.data.personInfo.fullName) {
            wx.showToast({
              title: '姓名不能为空',
              icon: 'error'
            })
            return
        }
        if (!self.data.personInfo.idCardNumber) {
            wx.showToast({
              title: '证件号不能为空',
              icon: 'error'
            })
            return
        }
        if (!self.data.personInfo.education) {
            wx.showToast({
              title: '请选择文化程度',
              icon: 'error'
            })
            return
        }
        if (!self.data.personInfo.telephone) {
            wx.showToast({
              title: '电话号不能为空',
              icon: 'error'
            })
            return
        }
        if (!self.data.personInfo.userPhotoId) {
            wx.showToast({
              title: '请上传头像',
              icon: 'error'
            })
            return
        }
        if (!self.data.personInfo.idCardFront) {
            wx.showToast({
              title: '请上传证件正面',
              icon: 'error'
            })
            return
        }
        if (!self.data.personInfo.idCardBack) {
            wx.showToast({
              title: '请上传证件反面',
              icon: 'error'
            })
            return
        }
        if (!self.data.personInfo.educationPhoto) {
            wx.showToast({
              title: '请上传学历证书',
              icon: 'error'
            })
            return
        }
        app.restAjax.post(app.restAjax.path(app.apis.savePersonInfo, [app.baseUrls.testUrl]), self.data.personInfo, {
            headers: {
              token: wx.getStorageSync('token')
            }
          },
          function (code, data) {
            if (code == '200') {
                wx.showToast({
                  title: '保存成功！',
                  icon: 'success'
                })
                setTimeout(function () {
                    wx.reLaunch({
                      url: '../index/index',
                    })
                }, 2000)
            }
          },
          function (code, err) {
            console.log(err)
          })
    },
    // 输入信息
    editInfo: function (e) {
        var from = e.currentTarget.dataset.type
        var info
        if (from == 'name') {
            info = 'personInfo.fullName'
        } else if (from == 'card') {
            info = 'personInfo.idCardNumber'
        } else if (from == 'telephone') {
            info = 'personInfo.telephone'
        }
        this.setData({
            [info]: e.detail.value
        })
    },
    // 检查是否绑定手机号
    checkPhone: function () {
        var self = this
        app.restAjax.post(app.restAjax.path(app.apis.checkPhone, [app.baseUrls.testUrl]), {}, {
            headers: {
                token: wx.getStorageSync('token')
            }
            },
            function (code, data) {
                console.log(data)
                self.setData({
                    isBindPhone: data.data
                })
            },
            function (code, err) {
                console.log(err)
            }
        )
    },
    // 绑定手机
    doBindPhone: function () {
        var self = this
        app.restAjax.post(app.restAjax.path(app.apis.bindPhone, [app.baseUrls.testUrl, self.data.bindPhone]), {}, {
            headers: {
                token: wx.getStorageSync('token')
            }
            },
            function (code, data) {
                if (code == '200') {
                    wx.showToast({
                      title: '绑定成功！',
                      icon: 'success'
                    })
                    setTimeout(function () {
                        self.setData({
                            isBindPhone: true
                        })
                    }, 1500)
                    self.doLogin()
                }
            },
            function (code, err) {
                console.log(err)
            }
        )
    },
    // 登录
    doLogin: function () {
        wx.login({
            success: function (res) {
                app.restAjax.post(app.restAjax.path(app.apis.login, [app.baseUrls.testUrl]), {
                    jsCode: res.code
                }, {},
                function (code, data) {
                    var token = data.data
                    wx.setStorage({
                        key: 'token',
                        data: token
                    })
                },
                function (code, err) {
                    console.log(err)
                })
            }
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.checkPhone()
        this.getCulture()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})