import { FinanceItemTypeArr, FinanceItemTypeObj } from '@/utils/config';
import { loginStatusPage, post } from '@/utils/https';
import {
  cloneLite,
  getAuthInfo,
  getDataSet,
  searchValueFormat,
  showModal,
  sleep,
  toArray,
  toastSuccess,
  toNumber,
} from '@/utils/util';

const defaultParams = { curr_page: 1, page_count: 20 };

Page({
  /**
   * 页面的初始数据
   */
  data: {
    params: cloneLite(defaultParams) as any,
    list: [] as any[],
    count: 0,
    sort: [{ label: '创建日期', value: 'create_date' }],
    FinanceItemTypeObj: FinanceItemTypeObj,
    FinanceItemTypeArr: [{ label: '全部', value: '' }, ...FinanceItemTypeArr],
  },
  handleLogin(e: any) {
    this.setData({ isLogin: e.detail });
    if (e.detail) {
      this.init();
    }
  },
  init() {
    this.setData({ authInfo: getAuthInfo() });
    this.getList();
  },
  searchChange(e: any) {
    const key = getDataSet(e).key;
    const val = searchValueFormat(e.detail.value);
    if (val) {
      this.data.params[key] = val;
    } else {
      delete this.data.params[key];
    }
    this.setData({ params: this.data.params });
  },
  searchOk() {
    this.getList(1);
  },
  searchReset() {
    this.data.params = cloneLite(defaultParams);
    this.getList(1);
  },
  onSort(e: any) {
    this.data.params.order = e.detail.value;
    this.setData({ params: this.data.params });
    this.getList(1);
  },
  paginationChange(e: any) {
    this.getList(e.detail.curr_page);
  },
  getList(curr: number = 1) {
    this.data.params.curr_page = curr;
    this.setData({ params: this.data.params });
    const temp = cloneLite(this.data.params);

    post('ErpFinanceItem/list', temp).then((res: any) => {
      const list = toArray(res.data?.list);
      // if (list.length == 0 && this.data.params.curr_page > 1) {
      //   this.getList(this.data.params.curr_page - 1);
      // }
      this.setData({ count: toNumber(res.data?.count), list: list });
    });
  },

  onOrderDel(e: any) {
    const data = getDataSet(e);
    const index = data.index;
    const item = this.data.list[index];
    showModal({ content: `确认删除 ${item.item_name}?` }).then(() => {
      post('ErpFinanceItem/del', { item_id: item.item_id }).then(() => {
        toastSuccess('删除成功');
        sleep(() => {
          this.getList();
        }, 1000);
      });
    });
  },
  onOrderState(e: any) {
    const data = getDataSet(e);
    const index = data.index;
    const item = this.data.list[index];
    showModal({
      content: `确认${item.state == 1 ? '禁用' : '启用'} ${item.item_name}?`,
    }).then(() => {
      // post('OrderCategory/setState', { order_cate_id: item.order_cate_id }).then(() => {
      //   toastSuccess(`${item.state == 1 ? '禁用' : '启用'}成功`);
      //   sleep(() => {
      //     this.getList();
      //   }, 1000);
      // });
    });
  },
  onOrderEdit(e: any) {
    const data = getDataSet(e);
    const index = data.index;

    wx.navigateTo({
      url: '/pages/base/item/edit/edit',
      success: (res) => {
        res.eventChannel.emit('itemEdit', {
          data: index == -1 ? {} : this.data.list[index],
        });
      },
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(_options) {},

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

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

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

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

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

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

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