import { onPageLoadInitAuth, post } from '@/utils/https';
import {
  cloneLite,
  getAuthInfo,
  getDataSet,
  searchValueFormat,
  toArray,
  toastSuccess,
  toNumber,
} from '@/utils/util';

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

Page({
  /**
   * 页面的初始数据
   */
  data: {
    params: cloneLite(defaultParams) as any,
    list: [] as any[],
    count: 0,
    dialog: {
      visible: false,
      content: '',
      item: {} as any,
      action: '' as 'end' | 'back',
    },
  },
  handleLogin(e: any) {
    this.setData({ isLogin: e.detail });
    if (e.detail) {
      this.init();
      this.getList();
    }
  },
  init() {
    this.setData({ authInfo: getAuthInfo() });
  },
  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);
  },
  tabChange(e: any) {
    const state = getDataSet(e).key;
    this.data.params.state = state;
    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 });
    return new Promise<void>((resolve, reject) => {
      post('CompanyProcessV2/processOrd', this.data.params)
        .then((res: any) => {
          const list = toArray(res.data);
          if (list.length == 0 && this.data.params.curr_page > 1) {
            this.getList(this.data.params.curr_page - 1);
          }
          this.setData({
            count: toNumber(res.count),
            list: list,
            isLogin: true,
          });
          resolve(res);
        })
        .catch((res) => {
          this.setData({ isLogin: false });
          reject(res);
        });
    });
  },
  confirmDialog() {
    post('ProduceOrder/setProcessV2', {
      info_process_id: JSON.stringify([this.data.dialog.item.info_process_id]),
      action: this.data.dialog.action,
    }).then(() => {
      this.closeDialog();
      toastSuccess(this.data.dialog.action == 'end' ? '订单工序完成成功' : '订单工序撤回成功');
      setTimeout(() => {
        this.getList(this.data.params.curr_page);
      }, 1500);
    });
  },
  closeDialog() {
    this.data.dialog.visible = false;
    this.setData({ dialog: this.data.dialog });
  },
  onEndOrBack(e: any) {
    const data = getDataSet(e);
    const index = data.index;
    const item = this.data.list[index];
    this.setData({
      dialog: {
        visible: true,
        content: `确认${data.action == 'end' ? '完成' : '撤回'} ${item.order_no} ?`,
        action: data.action,
        item,
      },
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    // console.log(options);
    wx.setNavigationBarTitle({
      title: decodeURIComponent(options.title || ''),
    });
    this.data.params.process_id = `${options.process_id || ''}`;
    onPageLoadInitAuth(this, () => this.getList());
  },

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

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

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

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

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

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

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