<template>
    <div class="container-xxl flex-grow-1 container-p-y">
        <div class="card">
            <div class="pb-4 rounded-top">
                <div class="container py-12 px-xl-10 px-4" style="padding-bottom: 0px !important">
                    <h3 class="text-center mb-2 mt-4">글 작성</h3>
                </div>
            </div>

            <div class="col-xl-12">
                <div class="card-body">
                    <FormInput
                        title="제목"
                        name="title"
                        :is-essential="true"
                        :is-alert="titleAlert"
                        v-model="title"
                    />

                    <!-- 카테고리 선택 -->
                    <div class="mb-4 d-flex align-items-center">
                        <label class="col-md-2 col-form-label">카테고리 <span class="text-danger">*</span></label>
                        <div class="d-flex flex-wrap align-items-center mt-3 ms-1">
                            <div
                                v-for="(category, index) in categoryList"
                                :key="index"
                                class="form-check me-3"
                            >
                                <input
                                    class="form-check-input"
                                    type="radio"
                                    :id="`category-${index}`"
                                    :value="category.CMNCODVAL"
                                    v-model="categoryValue"
                                />
                                <label class="form-check-label" :for="`category-${index}`">
                                    {{ category.CMNCODNAM }}
                                </label>
                            </div>
                        </div>
                        <div class="invalid-feedback" :class="categoryAlert ? 'display-block' : ''">카테고리를 선택해주세요.</div>
                    </div>

                    <!-- 비밀번호 필드 -->
                    <div v-if="categoryValue === 300102" class="mb-4">
                        <FormInput
                            title="비밀번호"
                            name="pw"
                            type="password"
                            :is-essential="true"
                            :is-alert="passwordAlert"
                            v-model="password"
                        />
                    </div>

                    <FormFile
                        title="첨부파일"
                        name="files"
                        :is-alert="attachFilesAlert"
                        @update:data="attachFiles = $event"
                        @update:isValid="isFileValid = $event"
                    />

                    <div class="mb-4">
                        <label for="html5-tel-input" class="col-md-2 col-form-label">
                            내용
                            <span class="text-danger">*</span>
                            <div class="invalid-feedback" :class="contentAlert ? 'display-block' : ''">내용을 확인해주세요.</div>
                        </label>
                        <div class="col-md-12">
                            <QEditor @update:data="content = $event" />
                        </div>
                    </div>

                    <div class="mb-4 d-flex justify-content-end">
                        <button type="button" class="btn btn-info" @click="goList">
                            <i class="bx bx-left-arrow-alt"></i>
                        </button>
                        <button
                            type="button"
                            class="btn btn-primary ms-1"
                            @click="write"
                            :disabled="!isFileValid"
                        >
                            <i class="bx bx-check"></i>
                        </button>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>

<script setup>
import QEditor from '@c/editor/QEditor.vue';
import FormInput from '@c/input/FormInput.vue';
import FormFile from '@c/input/FormFile.vue';
import { getCurrentInstance, ref, onMounted } from 'vue';
import router from '@/router';
import axios from '@api';

const categoryList = ref([]);
const title = ref('');
const password = ref('');
const categoryValue = ref(null);
const content = ref('');
const attachFiles = ref(null);
const isFileValid = ref(true);

const titleAlert = ref(false);
const passwordAlert = ref(false);
const contentAlert = ref(false);
const categoryAlert = ref(false);
const attachFilesAlert = ref(false);

const { appContext } = getCurrentInstance();
const $common = appContext.config.globalProperties.$common;

const fetchCategories = async () => {
    try {
        const response = await axios.get('board/categories');
        categoryList.value = response.data.data;
    } catch (error) {
        console.error('카테고리 불러오기 오류:', error);
    }
};

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

const goList = () => {
    router.push('/board');
};

const write = async () => {
    titleAlert.value = !title.value;
    passwordAlert.value = categoryValue.value === 300102 && !password.value;
    contentAlert.value = !content.value;
    categoryAlert.value = !categoryValue.value;

    if (titleAlert.value || passwordAlert.value || contentAlert.value || categoryAlert.value || !isFileValid.value) {
        return;
    }

    try {
        const boardData = {
            LOCBRDTTL: title.value,
            LOCBRDCON: $common.deltaAsJson(content.value),
            LOCBRDPWD: categoryValue.value === 300102 ? password.value : null,
            LOCBRDTYP: categoryValue.value
        };

        const { data: boardResponse } = await axios.post('board', boardData);
        const boardId = boardResponse.data;

        if (attachFiles.value && attachFiles.value.length > 0) {
            for (const file of attachFiles.value) {
                const formData = new FormData();
                const fileNameWithoutExt = file.name.replace(/\.[^/.]+$/, '');

                formData.append('CMNBRDSEQ', boardId);
                formData.append('CMNFLEORG', fileNameWithoutExt);
                formData.append('CMNFLEEXT', file.name.split('.').pop());
                formData.append('CMNFLESIZ', file.size);
                formData.append('CMNFLEPAT', 'boardfile');
                formData.append('file', file);

                await axios.post(`board/${boardId}/attachments`, formData, {
                    headers: {
                        'Content-Type': 'multipart/form-data',
                    },
                });
            }
        }

        alert('게시물이 작성되었습니다.');
        goList();
    } catch (error) {
        console.error(error);
        alert('게시물 작성 중 오류가 발생했습니다.');
    }
};
</script>
