const util = require("../../../utils/util.js");
const app = getApp();

Page({
  data:{
    info:[],
    birthday: "2010-01-01",
    sex: 1,
    cardTypeStr: "",
    cardType: "",
  },
  onLoad:function(options){
    options.cardtype = options.cardtype || 0;
    this.setData({ info: options, sex: options.sex || 1, birthday: options.birthday || "2010-01-01", cardTypeStr: util.cardType(options.cardtype), cardType: options.cardtype,});
    this.setTitle();
  },
  setTitle: function () {
    wx.setNavigationBarTitle({
      title: (this.data.info.act == "add" ? "新增" : this.data.info.act == "edit" ? "修改" : "") + "监护人信息"
    });
  },
  selectCard: function () {
    var self = this;
    wx.showActionSheet({
      itemList: util.config.cardTypeArr,
      success: function (res) {
        self.setData({ cardType: res.tapIndex, cardTypeStr: util.cardType(res.tapIndex) });
      }
    })
  },
  changeSex(e) {
    this.setData({ sex: e.detail.value });
  },
  del: function (e) {
    var self = this;
    wx.showModal({
      title: '温馨提示',
      content: '您确定不需要该监护人信息了吗？',
      success: function (res) {
        if (res.confirm) {
          util.ajax({
            func: "user/delete_idcardinfo",
            data: { id: self.data.info.id }
          }, function (data) {
            util.showTips(data.code == 0 ? '身份信息删除成功。' : data.reason);
            wx.setStorageSync("isDel", true);
            wx.navigateBack();
          });
        }
      }
    });
  },
  bindDateChange: function (e) {
    this.setData({
      birthday: e.detail.value
    })
  },
  formSubmit: function (e) {
    var data = e.detail.value;
    if (util.isEmpty(data.name)) {
      util.showTips('请填写您的姓名。');
      return false;
    }
    if (util.isEmpty(data.cardType)) {
      util.showTips('请选择证件的类型。');
      return false;
    }
    if (!util.identityCodeValid(data.cardType, data.idcard)) {
      util.showTips('请填写正确的' + util.cardType(data.cardType) + '。');
      return false;
    }
    if (util.getAge(data.birthday) < 18) {
      util.showTips('监护人年龄必须在18周岁以上，请检查出生日期！');
      return false;
    }
    if (this.data.cardtype == '0' && !util.isEmpty(data['idcard']) && parseInt(data['idcard'].substr(16, 1)) % 2 != data['sex']) {
      util.showTips('选择的性别与身份证不符。');
      return false;
    }
    var nowYear = new Date().getFullYear();
    if (util.isEmpty(data['idcard']) && parseInt(nowYear - data['idcard'].substring(6, 10)) >= 18) {
      util.showTips('选择的身份类型与身份证不符。');
      return false;
    }

    data['position'] = "成人";
    var url = this.data.info.act == "add" ? "user/add_idcardinfo" : "user/update_idcardinfo";
    util.ajax({
      func: url,
      data: data
    }, function (res) {
      if (res.code == 0) {
        if (res.data){
          wx.setStorageSync("parentid",res.data);
        }
        wx.navigateBack();
      }else{
        util.showTips(res.reason);
      }
    });
  },
  cardBlur:function(e){
    let val = e.detail.value;
    if (this.data.cardType == '0' && util.identityCodeValid(0, val)) {
      var year = parseInt(val.substr(6, 4));
      var month = parseInt(val.substr(10, 2));
      var day = parseInt(val.substr(12, 2));
      if (month < 10) month = '0' + month;
      if (day < 10) day = '0' + day;
      var birthday = year + '-' + month + '-' + day;
      this.setData({ birthday, sex: util.getSex(val).toString() });
    }
  }
})