<template>
    <div class="container-xxl flex-grow-1 container-p-y">
        <div class="card app-calendar-wrapper">
            <div class="row g-0">
                <!-- Sidebar: 사이드바 영역 -->
                <div class="col-3 border-end text-center" id="app-calendar-sidebar">
                    <div>

                        <div class="card-body">
                            <HalfDayButtons
                            ref="halfDayButtonsRef"
                            @toggleHalfDay="toggleHalfDay"
                            @addVacationRequests="saveVacationChanges"
                            :isDisabled="!hasChanges"
                            :selectedDate="selectedDate"
                            />
                            <ProfileList
                            @profileClick="handleProfileClick"
                            :remainingVacationData="remainingVacationData"
                            />
                        </div>
                        <VacationModal
                        v-if="isModalOpen"
                        :isOpen="isModalOpen"
                        :myVacations="filteredMyVacations"
                        :receivedVacations="filteredReceivedVacations"
                        :userColors="userColors"
                        @close="isModalOpen = false"
                        />
                        <VacationGrantModal
                        v-if="isGrantModalOpen"
                        :isOpen="isGrantModalOpen"
                        :targetUser="selectedUser"
                        :remainingQuota="remainingVacationData[selectedUser?.MEMBERSEQ] || 0"
                        @close="isGrantModalOpen = false"
                        @updateVacation="fetchRemainingVacation"
                        />
                    </div>
                </div>

                <!-- Main Content: 캘린더 영역 -->
                <div class="col app-calendar-content">
                    <div class="card shadow-none border-0">
                        <div class="card-body pb-0" style="position: relative;">
                        <full-calendar
                            ref="fullCalendarRef"
                            :options="calendarOptions"
                            class="flatpickr-calendar-only"
                        />
                        <!-- 숨겨진 데이트피커 인풋 -->
                        <input
                            ref="calendarDatepicker"
                            type="text"
                            style="display: none; position: absolute;"
                        />
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>

<script setup>
import { reactive, ref, onMounted, nextTick, computed, watch, onBeforeUnmount  } from "vue";
import axios from "@api";
import "bootstrap-icons/font/bootstrap-icons.css";
// 달력
import FullCalendar from "@fullcalendar/vue3";
import dayGridPlugin from "@fullcalendar/daygrid";
import interactionPlugin from "@fullcalendar/interaction";
// Flatpickr, MonthSelect
import flatpickr from "flatpickr";
import monthSelectPlugin from "flatpickr/dist/plugins/monthSelect/index";
import "flatpickr/dist/flatpickr.min.css";
import "flatpickr/dist/plugins/monthSelect/style.css";
// 컴포넌트
import HalfDayButtons from "@c/button/HalfDayButtons.vue";
import ProfileList from "@c/vacation/ProfileList.vue";
import VacationModal from "@c/modal/VacationModal.vue";
import VacationGrantModal from "@c/modal/VacationGrantModal.vue";
import { fetchHolidays } from "@c/calendar/holiday.js";
// 스토어
import { useUserStore } from "@s/userList";
import { useUserInfoStore } from "@s/useUserInfoStore";
import { useToastStore } from '@s/toastStore';
// 라우터
import { useRouter } from "vue-router";

const router = useRouter();
// 스토어
const toastStore = useToastStore();
const userStore = useUserInfoStore();
const userListStore = useUserStore();
// 프로필
const userList = ref([]);
const userColors = ref({});
// 휴가정보
const myVacations = ref([]);
const receivedVacations = ref([]);
const remainingVacationData = ref({});
// 모달상태
const isModalOpen = ref(false);
const isGrantModalOpen = ref(false);
// FullCalendar 및 이벤트 관련
const fullCalendarRef = ref(null);
const calendarEvents = ref([]);
const selectedDates = ref(new Map());
const halfDayType = ref(null);
const vacationCodeMap = ref({});
const holidayDates = ref(new Set());
const fetchedEvents = ref([]);
const halfDayButtonsRef = ref(null);
const selectedDate = ref(null);
const selectedUser = ref(null);
const lastRemainingYear = ref(new Date().getFullYear());
const lastRemainingMonth = ref(String(new Date().getMonth() + 1).padStart(2, "0"));
// 데이트피커 인풋 ref
const calendarDatepicker = ref(null);
let fpInstance = null;

