// pages/invoice/authentication/authentication.js
const app = getApp();
const api = app.api;
import WxValidate from '../../../utils/WxValidate'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    type:undefined,
    upFileInfoReverse:null,
    upFileInfoFont:null,
    otherOrganization:false,
    organizationPickList:[],
    organizationPickValue:null,
    formData:{
      idNo: null,
      idNoImgNegativeId: null, //反面
      idNoImgPositiveId: null, //正面
      organCode:"",
      organName:"",
      mobile: null,
      name: null,
    },
    rules: {
      name: {
        required: true,
      },
      mobile: {
        required: true,
        tel: true,
      },
      idNo: {
        required: true,
        idcard: true,
      },
      organCode:{
        required: true,
      },
      idNoImgNegativeId:{
        required: true,
      },
      idNoImgPositiveId:{
        required: true,
      }
    },
    messages: {
      name: {
        required: '请输入姓名',
      },
      mobile: {
        required: '请输入手机号',
        tel: '请输入正确的手机号',
      },
      idNo: {
        required: '请输入身份证号码',
        idcard: '请输入正确的身份证号码',
      },
      organCode:{
        required: '请选择组织机构',
      },
      idNoImgPositiveId:{
        required: '请上传身份证正面',
      },
      idNoImgNegativeId:{
        required: '请上传身份证反面',
      }
    },
    isValidate:false,
    isSubmit:false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const { rules, messages } = this.data;
    this.WxValidate = new WxValidate(rules, messages);
    this.setData({
      type:options.id
    })
    Promise.all([
      this.findByCode('organization'),
    ]).then((res)=>{
      console.log(res);
      const organizationPickList=res[0].data.dicInfoList;
      this.setData({
        organizationPickList:organizationPickList
      });
    });
  },

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

  },

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

  },
  validateForm(showInfo=false){
    const {formData,otherOrganization} = this.data;
    let flag=true;
    if(otherOrganization&&formData.organName==''){
      flag=false
      if(showInfo===true){
        wx.showToast({
          title: '请输入机构名称',
          icon: 'none',
          duration: 2000
        })
      }
    }
    //传入表单数据,调用验证方法
    if (!this.WxValidate.checkForm(formData)) {
      flag=false
      if(showInfo===true){
        const error = this.WxValidate.errorList[0];
        wx.showToast({
          title: error.msg,
          icon: 'none',
          duration: 2000
        })
      }
    }
    this.setData({
      isValidate:flag
    });
    return flag
  },
  saveData() {
    const {formData,type,isSubmit} = this.data;
    //重复提交
    if(isSubmit){
      return;
    }

    let flag=this.validateForm(true);
    if(!flag){
      return;
    }

    // if(otherOrganization&&formData.organName==''){
    //   wx.showToast({
    //     title: '请输入机构名称',
    //     icon: 'none',
    //     duration: 2000
    //   })
    //   return false
    // }
    // // 传入表单数据,调用验证方法
    // if (!this.WxValidate.checkForm(formData)) {
    //   const error = this.WxValidate.errorList[0];
    //   wx.showToast({
    //     title: error.msg,
    //     icon: 'none',
    //     duration: 2000
    //   })
    //   return false
    // }

    //console.log(this.WxValidate);
    this.setData({
      isSubmit:true,
    });
    app.post(api.updateArtistUserInvoice, formData).then(res => {
      //console.log(res);
      this.setData({
        isSubmit:false,
      });
      if(type==-1){
        wx.navigateBack({})
      }else if(type==0){
        wx.redirectTo({
          url:"/pages/user/personalInfo/personalInfo"
        })
      }else if(type==2){
        wx.redirectTo({
          url:"/pages/user/personalInfo/personalInfo?type=2"
        })
      /*  wx.redirectTo({
          url:"/pages/settled/curatorInfo/curatorInfo"
        })*/
      }else{
        wx.redirectTo({
          url:"/pages/invoice/contract/contract"
        })
      }
    }).catch(()=>{
      this.setData({
        isSubmit:false,
      });
    })

  },
  changeInput(e){
    console.log(e);
    const id = e.currentTarget.id;
    const value = e.detail.value;
    this.setData({
      [id]: value
    });
  },
  findByCode(code){
    return app.post(api.findByCode,{
      "code": code
    })
  },
  chooseFileFront(){
    app.uploadFile({
      extension:['jpg','png','jpeg']
    }).then(res=>{
      console.log(res)
      const data=JSON.parse(res.data);
      this.setData({
        upFileInfoFont:data,
        'formData.idNoImgPositiveId':data.id
      },()=>{
        this.validateForm()
      })
    }).catch(error=>{
      console.log(error)
    });
  },
  removeFileFront(){
    this.setData({
      upFileInfoFont:null,
      'formData.idNoImgPositiveId':''
    },()=>{
      this.validateForm()
    })
  },
  chooseFileReverse(){
    app.uploadFile({
      extension:['jpg','png','jpeg']
    }).then(res=>{
      console.log(res)
      const data=JSON.parse(res.data);
      this.setData({
        upFileInfoReverse:data,
        'formData.idNoImgNegativeId':data.id
      },()=>{
        this.validateForm()
      })
    }).catch(error=>{
      console.log(error)
    });
  },
  removeFileReverse(){
    this.setData({
      upFileInfoReverse:null,
      'formData.idNoImgNegativeId':''
    },()=>{
      this.validateForm()
    })
  },
  getPhoneNumber(e) {
    console.log(e);
    if (e.detail.errMsg !== "getPhoneNumber:ok"){
      return;
    }
    const { encryptedData, iv } = e.detail;
    app.post(api.getUserPhone, {
      wxPhoneEncrypted: {
        encryptedData,
        ivStr: iv
      }
    }).then(res => {
     this.setData({
       'formData.mobile': res.data.phone
     },()=>{
       this.validateForm()
     })
    });

  },
  organizationPickHandler: function(e) {
    const {value}=e.detail;
    let otherOrganization = false;
    if(this.data.organizationPickList[value].code === '99'){
      otherOrganization = true;
    }
    this.setData({
      organizationPickValue : value,
      otherOrganization : otherOrganization,
      'formData.organCode':this.data.organizationPickList[value].code,
      'formData.organName':this.data.organizationPickList[value].code==99?"":this.data.organizationPickList[value].name,
    },()=>{
      this.validateForm()
    });
    // this.setData({
    //   organizationPickValue:value,
    //   'formData.sex':sexPickList[value].value
    // });
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

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

  },

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

  },

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

  },

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

  }
})