import { getDettlementList, dettlementDedail } from './service';
import history from '../../utils/history';
import { reportCmPV_YL } from '../../utils/cloudMonitorHelper';
Component({
  data: {
    hospitalDistrictId: '',
    list: [] // 出院结算列表

  },

  didMount() {
    // 获取参数住院IDhospitalDistrictId
    const optionsValue = JSON.parse(JSON.stringify(this.$page.data.query));
    const {
      hospitalDistrictId
    } = optionsValue;
    this.setData({
      hospitalDistrictId
    }, () => {
      // 获取住院人列表
      this.settlementList();
    });
    /* 服务预警，出院结算 */

    reportCmPV_YL({
      title: '出院结算'
    });
  },

  methods: {
    getAuthCodeFn() {
      return new Promise(resolve => {
        my.getAuthCode({
          scopes: 'auth_user',
          success: res => {
            resolve(res.authCode);
          }
        });
      });
    },

    async settlementFn(e) {
      const {
        item
      } = e.target.dataset;
      const {
        hospitalDistrictId
      } = this.data;
      const {
        inpatientId
      } = item;

      if (item.status === 'INPATIENT') {
        history.push({
          query: {
            inpatientId,
            hospitalDistrictId,
            color: '#000',
            backBtnColor: '#000',
            background: '#fff'
          },
          title: '出院结算',
          pageType: 'discharge-settlement-detail'
        });
      } else {
        // 出院跳转成功页
        try {
          const authCode = await this.getAuthCodeFn();
          const details = await dettlementDedail({
            inpatientId,
            authCode
          });
          let settlementType = '';

          if (!details.medicareAble) {
            // 不支持医保
            if (Number(details.refundAmount) !== 0 || Number(details.repayAmount) !== 0) {
              // repayAmount应补缴金额需要唤起支付功能.refundAmount应退款金额
              if (item === 'repayAmount') {
                // 自费补交
                settlementType = 'notSupportMedicalSupplementary';
              } else {
                // 有退款
                settlementType = 'notSupportMedicalRefund';
              }
            }
          } else {
            // 支持医保
            settlementType = 'supportMedicalPreSettlement';
          }

          history.push({
            query: {
              settlementType,
              inpatientId
            },
            title: '出院结算',
            pageType: 'discharge-settlement'
          });
        } catch (error) {
          console.log(error, 'error');
        }
      } // my.navigateTo({
      //   url: `/pages/discharge-settlement-detail/index?inpatientId=${inpatientId}`
      // });

    },

    async settlementList() {
      const {
        hospitalDistrictId
      } = this.data;
      const list = await getDettlementList({
        hospitalDistrictId
      });
      this.setData({
        list
      }); // try {
      //   const resultList = await getDettlementList({ hospitalDistrictId });
      //   if (Array.isArray(resultList)) {
      //     // if (resultList.length === 0) {
      //     //   this.setData({ showScroll: false });
      //     // }
      //     const lists = resultList.map((item) => {
      //       const arr = [];
      //       Object.keys(item).forEach((items) => {
      //         if (getContent[items]) {
      //           arr.push({ label: getContent[items], value: item[items], changeFontSize: (items === 'refundAmount' || items === 'depositBalance') });
      //         }
      //       });
      //       return { obj: arr, status: item.status, inpatientName: item.inpatientName, inpatientId: item.inpatientId };
      //     });
      //     this.setData({ list: [...list, ...lists] });
      //   }
      // } catch (error) {
      //   console.log(error, 'error');
      // }
    }

  }
});