// pages/wodeBill/index.js
const app = getApp();
import { timeShowXXX, getUserApplyOrderStatusById, formatDateYMDHM } from "../../utils/dateUtil";
import { customRequest } from '../../utils/request.js';
Page({
  /**
   * 页面的初始数据
   */
  data: {
    inputVal: "搜索账单",
    searchParam: {
      pageNum: 1,
      pageSize: 20,
      keys: "",
      status: "",
    },
    recordList: [],
    currIndex: 0,
    chaShowed: false,
    hasMoreData: false, // 获取更多数据是否显示
    isTrigger: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad (options) {
    console.log(options);
    if (options.state == 0) {
      this.data.searchParam.status = "";
    } else if (options.state == 1) {
      this.data.searchParam.status = 10;
    } else if (options.state == 2) {
      this.data.searchParam.status = 20;
    } else if (options.state == 3) {
      this.data.searchParam.status = 30;
    }
    this.setData({
      currIndex: options.state,
      searchParam: this.data.searchParam,
    });
  },

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

  /**
   * 生命周期函数--监听页面显示
   */
  onShow () {
    console.log(this.data.inputVal);
    this.data.searchParam.pageNum = 1;
    this.setData({
      recordList: [],
      searchParam: this.data.searchParam,
    });
    this.getList();
  },

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

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

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh () { },

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

  /**
   * 用户点击右上角分享
   */
onShareAppMessage() {
  return app.sharePageImage()
},
  /* 
    跳转搜索页面
  */
  goSearch () {
    wx.navigateTo({
      url: "../search/index?from=wodeBill",
    });
  },
  /* 
    搜索关键词
  */
  searchKey: function (e) {
    console.log("onsearch");
    console.log(this.data.inputVal);
    this.data.recordList = [];
    this.data.searchParam.pageNum = 1;
    // this.data.searchParam.keys = this.data.inputVal;
    this.getList();
  },
  /* 
  清空输入框
  */
  clearInput: function () {
    console.log(123);
    this.setData({
      inputVal: "搜索账单",
      chaShowed: false,
    });
    this.data.recordList = [];
    this.data.searchParam.keys = "";
    this.data.searchParam.pageNum = 1;
    this.getList();
  },
  onScrollToLower: function () {
    this.data.searchParam.pageNum = this.data.searchParam.pageNum + 1;
    this.getList();
  },
  onScrollRefresh () {
    this.data.recordList = [];
    this.data.searchParam.pageNum = 1;
    console.log("isonScrollRefresh");
    this.getList();
  },
  getList () {
    let that = this;
    console.log("loading");
    wx.showLoading({
      title: "加载中...",
    });
    this.data.searchParam.keys = this.data.inputVal == "搜索账单" ? "" : this.data.inputVal;
    customRequest("/yishoudan/factory/bill/list",{header:'headers', method: 'POST', data:  that.data.searchParam}).then((res)=>{
      if (res.data.status == 200) {
        res.data.data.pageBean.recordList.forEach((item) => {
          item.showTime = formatDateYMDHM(item.updateTime);
          if (item.totalSalary) {
            item.totalSalaryStr = Math.floor(item.totalSalary);
            item.totalSalaryDot = item.totalSalary.toFixed(2).slice(-2);
          }
        });

        if (res.data.data.pageBean.recordList != null && res.data.data.pageBean.recordList.length < 20) {
          var recordListTemp = res.data.data.pageBean.recordList;
          that.data.recordList = that.data.recordList.concat(recordListTemp);
          that.setData({
            hasMoreData: false,
          });
        } else {
          var recordListTemp = res.data.data.pageBean.recordList;
          that.data.recordList = that.data.recordList.concat(recordListTemp);
          that.setData({
            hasMoreData: true,
          });
        }
        that.setData({
          recordList: that.data.recordList,
          isTrigger: false,
        });
      } else if (res.data.status == 9999) {
        app.logout().then(() => {
          wx.reLaunch({
            url: "/pages/login/index",
          });
        });
      }
      wx.hideLoading();
    })
    // wx.request({
    //   url: app.globalData.ip + "/yishoudan/factory/bill/list",
    //   data: that.data.searchParam,
    //   header: app.globalData.headers,
    //   method: "POST",
    //   success: function (res) {
    //     console.log(res);
    //     if (res.data.status == 200) {
    //       res.data.data.pageBean.recordList.forEach((item) => {
    //         item.showTime = formatDateYMDHM(item.updateTime);
    //         if (item.totalSalary) {
    //           item.totalSalaryStr = Math.floor(item.totalSalary);
    //           item.totalSalaryDot = item.totalSalary.toFixed(2).slice(-2);
    //         }
    //       });

    //       if (res.data.data.pageBean.recordList != null && res.data.data.pageBean.recordList.length < 20) {
    //         var recordListTemp = res.data.data.pageBean.recordList;
    //         that.data.recordList = that.data.recordList.concat(recordListTemp);
    //         that.setData({
    //           hasMoreData: false,
    //         });
    //       } else {
    //         var recordListTemp = res.data.data.pageBean.recordList;
    //         that.data.recordList = that.data.recordList.concat(recordListTemp);
    //         that.setData({
    //           hasMoreData: true,
    //         });
    //       }
    //       that.setData({
    //         recordList: that.data.recordList,
    //         isTrigger: false,
    //       });
    //     } else if (res.data.status == 9999) {
    //       app.logout().then(() => {
    //         wx.reLaunch({
    //           url: "/pages/login/index",
    //         });
    //       });
    //     }
    //     wx.hideLoading();
    //   },
    // });

    // this.data.recordList.push('123')
  },
  /* 
    跳转账单详情页
  */
  goDetail: function (e) {
    console.log(e);
    var info = JSON.stringify({ ...e.currentTarget.dataset.info, status: this.data.currIndex });

    wx.navigateTo({
      // url: '/pages/editAgent/index?agencyId=' + id,
      // url:'/pages/agentDetail/index?agencyId=' + id
      url: "/pages/wodeBillDetail/index?info=" + info,
      // url: '/pages/wodeBillSharePage/index?id=' +  info// 路径，传递参数到指定页面。
    });
  },

  changeMenu (e) {
    var that = this;
    that.data.searchParam.pageNum = 1;
    // that.data.searchParam.status = e.currentTarget.dataset.id;
    if (e.currentTarget.dataset.id == 0) {
      this.data.searchParam.status = "";
    } else if (e.currentTarget.dataset.id == 1) {
      this.data.searchParam.status = 10;
    } else if (e.currentTarget.dataset.id == 2) {
      this.data.searchParam.status = 20;
    } else if (e.currentTarget.dataset.id == 3) {
      this.data.searchParam.status = 30;
    }

    this.setData({
      currIndex: e.currentTarget.dataset.id,
      recordList: [],
      searchParam: this.data.searchParam,
    });
    this.getList();
  },
});
