"use strict";
const common_vendor = require("../../../common/vendor.js");
const common_libraries_apiLoading = require("../../../common/libraries/apiLoading.js");
const gen_Apis = require("../../../gen/Apis.js");
const ME_attendance_sign_clock_in_model = require("./model.js");
const common_libraries_naviHelper = require("../../../common/libraries/naviHelper.js");
require("../../../common/libraries/request.js");
const common_store_useWatermarkStore = require("../../../common/store/useWatermarkStore.js");
const common_store_useWeAppAuthStore = require("../../../common/store/useWeAppAuthStore.js");
require("../../../common/store/useWorkStore.js");
const common_libraries_mapDistance = require("../../../common/libraries/mapDistance.js");
const common_libraries_day = require("../../../common/libraries/day.js");
const common_libraries_tools = require("../../../common/libraries/tools.js");
common_store_useWeAppAuthStore.useWeAppAuthStore();
const watermark = common_store_useWatermarkStore.useWatermarkStore();
const getShow = (data, mapData) => {
  common_libraries_apiLoading.getApiLoading(gen_Apis.Apis.Attendance.AttendanceSchedules.GetAttendanceShiftPeriod, data).then((res) => {
    var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
    ME_attendance_sign_clock_in_model.useModel.showData.value = res == null ? void 0 : res.data;
    ME_attendance_sign_clock_in_model.useModel.distanceNumber.value = common_libraries_mapDistance.CalculateDistance(mapData, (_a = res == null ? void 0 : res.data) == null ? void 0 : _a.project);
    if ((_d = (_c = (_b = res == null ? void 0 : res.data) == null ? void 0 : _b.checkin_status) == null ? void 0 : _c.start) == null ? void 0 : _d.checked) {
      ME_attendance_sign_clock_in_model.useModel.current.value = 1;
    }
    if (((_g = (_f = (_e = res == null ? void 0 : res.data) == null ? void 0 : _e.checkin_status) == null ? void 0 : _f.start) == null ? void 0 : _g.checked) && ((_j = (_i = (_h = res == null ? void 0 : res.data) == null ? void 0 : _h.checkin_status) == null ? void 0 : _i.end) == null ? void 0 : _j.checked)) {
      ME_attendance_sign_clock_in_model.useModel.current.value = 3;
    }
    setTimeout(() => {
      ME_attendance_sign_clock_in_model.useModel.loading.value = true;
    }, 300);
    console.log(ME_attendance_sign_clock_in_model.useModel.metaData.value, "useModel.metaData.value");
  });
};
let checkInDebounceTimer = null;
let isCheckInSubmitting = false;
const getLocationMap = (fun) => {
  common_vendor.index.getLocation({
    type: "wgs84",
    altitude: true,
    isHighAccuracy: true,
    success(res) {
      console.log(res, "经纬度");
      ME_attendance_sign_clock_in_model.useModel.currentLocation.value = { latitude: res.latitude, longitude: res.longitude };
      common_vendor.index.hideLoading();
      return fun == null ? void 0 : fun(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, "获取经纬度失败");
    }
  });
};
const debouncedUpdateLocation = common_libraries_tools.debounce(function(callback) {
  getLocationMap(callback);
}, 1200);
const method = {
  handleUpdateLocation() {
    if (ME_attendance_sign_clock_in_model.useModel.mapNumber.value > 3) {
      return common_libraries_naviHelper.showToast("操作太频繁！");
    }
    common_vendor.index.showLoading({
      title: "加载中...",
      mask: true
    });
    debouncedUpdateLocation((res) => {
      var _a, _b, _c, _d, _e;
      ME_attendance_sign_clock_in_model.useModel.mapNumber.value += 1;
      common_vendor.index.hideLoading();
      ME_attendance_sign_clock_in_model.useModel.distanceNumber.value = common_libraries_mapDistance.CalculateDistance(
        (_b = (_a = ME_attendance_sign_clock_in_model.useModel) == null ? void 0 : _a.currentLocation) == null ? void 0 : _b.value,
        (_e = (_d = (_c = ME_attendance_sign_clock_in_model.useModel) == null ? void 0 : _c.showData) == null ? void 0 : _d.value) == null ? void 0 : _e.project
      );
    });
  },
  init() {
    var _a;
    let watermarkInfo = (_a = watermark == null ? void 0 : watermark.watermarkInfo) == null ? void 0 : _a.value;
    if (watermarkInfo == null ? void 0 : watermarkInfo.length) {
      ME_attendance_sign_clock_in_model.useModel.formData.value.photo = JSON.parse(JSON.stringify(watermarkInfo));
      watermark.watermarkInfo.value = [];
      console.log(watermarkInfo, "watermarkInfo");
    }
  },
  getLocation(data) {
    getLocationMap((res) => {
      getShow(data, res);
    });
  },
  onPreviewMedia(url) {
    console.log(url, "url1");
    if (url) {
      common_vendor.index.previewMedia({
        sources: [
          {
            url,
            type: "image"
          }
        ],
        current: 0
      });
    }
  },
  handleDelete() {
    ME_attendance_sign_clock_in_model.useModel.formData.value.photo = [];
  },
  handleCheckin() {
    var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u;
    if (isCheckInSubmitting) {
      return common_libraries_naviHelper.showToast("请勿重复点击");
    }
    if (!common_libraries_day.isWithinTimeRange(
      `${(_c = (_b = (_a = ME_attendance_sign_clock_in_model.useModel) == null ? void 0 : _a.formData) == null ? void 0 : _b.value) == null ? void 0 : _c.date} ${((_f = (_e = (_d = ME_attendance_sign_clock_in_model.useModel) == null ? void 0 : _d.formData) == null ? void 0 : _e.value) == null ? void 0 : _f.checkin_type) === "CheckOut" ? (_j = (_i = (_h = (_g = ME_attendance_sign_clock_in_model.useModel) == null ? void 0 : _g.showData) == null ? void 0 : _h.value) == null ? void 0 : _i.period) == null ? void 0 : _j.work_end_time : (_n = (_m = (_l = (_k = ME_attendance_sign_clock_in_model.useModel) == null ? void 0 : _k.showData) == null ? void 0 : _l.value) == null ? void 0 : _m.period) == null ? void 0 : _n.work_start_time}`
    )) {
      return common_libraries_naviHelper.showToast("不在打卡时间内！");
    }
    if (((_q = (_p = (_o = ME_attendance_sign_clock_in_model.useModel) == null ? void 0 : _o.attendanceConfigData) == null ? void 0 : _p.value) == null ? void 0 : _q.require_photo) && !((_u = (_t = (_s = (_r = ME_attendance_sign_clock_in_model.useModel) == null ? void 0 : _r.formData) == null ? void 0 : _s.value) == null ? void 0 : _t.photo) == null ? void 0 : _u.length)) {
      return common_libraries_naviHelper.showToast("请上传拍照！");
    }
    getLocationMap(() => {
      var _a2, _b2, _c2, _d2, _e2, _f2, _g2, _h2;
      let distanceNumber = common_libraries_mapDistance.CalculateDistance(
        (_b2 = (_a2 = ME_attendance_sign_clock_in_model.useModel) == null ? void 0 : _a2.currentLocation) == null ? void 0 : _b2.value,
        (_e2 = (_d2 = (_c2 = ME_attendance_sign_clock_in_model.useModel) == null ? void 0 : _c2.showData) == null ? void 0 : _d2.value) == null ? void 0 : _e2.project
      );
      ME_attendance_sign_clock_in_model.useModel.distanceNumber.value = distanceNumber;
      if (distanceNumber > ((_h2 = (_g2 = (_f2 = ME_attendance_sign_clock_in_model.useModel) == null ? void 0 : _f2.attendanceConfigData) == null ? void 0 : _g2.value) == null ? void 0 : _h2.check_in_range)) {
        return common_libraries_naviHelper.showToast("打卡距离超出范围！");
      }
      if (checkInDebounceTimer) {
        clearTimeout(checkInDebounceTimer);
      }
      isCheckInSubmitting = true;
      checkInDebounceTimer = setTimeout(() => {
        var _a3, _b3;
        common_libraries_apiLoading.getApiLoading(gen_Apis.Apis.Attendance.AttendanceRecords.Checkin, {
          ...ME_attendance_sign_clock_in_model.useModel.formData.value,
          ...(_b3 = (_a3 = ME_attendance_sign_clock_in_model.useModel) == null ? void 0 : _a3.currentLocation) == null ? void 0 : _b3.value
        }).then((res) => {
          var _a4, _b4, _c3, _d3;
          getShow((_b4 = (_a4 = ME_attendance_sign_clock_in_model.useModel) == null ? void 0 : _a4.formData) == null ? void 0 : _b4.value, (_d3 = (_c3 = ME_attendance_sign_clock_in_model.useModel) == null ? void 0 : _c3.currentLocation) == null ? void 0 : _d3.value);
          common_vendor.index.showToast({
            title: "打卡成功",
            icon: "success"
          });
        }).catch((error) => {
          console.error("打卡失败:", error);
          common_libraries_naviHelper.showToast("打卡失败，请重试");
        }).finally(() => {
          setTimeout(() => {
            isCheckInSubmitting = false;
          }, 1e3);
        });
      }, 500);
    });
  },
  YesNoCheckinStatus() {
    var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p;
    if (((_c = (_b = (_a = ME_attendance_sign_clock_in_model.useModel) == null ? void 0 : _a.formData) == null ? void 0 : _b.value) == null ? void 0 : _c.checkin_type) === "CheckOut" && !((_h = (_g = (_f = (_e = (_d = ME_attendance_sign_clock_in_model.useModel) == null ? void 0 : _d.showData) == null ? void 0 : _e.value) == null ? void 0 : _f.checkin_status) == null ? void 0 : _g.end) == null ? void 0 : _h.checked)) {
      return true;
    }
    if (((_k = (_j = (_i = ME_attendance_sign_clock_in_model.useModel) == null ? void 0 : _i.formData) == null ? void 0 : _j.value) == null ? void 0 : _k.checkin_type) === "CheckIn" && !((_p = (_o = (_n = (_m = (_l = ME_attendance_sign_clock_in_model.useModel) == null ? void 0 : _l.showData) == null ? void 0 : _m.value) == null ? void 0 : _n.checkin_status) == null ? void 0 : _o.start) == null ? void 0 : _p.checked)) {
      return true;
    }
    return false;
  },
  getAttendanceConfig() {
    common_libraries_apiLoading.getApiLoading(gen_Apis.Apis.Attendance.AttendanceSchedules.GetAttendanceConfig, {}).then((res) => {
      ME_attendance_sign_clock_in_model.useModel.attendanceConfigData.value = res == null ? void 0 : res.data;
    });
  },
  toUpload() {
    common_vendor.index.navigateTo({
      url: "/INDEX/watermark_camera/index?source=current"
    });
  }
};
exports.method = method;
