<template>
  <div>
    <app-board-header :h2data="compData.title"></app-board-header>

    <section class="bg-white dark:bg-gray-900 py-10 w-full min-h-[85vh]">
      <div v-if="!toggleEdit" class="mx-auto max-w-5xl w-full py-4 md:px-6">
        <app-board-body :board="board" />

        <app-board-action
          :listRouteName="compData.listRouteName"
          :uploadRouteName="compData.uploadRouteName"
          @edit="toggleEditBoard"
          @delete="handleDelete"
        />
      </div>

      <app-upload-notice-form
        v-if="toggleEdit"
        :isEdit="true"
        :board="board"
        @update-success="handleUpdateSuccess"
      />
    </section>

    <app-loading-overlay
      :isLoading="in_submission"
      :loadingMessage="deletingMessage"
    ></app-loading-overlay>
  </div>
</template>

<script setup lang="ts">
import { ref, computed, watch, onMounted } from 'vue';
import useUserStore from '@/stores/user';
import AppBoardHeader from '@/components/boards/BoardHeader.vue';
import AppBoardAction from '@/components/boards/BoardAction.vue';
import AppBoardBody from '@/components/boards/BoardBody.vue';
import AppLoadingOverlay from '@/components/LoadingOverlay.vue';
import { loadBoardDetails, deleteSingle } from '@/utils/boardUtils';
import type { BoardItem, OrderByDirection } from '@/types';
const { $firebase } = useNuxtApp();
const storage = $firebase.storage;

//***Variables, Things you need to change****)
const noticesCollection = $firebase.noticesCollection;
import AppUploadNoticeForm from '@/components/boards/notice/UploadNoticeForm.vue';
import AppUploadNotice from '~/pages/notice/upload.vue';
const currentCollection = noticesCollection;
const currentBoardRouteName = '/notice';
const compData = {
  title: '공지사항 | NOTICE',
  routeName: '/notice/[docId]', // path to single notice view
  itemsPerPage: 20,
  defaultSort: 'desc' as OrderByDirection,
  listRouteName: '/notice',
  uploadRouteName: '/notice/upload',
};

//
const in_submission = ref(false);
const deletingMessage = '삭제 중! 잠시만 기다려주세요...';

//Reactive variables
const router = useRouter();
const route = useRoute(); // Access the route object
const docId = computed(() => route.params.docId as string);

const board = ref<BoardItem>({
  docId: '',
  userId: '',
  title: '',
  description: '',
  announcement: false,
  ishidden: false,
  boards_number: 0,
  created: '',
  boardState: {
    state: 'pending',
    error: undefined,
  },
});

const userStore = useUserStore();
const userRole = computed(() => userStore.userRole);

const toggleEdit = ref(false);

const handleDelete = async () => {
  const result = await deleteSingle(board.value, currentCollection, storage);
  if (result) {
    router.push(currentBoardRouteName); // path string works directly in Nuxt3
  }
};

const handleUpdateSuccess = async () => {
  await loadBoardDetails(
    board.value.docId,
    board, // ref<BoardItem>
    currentCollection,
    router,
    currentBoardRouteName,
    userRole // Ref<number>
  );

  toggleEdit.value = false;
};

const toggleEditBoard = () => {
  toggleEdit.value = !toggleEdit.value;
};

watch(docId, (newId) => {
  if (!newId) return;
  console.log('newId', newId);
  loadBoardDetails(
    newId,
    board,
    currentCollection,
    router,
    currentBoardRouteName,
    userRole
  );
});

onMounted(() => {
  if (docId.value) {
    loadBoardDetails(
      docId.value,
      board,
      currentCollection,
      router,
      currentBoardRouteName,
      userRole
    );
  }
});
</script>

<style scoped>
.board {
  word-wrap: break-word;
}
</style>
