import { touristsOperate } from '../../../services';
import pubSub, { EDIT_TOURIST_ITEM, ADD_TOURIST_ITEM, DELETE_TOURIST_ITEM } from './../../../utils/pubSub';
Component({
  mixins: [],
  data: {
    isDesible: true,
    objInfo: {
      id: '',
      actionType: '',
      name: '',
      idCardNo: '',
      mobile: '',
      certType: '' // 证件类型

    },
    hasCertType: '',
    isRequent: '',
    objectArray: [{
      certType: 'Idcard',
      name: '身份证'
    }, {
      certType: 'Passport',
      name: '护照'
    }, {
      certType: 'HKAndMacauTaiwanPermit',
      name: '港澳台通行证'
    }],
    arrIndex: null
  },
  props: {},

  didMount() {
    // 组件获取当前页面的参数信息
    const query = getCurrentPages().pop().options || {};
    let certType = '';

    switch (query.certType) {
      case 'Idcard':
        certType = '身份证';
        break;

      case 'Passport':
        certType = '护照';
        break;

      case 'HKAndMacauTaiwanPermit':
        certType = '港澳台通行证';
        break;
    }

    this.setData({
      objInfo: {
        id: query.id || '',
        name: query.name || '',
        actionType: query.actionType || '',
        idCardNo: query.idCardNo || '',
        mobile: query.mobile || '',
        // idCardNo: '242342*****x2321321X',
        // mobile: '21323***234324324',
        certType: query.certType || ''
      },
      hasCertType: certType,
      isRequent: query.isRequent || ''
    });
  },

  didUpdate() {},

  didUnmount() {},

  methods: {
    onItemInput1(e) {
      this.setData({
        isDesible: false,
        [e.target.dataset.field]: e.detail.value
      });
    },

    onClear1(e) {
      this.setData({
        [e.target.dataset.field]: ''
      });
    },

    onItemInput2(e) {
      this.setData({
        isDesible: false,
        [e.target.dataset.field]: e.detail.value
      });
    },

    onClear2(e) {
      this.setData({
        [e.target.dataset.field]: ''
      });
    },

    onItemInput3(e) {
      this.setData({
        isDesible: false,
        [e.target.dataset.field]: e.detail.value
      });
    },

    onClear3(e) {
      this.setData({
        [e.target.dataset.field]: ''
      });
    },

    bindObjPickerChange(e) {
      let certType = '';

      switch (e.detail.value) {
        case 0:
          certType = 'Idcard';
          break;

        case 1:
          certType = 'Passport';
          break;

        case 2:
          certType = 'HKAndMacauTaiwanPermit';
          break;
      }

      this.setData({
        isDesible: false,
        arrIndex: e.detail.value,
        objInfo: {
          id: this.data.objInfo.id,
          actionType: this.data.objInfo.actionType,
          name: this.data.objInfo.name,
          idCardNo: this.data.objInfo.idCardNo,
          mobile: this.data.objInfo.mobile,
          certType // 证件类型

        }
      });
    },

    // 联系人操作
    goTouristsOperate(data) {
      const isTag = data.target.dataset.tag || '';
      const {
        objInfo
      } = this.data; // const testIdCardNo = /^[0-9a-zA-Z_]*$/
      // const testMobile = /[^\d\.]/g

      const test = /[\u4E00-\u9FA5]/g;

      if (isTag === 'add' || isTag === 'update') {
        if (this.data.isDesible) {
          return false;
        }

        if (!objInfo.name) {
          return my.showToast({
            content: '姓名不能为空！'
          });
        } // if (!objInfo.certType) {
        //   return my.showToast({
        //     content: '请选择个证件类型！'
        //   });
        // }
        // if (!objInfo.idCardNo) {
        //   return my.showToast({
        //     content: '身份证号码不能为空！'
        //   });
        // }
        // if (test.test(objInfo.idCardNo)) {
        //   return my.showToast({
        //     content: '身份证号不能输入中文'
        //   });
        // }


        if (!objInfo.mobile) {
          return my.showToast({
            content: '手机号不能为空！'
          });
        }

        if (test.test(objInfo.mobile)) {
          return my.showToast({
            content: '手机号不能输入中文'
          });
        }

        if (isTag === 'add') {
          const addInfo = {
            id: '',
            actionType: 'ADD',
            name: objInfo.name,
            mobile: objInfo.mobile,
            certType: objInfo.certType,
            idCardNo: objInfo.idCardNo
          };
          this.getTouristsOperate(addInfo, isTag);
        }

        if (isTag === 'update') {
          const strIdCardNo = JSON.stringify(objInfo.idCardNo);
          const strmobile = JSON.stringify(objInfo.mobile);
          let updateInfo = {};

          if (strIdCardNo.indexOf('*') === -1 && strmobile.indexOf('*') !== -1) {
            updateInfo = {
              id: objInfo.id,
              actionType: 'UPDATE',
              name: objInfo.name,
              certType: objInfo.certType,
              idCardNo: objInfo.idCardNo
            };
          }

          if (strIdCardNo.indexOf('*') !== -1 && strmobile.indexOf('*') === -1) {
            updateInfo = {
              id: objInfo.id,
              actionType: 'UPDATE',
              name: objInfo.name,
              certType: objInfo.certType,
              mobile: objInfo.mobile
            };
          }

          if (strIdCardNo.indexOf('*') === -1 && strmobile.indexOf('*') === -1) {
            updateInfo = {
              id: objInfo.id,
              actionType: 'UPDATE',
              name: objInfo.name,
              certType: objInfo.certType,
              idCardNo: objInfo.idCardNo,
              mobile: objInfo.mobile
            };
          }

          if (strIdCardNo.indexOf('*') !== -1 && strmobile.indexOf('*') !== -1) {
            updateInfo = {
              id: objInfo.id,
              actionType: 'UPDATE',
              name: objInfo.name,
              certType: objInfo.certType
            };
          }

          this.getTouristsOperate(updateInfo, isTag);
        }
      } else if (isTag === 'delete') {
        const deleteInfo = {
          id: objInfo.id,
          actionType: 'DELETE',
          name: objInfo.name,
          mobile: objInfo.mobile,
          certType: objInfo.certType,
          idCardNo: objInfo.idCardNo
        };
        my.confirm({
          title: '温馨提示',
          content: '确定要删除',
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          success: result => {
            if (result.confirm) {
              this.getTouristsOperate(deleteInfo, isTag);
            }
          }
        });
      }
    },

    async getTouristsOperate(objInfo, isTag) {
      const [err, result] = await touristsOperate(objInfo);

      if (err) {
        return my.showToast({
          type: 'fail',
          content: err.msg
        });
      } else {
        if (isTag === 'add') {
          const {
            id
          } = result || {};
          objInfo.id = id || `id_${Date.now()}`;
          my.showToast({
            type: 'success',
            content: '添加成功'
          });
          pubSub.emit(ADD_TOURIST_ITEM, objInfo);
        }

        if (isTag === 'update') {
          my.showToast({
            type: 'success',
            content: '编辑成功'
          });
          pubSub.emit(EDIT_TOURIST_ITEM, objInfo);
        }

        if (isTag === 'delete') {
          my.showToast({
            type: 'success',
            content: '删除成功'
          });
          pubSub.emit(DELETE_TOURIST_ITEM, objInfo);
        } // let { isRequent } = this.data
        // if (isRequent) {
        //   my.navigateTo({
        //     url: `${prefixPagePath}/${dynamicPageName}/index?pageCode=touristsList`,
        //   });
        // } else {
        //   my.navigateBack();
        // }


        my.navigateBack();
      }
    }

  }
});