import { base, defaultAvatarUrl } from '@/utils/config';
import { login } from '@/utils/https';
import { Subscribe } from '@/utils/subscribe';
import { 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,
  },

  // attached() {
  //   this.setData({ isLogin: getStorage("isLogin") == 1 });

  //   console.log("attached");
  //   Subscribe.on("isLogin", getCurrentPageRoute(), () => {
  //     this.setData({ isLogin: getStorage("isLogin") == 1 });
  //     console.log("监听到 isLogin 变化:");
  //   });
  // },
  // detached() {
  //   console.log("detached");
  //   Subscribe.off("isLogin", getCurrentPageRoute());
  // },

  data: {
    avatarUrl: defaultAvatarUrl,
    isAgree: false,
    appletName: base.appletName,
    show: false,
    companyList: [],
    encryptedData: '',
    iv: '',
  },
  methods: {
    handleLogin(e: any) {
      this.triggerEvent('handleLogin', e.detail);
    },
    getPhoneNumberToast() {
      setStorage('isLogin', 1);
      Subscribe.set('isLogin', 'zzzuz');
      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 });
    },
  },
});
