const util = require("../../../utils/util.js");
const app = getApp();
Page({
  data: {
    datas:{},
    endDate:"2018-01-01",
    birthday: "2010-01-01",
    cardTypeStr:'',
    mask:false,
    maskTwo:false,
    status:1,
    orderid:'',
    refundpwd:'',
    imgData:{},
    imgUrl: util.config.imgUrl,
    aid:'',
    title:'',
  },
  onLoad(options) {
    this.getData(options.orderid,options.refundpwd)
  },
  getData(orderid,refundpwd){
    console.log(orderid,refundpwd);
    let that =this;
    util.ajax({
        func:"order/getOrderChildInfo",
        data:{"orderid":orderid,"consumeId":refundpwd}
      },function(res){
        if(res.code == 0){
          let datas = res.data,imgData={};
          let cardTypeStr = util.cardType(datas.cardType);
          imgData['passport'] = datas.passport || '';
          imgData['householder'] = datas.householder || '';
          imgData['parentsPage'] = datas.parentsPage || '';
          imgData['childPage'] = datas.childPage || '';
          imgData['parentsIdCardFront'] = datas.parentsIdCardFront || '';
          imgData['parentsIdCardBack'] = datas.parentsIdCardBack || '';
          imgData['birthCertificate'] = datas.birthCertificate || '';
          imgData['idCardFront'] = datas.idCardFront || '';
          imgData['idCardBack'] = datas.idCardBack || '';
          imgData['sealPage'] = datas.sealPage || '';
          // if(!datas.bodyType || datas.bodyType == '') datas.bodyType = '1';
          that.setData({cardTypeStr,datas,orderid,refundpwd,imgData})
        }else{
          util.showTips(res.reason);
        }
      });
  },
  formSubmit(e) {
    let datas = this.data.datas,that = this,data = e.detail.value,imgData=this.data.imgData;
    data['oid'] = this.data.orderid;
    data['consumeId'] = this.data.refundpwd;
    data['birthday'] = this.data.cardTypeStr == '内地身份证号'? this.getBirthday(data.idcard):'';
    data['cardType'] = datas.cardType;
    if(datas.travelMode == 2 && util.isEmpty(data['passportName'])){
      util.showTips("请填写儿童护照姓名。");
      return false;
    }
    if(util.isEmpty(data['height']) || /[^0-9]/g.test(data['height'])){
      util.showTips("请填写数字类型的身高。");
      return false;
    }
    if(util.isEmpty(data['weight']) || /[^0-9]/g.test(data['weight'])){
      util.showTips("请填写数字类型的体重。");
      return false;
    }
    if(util.isEmpty(data['bodyType'])){
      util.showTips("请选择儿童体型信息。");
      return false;
    }
    if(util.isEmpty(data['allergy'])){
      util.showTips("请填写是否有药物或食品过敏。");
      return false;
    }
    if(util.isEmpty(data['diet'])){
      util.showTips("请填写是否有饮食禁忌。");
      return false;
    }
    if(util.isEmpty(data['bloodOrMedical'])){
      util.showTips("请填写是否有特殊血型/有无病史。");
      return false;
    }
    if(util.isEmpty(data['character'])){
      util.showTips("请填写在活动期间期待孩子哪方面性格的培养。");
      return false;
    }
    if(util.isEmpty(data['especiallyRemark'])){
      util.showTips("请填写特别备注。");
      return false;
    }

    if(datas.travelMode == 1){
      data['idCardFront'] =  imgData['idCardFront'] || "";
      data['idCardBack'] =  imgData['idCardBack'] || "";
      data['childPage'] =  imgData['childPage'] || "";
      data['sealPage'] =  imgData['sealPage'] || "";
      if(util.isEmpty(data['idCardFront']) && util.isEmpty(data['idCardBack']) && util.isEmpty(data['childPage']) && util.isEmpty(data['sealPage'])){
        util.showTips("请上传相关证件资料一或资料二。");
        return false;
      }else{
        if(!util.isEmpty(data['idCardFront']) && !util.isEmpty(data['idCardBack'])){
          data['cert'] = 0;
        }else if(!util.isEmpty(data['idCardFront']) || !util.isEmpty(data['idCardBack'])){
          data['cert'] = 1;
        }
        if(data['cert'] != 0){
          if(!util.isEmpty(data['childPage']) && !util.isEmpty(data['sealPage'])){
            data['cert'] = 0;
          }else if(!util.isEmpty(data['childPage']) || !util.isEmpty(data['sealPage'])){
            data['cert'] = 2;
          }
        }
        if(data['cert'] == 1){
          util.showTips("请上传完整的资料一相关证件。");
          return false;
        }else if(data['cert'] == 2){
          util.showTips("请上传完整的资料二相关证件。");
          return false;
        }
      }
    }else if(datas.travelMode == 2){
      data['passport'] = imgData['passport'] || "";
      data['householder'] = imgData['householder'] || "";
      data['parentsPage'] = imgData['parentsPage'] || "";
      data['childPage'] = imgData['childPage'] || "";
      data['parentsIdCardFront'] = imgData['parentsIdCardFront'] || "";
      data['parentsIdCardBack'] = imgData['parentsIdCardBack'] || "";
      data['birthCertificate'] = imgData['birthCertificate'] || "";
      if(util.isEmpty(data['passport'])){
        util.showTips("请上传护照证件。");
        return false;
      }
      if(util.isEmpty(data['householder']) && util.isEmpty(data['parentsPage']) && util.isEmpty(data['childPage']) && util.isEmpty(data['parentsIdCardFront']) && util.isEmpty(data['parentsIdCardBack']) && util.isEmpty(data['birthCertificate'])){
        util.showTips("请上传相关证件资料一或资料二。");
        return false;
      }else{
        if(!util.isEmpty(data['householder']) && !util.isEmpty(data['parentsPage']) && !util.isEmpty(data['childPage'])){
          data['cert'] = 0;
        }else if(!util.isEmpty(data['householder']) || !util.isEmpty(data['parentsPage']) || !util.isEmpty(data['childPage'])){
          data['cert'] = 1;
        }
        if(data['cert'] != 0){
          if(!util.isEmpty(data['parentsIdCardFront']) && !util.isEmpty(data['parentsIdCardBack']) && !util.isEmpty(data['birthCertificate'])){
            data['cert'] = 0;
          }else if(!util.isEmpty(data['parentsIdCardFront']) || !util.isEmpty(data['parentsIdCardBack']) || !util.isEmpty(data['birthCertificate'])){
            data['cert'] = 2;
          }
        }
        if(data['cert'] == 1){
          util.showTips("请上传完整的资料一相关证件。");
          return false;
        }else if(data['cert'] == 2){
          util.showTips("请上传完整的资料一或者资料二相关证件。");
          return false;
        }
      }
    }
    util.ajax({
      func:"order/updateChildArchives",
      data:data
    },function(res){
      if(res.code == 0){
        util.showTips("信息提交成功！",2000);
        setTimeout(function(){
          wx.navigateBack();
        },2000)
      }
    });
  },
  getBirthday(val){
    var birthday = [];
    birthday.push(val.substring(6, 10));
    birthday.push(val.substring(10, 12));
    birthday.push(val.substring(12, 14));
    return birthday.join("-");
  },
  prompt(){
    this.setData({mask:true})
  },
  maskBtn(){
    this.setData({mask:false,maskTwo:false})
  },
  maskShow(e){
    this.setData({
      maskTwo:true,
      status:e.currentTarget.dataset.id
    })
  },
  chooseImg(e){
    let id = e.currentTarget.dataset.id,
    imgData=this.data.imgData,
    that= this;
    wx.chooseImage({
      count: 1, 
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'], 
      success: function (res) {
        console.log(res);
        wx.uploadFile({
          url: util.config.apiServer + 'user/upload_img_app.do', 
          filePath: res.tempFilePaths[0],
          name: 'avatarFile',
          formData:{
            "rid":app.globalData.userInfo['rid']
          },
          success: function(reason){
            let obj = JSON.parse(reason.data)
            if(reason.statusCode==200&&obj.code == 0){
              switch(id)
              {
                case '0':imgData['passport'] = obj.data.image;
                break;
                case '1':imgData['householder'] = obj.data.image;
                break;
                case '2':imgData['parentsPage'] = obj.data.image;
                break;
                case '3':imgData['childPage'] = obj.data.image;
                break;
                case '4':imgData['parentsIdCardFront'] = obj.data.image;
                break;
                case '5':imgData['parentsIdCardBack'] = obj.data.image;
                break;
                case '6':imgData['birthCertificate'] = obj.data.image;
                break;
                case '7':imgData['idCardFront'] = obj.data.image;
                break;
                case '8':imgData['idCardBack'] = obj.data.image;
                break;
                case '9':imgData['sealPage'] = obj.data.image;
                break;
              }
              that.setData({imgData})
            }
          }
        })
      }
    })
  },
  delImg(e){
    let id = e.currentTarget.dataset.id,imgData=this.data.imgData;
    switch(id)
    {
      case '0':imgData['passport'] = '';
      break;
      case '1':imgData['householder'] = '';
      break;
      case '2':imgData['parentsPage'] = '';
      break;
      case '3':imgData['childPage'] = '';
      break;
      case '4':imgData['parentsIdCardFront'] = '';
      break;
      case '5':imgData['parentsIdCardBack'] = '';
      break;
      case '6':imgData['birthCertificate'] = '';
      break;
      case '7':imgData['idCardFront'] = '';
      break;
      case '8':imgData['idCardBack'] = '';
      break;
      case '9':imgData['sealPage'] = '';
      break;
    }
    this.setData({imgData})
  }
})