// package_mine/pages/approveId/index.js
import {approveIdenty} from '../../../api/mine.js'
import {isPositiveInteger} from '../../../utils/check.js'

Page({

    /**
     * 页面的初始数据
     */
    data: {
        checkAgree: false,
        form: {},
        // 表单验证
        formRules: {
            applyName: {
                validator: function (value) {
                  return value;
                },
                warning: false
            },
            applyPhone: {
                validator: function (value) {
                  return value && isPositiveInteger(value);
                },
                warning: false
            }
        },
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        if(options.data) {
            const params = JSON.parse(options.data)
            const applyOpenid = wx.getStorageSync('openid')
            this.setData({
                form: {
                    applyOpenid,
                    ...params
                }
            })
        }
    },
    // 输入框输入
    bindValue(e) {
        let name = e.currentTarget.dataset.name;
        let form = this.data.form;
        form[name] = e.detail.value;
        this.setData({
          form,
        })
        this.validate(name, e.detail.value)
    },
    // 表项验证
    validate(name) {
        if(name !== 'applyRemark') {
            let formRules = this.data.formRules;
            let validator = formRules[name].validator
            let result = validator ? !validator(this.data.form[name]) : false;
            formRules[name].warning = result
            this.setData({formRules})
            return result
        }
    },
    // 表单验证
    validateForm() {
        return new Promise((resolve, reject) => {
          try {
            let formRules = this.data.formRules;
            let result = false;
            for (let key in formRules) {
              let temp = this.validate(key)
              if (temp) {
                result = temp
              }
            }
            resolve(!result)
          } catch (e) {
            reject(e)
          }
        })
    },
    /**
     * 同意用户协议
     */
    checkBoxChange() {
        let checkAgree = this.data.checkAgree
        this.setData({
            checkAgree: !checkAgree
        })
    },
    // 用户服务协议
    showAgree() {
        wx.navigateTo({
          url: '/package_A/pages/agree/index',
        })
    },
    // 隐私政策
    showConceal() {
        wx.navigateTo({
            url: '/package_A/pages/conceal/index',
        })
    },
    // 提交申请
    submit(){
        this.validateForm().then(res => {
            if(res) {
                wx.showLoading({title:"上传中",mask:true})
                const params = this.data.form
                approveIdenty(params).then(res => {
                    if (res.code === 0) {
                        wx.reLaunch({
                            url: '/package_mine/pages/submitSuccess/index',
                        })
                    }
                }).finally(()=>{
                    wx.hideLoading();
                })
            }
        })
    },

    // 取消
    returnLastPage() {
        wx.navigateBack({
            delta: 1,
        })
    }
})