/* eslint-disable no-mixed-operators */
import { touristsList, touristsOperate } from '../../../services';
import { prefixPagePath, dynamicPageName } from "../../../../../core/utils";
import pubSub, { EDIT_TOURIST_ITEM, ADD_TOURIST_ITEM, DELETE_TOURIST_ITEM } from './../../../utils/pubSub';
Component({
  mixins: [],
  data: {
    swipeIndex: null,
    seePassengerList: []
  },
  props: {},

  onInit() {
    this.getTouristsList = this.getTouristsList.bind(this);
  },

  didMount() {
    this.getTouristsList();
    pubSub.on(EDIT_TOURIST_ITEM, this.getTouristsList);
    pubSub.on(ADD_TOURIST_ITEM, this.getTouristsList);
    pubSub.on(DELETE_TOURIST_ITEM, this.getTouristsList);
  },

  didUpdate() {},

  didUnmount() {
    pubSub.off(EDIT_TOURIST_ITEM, this.getTouristsList);
    pubSub.off(ADD_TOURIST_ITEM, this.getTouristsList);
    pubSub.off(DELETE_TOURIST_ITEM, this.getTouristsList);
  },

  methods: {
    // 查询数据接口
    async getTouristsList() {
      const [err, result] = await touristsList({});

      if (result) {
        const resultValue = result;
        resultValue.forEach(item => {
          switch (item.certType) {
            case 'Idcard':
              item.certTypeValue = '身份证';
              break;

            case 'Passport':
              item.certTypeValue = '护照';
              break;

            case 'HKAndMacauTaiwanPermit':
              item.certTypeValue = '港澳台通行证';
              break;
          }

          item.right = [{
            type: 'delete',
            text: '删除',
            certType: item.certType,
            gmtCreate: item.gmtCreate,
            gmtModified: item.gmtModified,
            id: item.id,
            idCardNo: item.idCardNo,
            mobile: item.mobile,
            name: item.name
          }];
        });
        const update = {
          seePassengerList: [...resultValue]
        };
        await this.updateState(update);
      }

      if (err) {
        return my.showToast({
          type: 'fail',
          content: err.msg
        });
      }
    },

    updateState(state) {
      const func = resolve => this.setData(state, resolve);

      return new Promise(func);
    },

    // 联系人操作
    goTouristsOperate(data) {
      const objInfo = data.target && data.target.dataset.info || data;
      const isTag = data.target && data.target.dataset.tag || 'delete';
      const id = isTag === 'add' ? '' : objInfo.id;
      const name = objInfo.name || '';
      const mobile = objInfo.mobile || '';
      const certType = objInfo.certType || '';
      const idCardNo = objInfo.idCardNo || '';
      let actionType = '';

      switch (isTag) {
        case 'update':
          actionType = 'UPDATE';
          break;

        case 'delete':
          actionType = 'DELETE';
          break;

        case 'add':
          actionType = 'ADD';
          break;
      }

      if (isTag === 'add') {
        my.navigateTo({
          url: `${prefixPagePath}/${dynamicPageName}/index?pageCode=touristsOperate&isRequent=requent`
        });
      }

      if (isTag === 'update') {
        my.navigateTo({
          url: `${prefixPagePath}/${dynamicPageName}/index?pageCode=touristsOperate&id=${id}&actionType=${actionType}&name=${name}&mobile=${mobile}&certType=${certType}&idCardNo=${idCardNo}&isRequent=requent`
        });
      }

      if (isTag === 'delete') {
        this.getTouristsOperate(objInfo);
      }
    },

    async getTouristsOperate(objInfo) {
      const data = {
        id: objInfo.id,
        actionType: 'DELETE',
        name: objInfo.name,
        mobile: objInfo.mobile,
        certType: objInfo.certType,
        idCardNo: objInfo.idCardNo
      }; // eslint-disable-next-line no-unused-vars

      const [err, result] = await touristsOperate(data);

      if (err) {
        return my.showToast({
          type: 'fail',
          content: err.msg
        });
      } else {
        my.showToast({
          type: 'success',
          content: '删除成功'
        });
        this.getTouristsList();
      }
    },

    onRightItemClick(e) {
      const {
        detail
      } = e;
      my.confirm({
        title: '温馨提示',
        content: '确定要删除',
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        success: result => {
          const {
            seePassengerList
          } = this.data;

          if (result.confirm) {
            if (detail.type === 'delete') {
              seePassengerList.splice(this.data.swipeIndex, 1);
            }

            e.done();
            this.goTouristsOperate(detail);
          }
        }
      });
    }

  }
});