import { payDetail } from './service';
import { tradePay } from '../../utils/tradePay';
import history from '../../utils/history';
import { reportApi, reportCmPV_YL } from '../../utils/cloudMonitorHelper';
Component({
  props: {},
  data: {
    order: {
      orderItems: [// {
        //   itemName: '项目名称',
        //   itemNum: 2,
        //   amount: 999.99,
        // },
      ],
      depName: '',
      outTradeNo: '',
      tradeNo: '',
      type: null,
      // 1-挂号费订单 2-充值订单 3-诊间缴费订单
      userName: '',
      cardNum: '',
      amount: 0,
      createTime: '',
      payTime: '',
      payType: null,
      // 1-自费 2-医保 3-其他
      status: null,
      // 0-待支付 1-已支付 9-已取消
      medicareBinded: false,
      idNum: '',
      customInfo: '',
      payDetails: '',
      payDate: '',
      doctorName: '',
      reportRemind: '',
      checkRemind: '',
      medicineRemind: '',
      refundRemind: '',
      receiptRemind: ''
    },
    // 订单项
    fromItem: [{
      label: '就诊人',
      key: 'name'
    }, {
      label: '就诊卡号',
      key: 'cardNum'
    }, {
      label: '就诊科室',
      key: 'depName'
    }, {
      label: '医生',
      key: 'doctorName'
    }, {
      label: '费用日期',
      key: 'payDate'
    }, {
      label: '支付单号',
      key: 'outTradeNo'
    }, {
      label: '创建时间',
      key: 'createTime'
    }, {
      label: '付款时间',
      key: 'payTime'
    }],
    medicareExpand: false,
    expand: false,
    medicare: {
      total: 0,
      own: 0,
      detail: {}
    },
    checkRemind: {}
  },

  didMount() {
    this.payDetail(this.$page.data.query);
    /* 服务办结，门诊缴费 */

    reportApi('门诊缴费');
    /* 服务预警，门诊缴费 */

    reportCmPV_YL({
      title: '门诊缴费'
    });
  },

  methods: {
    saveSubscribe(ref) {
      this.subscribe = ref;
    },

    payDetail(send) {
      payDetail(send).then(data => {
        const payDetails = JSON.parse(data.payDetails || '{}');
        const detail = payDetails['医保支付'] || {};
        let total = 0;
        Object.entries(detail).forEach(([k, v]) => {
          detail[k] = numFix(v);
          total += v * 1;
        });
        this.setData({
          order: Object.assign({}, this.data.order, data),
          medicare: {
            total: numFix(total),
            own: numFix(payDetails['自费支付金额']),
            detail
          },
          checkRemind: JSON.parse(data.checkRemind || '{}')
        });
      });
    },

    // 查看更多
    openForm() {
      this.setData({
        expand: !this.data.expand
      });
    },

    medicareExpand() {
      this.setData({
        medicareExpand: !this.data.medicareExpand
      });
    },

    // 发起支付
    async onPay(e) {
      const {
        medicareBinded = false
      } = e.target.dataset;
      const {
        order: oreder
      } = this.data;

      if (oreder.type === 1) {
        await tradePay({
          type: oreder.type,
          idNum: oreder.outTradeNo,
          depName: oreder.depName
        }, {
          tradeType: 'Appointment'
        });
      } else if (oreder.type === 2) {
        // 充值
        await tradePay({
          type: oreder.type,
          idNum: oreder.outTradeNo,
          amount: oreder.amount
        }, {
          tradeType: 'Appointment'
        });
      } else {
        // 门诊订单
        await tradePay({
          useBalance: !medicareBinded,
          useMedicare: medicareBinded,
          outTradeNo: oreder.outTradeNo
        }, {
          tradeType: 'Outpatient'
        });
      } // 支付成功刷新页面


      this.payDetail(this.$page.data.query); // 支付成功以后，发起消息订阅

      this.subscribe.subscribeMessage();
    },

    hrefTo() {
      my.ap.navigateToAlipayPage({
        path: 'alipays://platformapi/startapp?appId=77700284&page=pages/medical/index?chInfo=YY_xiaochengxu'
      });
    },

    toH5(e) {
      const {
        url
      } = e.target.dataset;
      history.toH5(url);
    }

  }
});

function numFix(val) {
  const num = val * 1;
  return num.toFixed(2);
}