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

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

Page({
  /**
   * 页面的初始数据
   */
  data: {
    params: cloneLite(defaultParams) as any,
    list: [] as any[],
    count: 0,
    goods_id: '',
    sort: [],
    goodsSubAttr: {} as any,
  },
  handleLogin(e: any) {
    this.setData({ isLogin: e.detail });
    if (e.detail) {
      this.init();
      this.getGoodsAttr();
    }
  },
  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();
  },
  searchReset() {
    this.data.params = cloneLite(defaultParams);
    this.getList();
  },
  onSort(e: any) {
    this.data.params.order = e.detail.value;
    this.setData({ params: this.data.params });
    this.getList();
  },
  getGoodsAttr() {
    return new Promise<void>((resolve, reject) => {
      post('GoodsAttr/list')
        .then((res: any) => {
          this.data.goodsSubAttr = {};
          toArray(res?.data?.list).forEach((el) => {
            toArray(el.sub_attr).forEach((ell) => {
              this.data.goodsSubAttr[ell.attr_id] = {
                ...ell,
                p_attr_name: el.attr_name,
              };
            });
          });
          // console.log(this.data.goodsSubAttr);
          this.setData({ goodsSubAttr: this.data.goodsSubAttr, isLogin: true });
          this.getList();
          resolve(res);
        })
        .catch((res) => {
          this.setData({ isLogin: false });
          reject(res);
        });
    });
  },
  getList() {
    post('ErpGoods/getSkuPrice', { goods_id: this.data.goods_id }).then((res: any) => {
      const list = toArray(res.data).map((el) => {
        el.attr_id_arr = `${el.attr_id || ''}`.split(',');
        el.attr_name_arr = `${el.attr_name || ''}`.split(',');
        return el;
      });

      // console.log(list);
      this.setData({ list });
    });
  },

  onOrderDel(e: any) {
    const data = getDataSet(e);
    const index = data.index;
    const item = this.data.list[index];
    showModal({ content: `确认删除 ${item.order_no} 订单?` }).then(() => {
      post('Orders/delSaleOrders', { order_no: JSON.stringify([item.order_no]) }).then(() => {
        toastSuccess('删除成功');
        sleep(() => {
          this.getList();
        }, 1000);
      });
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const { goods_id = '', title } = options;

    this.data.goods_id = goods_id;
    if (title) {
      wx.setNavigationBarTitle({ title: decodeURIComponent(title) });
    }
    onPageLoadInitAuth(this, () => this.getGoodsAttr());
  },

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

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

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

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

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

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

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