<template>
  <div>
    <div class="inner--headers">
      <h2>{{ pageId }}</h2>
      <div class="bread--crumbs--wrap">
        <span>홈</span>
        <span>{{ pageId }}</span>
      </div>
    </div>

    <div class="data--list--wrap">
      <div class="item--list--wrap">
        <div class="cs--list--wrap">
          <div class="cs--header">
            <h3>문의 등록</h3>
            <v-btn class="custom-btn" @click="addLocated()">문의 등록</v-btn>
          </div>
          <div class="cs--header">
            <h3>나의 문의 내역</h3>
          </div>
          <!-- 검색 영역 -->
          <div class="cs--search-area">
            <div class="search-controls">
              <div class="search-filter">
                <v-select
                  v-model="filter"
                  :items="filderArr"
                  variant="outlined"
                  density="compact"
                  hide-details
                ></v-select>
              </div>
              <div class="search-input">
                <v-text-field
                  v-model="searchModel"
                  placeholder="검색어를 입력하세요"
                  variant="outlined"
                  density="compact"
                  hide-details
                  prepend-inner-icon="mdi-magnify"
                  @keyup.enter="fnSearch(searchModel, filter)"
                ></v-text-field>
              </div>
              <div class="search-actions">
                <v-btn
                  class="custom-btn mini btn--pp"
                  color="primary"
                  @click="fnSearch(searchModel, filter)"
                >
                  검색
                </v-btn>
                <v-btn
                  class="custom-btn mini btn-white"
                  color="secondary"
                  variant="outlined"
                  @click="resetSearch"
                >
                  초기화
                </v-btn>
              </div>
            </div>
          </div>
          <div class="cs--list" v-if="csList.length > 0">
            <div v-for="(items, index) in paginatedItems" :key="index">
              <div class="list" @click="toItemDetail(items.SEQ)">
                <span class="list--seq">{{ items.SEQ }}</span>
                <span class="list--circle">{{ items.CATEGORY == 'D' ? '기능문의' : '기타문의' }}</span>
                <span class="list--writer">{{ items.NICK_NAME ? items.NICK_NAME : items.COMPANY_NAME }}</span>
                <span class="list--title">{{ items.TITLE }}</span>
                <span class="list--ml" :class="getStatusClass(items.STATUS)">{{ items.STATUS == '0' ? "답변대기" : "답변완료" }}</span>
                <span class="list--date">{{ formatDate(items.REGDATE) }}</span>
              </div>
            </div>
          </div>
          <div class="cs--list" v-else>
            <div class="no-data">
              <h4>등록된 문의가 없습니다</h4>
              <p>새로운 문의를 등록해보세요</p>
            </div>
          </div>
        </div>
        <div class="item--pagination" v-if="csList.length > 0">
          <v-pagination
            v-model="currentPage"
            :length="Math.ceil(csList.length / itemsPerPage)"
          ></v-pagination>
        </div>
      </div>
    </div>

    <!-- 문의 등록 팝업 -->
    <v-dialog v-model="showInquiryModal" max-width="600px" persistent>
      <v-card class="inquiry-modal">
        <v-card-title class="modal-header">
          <h3>문의 등록</h3>
          <v-btn icon @click="closeModal" class="close-btn">
            <v-icon>mdi-close</v-icon>
          </v-btn>
        </v-card-title>

        <v-card-text class="modal-body">
          <form @submit.prevent="submitInquiry">
            <div class="form-group">
              <label>문의 유형 <span class="required">*</span></label>
              <v-select
                v-model="inquiryForm.category"
                :items="categoryOptions"
                variant="outlined"
                placeholder="문의 유형을 선택하세요"
                class="custom-select"
                :rules="[v => !!v || '문의 유형을 선택해주세요']"
              ></v-select>
            </div>

            <div class="form-group">
              <label>제목 <span class="required">*</span></label>
              <v-text-field
                v-model="inquiryForm.title"
                variant="outlined"
                placeholder="문의 제목을 입력하세요"
                class="custom-input"
                :rules="[v => !!v || '제목을 입력해주세요']"
              ></v-text-field>
            </div>

            <div class="form-group">
              <label>내용 <span class="required">*</span></label>
              <v-textarea
                v-model="inquiryForm.content"
                variant="outlined"
                placeholder="문의 내용을 상세히 입력해주세요"
                rows="6"
                class="custom-textarea"
                :rules="[v => !!v || '내용을 입력해주세요']"
              ></v-textarea>
            </div>

            <div class="form-group">
              <label>첨부파일</label>
              <v-file-input
                v-model="inquiryForm.attachments"
                variant="outlined"
                placeholder="파일을 선택하세요"
                hide-details=""
                prepend-icon=""
                append-inner-icon="mdi-paperclip"
                class="custom-file-input mb--30"
                accept="image/*,.pdf,.doc,.docx,.hwp"
                multiple
                show-size
              ></v-file-input>
              <p class="file-info">* 이미지, PDF, 문서 파일만 업로드 가능 (최대 10MB)</p>
            </div>
          </form>
        </v-card-text>

        <v-card-actions class="modal-footer">
          <v-btn @click="closeModal" class="cancel-btn">취소</v-btn>
          <v-btn @click="submitInquiry" class="submit-btn" :loading="isSubmitting">등록하기</v-btn>
        </v-card-actions>
      </v-card>
    </v-dialog>
  </div>
