// packageA/pages/refuelingCharging/RefuelingChargingRecord/RefuelingChargingRecord.ts
import { getAction } from '../../../../api/base';
const dayjs = require('dayjs');

Page({
  /**
   * 页面的初始数据
   */
  data: {
    startDate: '',
    endDate: '',
    startDateStr: '',
    endDateStr: '',
    startDateTmp: new Date().getTime(),
    endDateTmp: '',
    selectDate: 'startDate',
    showDateSelect: false,
    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],
    plateNumber: '',
    showSelect: {
      timeSelectShow: false
    },

    list: [],
    current: 1,
    isLast: false,
    loading: false
  },
  changePlateNumber(e: any) {
    console.log(e);
    this.setData({
      list: [],
      current: 1,
      isLast: false,
      loading: false,
      plateNumber: e.detail.value
    });
    this.getList();
  },
  getList() {
    console.log(this.data);
    let date = this.data.startDateStr.split('/').join('-');
    // let date2 = this.data.endDateStr.split('/').join('-');
    let parms = {
      recordDate: date,
      projectId: '',
      plateNumber: this.data.plateNumber,
      vehicleType: '',
      pageNo: this.data.current,
      pageSize: 10
    };
    wx.showLoading({
      title: '加载中'
    });
    if (!this.data.loading) {
      this.setData({
        loading: true
      });
    }
    getAction('api/gis/refuelChargeRecord/page', parms).then((res: any) => {
      wx.hideLoading();
      this.setData({
        loading: false
      });
      if (res.code == 200) {
        let data: any = { ...this.data };
        data.list = data.list.concat(res.result.records);
        console.log(data);
        this.setData(data);
        if (data.list.length == res.result.total) {
          this.setData({
            isLast: true
          });
        }
      } else {
        wx.showToast({
          title: res.message,
          icon: 'none'
        });
      }
    });
  },
  toDetail(e: any) {
    wx.navigateTo({
      url: '../RefuelingChargingDetail/RefuelingChargingDetail',
      success: function (res) {
        // 通过eventChannel向被打开页面传送数据
        res.eventChannel.emit('eventName', {
          ...e.currentTarget.dataset.inf
        });
      },
      fail: e => {
        console.log(e);
      }
    });
  },
  eventReportAdd() {
    // let that = this;
    let url = '../RefuelingChargingReporting/RefuelingChargingReporting';
    wx.navigateTo({
      url: url,
      success: function (res) {
        // 通过eventChannel向被打开页面传送数据
        res.eventChannel.emit('eventName', {
          isAdd: true
        });
      },
      fail: e => {
        console.log(e);
      }
    });
  },
  lower(e: any) {
    console.log(e.detail.direction == 'bottom');
    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();
    }
  },

  //下拉框相关----------
  pickerChange(event: any) {
    //下拉框切换是否展示
    let fieldName: any = event.currentTarget.dataset.fieldname;
    let data: any = { ...this.data };
    data.showSelect[fieldName] = true;
    this.setData(data);
  },
  hidePicker() {
    let showSelect = this.data.showSelect;
    showSelect = {
      timeSelectShow: false
    };
    this.setData({
      showSelect: showSelect
    });
  },
  pickerConfirm(event: any) {
    console.log(event);
    //下拉框点击确认
    let { fieldname, fieldindex } = event.currentTarget.dataset;
    let detail = event.detail;
    let data: any = { ...this.data };
    if (fieldname === 'startDate') {
      data['startDateStr'] = dayjs(new Date(detail)).format('YYYY-MM-DD');
    } else {
      data.params[fieldname] = detail.value.value;
      data[fieldindex] = detail.index;
    }

    // if (data.params[fieldname]) {
    //   // delete data.requireObj[fieldname];
    //   data.requireObj[fieldname] = '';
    // } else {
    //   data.requireObj[fieldname] = '此项为必填项';
    // }
    console.log(this.data);
    this.setData(data);
    this.hidePicker();

    this.setData({
      list: [],
      current: 1,
      isLast: false,
      loading: false
    });
    this.getList();
  },
  clearDate() {
    this.setData({
      startDateStr: '',
      list: [],
      current: 1,
      isLast: false,
      loading: false
    });
    this.getList();
  },
  // 下拉框相关----------end
  // 日期相关--------------
  showDate(e: any) {
    // selectDate
    let clickDate = null as any;
    if (e.currentTarget.dataset.key == 'startDate') {
      clickDate = this.data.startDateStr;
    } else {
      clickDate = this.data.endDateStr;
    }
    if (!clickDate) {
      let year = new Date().getFullYear();
      let mon =
        new Date().getMonth() + 1 < 10
          ? '0' + (new Date().getMonth() + 1)
          : new Date().getMonth() + 1;
      let day =
        new Date().getDate() < 10
          ? '0' + new Date().getDate()
          : new Date().getDate();
      clickDate = `${year}/${mon}/${day}`;
    }
    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])
    );
    // console.log(this.data.startDate.split('/'))
    console.log([y, m, d]);
    this.setData({
      showDateSelect: true,
      selectDate: e.currentTarget.dataset.key,

      startDateTmp:
        e.currentTarget.dataset.key == 'startDate'
          ? clickDate
          : this.data.startDateStr,
      endDateTmp:
        e.currentTarget.dataset.key == 'endDate'
          ? clickDate
          : this.data.endDateStr,
      dateValue: [y, m, d]
    });
  },
  hideDate() {
    this.setData({
      showDateSelect: false
    });
  },
  changeSelectDate(e: any) {
    this.setData({
      selectDate: e.currentTarget.dataset.key
    });
  },
  selectDate(e: any) {
    console.log(e.detail.value);
    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}`
      });
    }
  },
  changeDate(e: any) {
    console.log(
      new Date(this.data.startDateTmp).getTime(),
      new Date(this.data.endDateTmp).getTime()
    );
    if (
      new Date(this.data.startDateTmp).getTime() >
      new Date(this.data.endDateTmp).getTime()
    ) {
      wx.showToast({
        title: '结束时间不能早于开始时间',
        icon: 'none'
      });
      return;
    }
    this.setData({
      startDateStr: this.data.startDateTmp,
      endDateStr: this.data.endDateTmp,
      startDate: this.data.startDateTmp.slice(5, this.data.startDateTmp.length),
      endDate: this.data.endDateTmp.slice(5, this.data.endDateTmp.length),
      showDateSelect: false
    });
    this.getList();
  },
  // 日期初始化
  initDate() {
    let year = new Date().getFullYear();
    let mon =
      new Date().getMonth() + 1 < 10
        ? '0' + (new Date().getMonth() + 1)
        : ((new Date().getMonth() + 1) as any);
    let day =
      new Date(year, mon, 0).getDate() < 10
        ? '0' + new Date(year, mon, 0).getDate()
        : new Date(year, mon, 0).getDate();
    let sday =
      new Date(year, mon, 1).getDate() < 10
        ? '0' + new Date(year, mon, 1).getDate()
        : new Date(year, mon, 1).getDate();
    let years = [];
    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: `${mon}/${sday}`,
      endDate: `${mon}/${day}`,
      startDateStr: `${year}/${mon}/${sday}`,
      endDateStr: `${year}/${mon}/${day}`,
      startDateTmp: `${year}/${mon}/${sday}`,
      endDateTmp: `${year}/${mon}/${day}`
    });
  },
  // 日期相关--------------end
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    // this.initDate();
  },

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

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

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

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

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

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

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