"use strict";
const common_vendor = require("../vendor.js");
const gen_Apis = require("../../gen/Apis.js");
const common_libraries_apiLoading = require("../libraries/apiLoading.js");
const wxworkId = "ww96c806131a95c739";
const wxAppId = "wx04d2d27ca198367b";
const useWeAppAuthStore = common_vendor.defineStore("we_app_auth", () => {
  const loading = common_vendor.ref(false);
  const hasError = common_vendor.ref(false);
  const data = common_vendor.ref({
    user: { id: 0, name: "", phone: "" },
    work_info: { session_key: "", openid: "" },
    permissions: [],
    environment: ""
  });
  const trackTimer = common_vendor.ref(null);
  const startTrackTimer = () => {
    if (trackTimer.value) {
      clearInterval(trackTimer.value);
      trackTimer.value = null;
    }
    trackTimer.value = setInterval(() => {
      setTrack();
    }, 5 * 60 * 1e3);
  };
  const setTrack = () => {
    common_vendor.index.getLocation({
      type: "wgs84",
      altitude: true,
      isHighAccuracy: true,
      success(res) {
        gen_Apis.Apis.Attendance.AttendanceEmployeeTracks.Track({ latitude: res.latitude, longitude: res.longitude });
        console.log(res, "经纬度");
      },
      fail(res) {
        common_vendor.index.hideLoading();
        common_vendor.index.showModal({
          title: "提示",
          content: "定位失败，员工轨迹失效！请打开定位！",
          success: function(res2) {
            if (res2.confirm) {
              common_vendor.index.openSetting();
              console.log("用户点击确定");
            } else if (res2.cancel) {
              console.log("用户点击取消");
            }
          }
        });
        console.log(res, "获取经纬度失败");
      }
    });
  };
  function login(app) {
    const res = common_vendor.index.getSystemInfoSync();
    data.value.environment = res == null ? void 0 : res.environment;
    console.log(res == null ? void 0 : res.environment, "res?.environment");
    if ((res == null ? void 0 : res.environment) === "wxwork") {
      common_vendor.wx$1.qy.login({
        success: function(res2) {
          console.log(res2);
          getWorkToken(app, res2.code);
        }
      });
    } else {
      common_vendor.index.login({
        provider: "weixin",
        //使用微信登录
        success: function(loginRes) {
          console.log(loginRes);
          getWXToken(app, loginRes.code);
        }
      });
    }
  }
  const getWorkToken = (app, code) => {
    gen_Apis.Apis.Login.Auth.WorkLogin({ code, app_id: wxworkId }).then((res) => {
      var _a, _b, _c, _d, _e, _f, _g, _h;
      console.log("企业微信登录", res == null ? void 0 : res.data);
      data.value.user = (_a = res.data) == null ? void 0 : _a.user;
      data.value.work_info = (_b = res.data) == null ? void 0 : _b.work_info;
      data.value.permissions = (_d = (_c = res.data) == null ? void 0 : _c.permissions[0]) == null ? void 0 : _d.children;
      loading.value = false;
      common_vendor.index.setStorageSync("ACCESS_TOKEN_CUSTOMER", (_f = (_e = res == null ? void 0 : res.data) == null ? void 0 : _e.token) == null ? void 0 : _f.token);
      if ((_h = (_g = res == null ? void 0 : res.data) == null ? void 0 : _g.user) == null ? void 0 : _h.phone) {
        setTrack();
        startTrackTimer();
      }
      app == null ? void 0 : app.appContext.config.globalProperties.$isResolve();
    }).catch(() => {
      loading.value = false;
      hasError.value = true;
    });
  };
  const getWXToken = (app, code) => {
    gen_Apis.Apis.Login.Auth.Login({ code, app_id: wxAppId }).then((res) => {
      var _a, _b, _c, _d, _e, _f, _g;
      console.log("登录", res == null ? void 0 : res.data);
      data.value.user = (_a = res.data) == null ? void 0 : _a.user;
      data.value.permissions = (_c = (_b = res.data) == null ? void 0 : _b.permissions[0]) == null ? void 0 : _c.children;
      loading.value = false;
      common_vendor.index.setStorageSync("ACCESS_TOKEN_CUSTOMER", (_e = (_d = res == null ? void 0 : res.data) == null ? void 0 : _d.token) == null ? void 0 : _e.token);
      if ((_g = (_f = res == null ? void 0 : res.data) == null ? void 0 : _f.user) == null ? void 0 : _g.phone) {
        setTrack();
        startTrackTimer();
      }
      app == null ? void 0 : app.appContext.config.globalProperties.$isResolve();
    }).catch(() => {
      loading.value = false;
      hasError.value = true;
    });
  };
  function me(fun) {
    common_libraries_apiLoading.getApiLoading(gen_Apis.Apis.Login.Auth.Me, {}).then((res) => {
      var _a, _b;
      data.value.user = res.data.user;
      data.value.permissions = (_b = (_a = res.data) == null ? void 0 : _a.permissions[0]) == null ? void 0 : _b.children;
      console.log("me", res.data);
      loading.value = false;
      hasError.value = false;
      return fun();
    });
  }
  const handleUserLoginWxwork = async (from_data) => {
    var _a, _b;
    common_libraries_apiLoading.getApiLoading(gen_Apis.Apis.Login.Auth.BindWork, {
      app_id: wxAppId,
      session_key: (_b = (_a = data == null ? void 0 : data.value) == null ? void 0 : _a.work_info) == null ? void 0 : _b.session_key,
      iv: from_data == null ? void 0 : from_data.iv,
      encrypted_data: from_data == null ? void 0 : from_data.encryptedData
    }).then((res) => {
      me(() => {
        common_vendor.index.navigateBack({ delta: 1 });
      });
    });
  };
  const handleUserLogin = async (from_data) => {
    common_libraries_apiLoading.getApiLoading(gen_Apis.Apis.Login.Auth.BindPhoneNumber, {
      app_id: wxAppId,
      ...from_data
    }).then((res) => {
      me(() => {
        common_vendor.index.navigateBack({ delta: 1 });
      });
    });
  };
  return {
    loading,
    hasError,
    data,
    login,
    me,
    handleUserLogin,
    handleUserLoginWxwork
  };
});
exports.useWeAppAuthStore = useWeAppAuthStore;
