import { getHospitalRecordList, getCashPledgeList } from './service';
import history from '../../utils/history';
import day from 'dayjs';
Component({
  data: {
    typeOptions: [],
    // 住院人的选项
    requestDetail: {
      // 请求参数
      startDate: '',
      endDate: '',
      inpatientId: '' // 住院人Id
      // pageNum: 1,
      // pageSize: 10

    },
    hospitalList: [],
    // 缴纳记录列表
    showInpatient: false,
    showScroll: true,
    // 是否还进行触底请求
    defaultName: '全部住院人',
    defaultLabel: '全部住院人'
  },

  didMount() {
    this.getCashPledgeListRe();
    this.getHospitalRecordLists();
  },

  methods: {
    getMonth(params) {
      return day(params).get('month');
    },

    getYears(params) {
      return day(params).get('year');
    },

    goToDetail(e) {
      const {
        depositId
      } = e.target.dataset.item;
      history.push({
        query: {
          depositId
        },
        title: '押金缴纳详情',
        pageType: 'pay-result'
      });
    },

    // 获取住院人列表
    async getHospitalRecordLists() {
      try {
        const recordList = await getHospitalRecordList();
        const typeOptions = recordList.map(item => ({
          value: item.inpatientId,
          label: item.inpatientName
        }));
        this.setData({
          typeOptions
        });
      } catch (error) {
        console.log(error, 'error');
      }
    },

    // 获取缴纳费用列表
    async getCashPledgeListRe() {
      try {
        const {
          requestDetail
        } = this.data;
        const hospitalList = await getCashPledgeList(requestDetail);
        const monthArray = {};
        hospitalList.forEach(item => {
          const year = this.getYears(item.payTime);
          const month = this.getMonth(item.payTime) + 1;
          const currentYear = day().get('year');

          if (currentYear === year) {
            item.month = `${month}月`;
          } else {
            item.month = `${year}年${month}月`;
          }

          if (!monthArray[item.month]) {
            monthArray[item.month] = [];
            monthArray[item.month].push(item);
          } else {
            monthArray[item.month].push(item);
          }
        });
        const hospitalListDetail = [];
        Object.keys(monthArray).forEach(res => {
          hospitalListDetail.push({
            month: res,
            value: monthArray[res]
          });
        });

        if (hospitalListDetail && hospitalListDetail.length > 0) {
          this.setData({
            hospitalList: hospitalListDetail
          });
        } else {
          this.setData({
            showScroll: false
          });
        }
      } catch (error) {
        console.log(error, 'error');
      }
    },

    // 选择不同住院人，或者时间改变时候重新请求列表
    onFilterHandel(filters) {
      const {
        requestDetail
      } = this.data;
      const {
        startTime,
        endTime,
        type
      } = filters;
      this.setData({
        requestDetail: { ...requestDetail,
          startTime,
          endTime,
          inpatientId: type
        }
      });
      this.getCashPledgeListRe();
    }

  }
});