/* 변경사항 여부 확인 */
const hasChanges = computed(() => {
    return (
        selectedDates.value.size > 0 ||
        myVacations.value.some(vac => selectedDates.value.has(vac.date.split("T")[0]))
    );
});

/* 캘린더 설정 */
// 풀 캘린더 옵션,이벤트
const calendarOptions = reactive({
    plugins: [dayGridPlugin, interactionPlugin],
    initialView: "dayGridMonth",
    headerToolbar: {
        left: "today",
        center: "title",
        right: "prev,next",
    },
    contentHeight:"auto",
    locale: "ko",
    selectable: false,
    dateClick: handleDateClick,
    datesSet: handleMonthChange,
    events: calendarEvents,
});
// 캘린더 월 변경
function handleMonthChange(viewInfo) {
    const currentDate = viewInfo.view.currentStart;
    const year = currentDate.getFullYear();
    const month = String(currentDate.getMonth() + 1).padStart(2, "0");
    loadCalendarData(year, month);
}
// 캘린더 클릭
function handleDateClick(info) {
    const clickedDateStr = info.dateStr;
    const clickedDate = info.date;
    const todayStr = new Date().toISOString().split("T")[0];

    if (
        clickedDate.getDay() === 0 ||
        clickedDate.getDay() === 6 ||
        holidayDates.value.has(clickedDateStr) ||
        clickedDateStr < todayStr
    ) {
        return;
    }
    const isMyVacation = myVacations.value.some(vac => {
        const vacDate = vac.date ? vac.date.substring(0, 10) : "";
        return vacDate === clickedDateStr && !vac.receiverId;
    });
    if (isMyVacation) {
        if (selectedDates.value.get(clickedDateStr) === "delete") {
            selectedDates.value.delete(clickedDateStr);
        } else {
            selectedDates.value.set(clickedDateStr, "delete");
        }
        updateCalendarEvents();
        return;
    }
    if (selectedDates.value.has(clickedDateStr)) {
        selectedDates.value.delete(clickedDateStr);
        updateCalendarEvents();
        return;
    }
    const type = halfDayType.value
        ? (halfDayType.value === "AM" ? "700101" : "700102")
        : "700103";
    selectedDates.value.set(clickedDateStr, type);
    halfDayType.value = null;
    updateCalendarEvents();
    if (halfDayButtonsRef.value) {
        halfDayButtonsRef.value.resetHalfDay();
    }
}
// 오늘 이후의 날짜만 클릭 가능하도록 설정
function markClickableDates() {
    nextTick(() => {
        const todayStr = new Date().toISOString().split("T")[0]; // 오늘 날짜 YYYY-MM-DD
        const todayObj = new Date(todayStr);

        document.querySelectorAll(".fc-daygrid-day").forEach((cell) => {
            const dateStr = cell.getAttribute("data-date");
            if (!dateStr) return; // 날짜가 없으면 스킵
            const dateObj = new Date(dateStr);
            // 주말 (토요일, 일요일)
            if (dateObj.getDay() === 0 || dateObj.getDay() === 6 || holidayDates.value.has(dateStr)) {
                cell.classList.remove("clickable");
                cell.classList.add("fc-day-sat-sun");
            }
            // 과거 날짜 (오늘 이전)
            else if (dateObj < todayObj) {
                cell.classList.remove("clickable");
                cell.classList.add("past"); // 과거 날짜 비활성화
            }
            // 오늘 & 미래 날짜 (클릭 가능)
            else {
                cell.classList.add("clickable");
                cell.classList.remove("past", "fc-day-sat-sun");
            }
        });
    });
}
//캘린더에 데이터 로드
async function loadCalendarData(year, month) {
    if (lastRemainingYear.value !== year || lastRemainingMonth.value !== month) {
        await fetchRemainingVacation();
        lastRemainingYear.value = year;
        lastRemainingMonth.value = month;
    }
    fetchedEvents.value = [];
    const [vacationEvents, holidayEvents] = await Promise.all([
        fetchVacationData(year, month),
        fetchHolidays(year, month),
    ]);
    holidayDates.value = new Set(holidayEvents.map((event) => event.start));
    fetchedEvents.value = [...vacationEvents, ...holidayEvents];
    updateCalendarEvents();
    await nextTick();
    fullCalendarRef.value.getApi().refetchEvents();
}

