import { post, onPageLoadInitAuth } from '@/utils/https';
import { menuConfig } from '@/utils/menuConfig';
import { getDataSet, toArray, toObject } from '@/utils/util';

// const app = getApp();
// const Storage = app.Storage;
Page({
  /**
   * 页面的初始数据
   */
  data: {
    isLogin: false,
    loading: true,
    BGColors: [
      'linear-gradient( 135deg, #FDEB71 10%, #F8D800 100%)',
      'linear-gradient( 135deg, #ABDCFF 10%, #0396FF 100%)',
      'linear-gradient( 135deg, #FEB692 10%, #EA5455 100%)',
      'linear-gradient( 135deg, #CE9FFC 10%, #7367F0 100%)',
      'linear-gradient( 135deg, #90F7EC 10%, #32CCBC 100%)',
      'linear-gradient( 135deg, #FFF6B7 10%, #F6416C 100%)',
      'linear-gradient( 135deg, #81FBB8 10%, #28C76F 100%)',
      'linear-gradient( 135deg, #E2B0FF 10%, #9F44D3 100%)',
      'linear-gradient( 135deg, #F97794 10%, #623AA2 100%)',
      'linear-gradient( 135deg, #FCCF31 10%, #F55555 100%)',
      'linear-gradient( 135deg, #F761A1 10%, #8C1BAB 100%)',
      'linear-gradient( 135deg, #43CBFF 10%, #9708CC 100%)',
      'linear-gradient( 135deg, #5EFCE8 10%, #736EFE 100%)',
      'linear-gradient( 135deg, #FAD7A1 10%, #E96D71 100%)',
      'linear-gradient( 135deg, #FFD26F 10%, #3677FF 100%)',
      'linear-gradient( 135deg, #A0FE65 10%, #FA016D 100%)',
      'linear-gradient( 135deg, #FFDB01 10%, #0E197D 100%)',
      'linear-gradient( 135deg, #FEC163 10%, #DE4313 100%)',
      'linear-gradient( 135deg, #92FFC0 10%, #002661 100%)',
      'linear-gradient( 135deg, #EEAD92 10%, #6018DC 100%)',
      'linear-gradient( 135deg, #F6CEEC 10%, #D939CD 100%)',
      'linear-gradient( 135deg, #52E5E7 10%, #130CB7 100%)',
      'linear-gradient( 135deg, #F1CA74 10%, #A64DB6 100%)',
      'linear-gradient( 135deg, #E8D07A 10%, #5312D6 100%)',
      'linear-gradient( 135deg, #EECE13 10%, #B210FF 100%)',
      'linear-gradient( 135deg, #79F1A4 10%, #0E5CAD 100%)',
    ],
    block: [] as any[],
    authInfo: {} as any,
    menuList: [] as any,
    blockUrls: {
      今日销售订单: '/pages/orders/ordersList/ordersList?type=today',
      今日请购订单: '/pages/purchase/requestOrder/requestOrder?type=today',
      今日采购订单: '/pages/purchase/buyOrder/buyOrder?type=today',
      今日采购入库: '/pages/purchase/buyInOrder/buyInOrder?type=today',
      今日原料出库单: '/pages/other/outbound/outbound?type=today',
      工作量查看: '/pages/base/workload/workload',
    } as any,
  },
  handleLogin(e: any) {
    this.setData({ isLogin: e.detail });
    if (e.detail) {
      this.init();
      this.getIndex();
    }
  },
  init() {
    this.data.authInfo = {};
    const companyInfo = toObject(wx.getStorageSync('company_info'));
    this.data.menuList.length = 0;
    menuConfig.forEach((el) => {
      const children: any[] = [];
      el.children.forEach((ell) => {
        // todo 权限判断
        // if (!ell.auth || this.data.authInfo[ell.auth]) {
        children.push(ell);
        // }
      });
      if (children.length) {
        this.data.menuList.push({
          ...el,
          children: children,
        });
      }
    });
    this.setData({
      authInfo: this.data.authInfo,
      menuList: this.data.menuList,
      hideTabKeys: companyInfo.staff_type == 3 ? ['2'] : [],
    });
  },
  getIndex() {
    return new Promise<void>((resolve, reject) => {
      post('Index/index', {})
        .then((res: any) => {
          const block = toArray(res?.block);
          if (block.length % 2 != 0) {
            block.push({ value: '', name: '工作量查看', unit: '' });
          }
          this.setData({ block, isLogin: true });
          resolve(res);
        })
        .catch((res) => {
          this.setData({ isLogin: false });
          reject(res);
        })
        .finally(() => {
          wx.stopPullDownRefresh();
        });
    });
  },
  onBlockNavTo(e: any) {
    const data = getDataSet(e);
    const item = this.data.block[data.index];
    const url = this.data.blockUrls[item.name];
    if (url) {
      wx.navigateTo({ url });
    }
  },
  nav(e: any) {
    const data = getDataSet(e);
    wx.navigateTo({ url: data.url });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {},

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

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    onPageLoadInitAuth(this, () => this.getIndex());
    wx.hideHomeButton();
  },

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

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

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    if (this.data.isLogin) {
      this.getIndex();
    } else {
      wx.stopPullDownRefresh();
    }
  },

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

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