import { getAction } from '../../../api/base';
const dayjs = require('dayjs');

Page({
  /**
   * 页面的初始数据
   */
  data: {
    current: 1,
    isLast: false,
    loading: false,
    list1: [],
    projectId: '', // 项目名
    startDate: '', // 开始日期
    endDate: '', // 结束日期
    startDateTmp: '',
    endDateTmp: '',
    showDateSelect: false, // 日期弹窗
    selectDate: '', // 开始/结束
    dateList: {
      // 日期列表
      years: [2023, 2024, 2025],
      months: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],
      date: [
        1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
        21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31
      ]
    },
    dateValue: [0, 0, 0]
  },
  // 获取数据
  getList() {
    let parms = {
      projectId: this.data.projectId,
      startTime: this.data.startDate + ' ' + '00:00:00',
      endTime: this.data.endDate + ' ' + '23:59:59',
      pageNo: this.data.current,
      pageSize: 10
    };
    wx.showLoading({
      title: '加载中'
    });

    if (!this.data.loading) {
      this.setData({
        loading: true
      });
    }
    getAction('api/finance/calculate/list', parms).then((res: any) => {
      wx.hideLoading();
      this.setData({
        loading: false
      });
      if (res.code == 200) {
        let data: any = { ...this.data };
        data.list1 = data.list1.concat(res.result.records);
        this.setData(data);
        if (data.list1.length == res.result.total) {
          this.setData({
            isLast: true
          });
        }
      } else {
        wx.showToast({
          title: res.message,
          icon: 'none'
        });
      }
    });
  },
  hidePicker() {
    this.setData({
      showDateSelect: false
    });
  },
  setProject(event: any) {
    let data: any = { ...this.data };
    // 初始化列表
    data.current = 1;
    data.isLast = false;
    data.list1 = [];
    data.projectId = event.detail.value;
    this.setData(data);
    this.getList();
  },
  // 跳转
  toDetail(event: any) {
    let { inf } = event.currentTarget.dataset;
    getAction('api/finance/calculate/getById', { id: inf.id })
      .then((res: any) => {
        if (res.success) {
          wx.setStorageSync('costEstimationData', {
            info: res.result,
            type: 'check'
          });
          wx.navigateTo({
            url: '../costEstimationDetail/costEstimationDetail',
            fail: e => {
              console.log(e);
            }
          });
        } else {
          wx.showToast({
            title: res.message,
            icon: 'none'
          });
        }
      })
      .catch((err: any) => {
        console.log(err);
      });
  },
  add() {
    wx.navigateTo({
      url: '../costEstimationAdd/costEstimationAdd',
      fail: e => {
        console.log(e);
      }
    });
  },
  // 日期相关--------------
  showDate(e: any) {
    let clickDate = null as any;
    if (e.currentTarget.dataset.key == 'startDate') {
      clickDate = this.data.startDate;
    } else {
      clickDate = this.data.endDate;
    }
    let y = this.data.dateList.years.findIndex(
      (x: number) => x == Number(clickDate.split('-')[0])
    );
    let m = this.data.dateList.months.findIndex(
      (x: number) => x == Number(clickDate.split('-')[1])
    );
    let d = this.data.dateList.date.findIndex(
      (x: number) => x == Number(clickDate.split('-')[2])
    );
    this.setData({
      showDateSelect: true,
      selectDate: e.currentTarget.dataset.key,
      startDateTmp: this.data.startDate,
      endDateTmp: this.data.endDate,
      dateValue: [y, m, d]
    });
  },
  // 确认选择时间
  changeDate(e: any) {
    if (
      new Date(this.data.startDateTmp).getTime() >
      new Date(this.data.endDateTmp).getTime()
    ) {
      wx.showToast({
        title: '结束时间不能早于开始时间',
        icon: 'none'
      });
      return;
    }
    this.setData({
      startDate: this.data.startDateTmp,
      endDate: this.data.endDateTmp,
      showDateSelect: false,
      list1: [],
      current: 1,
      isLast: false
    });
    this.getList();
  },
  // 选择时间
  selectDate(e: any) {
    let y: string | number = this.data.dateList.years[e.detail.value[0]];
    let m: string | number = this.data.dateList.months[e.detail.value[1]];
    let d: string | number = this.data.dateList.date[e.detail.value[2]];
    y = y < 10 ? '0' + y : y;
    m = m < 10 ? '0' + m : m;
    d = d < 10 ? '0' + d : d;
    if (this.data.selectDate == 'startDate') {
      this.setData({
        startDateTmp: `${y}-${m}-${d}`
      });
    } else {
      this.setData({
        endDateTmp: `${y}-${m}-${d}`
      });
    }
  },
  // 日期初始化
  initDate() {
    if (this.data.startDate) {
      this.getList();
    } else {
      let year = new Date().getFullYear();
      let mon = (new Date().getMonth() + 1) as any;
      let day =
        new Date().getDate() < 10
          ? '0' + new Date().getDate()
          : new Date().getDate();
      let years = [] as any;

      let sy = mon > 1 ? year : year - 1;
      let smon = mon > 1 ? mon - 1 : (12 as any);
      mon = mon < 10 ? '0' + mon : mon;
      smon = smon < 10 ? '0' + smon : smon;
      for (let i = -3; i < 6; i++) {
        years.push(year + i);
      }
      this.setData({
        dateList: {
          years: years,
          months: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],
          date: [
            1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
            20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31
          ]
        },
        dateValue: [0, 0, 0],

        startDate: `${sy}-${smon}-${day}`,
        endDate: `${year}-${mon}-${day}`,
        startDateTmp: `${sy}-${smon}-${day}`,
        endDateTmp: `${year}-${mon}-${day}`
      });
      this.getList();
    }
  },
  // 下拉
  lower(e: any) {
    if (this.data.isLast) {
      wx.showToast({
        title: '已经是最后一页',
        icon: 'none'
      });
      return;
    }
    if (e.detail.direction == 'bottom' && !this.data.loading) {
      // this.data.current += 1
      this.setData({
        loading: true,
        current: (this.data.current += 1)
      });
      this.getList();
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {},

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.setData({
      current: 1,
      isLast: false,
      list1: []
    });
    this.initDate();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    this.setData({
      current: 1,
      list1: []
    });
    this.getList();
    // 手动控制回弹
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {}
});
