import { base, defaultAvatarUrl } from '@/utils/config';
import { login } from '@/utils/https';
import { IIsLogin, Subscribe } from '@/utils/subscribe';
import { getCurrentPageRoute, getStorage, isArray, setStorage } from '@/utils/util';
// import { IStorage } from "@/utils/storage";

// const app = getApp();
// const Storage: IStorage = app.Storage;

Component({
  options: { addGlobalClass: true },
  properties: {
    // isLogin: Boolean,
    // loading: Boolean,
    isAuth: null,
    customStyle: null,
    hasTabBar: null,
  },

  lifetimes: {
    attached() {
      this.setData({ isLogin: getStorage('isLogin') == 1 });
      // console.log('attached');
      Subscribe.on<IIsLogin>('isLogin', getCurrentPageRoute(), (data) => {
        // console.log(data);
        setStorage('isLogin', data ? 1 : 0);
        this.setData({ isLogin: data, loading: false });
        // console.log('监听到 isLogin 变化:');
      });
    },
    detached() {
      // console.log('detached');
      Subscribe.off('isLogin', getCurrentPageRoute());
    },
  },

  data: {
    avatarUrl: defaultAvatarUrl,
    isAgree: false,
    appletName: base.appletName,
    show: false,
    companyList: [],
    encryptedData: '',
    iv: '',
    isLogin: false,
    loading: true,
  },
  methods: {
    handleLogin(e: any) {
      this.triggerEvent('handleLogin', e.detail);
    },
    getPhoneNumberToast() {
      wx.showToast({ title: '请先勾选协议', icon: 'none' });
    },
    changeAgreementCheck(event: any) {
      this.setData({ isAgree: event.detail.checked });
    },
    navAgreement(e: any) {
      wx.navigateTo({
        url: '../agreement/agreement?type=' + e.target.dataset.type,
      });
    },
    // getPhoneNumber(e) {
    //   this.triggerEvent("getPhoneNumber", e);
    // },
    getPhoneNumber(e: any) {
      if (e.detail.encryptedData) {
        this.data.encryptedData = e.detail.encryptedData;
        this.data.iv = e.detail.iv;
        // console.log(e);

        login(e.detail.encryptedData, e.detail.iv)
          .then((res) => {
            // console.log(res);
            if (isArray(res.data)) {
              this.setData({
                companyList: res.data,
                show: true,
              });
            } else {
              this.triggerEvent('handleLogin', true);
            }
          })
          .catch(() => {
            this.triggerEvent('handleLogin', false);
          });
      } else {
        // wx.showToast({ title: e.detail.errMsg, icon: 'none' });
      }
    },
    login2(e: any) {
      console.log(e.currentTarget.dataset.company_id);
      // login(
      //   this.data.encryptedData,
      //   this.data.iv,
      //   2,
      //   e.currentTarget.dataset.company_id
      // )
      //   .then((res) => {
      //     console.log(res);
      //     if (isArray(res.data)) {
      //       this.setData({
      //         companyList: res.data,
      //       });
      //     } else {
      //       this.setData({
      //         show: false,
      //       });
      //       this.triggerEvent("handleLogin", true);
      //     }
      //   })
      //   .catch((err) => {
      //     this.triggerEvent("handleLogin", false);
      //   });
    },
    close() {
      this.setData({ show: false });
    },
  },
});
