// pages/my/index.js
import { alertInfo, alertSuccess } from "../../utils/index";
import Dialog from '@vant/weapp/dialog/dialog';
import { getUserInfo, logout } from "../../service/user";
import { getDot } from "../../utils/system";
import request from "../../utils/request"
import { getCurrentIntegral } from "../../service/system";
const { OK } = request;


Page({

  /**
   * 页面的初始数据
   */
  data: {
    user: {},
    tenement: {},
    visible: false,
    integral: 0,
  },

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

  },
  jumpToQuestions() {
    wx.navigateTo({
      url: '/pages/questions/index',
    })
  },
  jumpToMember() {
    wx.navigateTo({
      url: '/pages/member/index',
    })
  },
  jumpToQrCode() {
    const { user = {} } = this.data;
    if (!user?.id) {
      alertInfo("请先登录")
      return;
    }
    wx.navigateTo({
      url: '/pages/qrCode/index',
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    const user = wx.getStorageSync('user');
    if (!user || !user.id) {
      wx.switchTab({
        url: '/pages/home/index',
      })
      alertInfo("请先登录");
      return false;
    }
    this.init()
    this.getUnReadNumber()
    this.getIntegral()
    const tenement = wx.getStorageSync('tenement')
    this.setData({ tenement })
  },
  async getIntegral() {
    const { code, message, data } = await getCurrentIntegral();
    if (code !== OK) {
      alertInfo(message)
      return;
    }
    this.setData({
      integral: data?.balance || 0
    })
  },
  async init() {
    const tenement = wx.getStorageSync('tenement')
    const result = await getUserInfo(tenement?.id);
    if (result.code !== OK) {
      alertInfo(result.message)
      return;
    }
    this.setData({ user: result.data })
    wx.setStorageSync('user', result.data)
  },
  jumpToUpdateInvoice() {
    wx.navigateTo({
      url: '/pages/invoiceList/index?tab=2',
    })
  },
  jumpToWorkBench() {
    wx.navigateTo({
      url: '/pages/workBenchNew/index',
    })
  },
  jumpToIntegral() {
    wx.navigateTo({
      url: '/pages/integral/index',
    })
  },
  jumpToDiscountCoupon() {
    wx.navigateTo({
      url: '/pages/discountCoupon/index',
    })
  },
  async getUnReadNumber() {
    const dot = await getDot();
    this.setData({
      dot
    })
  },
  async logout() {
    Dialog.alert({
      title: '提示',
      message: '确认退出登录？',
      showCancelButton: true,
    }).then(async () => {
      const { code, message } = await logout()
      if (code !== OK) {
        alertInfo(message)
        return;
      } 
      wx.clearStorageSync()
      wx.redirectTo({
        url: '/pages/login/index',
      })
    });  
  },
  jumpToQuestions() {
    wx.navigateTo({
      url: '/pages/questions/index',
    })
  },
  bindTenement() {
    wx.navigateTo({
      url: '/pages/handleLogin/index',
    })
  },
  connect() {
    // Dialog.alert({
    //   title: '提示',
    //   message: '将进入咨询客服页面',
    //   "confirm-button-open-type": "contact",
    //   showCancelButton: true,
    // }).then(() => {
    //   // on close
    // });
    this.setData({
      connectShow: true
    })
  },
  bindConnect() {
    this.setData({
      connectShow: false
    })
  },
  bindCancelConnect() {
    this.setData({
      connectShow: false
    })
  },
  jumpToFinance() {
    wx.navigateTo({
      url: '/pages/aid/finance/index',
    })
  },
  jumpToLaw() {
    wx.navigateTo({
      url: '/pages/aid/law/index',
    })
  },
  jumpToEncyclopedia() {
    wx.navigateTo({
      url: '/pages/encyclopedia/index',
    })
  }
})