// pages/eventReport/eventReportRecord/eventReportRecord.ts
import { getAction } from '../../../api/base';
const dayjs = require('dayjs');
Page({
  /**
   * 页面的初始数据
   */
  data: {
    projectSelectShow: false,
    projectOptions: [
      {
        text: '全部',
        value: ''
      }
    ],
    selectProjectIndex: 0,
    timeSelectShow: false,
    reportTimestamp: new Date().getTime(),
    reportTimeStr: '',
    params: {
      reportTime: '',
      projectId: '',
      eventTheme: '',
      pageNo: 1,
      pageSize: 10
    } as any,
    list: [],
    loading: false,
    isLast: false
  },
  pickerChange(event: any) {
    console.log(event);
    //下拉框切换是否展示
    let fieldName: any = event.currentTarget.dataset.fieldname;
    let data: any = { ...this.data };
    data[fieldName] = true;
    data.isLast = true;
    this.setData(data);
  },
  hidePicker() {
    this.setData({
      projectSelectShow: false,
      timeSelectShow: false
    });
  },
  pickerConfirm(event: any) {
    //下拉框点击确认
    let { fieldname } = event.currentTarget.dataset;
    let detail = event.detail;
    let data: any = { ...this.data };
    if (fieldname === 'reportTime') {
      data.params[fieldname] =
        dayjs(detail).format('YYYY-MM-DD') +
        ' 00:00:00,' +
        dayjs(detail).format('YYYY-MM-DD') +
        ' 23:59:59';
      data.reportTimestamp = detail;
      data.reportTimeStr = dayjs(detail).format('YYYY-MM-DD');
    } else if (fieldname === 'projectId') {
      data.params[fieldname] = detail.value.value;
      data.selectProjectIndex = detail.index;
    } else {
      data.params[fieldname] = detail.value.value;
      // data.params[fieldvalue] = detail.value.value;
    }
    data.list = [];
    data.params.pageNo = 1;
    data.isLast = false;
    this.setData(data);
    this.hidePicker();
    this.getList();
  },
  getProjectOptions() {
    getAction('api/projects/list').then((res: any) => {
      if (res.code == 200) {
        res.result.unshift({
          id: '',
          projectName: '全部'
        });
        let arr = [] as any;
        res.result.forEach((item: any) => {
          arr.push({
            text: item.projectName,
            value: item.id
          });
        });
        this.setData({
          projectOptions: arr,
          selectProjectIndex: 0
          // selectProject: res.result[0]
        });
      } else {
        wx.showToast({
          title: res.message,
          icon: 'none'
        });
      }
    });
  },
  toDetail(e: any) {
    console.log('跳转', e.currentTarget.dataset.inf);
    wx.navigateTo({
      url: '../eventReportDetail/eventReportDetail',
      success: function (res) {
        // 通过eventChannel向被打开页面传送数据
        res.eventChannel.emit('eventName', { ...e.currentTarget.dataset.inf });
      },
      fail: e => {
        console.log(e);
      }
    });
    // // 接收传参
    // const eventChannel = this.getOpenerEventChannel();

    // eventChannel &&
    //   eventChannel.on &&
    //   eventChannel.on('eventName', data => {
    //     console.log('接收参数', data);
    //     this.setData({
    //       detailInf: data
    //     });
    //   });
  },
  eventReportAdd() {
    wx.navigateTo({
      url: '../eventReportAdd/eventReportAdd',
      // success: function (res) {
      //   // 通过eventChannel向被打开页面传送数据
      //   res.eventChannel.emit('eventName', { ...e.currentTarget.dataset.inf });
      // },
      fail: e => {
        console.log(e);
      }
    });
  },
  getList() {
    console.log(this.data);
    let parms = this.data.params;
    wx.showLoading({
      title: '加载中'
    });
    if (!this.data.loading) {
      this.setData({
        loading: true
      });
    }
    getAction('api/event/info/list', 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'
        });
      }
    });
  },
  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
      let { params } = this.data;
      params.pageNo = params.pageNo += 1;
      this.setData({
        loading: true,
        params: params
      });
      this.getList();
    }
  },

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

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

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    let params = {
      reportTime: '',
      projectId: '',
      // eventTheme: '', // 事件主题
      pageSize: 10,
      pageNo: 1
    };
    this.setData({
      reportTimestamp: new Date().getTime(),
      reportTimeStr: '',
      list: [],
      params: params,
      isLast: false
    });
    this.getList();
  },

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

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

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    let params = {
      reportTime: '',
      projectId: '',
      // eventTheme: '', // 事件主题
      pageSize: 10,
      pageNo: 1
    };
    this.setData({
      reportTimestamp: new Date().getTime(),
      reportTimeStr: '',
      list: [],
      params: params,
      isLast: false
    });
    this.getList();

    //停止下拉刷新
    wx.stopPullDownRefresh();
  },

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

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