import { base } from '@/utils/config';
import { checkSesskey, loginStatusPage, post } from '@/utils/https';
import { IIsLogin, Subscribe } from '@/utils/subscribe';
import { getStorage, toObject } from '@/utils/util';

Page({
  data: {
    version: wx.getAccountInfoSync().miniProgram.version || '开发1.0',
    isLogin: false,
    host: base.apiHost,
    user_info: {},
    company_info: {},
    loading: true,
    // staffType: StaffType,
    company_list: [],
    show: false,
    hideTabKeys: [],
  },
  init() {
    const companyInfo = toObject(getStorage('company_info'));
    this.setData({
      user_info: toObject(getStorage('user_info')),
      company_info: companyInfo,
    });
  },
  // 腾讯文档
  txDoc() {
    return;
    wx.navigateToMiniProgram({
      appId: 'wxd45c635d754dbf59',
      path:
        `pages/detail/detail?url=` +
        encodeURIComponent('https://docs.qq.com/aio/DS2NCRFFseG9Ma3Ja?p=7umJTJ6bznQtaBK2RSLmPD'),
    });
  },
  txOfficialAccount() {
    wx.openOfficialAccountChat({
      username: 'gh_bc65d0d9f0a0',
      fail: (res) => {
        console.log(res);
      },
    });
  },
  handleLogin(e: any) {
    this.setData({ isLogin: e.detail });
    if (e.detail) {
      this.init();
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    loginStatusPage(this);
  },
  loginOut() {
    wx.showModal({
      title: '确认退出?',
      // content: '',
      complete: (res: any) => {
        if (res.confirm) {
          post('Applet/loginOut').then(() => {
            Subscribe.set<IIsLogin>('isLogin', false);
            this.setData({ isLogin: false });
            checkSesskey({ showLoading: false, showError: false })
              .then(() => {})
              .catch((err) => {
                console.log('checkSesskey', err);
              });
          });
        }
      },
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

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

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

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

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

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

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