<script setup>
import { ref, watch, onMounted } from "vue";
import useActiveBtn from "@/hooks/useActiveBtn";
import useSearchParam from "@/hooks/useSearchParam";
import dayjs from "dayjs";
import { useI18n } from "vue-i18n";
const { t, locale } = useI18n();
const { searchParams, changeParams } = useSearchParam();

// 選時間或日期
const {
  items: searchTypeItems,
  changeActiveBtn,
  setItems,
  selectedBtn: selectedType,
} = useActiveBtn();

const itemsForStartTime = ref([]);

const itemsForEndTime = ref();

const initializeItems = () => {
  setItems([
    {
      title: t("history.time_range"),
      key: "dateRange",
      active: searchParams.value.Type
        ? parseInt(searchParams.value.Type) === 2
        : true,
      Type: 2,
    },
    // {
    //   title: t("history.time_range"),
    //   key: "startMonthTime",
    //   active: searchParams.value.Type
    //     ? parseInt(searchParams.value.Type) === 2
    //     : false,
    //   Type: 2,
    // },
  ]);

  itemsForStartTime.value = [
    {
      key: "Start_date",
      value: searchParams.value?.Start_date
        ? dayjs(searchParams.value?.Start_date)
        : dayjs(),
      dateFormat: "yyyy-MM-dd",
      placeholder: t("history.start_date"),
    },
    {
      key: "Start_time",
      value: { hours: 0, minutes: 0, seconds: 0 },
      dateFormat: "HH:mm",
      timePicker: true,
      placeholder: t("history.start_time"),
    },
  ];

  itemsForEndTime.value = [
    {
      key: "End_date",
      value: searchParams.value?.End_date
        ? dayjs(searchParams.value?.End_date)
        : dayjs(),
      dateFormat: "yyyy-MM-dd",
      placeholder:  t("history.end_date"),
    },
    {
      key: "End_time",
      value: {
        hours: dayjs().format("HH"),
        minutes: dayjs().format("mm"),
        seconds: 0,
      },
      dateFormat: "HH:mm",
      timePicker: true,
      placeholder:  t("history.end_time"),
    },
  ];
};

onMounted(() => {
  initializeItems();
});

watch(locale, () => {
  initializeItems();
});

watch(
  [itemsForStartTime, itemsForEndTime],
  ([newStart, newEnd]) => {
    if (newStart && newEnd) {
      const dates = Object.fromEntries(
        [newStart[0], newEnd[0]].map(({ key, value, dateFormat }) => [
          key,
          dayjs(value).format(dateFormat.toLocaleUpperCase()),
        ])
      );

      const times = Object.fromEntries(
        [newStart[1], newEnd[1]].map(
          ({ key, value, dateFormat }, index, arr) => [
            key,
            `${value.hours}:${value.minutes}`,
          ]
        )
      );
      changeParams({
        ...searchParams.value,
        ...dates,
        Type: selectedType.value.Type,
        Start_time: dayjs(`${dates.Start_date} ${times.Start_time}`).format(
          "HH:mm"
        ),
        End_time: dayjs(`${dates.End_date} ${times.End_time}`).format("HH:mm"),
      });
    }
  },
  {
    deep: true,
  }
);

watch(selectedType, (newVal, oldVal) => {
  if (newVal && newVal.Type != oldVal?.Type && newVal.sub_system_tag)
    changeParams({ ...searchParams.value, Type: newVal.Type });
});
</script>

<template>
  <ButtonGroup
    :items="searchTypeItems"
    :withLine="true"
    :onclick="
      (e, item) => {
        changeActiveBtn(item);
      }
    "
  />
  <DateGroup class="mr-3" :items="itemsForStartTime" :withLine="true" />
  <DateGroup :items="itemsForEndTime" :withLine="true" />
</template>

<style lang="scss" scoped></style>
