"use strict";
const common_vendor = require("../../common/vendor.js");
const utils_api = require("../../utils/api.js");
if (!Array) {
  const _easycom_t_loading2 = common_vendor.resolveComponent("t-loading");
  const _easycom_t_empty2 = common_vendor.resolveComponent("t-empty");
  const _easycom_t_tag2 = common_vendor.resolveComponent("t-tag");
  const _easycom_t_button2 = common_vendor.resolveComponent("t-button");
  const _easycom_t_stepper2 = common_vendor.resolveComponent("t-stepper");
  const _easycom_t_dialog2 = common_vendor.resolveComponent("t-dialog");
  (_easycom_t_loading2 + _easycom_t_empty2 + _easycom_t_tag2 + _easycom_t_button2 + _easycom_t_stepper2 + _easycom_t_dialog2)();
}
const _easycom_t_loading = () => "../../uni_modules/tdesign-uniapp/components/loading/loading.js";
const _easycom_t_empty = () => "../../uni_modules/tdesign-uniapp/components/empty/empty.js";
const _easycom_t_tag = () => "../../uni_modules/tdesign-uniapp/components/tag/tag.js";
const _easycom_t_button = () => "../../uni_modules/tdesign-uniapp/components/button/button.js";
const _easycom_t_stepper = () => "../../uni_modules/tdesign-uniapp/components/stepper/stepper.js";
const _easycom_t_dialog = () => "../../uni_modules/tdesign-uniapp/components/dialog/dialog.js";
if (!Math) {
  (_easycom_t_loading + _easycom_t_empty + _easycom_t_tag + _easycom_t_button + _easycom_t_stepper + _easycom_t_dialog)();
}
const _sfc_main = /* @__PURE__ */ common_vendor.defineComponent({
  __name: "booking",
  setup(__props) {
    const getLocalDateStr = () => {
      const now = /* @__PURE__ */ new Date();
      const year = now.getFullYear();
      const month = String(now.getMonth() + 1).padStart(2, "0");
      const day = String(now.getDate()).padStart(2, "0");
      return `${year}-${month}-${day}`;
    };
    const selectedDate = common_vendor.ref(getLocalDateStr());
    const timeslots = common_vendor.ref([]);
    const loading = common_vendor.ref(false);
    const showDialog = common_vendor.ref(false);
    const selectedSlot = common_vendor.ref(null);
    const peopleCount = common_vendor.ref(1);
    const notes = common_vendor.ref("");
    const isBooking = common_vendor.ref(false);
    const currentDate = common_vendor.ref(/* @__PURE__ */ new Date());
    const weekdays = ["日", "一", "二", "三", "四", "五", "六"];
    const dateBookings = common_vendor.ref({});
    const dateHasSlots = common_vendor.ref({});
    const currentDateStr = common_vendor.computed(() => {
      const year = currentDate.value.getFullYear();
      const month = currentDate.value.getMonth() + 1;
      return `${year}年${month}月`;
    });
    const totalBookingCount = common_vendor.computed(() => {
      return timeslots.value.reduce((sum, slot) => sum + slot.current_people, 0);
    });
    const calendarDays = common_vendor.computed(() => {
      const year = currentDate.value.getFullYear();
      const month = currentDate.value.getMonth();
      const firstDay = new Date(year, month, 1);
      const lastDay = new Date(year, month + 1, 0);
      const today = /* @__PURE__ */ new Date();
      const startWeekday = firstDay.getDay();
      const days = [];
      for (let i = startWeekday - 1; i >= 0; i--) {
        days.push({
          day: "",
          dateStr: "",
          isPlaceholder: true,
          isOtherMonth: false,
          isToday: false,
          bookingCount: 0,
          hasBookings: false
        });
      }
      for (let i = 1; i <= lastDay.getDate(); i++) {
        const date = new Date(year, month, i);
        const yearNum = date.getFullYear();
        const monthNum = date.getMonth() + 1;
        const dayNum = date.getDate();
        const dateStr = `${yearNum}-${String(monthNum).padStart(2, "0")}-${String(dayNum).padStart(2, "0")}`;
        const todayYear = today.getFullYear();
        const todayMonth = today.getMonth();
        const todayDay = today.getDate();
        const isToday = yearNum === todayYear && monthNum - 1 === todayMonth && dayNum === todayDay;
        days.push({
          day: i,
          dateStr,
          isPlaceholder: false,
          isOtherMonth: false,
          isToday,
          bookingCount: dateBookings.value[dateStr] || 0,
          hasBookings: dateHasSlots.value[dateStr] || false
        });
      }
      return days;
    });
    common_vendor.onMounted(() => {
      const token = common_vendor.index.getStorageSync("token");
      if (!token) {
        common_vendor.index.redirectTo({ url: "/pages/login/login" });
      } else {
        loadTimeSlots();
        loadDateBookings();
      }
    });
    const loadDateBookings = async () => {
      try {
        const slots = await utils_api.api.timeslots.getList({ is_active: true });
        const bookings = {};
        const slotsMap = {};
        slots.forEach((slot) => {
          const dateStr = slot.date.split("T")[0];
          bookings[dateStr] = (bookings[dateStr] || 0) + slot.current_people;
          slotsMap[dateStr] = true;
        });
        dateBookings.value = bookings;
        dateHasSlots.value = slotsMap;
      } catch (error) {
        common_vendor.index.__f__("error", "at pages/booking/booking.vue:230", "加载日期预约统计失败", error);
      }
    };
    const loadTimeSlots = async () => {
      loading.value = true;
      try {
        const slots = await utils_api.api.timeslots.getList({
          date: selectedDate.value,
          is_active: true
        });
        timeslots.value = slots;
      } catch (error) {
        common_vendor.index.__f__("error", "at pages/booking/booking.vue:244", "加载时间槽失败", error);
      } finally {
        loading.value = false;
      }
    };
    const selectDate = (day) => {
      selectedDate.value = day.dateStr;
      loadTimeSlots();
    };
    const formatTime = (timeStr) => {
      const timePart = timeStr.split("T")[1] || timeStr;
      const timeWithoutZone = timePart.split("+")[0].split("Z")[0];
      const [hours, minutes] = timeWithoutZone.split(":");
      return `${hours}:${minutes}`;
    };
    const showBookingModal = (slot) => {
      if (isBooking.value) {
        common_vendor.index.showToast({
          title: "正在处理，请稍候",
          icon: "none"
        });
        return;
      }
      if (!slot.is_active || slot.current_people >= slot.max_people) {
        common_vendor.index.__f__("log", "at pages/booking/booking.vue:278", "时间槽不可用", slot);
        common_vendor.index.showToast({
          title: "该时间段已不可用",
          icon: "none"
        });
        return;
      }
      selectedSlot.value = slot;
      peopleCount.value = 1;
      notes.value = "";
      showDialog.value = true;
      common_vendor.index.__f__("log", "at pages/booking/booking.vue:289", "打开弹窗", showDialog.value);
    };
    const confirmBooking = async () => {
      if (!selectedSlot.value)
        return;
      isBooking.value = true;
      try {
        await utils_api.api.appointments.create(selectedSlot.value.id, peopleCount.value, notes.value);
        common_vendor.index.showToast({
          title: "预约成功",
          icon: "success"
        });
        showDialog.value = false;
        loadTimeSlots();
        loadDateBookings();
      } catch (error) {
        showDialog.value = false;
      } finally {
        isBooking.value = false;
      }
    };
    return (_ctx, _cache) => {
      var _a, _b, _c, _d;
      return common_vendor.e({
        a: common_vendor.t(currentDateStr.value),
        b: common_vendor.f(weekdays, (day, k0, i0) => {
          return {
            a: common_vendor.t(day),
            b: day
          };
        }),
        c: common_vendor.f(calendarDays.value, (day, index, i0) => {
          return common_vendor.e({
            a: !day.isPlaceholder
          }, !day.isPlaceholder ? {
            b: common_vendor.t(day.day)
          } : {}, {
            c: !day.isPlaceholder && day.bookingCount > 0
          }, !day.isPlaceholder && day.bookingCount > 0 ? {
            d: common_vendor.t(day.bookingCount)
          } : {}, {
            e: day.dateStr || `placeholder-${index}`,
            f: day.isPlaceholder ? 1 : "",
            g: day.dateStr === selectedDate.value ? 1 : "",
            h: day.isToday ? 1 : "",
            i: !day.hasBookings && !day.isPlaceholder ? 1 : "",
            j: day.hasBookings ? 1 : "",
            k: common_vendor.o(($event) => !day.isPlaceholder && selectDate(day), day.dateStr || `placeholder-${index}`)
          });
        }),
        d: common_vendor.t(selectedDate.value),
        e: totalBookingCount.value > 0
      }, totalBookingCount.value > 0 ? {
        f: common_vendor.t(totalBookingCount.value)
      } : {}, {
        g: loading.value
      }, loading.value ? {
        h: common_vendor.p({
          loading: true
        })
      } : timeslots.value.length === 0 ? {
        j: common_vendor.p({
          description: "暂无可预约时间段"
        })
      } : {
        k: common_vendor.f(timeslots.value, (slot, k0, i0) => {
          return {
            a: common_vendor.t(formatTime(slot.start_time)),
            b: common_vendor.t(formatTime(slot.end_time)),
            c: common_vendor.t(slot.current_people),
            d: common_vendor.t(slot.max_people),
            e: "d331dabb-2-" + i0,
            f: common_vendor.p({
              theme: slot.current_people >= slot.max_people ? "danger" : "success",
              size: "small"
            }),
            g: common_vendor.t(slot.current_people >= slot.max_people ? "已满" : "预约"),
            h: common_vendor.o(($event) => showBookingModal(slot), slot.id),
            i: "d331dabb-3-" + i0,
            j: common_vendor.p({
              ["t-class"]: "btn-primary",
              size: "small",
              theme: "primary",
              disabled: !slot.is_active || slot.current_people >= slot.max_people
            }),
            k: slot.id,
            l: !slot.is_active || slot.current_people >= slot.max_people ? 1 : ""
          };
        })
      }, {
        i: timeslots.value.length === 0,
        l: common_vendor.t(selectedDate.value),
        m: common_vendor.t(((_a = selectedSlot.value) == null ? void 0 : _a.start_time) ? formatTime(selectedSlot.value.start_time) : ""),
        n: common_vendor.t(((_b = selectedSlot.value) == null ? void 0 : _b.end_time) ? formatTime(selectedSlot.value.end_time) : ""),
        o: common_vendor.o(($event) => peopleCount.value = $event),
        p: common_vendor.p({
          min: 1,
          max: ((_c = selectedSlot.value) == null ? void 0 : _c.max_people) - ((_d = selectedSlot.value) == null ? void 0 : _d.current_people) || 1,
          disabled: true,
          size: "small",
          modelValue: peopleCount.value
        }),
        q: common_vendor.o(confirmBooking),
        r: common_vendor.o(($event) => showDialog.value = false),
        s: common_vendor.o(($event) => showDialog.value = $event),
        t: common_vendor.p({
          title: "预约确认",
          cancelBtn: "取消",
          confirmBtn: "确认预约",
          ["t-class-confirm"]: "btn-primary",
          visible: showDialog.value
        })
      });
    };
  }
});
const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["__scopeId", "data-v-d331dabb"]]);
wx.createPage(MiniProgramPage);
//# sourceMappingURL=../../../.sourcemap/mp-weixin/pages/booking/booking.js.map
