import dayjs from 'dayjs';
import { reportCmPV } from "../../../../../../core/utils";
import request from "../../../../../../core/utils/request";
import { paymentUrl, expenseUrl } from "../../../../../../core/utils/constants";
import monitor from "../../../../../../core/utils/alipayLogger";
import { getCurrentPageQuery } from '../../../../utils/';
const now = dayjs().format('YYYY-MM');
Component({
  isReportPaymentCmPV: false,
  isReportExpenseCmPV: false,
  props: {
    success: false
  },
  data: {
    total: '',
    // 支出明细，本月总支出
    date: '',
    // 支出选择的时间
    startDate: '',
    // 开始时间（每月第一天）
    endDate: '',
    tabs: [{
      title: '缴费明细',
      id: '1'
    }, {
      title: '支出明细',
      id: '2'
    }]
  },

  didUpdate(preProps) {
    const preSuccess = preProps.success;
    const {
      success
    } = this.props;

    if (success && success !== preSuccess) {
      // 解决吸顶高度不准确，等summary渲染完成后重新获取高度
      this.ref.getTabBarTop();
    }
  },

  didMount() {
    const date = dayjs().format('YYYY-MM');
    const startDate = dayjs().startOf('month').format('YYYY-MM-DD');
    const endDate = dayjs().endOf('month').format('YYYY-MM-DD');
    this.setData({
      date,
      startDate,
      endDate
    });
  },

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

    async onLoadData(params) {
      const {
        startDate,
        endDate
      } = this.data;
      const {
        current,
        activeTab
      } = params;
      const reqParam = {
        url: '',
        isShowLoadig: true,
        isShowError: true,
        method: 'GET',
        data: {
          pageSize: 10,
          page: current
        }
      };
      const query = getCurrentPageQuery.call(this);
      console.log('query', query); // 缴费明细

      if (activeTab === 0) {
        if (!this.isReportPaymentCmPV) {
          reportCmPV({
            title: '参保状态',
            query
          });
          reportCmPV({
            title: '账户余额',
            query
          });
          reportCmPV({
            title: '缴存明细',
            query
          });
          this.isReportPaymentCmPV = true;
        }

        reqParam.url = paymentUrl;
      } // 支出明细


      if (activeTab === 1) {
        if (!this.isReportExpenseCmPV) {
          reportCmPV({
            title: '消费明细',
            query: {}
          });
          this.isReportExpenseCmPV = true;
        }

        reqParam.url = expenseUrl;
        reqParam.data.startDate = startDate;
        reqParam.data.endDate = endDate;
      }

      const startTime = Date.now();
      const [err, res = {}] = await request(reqParam);

      if (err) {
        return this.setData({
          total: ''
        });
      }

      console.log('===res-err', [err, res]);
      const endTime = Date.now();
      const time = endTime - startTime;

      if (reqParam.url === paymentUrl) {
        monitor.api({
          api: '缴存明细',
          success: true,
          c1: "taSR_YL",
          time
        });
      } else if (reqParam.url === expenseUrl) {
        monitor.api({
          api: "消费明细",
          success: true,
          c1: "taSR_YL",
          time
        });
      }

      const {
        totalCount,
        total,
        detail = []
      } = res;
      console.log('明细列表接口', reqParam, res);
      this.setData({
        total
      });
      return {
        total: totalCount,
        list: detail
      };
    },

    // 支出明细，选择月份
    onChooseDate() {
      const currentDate = this.data.date;
      my.datePicker({
        format: 'yyyy-MM',
        currentDate,
        // startDate: '',
        endDate: now,
        success: res => {
          const {
            date
          } = res;
          const startDate = dayjs(date).startOf('month').format('YYYY-MM-DD');
          const endDate = dayjs(date).endOf('month').format('YYYY-MM-DD');
          this.setData({
            date,
            startDate,
            endDate
          }); // 重新加载明细数据

          this.ref.resetData();
        }
      });
    }

  }
});