/* 프로필 구역 */
// 프로필 클릭 시 모달 열기
const handleProfileClick = async (user) => {
    try {
        if (isModalOpen.value && user.MEMBERSEQ === userStore.user.id) {
            return;
        }
        if (isGrantModalOpen.value && selectedUser.value?.MEMBERSEQ === user.MEMBERSEQ) {
            return;
        }
        isModalOpen.value = false;
        isGrantModalOpen.value = false;
        if (user.MEMBERSEQ === userStore.user.id) {
            const displayedYear = lastRemainingYear.value;
            await fetchVacationHistory(displayedYear);
            isModalOpen.value = true;
        } else {
            selectedUser.value = user;
            isGrantModalOpen.value = true;
        }
    } catch (error) {
        console.error("🚨 연차 데이터 불러오기 실패:", error);
    }
};
// 프로필 사원 리스트
const fetchUserList = async () => {
    try {
        await userListStore.fetchUserList();

        // "ROLE_ADMIN"을 제외하고 필터링
        const filteredUsers = userListStore.userList.filter(user => user.MEMBERROL !== "ROLE_ADMIN");

        // 필터링된 리스트를 userList에 반영
        userList.value = [...filteredUsers];

        if (!userList.value.length) {
            console.warn("📌 사용자 목록이 비어 있음!");
            return;
        }

        // 사용자별 색상 저장
        userColors.value = {};
        userList.value.forEach((user) => {
            userColors.value[user.MEMBERSEQ] = user.usercolor;
        });

    } catch (error) {
        console.error("📌 사용자 목록 불러오기 오류:", error);
    }
};
// 사원별 남은 연차 개수
const fetchRemainingVacation = async () => {
    try {
        const response = await axios.get("vacation/remaining");
        if (response.status === 200) {
            remainingVacationData.value = response.data.data.reduce((acc, vacation) => {
                acc[vacation.employeeId] = vacation.remainingQuota;
                return acc;
            }, {});
        }
    } catch (error) {
        console.error("🚨 남은 연차 데이터를 불러오지 못했습니다:", error);
    }
};
// 로그인한 사원이 사용한 휴가 필터링
const filteredMyVacations = computed(() => {
    return myVacations.value.filter(vac => {
        const dateStr = vac.date;
        const year = dateStr ? dateStr.split("T")[0].substring(0, 4) : null;
        return year === String(lastRemainingYear.value);
    });
});
// 로그인한 사원이 받은 휴가 필터링
const filteredReceivedVacations = computed(() => {
    return receivedVacations.value.filter(vac => {
        const dateStr = vac.date;
        const year = dateStr ? dateStr.split("T")[0].substring(0, 4) : null;
        return dateStr && year === String(lastRemainingYear.value);
    });
});