</template>

<script setup>
import "@vuepic/vue-datepicker/dist/main.css";
import dayjs from 'dayjs';
  /************************************************************************
|    레이아웃
************************************************************************/
  definePageMeta({
    layout: "default",
  });
  /************************************************************************
|   PROPS
 ************************************************************************/
  const props = defineProps({
    propsData: {
      type: Object,
      default: () => {},
    },
  });
  /************************************************************************
|    스토어
 ************************************************************************/
  const useDtStore = useDetailStore();
  const useAtStore = useAuthStore();
  /************************************************************************
|    전역
 ************************************************************************/
  const memberType = useAtStore.auth.memberType;
  const searchModel = ref("");
  const selectedRange = ref('all');
  const searchStartDate = ref("");
  const searchEndDate = ref("");
  const dateOptions = [
    { label: '오늘', value: 'today' },
    { label: '7일', value: '7d' },
    { label: '1개월', value: '1m' },
    { label: '3개월', value: '3m' },
    { label: '전체', value: 'all' },
  ]
  const datePickerFormat = "yyyy-MM-dd";
  const filter = ref("");
  const filderArr = ref([
    { title: "전체", value: "" },
    { title: "제목", value: "title" },
    { title: "내용", value: "content" },
    { title: "작성자", value: "writer" }
  ]);
  const { $toast, $log, $dayjs, $eventBus } = useNuxtApp();
  const router = useRouter();
  const pageId = computed(() => {
    return '고객센터';
  });
  const csList = ref([]);
  const allCsList = ref([]); // 전체 데이터 저장용
  const itemsPerPage = 7;
  const currentPage = ref(1);

  // 문의 등록 팝업 관련
  const showInquiryModal = ref(false);
  const isSubmitting = ref(false);
  const inquiryForm = ref({
    category: '',
    title: '',
    content: '',
    attachments: []
  });
  const categoryOptions = ref([
    { title: '기능문의', value: 'D' },
    { title: '기타문의', value: 'E' }
  ]);

  /* eslint-disable */
  /* prettier-ignore */

  /************************************************************************
|    함수(METHODS)
************************************************************************/

  const isRecentUpdate = (dateStr) => {
    const today = new Date();
    const updateDate = new Date(dateStr);
    const diffDays = (today - updateDate) / (1000 * 60 * 60 * 24);
    // 업데이트 날짜가 오늘 날짜 기준 최근 7일인지 확인
    return diffDays <= 7;
  }

  const paginatedItems = computed(() => {
    const start = (currentPage.value - 1) * itemsPerPage;
    return csList.value.slice(start, start + itemsPerPage);
  });

  const setDateRange = (range) => {
    const today = dayjs();

    switch(range) {
      case 'today' :
        searchStartDate.value = today.format('YYYY-MM-DD');
        searchEndDate.value = today.format('YYYY-MM-DD');
        selectedRange.value = 'today';
        break;
      case '7d':
        searchStartDate.value = today.subtract(7, 'day').format('YYYY-MM-DD');
        searchEndDate.value = today.format('YYYY-MM-DD');
        selectedRange.value = '7d';
        break;
      case '1m':
        searchStartDate.value = today.subtract(1, 'month').format('YYYY-MM-DD');
        searchEndDate.value = today.format('YYYY-MM-DD');
        selectedRange.value = '1m';
        break;
      case '3m':
        searchStartDate.value = today.subtract(3, 'month').format('YYYY-MM-DD');
        searchEndDate.value = today.format('YYYY-MM-DD');
        selectedRange.value = '3m';
        break;
      case 'all':
        searchStartDate.value = "";
        searchEndDate.value = today.format('YYYY-MM-DD');
        selectedRange.value = 'all';
        break
    }
  }

  const addLocated = () => {
    showInquiryModal.value = true;
  };

  const toItemDetail = (__EVENT) => {
    router.push({
      path: "/view/common/cs/detail",
    });
    useDtStore.boardInfo.seq = __EVENT;
  };

  const csListGet = async () => {
    let _req = {
      USER_SEQ : useAtStore.auth.seq,
      keyword: '',
      filter: '',
      startDate: '',
      endDate: ''
    };
    if(useAtStore.auth.seq == 2){
      _req.USER_SEQ = 0;
    }

    await useAxios()
      .post("/cs/list", _req)
      .then((res) => {
        allCsList.value = res.data; // 전체 데이터 저장
        csList.value = res.data; // 초기 표시용
      });
  };

  const fnSearch = (__KEYWORD, __FILTER) => {
    let filteredItems = [...allCsList.value];
    
    // 키워드 검색
    if (__KEYWORD && __KEYWORD.trim() !== '') {
      const keyword = __KEYWORD.toLowerCase();
      filteredItems = filteredItems.filter(item => {
        if (__FILTER === 'title') {
          return item.TITLE && item.TITLE.toLowerCase().includes(keyword);
        } else if (__FILTER === 'content') {
          return item.CONTENT && item.CONTENT.toLowerCase().includes(keyword);
        } else if (__FILTER === 'writer') {
          // 작성자 검색: 닉네임, 회사명, 이름 모두 검색
          return (item.NICK_NAME && item.NICK_NAME.toLowerCase().includes(keyword)) ||
                 (item.COMPANY_NAME && item.COMPANY_NAME.toLowerCase().includes(keyword)) ||
                 (item.NAME && item.NAME.toLowerCase().includes(keyword));
        } else {
          // 전체 검색 (제목, 내용, 작성자 모두)
          return (item.TITLE && item.TITLE.toLowerCase().includes(keyword)) ||
                 (item.CONTENT && item.CONTENT.toLowerCase().includes(keyword)) ||
                 (item.NICK_NAME && item.NICK_NAME.toLowerCase().includes(keyword)) ||
                 (item.COMPANY_NAME && item.COMPANY_NAME.toLowerCase().includes(keyword)) ||
                 (item.NAME && item.NAME.toLowerCase().includes(keyword));
        }
      });
    }
    
    csList.value = filteredItems;
    currentPage.value = 1; // 검색 시 첫 페이지로 이동
  };

  // 검색 초기화
  const resetSearch = () => {
    searchModel.value = '';
    filter.value = '';
    csList.value = [...allCsList.value];
    currentPage.value = 1;
  };


  const goToDeliveryDetail = (item) => {
    // 제품 정보를 스토어에 저장
    useDtStore.boardInfo.seq = item.SEQ;
    useDtStore.boardInfo.pageType = "D";
    
    // 배송 관리 페이지로 이동
    router.push({
      path: "/view/common/deli/detail",
      query: {
        itemId: item.SEQ,
        itemName: item.NAME,
        price1: item.PRICE1,
        price2: item.PRICE2 || item.PRICE1,
        thumbFile: item.THUMB_FILE || ''
      }
    });
  };

  const getStatusClass = (status) => {
    switch(status) {
      case '0':
        return 'status-waiting';
      case '1':
        return 'status-completed';
    }
  };

  const formatDate = (dateStr) => {
    if (!dateStr) return '';
    const date = new Date(dateStr);
    const year = date.getFullYear();
    const month = String(date.getMonth() + 1).padStart(2, '0');
    const day = String(date.getDate()).padStart(2, '0');
    return `${year}.${month}.${day}`;
  };

  // 팝업 관련 함수들
  const closeModal = () => {
    showInquiryModal.value = false;
    resetForm();
  };

  const resetForm = () => {
    inquiryForm.value = {
      category: '',
      title: '',
      content: '',
      attachments: []
    };
  };

  const submitInquiry = async () => {
    // 폼 유효성 검사
    if (!inquiryForm.value.category) {
      $toast.error('문의 유형을 선택해주세요.');
      return;
    }
    if (!inquiryForm.value.title) {
      $toast.error('제목을 입력해주세요.');
      return;
    }
    if (!inquiryForm.value.content) {
      $toast.error('내용을 입력해주세요.');
      return;
    }

    isSubmitting.value = true;
    
    try {
      const formData = new FormData();
      formData.append('USER_SEQ', useAtStore.auth.seq);
      formData.append('CATEGORY', inquiryForm.value.category);
      formData.append('TITLE', inquiryForm.value.title);
      formData.append('CONTENT', inquiryForm.value.content);
      
      // 첨부파일 처리
      if (inquiryForm.value.attachments && inquiryForm.value.attachments.length > 0) {
        inquiryForm.value.attachments.forEach((file, index) => {
          formData.append(`files[${index}]`, file);
        });
      }

      await useAxios()
        .post("/cs/reg", formData, {
          headers: {
            'Content-Type': 'multipart/form-data'
          }
        })
        .then((res) => {
          if (res.data.success) {
            $toast.success('문의가 성공적으로 등록되었습니다.');
            closeModal();
            csListGet(); // 목록 새로고침
          } else {
            $toast.error('문의 등록에 실패했습니다.');
          }
        });
    } catch (error) {
      $toast.error('문의 등록 중 오류가 발생했습니다.');
      console.error('Error submitting inquiry:', error);
    } finally {
      isSubmitting.value = false;
    }
  };

  /************************************************************************
|    WATCH
************************************************************************/
  onMounted(() => {
    csListGet();

    // 날짜 초기화
    const today = dayjs();
    searchEndDate.value = today.format('YYYY-MM-DD');
  });
</script>