import { getMedicineInfo, getDischargeSettlementDetails } from './service'; // import history from '../../utils/history';

import { reportCmPV_YL, reportApi } from '../../utils/cloudMonitorHelper';
Component({
  data: {
    jsonData: {
      Inpatient: {},
      // 住院人详情
      details: [] // 日清单

    },
    selectedDateStr: '',
    // 日清单的时间
    calendarShow: false,
    // 是否显示时间
    selectedDate: null,
    // 选择的时间
    isError: false,
    // 显示没有数据的情况
    inpatientId: '',
    // 住院人id
    selectIndex: 0,
    // 回显选中住院人
    attention: ''
  },

  didMount() {
    const {
      inpatientId,
      billDate,
      attention
    } = this.$page.data.query || {};
    const toDay = new Date();
    this.setData({
      selectedDate: toDay,
      selectedDateStr: billDate || `${toDay.getFullYear()}-${this.changeMonth(toDay.getMonth())}-${this.changeDate(toDay.getDate())}`,
      inpatientId,
      attention
    });
    /* 服务预警，日清单查询 */

    reportCmPV_YL({
      title: '日清单查询'
    });
    /* 服务办结，日清单查询 */

    reportApi('日清单查询');
  },

  methods: {
    // 获取日清单详情
    async getDeails() {
      try {
        const {
          inpatientId,
          selectedDateStr,
          jsonData
        } = this.data;
        const details = await getMedicineInfo({
          inpatientId,
          billDate: selectedDateStr
        });
        const payItemList = details.payItemList ? details.payItemList.length : 0;
        this.setData({
          jsonData: { ...jsonData,
            details
          },
          isError: payItemList === 0
        });
      } catch (error) {
        console.log(error, 'error');
      }
    },

    popOverClose() {
      // 关闭弹出框
      this.$page.$popModal.hidePopover();
    },

    onAdmissionRegistration() {
      this.$page.$popModal.showPopover({
        title: '选择就诊人',
        popupPosition: 'bottom',
        onButtonClose: () => this.popOverClose(),
        // 弹出框关闭
        onChoosePatient: info => this.choosePatient(info) // 选择就诊人

      });
    },

    choosePatient(info) {
      const {
        jsonData
      } = this.data;
      const {
        hospitalizationRecordList
      } = this.$page.$popModal.data;
      const selectIndex = hospitalizationRecordList.findIndex(item => info.inpatientId === item.inpatientId); // 获取用户id

      this.setData({
        jsonData: { ...jsonData,
          Inpatient: info
        },
        inpatientId: info.inpatientId,
        selectIndex
      });
      this.getDeails();
      this.$page.$popModal.hidePopover();
    },

    async getJsonData(info) {
      const {
        inpatientId
      } = this.data;

      try {
        // 分为两种情况，第一种当从住院人列表跳转过来的时候，有住院人id，第二种是从首页跳转过来，无住院人id
        if (!inpatientId) {
          const infoDetail = info && info.length > 0;
          const {
            jsonData
          } = this.data;
          this.setData({
            jsonData: { ...jsonData,
              Inpatient: infoDetail ? info[0] : {}
            },
            inpatientId: infoDetail ? info[0].inpatientId : ''
          });
        } else {
          const res = await getDischargeSettlementDetails({
            inpatientId
          });
          this.choosePatient(res);
        }
      } catch (error) {
        console.log(error, 'error');
      }

      this.getDeails();
    },

    getJsonDataFn(info) {
      if (!info || info.length === 0) {
        my.alert({
          content: this.data.attention || '请绑定住院人',
          success: () => {
            my.navigateBack();
          }
        });
        return false;
      }

      this.getJsonData(info);
    },

    handleSelect(date) {
      if (date[0].getTime() > new Date().getTime()) {
        my.showToast({
          type: 'none',
          content: '不能选未来的日子',
          duration: 2000
        });
        this.setData({
          calendarShow: false
        });
        return;
      }

      this.setData({
        selectedDate: date[0],
        selectedDateStr: `${date[0].getFullYear()}-${this.changeMonth(date[0].getMonth())}-${this.changeDate(date[0].getDate())}`,
        calendarShow: false
      });
      this.getDeails();
    },

    selectDate() {
      this.setData({
        calendarShow: true
      });
    },

    closeCalendar() {
      this.setData({
        calendarShow: false
      });
    },

    changeMonth(month) {
      return month + 1 >= 10 ? month + 1 : `0${month + 1}`;
    },

    changeDate(date) {
      return date >= 10 ? date : `0${date}`;
    },

    // 获取前一天的日清单详情
    queryPreDay() {
      if (this.data.selectedDate) {
        const preDay = this.data.selectedDate.getTime() - 24 * 60 * 60 * 1000;
        const preDayDate = new Date(preDay);
        this.setData({
          selectedDate: preDayDate,
          selectedDateStr: `${preDayDate.getFullYear()}-${this.changeMonth(preDayDate.getMonth())}-${this.changeDate(preDayDate.getDate())}`
        });
        this.getDeails();
      }
    },

    // 获取下一天的日清单日详情
    queryNextDay() {
      if (this.data.selectedDate) {
        const nextDay = this.data.selectedDate.getTime() + 24 * 60 * 60 * 1000;

        if (nextDay > new Date().getTime()) {
          my.showToast({
            type: 'none',
            content: '不能选未来的日子',
            duration: 2000
          });
          return;
        }

        const nextDayDate = new Date(nextDay);
        this.setData({
          selectedDate: nextDayDate,
          selectedDateStr: `${nextDayDate.getFullYear()}-${this.changeMonth(nextDayDate.getMonth())}-${this.changeDate(nextDayDate.getDate())}`
        });
        this.getDeails();
      }
    }

  }
});