/* 휴가 저장 */
// 휴가 변경사항 저장
async function saveVacationChanges() {
    if (!hasChanges.value) return;

    const selectedDatesArray = Array.from(selectedDates.value);

    // 전체 연도를 고려하여 삭제 및 추가할 데이터를 분리
    const vacationChanges = selectedDatesArray.reduce((acc, [date, type]) => {
        if (type !== "delete") {
            acc.add.push({ date, type });
        } else {
            acc.delete.push(date);
        }
        return acc;
    }, { add: [], delete: [] });

    try {
        // 모든 연도의 데이터를 고려하여 삭제할 ID 찾기
        const allYears = new Set(vacationChanges.delete.map(date => date.split("-")[0]));
        let vacationIdsToDelete = [];
        for (const year of allYears) {
            await fetchVacationHistory(year);  // 각 연도의 최신 데이터를 가져오기
            const vacationsToDelete = myVacations.value.filter(vac => {
                if (!vac.date) return false;
                const vacDate = vac.date.split("T")[0];
                return vacationChanges.delete.includes(vacDate);
            });
            vacationIdsToDelete.push(...vacationsToDelete.map(vac => vac.id));
        }

        if (vacationChanges.add.length > 0 || vacationIdsToDelete.length > 0) {
            const response = await axios.post("vacation/batchUpdate", {
                add: vacationChanges.add,
                delete: vacationIdsToDelete,
            });
            if (response.data && response.data.status === "OK") {
                toastStore.onToast(`휴가 변경 사항이 저장되었습니다.`, 's');

                // 삭제된 ID를 반영하여 모든 연도의 `myVacations.value`를 업데이트
                myVacations.value = myVacations.value.filter(vac => !vacationIdsToDelete.includes(vac.id));

                // 삭제 후 최신 데이터 불러오기 (기존 데이터를 유지하면서 추가)
                const yearsToUpdate = new Set(
                    [...vacationChanges.add.map(v => v.date.split("-")[0]),
                        ...vacationChanges.delete.map(v => v.split("-")[0])]
                );

                for (const year of yearsToUpdate) {
                    const updatedVacations = await fetchVacationHistory(year);
                    if (updatedVacations) {
                        myVacations.value = [...myVacations.value, ...updatedVacations.filter(newVac =>
                            !myVacations.value.some(oldVac => oldVac.id === newVac.id)
                        )];
                    }
                }
            } else {
                toastStore.onToast(`휴가 변경 중 오류가 발생했습니다.`, 'e');
            }
        }
        await fetchRemainingVacation();
        selectedDates.value.clear();
        updateCalendarEvents();
        // 캘린더 새로고침
        const currentDate = fullCalendarRef.value.getApi().getDate();
        await loadCalendarData(currentDate.getFullYear(), currentDate.getMonth() + 1);
    } catch (error) {
        console.error("🚨 휴가 변경 저장 실패:", error);
        toastStore.onToast('휴가 저장 요청에 실패했습니다.', 'e');
    }
}
/* 휴가 조회 */
// 로그인 사용자의 연차 사용 내역
async function fetchVacationHistory(year) {
    try {
        const response = await axios.get(`vacation/history?year=${year}`);
        if (response.status === 200 && response.data) {
            myVacations.value = response.data.data.usedVacations || [];
            receivedVacations.value = response.data.data.receivedVacations || []
        }
    } catch (error) {
        console.error(`🚨 휴가 데이터 불러오기 실패:`, error);
    }
}
// 모든 사원 연차 내역 및 그래프화
async function fetchVacationData(year, month) {
    try {
        const response = await axios.get(`vacation/list/${year}/${month}`);
        if (response.status === 200) {
            const vacationList = response.data;
            // 회원 정보가 없거나 색상 정보가 없는 데이터는 제외
            const filteredVacations = vacationList.filter(vac =>
                userColors.value[vac.MEMBERSEQ] && userColors.value[vac.MEMBERSEQ] && !vac.LOCVACRMM
            );
            const events = filteredVacations.map(vac => {
                let dateStr = vac.LOCVACUDT ? vac.LOCVACUDT.split("T")[0] : "";
                let backgroundColor = userColors.value[vac.MEMBERSEQ];
                return {
                    title: getVacationType(vac.LOCVACTYP),
                    start: dateStr,
                    backgroundColor,
                    classNames: [getVacationTypeClass(vac.LOCVACTYP)],
                    saved: true,
                    memberSeq: vac.MEMBERSEQ,
                };
            }).filter(event => event.start);
            return events;
        } else {
            console.warn("📌 휴가 데이터를 불러오지 못함");
            return [];
        }
        } catch (error) {
        console.error("Error fetching vacation data:", error);
        return [];
    }
}
// 캘린더 이벤트 업데이트
function updateCalendarEvents() {
    const selectedEvents = Array.from(selectedDates.value)
        .filter(([date, type]) => type !== "delete")
        .map(([date, type]) => ({
            start: date,
            backgroundColor: "rgb(113 212 243 / 76%)",
            textColor: "#fff",
            display: "background",
            classNames: [getVacationTypeClass(type), "selected-event"]
        }));
    const filteredFetchedEvents = fetchedEvents.value.filter(event => {
        if (event.saved && selectedDates.value.get(event.start) === "delete") {
            if (event.memberSeq === userStore.user.id) {
                return false;
            }
        }
        return true;
    });
    calendarEvents.value = [...filteredFetchedEvents, ...selectedEvents];
}
// 휴가 종류에 따른 클래스명
const getVacationTypeClass = (type) => {
    if (type === "700101") return "half-day-am";
    if (type === "700102") return "half-day-pm";
    return "full-day";
};
// 휴가종류
const fetchVacationCodes = async () => {
    try {
        const response = await axios.get("vacation/codes");
        if (response.status === 200 && response.data) {
            vacationCodeMap.value = response.data.data.reduce((acc, item) => {
                acc[item.code] = item.name;
                return acc;
            }, {});
        } else {
            console.warn("❌ 공통 코드 데이터를 불러오지 못했습니다.");
        }
    } catch (error) {
        console.error("🚨 공통 코드 API 호출 실패:", error);
    }
};
const getVacationType = (typeCode) => {
    return vacationCodeMap.value[typeCode] || "기타";
};

