"use strict";
const common_vendor = require("../../common/vendor.js");
const common_assets = require("../../common/assets.js");
const common_util = require("../../common/util.js");
function setSignInfo(info) {
  const signInfo = { main: [info] };
  common_vendor.index.setStorageSync("signInfo", JSON.stringify(signInfo));
}
function addSignInfo(info, sign) {
  sign.main.push(info);
  common_vendor.index.setStorageSync("signInfo", JSON.stringify(sign));
}
function getSignInfo() {
  const sign = common_vendor.index.getStorageSync("signInfo");
  return sign ? JSON.parse(sign) : null;
}
function getInfo(signInfo) {
  const nowT = /* @__PURE__ */ new Date();
  return {
    mode: signInfo.mode,
    nowT,
    address: signInfo.address,
    time: signInfo.time,
    latitude: signInfo.latitude,
    longitude: signInfo.longitude,
    remarks: signInfo.remarks
  };
}
const key = "SOEBZ-J2BHS-7YSOI-64K33-6RO65-G2FEJ";
const _sfc_main = {
  __name: "clockIn",
  setup(__props) {
    const moduleColor = common_vendor.ref("CBlue");
    const amSign = common_vendor.ref({
      time: "",
      address: "",
      remarks: "",
      img: ""
    });
    const time = common_vendor.ref(common_util.formateDate(/* @__PURE__ */ new Date(), "h:min:s"));
    const week = common_vendor.ref("");
    const latitude = common_vendor.ref("");
    const longitude = common_vendor.ref("");
    const address = common_vendor.ref("我的位置");
    const allSign = common_vendor.ref([]);
    const signInfo = common_vendor.ref({
      mode: "",
      latitude: "",
      longitude: "",
      address: "",
      time: "",
      remarks: ""
    });
    const initializePage = async () => {
      const sign = getSignInfo();
      if (sign && sign.main) {
        const signA = [...sign.main].reverse();
        allSign.value = signA;
        checkIfSigned(signA);
      }
      await getLocation();
      getTime();
      getWeekDate();
    };
    const checkIfSigned = (signA) => {
      if (signA.length === 1 && common_util.isSameDay(signA[0].nowT)) {
        amSign.value = signA[0];
      } else if (signA.length > 1) {
        amSign.value = signA[1];
      }
    };
    const getWeekDate = () => {
      const now = /* @__PURE__ */ new Date();
      const weeks = ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"];
      week.value = weeks[now.getDay()];
    };
    const getAdd = async () => {
      try {
        const res = await common_vendor.index.request({
          url: `https://apis.map.qq.com/ws/geocoder/v1/?location=${latitude.value},${longitude.value}&key=${key}`
        });
        const data = res.data;
        if (data.status !== 0) {
          throw new Error(data.message);
        }
        let addressStr = `${data.result.address} ${data.result.formatted_addresses.recommend}`;
        address.value = addressStr;
        signInfo.value.address = addressStr;
      } catch (error) {
        common_vendor.index.showToast({
          title: error.message || "获取地址失败",
          icon: "none"
        });
      }
    };
    const getTime = async () => {
      setInterval(function() {
        time.value = common_util.formateDate(/* @__PURE__ */ new Date(), "h:min:s");
      }, 1e3);
    };
    const getLocation = async () => {
      try {
        common_vendor.index.showLoading({
          title: "获取中...",
          mask: true
        });
        const res = await common_vendor.index.getLocation({
          type: "gcj02"
        });
        common_vendor.index.hideLoading();
        latitude.value = res.latitude;
        longitude.value = res.longitude;
        updateLocation(res.latitude, res.longitude);
      } catch (error) {
        common_vendor.index.hideLoading();
        address.value = "请检查位置信息";
        common_vendor.index.showToast({
          title: "请检查位置信息状态！",
          icon: "none",
          mask: true,
          duration: 3e3
        });
      }
    };
    const updateLocation = (lat, lng) => {
      signInfo.value.latitude = lat;
      signInfo.value.longitude = lng;
      getAdd();
    };
    const clickSign = async () => {
      try {
        await getLocation();
        common_vendor.index.showLoading({
          title: "打卡记录中...",
          mask: true
        });
        signInfo.value.time = common_util.formateDate(/* @__PURE__ */ new Date(), "Y-M-D h:min:s");
        const a = getSignInfo();
        if (a) {
          addSignInfo(getInfo(signInfo.value), a);
        } else {
          setSignInfo(getInfo(signInfo.value));
        }
        const updatedSign = getSignInfo().main.reverse();
        allSign.value = updatedSign;
        amSign.value = updatedSign[0];
        common_vendor.index.showToast({
          title: "打卡成功！"
        });
      } catch (error) {
        common_vendor.index.showToast({
          title: "打卡失败，请稍后再试！",
          icon: "none",
          mask: true
        });
      } finally {
        common_vendor.index.hideLoading();
      }
    };
    common_vendor.onMounted(() => {
      initializePage();
    });
    return (_ctx, _cache) => {
      return common_vendor.e({
        a: common_vendor.t(time.value),
        b: common_vendor.n(moduleColor.value),
        c: common_vendor.o(clickSign),
        d: amSign.value.address
      }, amSign.value.address ? {
        e: common_vendor.t(amSign.value.time),
        f: common_vendor.t(amSign.value.address)
      } : {}, {
        g: common_assets._imports_0$3
      });
    };
  }
};
const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["__scopeId", "data-v-10d1d18a"]]);
wx.createPage(MiniProgramPage);
//# sourceMappingURL=../../../.sourcemap/mp-weixin/pages/clockIn/clockIn.js.map
