import { tradePay } from '../../utils/tradePay';
import history from '../../utils/history';
import { reportCmPV_YL } from '../../utils/cloudMonitorHelper';
Component({
  props: {},
  data: {
    modalOpened: false,
    buttons: [{
      text: '不，先留着'
    }, {
      text: '取消预约',
      extClass: 'buttonBold'
    }],
    clinicCard: {
      balance: 0,
      cardNum: ''
    },
    // 就诊卡
    cardList: [],
    amountItem: [100, 200, 300, 500, 1000, 2000],
    amountItemChecked: -1,
    amount: '',
    loading: false,
    patientId: '',
    defaultCardNum: '',
    patientName: ''
  },

  didMount() {
    const {
      query
    } = this.$page.data;
    const {
      id,
      cardNum
    } = query || {};
    this.setData({
      patientId: id,
      defaultCardNum: cardNum
    });
    /* 服务预警，门诊充值 */

    reportCmPV_YL({
      title: '门诊充值'
    });
  },

  methods: {
    // 切换就诊人
    onChange(item) {
      const patientName = item.name;
      const cardList = item.medicCards;
      const clinicCard = cardList.find(m => {
        if (item.id === this.data.patientId) {
          return m.cardNum === this.data.defaultCardNum;
        }

        return m.cardNum === item.bindCardNum;
      }) || cardList[0] || {};
      this.setData({
        clinicCard,
        cardList,
        patientName
      });
    },

    // 修改充值金额
    changeAmount(e) {
      this.setData({
        amountItemChecked: e.target.dataset.idx,
        amount: e.target.dataset.val
      });
    },

    bindKeyInput(e) {
      let amount = e.detail.value;
      const idx = amount.indexOf('.');
      const second = amount.indexOf('.', idx + 1);

      if (idx === 0) {
        amount = `0${amount}`;
      } else if (idx > 0) {
        amount = amount.slice(0, idx + 3);
      }

      if (second > 1) {
        amount = amount.slice(0, second);
      }

      this.setData({
        amountItemChecked: -1,
        amount
      });
    },

    // 发起付款
    onRecharge() {
      this.setData({
        loading: true
      });
      const {
        amount,
        clinicCard,
        patientName
      } = this.data;
      tradePay({
        type: 2,
        amount,
        idNum: clinicCard.cardNum
      }, {
        tradeType: 'Appointment'
      }).then(() => {
        history.push({
          pageType: 'payment-success',
          title: '充值成功',
          componentData: {
            result: [{
              name: '就诊人',
              value: patientName
            }, {
              name: '就诊卡号',
              value: clinicCard.cardNum
            }, {
              name: '充值金额',
              value: amount
            }]
          }
        });
      }).finally(() => {
        this.setData({
          loading: false
        });
      });
    },

    // 切换就诊卡
    changePatient() {
      this.setData({
        modalOpened: true
      });
    },

    onClose() {
      this.setData({
        modalOpened: false
      });
    },

    chooseCard(e) {
      this.setData({
        clinicCard: e
      });
    }

  }
});