/* 버튼 */
// 반차버튼 토글
function toggleHalfDay(type) {
    halfDayType.value = halfDayType.value === type ? null : type;
}

/* 페이지 이동 시 변경 사항 확인 */
router.beforeEach((to, from, next) => {
    if (hasChanges.value) {
        const answer = window.confirm("저장하지 않은 변경 사항이 있습니다. 이동하시겠습니까?");
        if (!answer) {
            return next(false);
        }
    }
    selectedDates.value.clear();
    next();
});
onBeforeUnmount(() => {
    window.removeEventListener("beforeunload", preventUnsavedChanges);

});
/* 새로고침 또는 페이지 종료 시 알림 */
function preventUnsavedChanges(event) {
    if (hasChanges.value) {
        event.preventDefault();
        event.returnValue = "";
    }
}

/* watch */
watch(() => lastRemainingYear.value, async (newYear, oldYear) => {
    if (newYear !== oldYear) {
        await fetchVacationHistory(newYear);
    }
});
// `selectedDates` 변경 시 반차 버튼 초기화
watch(
    () => Array.from(selectedDates.value.keys()), // 선택된 날짜 리스트 감시
    (newKeys) => {
        if (halfDayButtonsRef.value) {
            halfDayButtonsRef.value.resetHalfDay();
        }
    },
    { deep: true }
);
// selectedDates가 변경될 때 버튼 상태 즉시 업데이트
watch(
    () => Array.from(selectedDates.value.keys()),
    (newKeys) => {
    },
    { deep: true }
);
watch([holidayDates, lastRemainingYear, lastRemainingMonth], () => {
    markClickableDates();
});

/* onMounted */
// onMounted 및 달력 변경 시 실행
onMounted(() => {
    markClickableDates();
});
// onMounted 시 데이터 로드
onMounted(async () => {
    await userStore.userInfo();
    await fetchUserList();
    await fetchVacationCodes();
    const today = new Date();
    const year = today.getFullYear();
    const month = String(today.getMonth() + 1).padStart(2, "0");
    await fetchVacationData(year, month);
    await loadCalendarData(year, month);
    await fetchRemainingVacation();
    const currentYear = new Date().getFullYear();
    await fetchVacationHistory(currentYear);
    window.addEventListener("beforeunload", preventUnsavedChanges);
    // Flatpickr 초기화 (달 선택)
    fpInstance = flatpickr(calendarDatepicker.value, {
        dateFormat: "Y-m",
        plugins: [
            new monthSelectPlugin({
                shorthand: true,
                dateFormat: "Y-m",
                altFormat: "F Y"
            })
        ],
        onOpen: function() {
            document.querySelector('.flatpickr-input').style.visibility = 'hidden';
        },
        onChange: function(selectedDatesArr, dateStr) {
            // 선택한 달의 첫날로 달력을 이동
            fullCalendarRef.value.getApi().gotoDate(dateStr + "-01");
            const [year, month] = dateStr.split("-");
            lastRemainingYear.value = parseInt(year, 10);
            lastRemainingMonth.value = month;
            loadCalendarData(lastRemainingYear.value, lastRemainingMonth.value);
        },
        onClose: function() {
            calendarDatepicker.value.style.display = "none";
        }
    });
    // FullCalendar 년월월(.fc-toolbar-title) 클릭 시 데이트피커 열기
    nextTick(() => {
        const titleEl = document.querySelector('.fc-toolbar-title');
        if (titleEl) {
            titleEl.style.cursor = 'pointer';
            titleEl.addEventListener('click', () => {
                const dpEl = calendarDatepicker.value;
                dpEl.style.display = 'block';
                dpEl.style.position = 'fixed';
                dpEl.style.top = '25%';
                dpEl.style.left = '50%';
                dpEl.style.transform = 'translate(-50%, -50%)';
                dpEl.style.zIndex = '9999';
                dpEl.style.border = 'none';
                dpEl.style.outline = 'none';
                dpEl.style.backgroundColor = 'transparent';
                fpInstance.open();
            });
        }
    });
})
</script>
<style>
</style>
