const { default: DPagination } = require("../../../utils/DPagination");
const { formatTimeTwo } = require("../../../utils/util");
const { getProjectNameLang } = require("../common");

// packageH/project_verification/WriteOffRecord/WriteOffRecord.js
const App = getApp();
const GetProjectsUrl = App.getNetAddresss("plugin.store-projects.frontend.project-service.get-list");
const GetStaffStatisticsUrl = App.getNetAddresss("plugin.store-projects.frontend.project-service.get-worker-summary");
const GetStoreStatisticsUrl = App.getNetAddresss("plugin.store-projects.frontend.project-service.get-store-summary");
const ProjectPagination = new DPagination("projects");
let requestOwnerType = 0;
Page({
  /**
   * 页面的初始数据
   */
  data: {
    pageType: "used",
    displayRecordTimeRangeSelect: false,
    storeName: "",
    staffName: "",
    projectName: "",
    staffKeyword: "",
    timeRange: {
      start: formatTimeTwo(Date.now() / 1000 - 604800, "Y-M-D"), //* 7天前
      end: formatTimeTwo(Date.now() / 1000, "Y-M-D"),
    },
    projects: {},
    projectCount: 0,
    statistics: null,
    projectNameLang: getProjectNameLang(),

    select_use_type_arr: ['全部', '核销使用', '转赠-转出'],
    select_use_type_index:0

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const pageType = options.type;
    switch (pageType) {
    case "store":
      wx.setNavigationBarTitle({
        title: "商户记录",
      });
      requestOwnerType = 2;
      this.getStoreStatistics();
      break;
    case "used":
      wx.setNavigationBarTitle({
        title: "使用记录",
      });
      requestOwnerType = 0;
      break;
    case "staff":
      wx.setNavigationBarTitle({
        title: "核销员记录",
      });
      requestOwnerType = 1;
      this.getStaffStatistics();
      break;
    }
    const setData = {
      pageType,
    };
    if (options.project_name) {
      setData["projectName"] = options.project_name;
    }
    this.setData(setData);
    ProjectPagination.bind(this);
    this.getProjects();
  },

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

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

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

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    ProjectPagination.clean();
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    ProjectPagination.clean();
    this.getProjects();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.getProjects();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
  displayCaleandar() {
    this.setData({
      displayRecordTimeRangeSelect: !this.data.displayRecordTimeRangeSelect,
    });
  },
  calendarConfirmSelect({ detail }) {
    this.setData({
      displayRecordTimeRangeSelect: false,
      timeRange: {
        start: formatTimeTwo(new Date(detail[0]).getTime() / 1000, "Y-M-D"),
        end: formatTimeTwo(new Date(detail[1]).getTime() / 1000, "Y-M-D"),
      },
    });
  },
  inputElChanged({ detail: { value }, target: { dataset } }) {
    this.setData({
      [dataset.name]: value,
    });
  },
  searchProjects(event) {
    ProjectPagination.clean();
    this.getProjects();
  },
  getProjects() {
    if (ProjectPagination.finished || ProjectPagination.loading) {
      return;
    }
    ProjectPagination.loading = true;
    wx.showLoading();
    App._getNetWork({
      url: GetProjectsUrl,
      data: {
        page: ProjectPagination.loadPage,
        owner_type: requestOwnerType,
        project_name: this.data.projectName,
        store_name: this.data.storeName,
        verify_kwd: this.data.staffName,
        start_date: this.data.timeRange.start,
        end_date: this.data.timeRange.end,
        use_type: this.data.select_use_type_index,
      },
      success: ({ data: { data: response, result, msg } }) => {
        if (result == 0) {
          wx.showToast({
            title: msg,
            icon: "none",
          });
          return;
        }
        wx.hideLoading();
        ProjectPagination.loading = false;
        wx.stopPullDownRefresh();
        if (response.last_page == response.current_page || response.data.length < response.per_page) {
          ProjectPagination.finished = true;
        }
        ProjectPagination.push(response.data);
        this.setData({
          projectCount: ProjectPagination.length,
        });
      },
    });
  },
  getStatistics(url) {
    App._getNetWork({
      url,
      success: ({ data: { data }, result, msg }) => {
        if (result === 0) {
          wx.showToast({
            title: msg,
            icon: "none",
          });
          return;
        }
        this.setData({
          statistics: data,
        });
      },
    });
  },
  getStaffStatistics() {
    this.getStatistics(GetStaffStatisticsUrl);
  },
  getStoreStatistics() {
    this.getStatistics(GetStoreStatisticsUrl);
  },
  dateRangeChanged({
    detail: { value },
    target: {
      dataset: { type },
    },
  }) {
    this.setData({
      [`timeRange.${type}`]: value,
    });
  },
  cleanTime() {
    if (this.data.timeRange.start && this.data.timeRange.end) {
      this.setData({
        timeRange: {
          start: "",
          end: "",
        },
      });
    }
  },
  onSelectType(){
    wx.showActionSheet({
      itemList: this.data.select_use_type_arr,
      success: (res)=> {
        this.setData({
          select_use_type_index:res.tapIndex
        });
        console.log(res.tapIndex);
      },
      fail (res) {
        console.log(res.errMsg);
      }
    });
  }
});
