const app = getApp();
const mydate = new Date();
var dateUtil = require("../../utils/dateUtil.js");
var dateTimePicker = require("../../utils/dateTimePicker.js");
Component({
  data: {
    disabled: false,
    idCardImageUrl: "",
    idCardNum: "",
    tel:'',
    dateTime: "",
    currentTime: dateUtil.formatDateYMD(mydate.toLocaleDateString()),
    copyTime: dateUtil.formatDateYMD(mydate.toLocaleDateString()),
    sex: 1,
    ageArray: [],
    ageIndex: -1,
    morePeople: false,
    recordList: [
      {
        id: 0,
        name: '李红霞',
        tel: 13488887125,
        gender: '女',
        checked: false
      },
      {
        id: 1,
        name: '张卫国',
        tel: 13488887125,
        gender: '男',
        checked: false
      },
      {
        id: 2,
        name: '王丽丽',
        tel: 13488887125,
        gender: '男',
        checked: false
      },
      {
        id: 3,
        name: '王丽丽',
        tel: 13488887125,
        gender: '男',
        checked: false
      },
      {
        id: 4,
        name: '王丽丽',
        tel: 13488887125,
        gender: '男',
        checked: false
      },
    ]
  },
  properties: {
    // 抽屉高度
    height: {
      type: String,
      value: "",
    },
    drawerType: {
      type: String,
      value: "",
    },
    //抽屉显示隐藏
    show: {
      type: Boolean,
      value: false
    },
    titleText: {
      type: String,
      value: ''
    },
    // 是否需要显示tabbar
    hideTabbar: {
      type: Boolean,
      value: false
    },
    cancerShow: {
      type: Boolean,
      value: true
    },
    // 
    touchClose: {
      type: Boolean,
      value: true
    },
    info: {
      type: Object,
      value: {

      },
    }

  },
  ready () {
    let that = this;
    console.log('this.data.info',this.data.info);
    for (let i = 16; i < 70; i++) {
      that.data.ageArray.push(i);
    }
    this.setData({
      ageArray: that.data.ageArray,
      info: that.data.info,
      tel:app.globalData.loginUserInfo.tel
    })

    let hour = new Date().getHours();
    if (hour < 20 && hour >= 6) {
      if (that.data.currentTime == that.data.copyTime) {
        that.data.currentTime = that.data.currentTime + " " + (hour + 1) + ":00";
      }
    } else if (hour >= 20) {
      if (that.data.currentTime == that.data.copyTime) {
        that.data.currentTime = that.data.currentTime + " " + "19:30";
      }
    } else if (hour < 6) {
      if (that.data.currentTime == that.data.copyTime) {
        that.data.currentTime = that.data.currentTime + " " + "6:00";
      }
    }
    this.setData({
      currentTime: that.data.currentTime,
    });
    var obj = dateTimePicker.dateTimePicker(this.data.startYear, this.data.endYear, this.data.currentTime);
    console.log(obj);
    console.log(obj.dateTimeArray);
    for (let index = 0; index < obj.dateTimeArray[0].length; index++) {
      obj.dateTimeArray[0][index] = obj.dateTimeArray[0][index] + "年";
    }
    for (let index = 0; index < obj.dateTimeArray[1].length; index++) {
      obj.dateTimeArray[1][index] = obj.dateTimeArray[1][index] + "月";
    }
    for (let index = 0; index < obj.dateTimeArray[2].length; index++) {
      obj.dateTimeArray[2][index] = obj.dateTimeArray[2][index] + "日";
    }

    this.setData({
      dateTimeArray: obj.dateTimeArray,
      dateTime: obj.dateTime,
    });
  },
  methods: {
    changeDateTime (e) {
      var dateTimeArray = this.data.dateTimeArray,
        dateTime = e.detail.value;
      this.setData({
        // dateTime: e.detail.value,
        currentTime: dateTimeArray[0][dateTime[0]].replace("年", "") + "-" + dateTimeArray[1][dateTime[1]].replace("月", "") + "-" + dateTimeArray[2][dateTime[2]].replace("日", "") + " " + dateTimeArray[3][dateTime[3]],
      });
    },

    changeDateTimeColumn (e) {
      console.log(e);
      console.log(e.detail.value);
      console.log(e.detail.column);
      var arr = this.data.dateTime,
        dateArr = this.data.dateTimeArray;
      console.log(arr);
      console.log(dateArr);


      arr[e.detail.column] = e.detail.value;
      console.log(dateArr[0][arr[0]], dateArr[1][arr[1]]);

      dateArr[2] = dateTimePicker.getMonthDay(dateArr[0][arr[0]], dateArr[1][arr[1]]);
      for (let index = 0; index < dateArr[2].length; index++) {
        dateArr[2][index] = dateArr[2][index] + "日";
      }
      // .replace("年",'').replace("月",'').replace("日",'')
      this.setData({
        dateTimeArray: dateArr,
        dateTime: arr,
      });
    },

    bindPickerChangeAge (e) {
      this.setData({
        ageIndex: e.detail.value,
        ["userInfo.age"]: parseInt(e.detail.value) + 16,
      });
    },
    formSubmit (e) {
      let that = this;
      console.log(e.detail.value);
      if (e.detail.value.userName == "") {
        wx.showToast({
          icon: "none",
          title: "请输入姓名",
        });
        return;
      }
      if (e.detail.value.tel == "") {
        wx.showToast({
          icon: "none",
          title: "请输入手机号",
        });
        return;
      }
      var reg = /^1[3|4|5|6|9|7|8][0-9]\d{8}$/;
      if (reg.test(e.detail.value.tel) == false) {
        wx.showToast({
          icon: "none",
          title: "请输入正确的手机号",
        });
        return;
      }
      if (that.data.info.agencyOperation == 2) {
        if (that.data.ageIndex == -1) {
          wx.showToast({
            icon: "none",
            title: "请选择年龄",
          });
          return;
        }
        e.detail.value.age = parseInt(that.data.ageIndex) + 16;
        e.detail.value.sex = that.data.sex;
      } else {
        if (e.detail.value.idCard == "") {
          wx.showToast({
            icon: "none",
            title: "请输入身份证号",
          });
          return;
        }
        var regIdCard = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
        if (regIdCard.test(e.detail.value.idCard) == false) {
          wx.showToast({
            icon: "none",
            title: "请输入正确的身份证号",
          });
          return;
        }

        e.detail.value.interviewTimeStr = that.data.currentTime;
      }
      if (that.data.idCardImageUrl != "") {
        e.detail.value.idCardImageUrl = that.data.idCardImageUrl
      }
      e.detail.value.jobId = that.data.info.id
      that.setData({
        disabled: true
      })



      console.log('form发生了submit事件，携带数据为：', e.detail.value)
      wx.request({
        url: app.globalData.ip + '/yishoudan/common/order/add',
        data: e.detail.value,
        header: app.globalData.headers,
        method: "POST",
        success: function (res) {
          console.log(res);
          if (res.data.status == 200) {
            wx.showToast({
              icon: "none",
              title: "报名成功",
            })
            that.setData({
              show: false,
              ageIndex: -1,
              disabled: false,
              idCardNum: ""
            })
            wx.showTabBar({
              animation: false,
              success: (result) => {
              },
              fail: () => { },
              complete: () => { }
            });

          } else {
            that.setData({
              disabled: false,
            })
            wx.showToast({
              icon: "none",
              title: res.data.msg,
            })
          }
        }
      })
    },

    /**
    * 阻止滑动穿透
    *
    *
    */
    modalMove () {
      // console.log(this.data.color);
      return false;
    },
    // 触摸开始事件
    handletouchtart: function (event) {
      this.data.lastX = event.touches[0].pageX;
      this.data.lastY = event.touches[0].pageY;
    },
    filterTouchStart (event) {
      this.handletouchtart(event);
    },
    filterTouchMove (event) {
      let tx = this.handletouchmove(event);
      if (this.data.touchClose) {
        if (tx.ty > 100) {
          this.hideLeft();

        }
      } else {
        return false
      }
    },
    /**
    * 页面滑动事件监听
    *
    *
    */
    handletouchmove: function (event) {
      let that = this;
      var currentX = event.touches[0].pageX;
      var currentY = event.touches[0].pageY;
      var tx = currentX - this.data.lastX;
      var ty = currentY - this.data.lastY;
      var text = "";
      //左右方向滑动
      if (Math.abs(tx) > Math.abs(ty)) {
        return {
          tx,
        };
      }
      //上下方向滑动
      else {
        return {
          ty,
        };
      }
    },
    hideLeft () {
      // this.data.show = false
      this.setData({
        ageIndex: -1,
        idCardNum: "",
      })
      var eventDetail = {
        type: this.data.drawerType
      };
      this.triggerEvent("hidedrawer", eventDetail);

      if (this.data.hideTabbar) {
        setTimeout(() => {
          this.getTabBar().setData({
            isShow: true,
          });
        }, 200);
      }

    },
    changeSex (e) {
      this.setData({
        sex: e.currentTarget.dataset.type
      })
    },
    addPersion () {
      wx.navigateTo({
        url: '/pages/mine/vitaeList/index?type=add'
      })
    },
    handlerMore () {
      this.setData({
        morePeople: !this.data.morePeople
      })
    },
    chooseIdCard () {
      var that = this;
      console.log("ischoise");
      wx.chooseMedia({
        count: 1,
        sizeType: ["original", "compressed"],
        sourceType: ["album", "camera"],
        success (res) {
          console.log(res);
          // debugger;
          that.uploadIdcardImg(res);
        },
      });
    },
    uploadIdcardImg (imgPath) {
      let that = this;
      // tempFilePath可以作为img标签的src属性显示图片
      let filePath;
      console.log("22222222222222222");
      console.log(imgPath);
      if (imgPath.tempFiles[0].tempFilePath) {
        filePath = imgPath.tempFiles[0].tempFilePath
      } else {
        filePath = imgPath;
      }

      console.log(filePath);
      wx.showLoading({
        title: "识别中...",
      });
      wx.uploadFile({
        // url: app.globalData.ip + '/commons/idCardOcrRecognize',
        url: app.globalData.ip + "/commons/idCardOcrRecognizeAndUploadToAliyunOss",
        filePath,
        name: "uploadFile",
        header: app.globalData.headers,
        success: function (res) {
          console.log("9999999999");

          console.log(res);
          if (res.statusCode == 413) {
            wx.showToast({
              duration: 2000,
              title: "身份证未识别",
              icon: "none",
              mask: true,
            });
            return
          }
          var idcard = JSON.parse(res.data);
          console.log(idcard);
          if (idcard.status == 200) {
            that.setData({
              idCardNum: idcard.data.num,
              idCardImageUrl: idcard.data.idCardImageUrl
            });
          } else {
            wx.showToast({
              duration: 2000,
              title: "身份证未识别",
              icon: "none",
              mask: true,
            });
          }
          wx.hideLoading();
        },
        fail: function (res) {
          console.log("99999999998");
          console.log(res);
          wx.hideLoading()
          wx.showToast({
            duration: 2000,
            title: "身份证未识别",
            icon: "none",
            mask: true,
          });
        },
      });
    },

  }
})