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

    <div class="product-register--form">
      <div class="form--container">
        <div class="form--section">
          <h3>기본 정보</h3>
          <div class="form--row">
            <div class="form--field">
              <label class="form--label">제품명 <span class="required">*</span></label>
              <v-text-field
                v-model="form.productName"
                placeholder="제품명을 입력하세요"
                class="custom-input"
                :rules="[rules.required]"
              ></v-text-field>
            </div>
          </div>

          <div class="form--row">
            <div class="form--field">
              <label class="form--label">공급가 <span class="required">*</span></label>
              <v-text-field
                v-model="form.supplyPrice"
                type="number"
                placeholder="공급가를 입력하세요"
                class="custom-input"
                :rules="[rules.required]"
              ></v-text-field>
            </div>
            <div class="form--field">
              <label class="form--label">판매가 <span class="required">*</span></label>
              <v-text-field
                v-model="form.sellPrice"
                type="number"
                placeholder="판매가를 입력하세요"
                class="custom-input"
                :rules="[rules.required]"
              ></v-text-field>
            </div>
          </div>

          <div class="form--row">
            <div class="form--field">
              <label class="form--label">배송비 <span class="required">*</span></label>
              <v-text-field
                v-model="form.shippingCost"
                placeholder="배송비 정보를 입력하세요 (예: 3,000원, 무료배송)"
                class="custom-input"
                :rules="[rules.required]"
              ></v-text-field>
            </div>
          </div>

          <div class="form--row">
            <div class="form--field">
              <label class="form--label">소타이틀</label>
              <v-text-field
                v-model="form.subtitle"
                placeholder="소타이틀을 입력하세요"
                class="custom-input"
              ></v-text-field>
            </div>
          </div>
        </div>

        <div class="form--section">
          <h3>상세 정보</h3>
          <div class="form--row">
            <div class="form--field full-width">
              <label class="form--label">상세내용</label>
              <div class="editor--container">
                <div class="editor--toolbar">
                  <v-btn-toggle v-model="editorMode" density="compact" class="editor--mode-toggle">
                    <v-btn value="html" size="small">HTML</v-btn>
                    <v-btn value="text" size="small">TEXT</v-btn>
                  </v-btn-toggle>
                </div>
                <div v-show="editorMode === 'html'" class="html-editor">
                  <textarea
                    v-model="form.detailContent"
                    placeholder="HTML 내용을 입력하세요"
                    class="html-textarea"
                    rows="15"
                  ></textarea>
                </div>
                <div v-show="editorMode === 'text'" class="text-editor">
                  <v-textarea
                    v-model="form.detailContent"
                    placeholder="텍스트 내용을 입력하세요"
                    class="custom-input"
                    rows="15"
                  ></v-textarea>
                </div>
              </div>
            </div>
          </div>
        </div>

        <div class="form--section">
          <h3>첨부파일</h3>
          <div class="form--row">
            <div class="form--field">
              <label class="form--label">상세다운로드</label>
              <div class="file-upload--container">
                <input
                  type="file"
                  ref="fileInput"
                  @change="handleFileUpload"
                  accept=".zip"
                  class="file-input"
                  style="display: none"
                />
                <div class="file-upload--area" @click="triggerFileUpload">
                  <div v-if="!form.detailFile" class="file-upload--placeholder">
                    <i class="upload-icon">📎</i>
                    <p>ZIP 파일을 선택하세요</p>
                    <small>클릭하여 파일 선택</small>
                  </div>
                  <div v-else class="file-upload--selected">
                    <i class="file-icon">📁</i>
                    <div class="file-info">
                      <p class="file-name">{{ form.detailFile.name }}</p>
                      <small class="file-size">{{ formatFileSize(form.detailFile.size) }}</small>
                    </div>
                    <v-btn
                      @click.stop="removeFile"
                      class="file-remove"
                      size="small"
                      color="error"
                      icon="mdi-close"
                    ></v-btn>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>

        <div class="form--section">
          <h3>상태 설정</h3>
          <div class="form--row">
            <div class="form--field">
              <label class="form--label">상태 <span class="required">*</span></label>
              <v-select
                v-model="form.status"
                :items="statusOptions"
                variant="outlined"
                class="custom-select"
                :rules="[rules.required]"
              ></v-select>
            </div>
            <div class="form--field">
              <label class="form--label">노출상태 <span class="required">*</span></label>
              <v-select
                v-model="form.displayStatus"
                :items="displayStatusOptions"
                variant="outlined"
                class="custom-select"
                :rules="[rules.required]"
              ></v-select>
            </div>
          </div>
        </div>

        <div class="form--section">
          <h3>업데이트 내역</h3>
          <div class="form--row">
            <div class="form--field full-width">
              <label class="form--label">업데이트 내역</label>
              <v-textarea
                v-model="form.updateHistory"
                placeholder="업데이트 내역을 입력하세요 (최대 500자)"
                class="custom-input"
                rows="5"
                :counter="500"
                :rules="[rules.maxLength(500)]"
              ></v-textarea>
            </div>
          </div>
        </div>

        <div class="form--actions">
          <v-btn
            class="custom-btn btn-white"
            @click="goBack"
          >
            취소
          </v-btn>
          <v-btn
            class="custom-btn btn-blue"
            @click="saveProduct"
            :loading="loading"
          >
            저장
          </v-btn>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
/************************************************************************
|    레이아웃
************************************************************************/
definePageMeta({
  layout: "default",
});

/************************************************************************
|    스토어
************************************************************************/
const useDtStore = useDetailStore();

/************************************************************************
|    전역 변수
************************************************************************/
const { $toast, $log } = useNuxtApp();
const router = useRouter();
const pageId = ref("제품 등록");
const loading = ref(false);
const editorMode = ref("text");
const fileInput = ref(null);

/************************************************************************
|    폼 데이터
************************************************************************/
const form = ref({
  productName: "",
  supplyPrice: "",
  sellPrice: "",
  shippingCost: "",
  subtitle: "",
  detailContent: "",
  detailFile: null,
  status: "판매중",
  displayStatus: "노출",
  updateHistory: ""
});

/************************************************************************
|    옵션 데이터
************************************************************************/
const statusOptions = ref([
  { title: "판매중", value: "판매중" },
  { title: "품절", value: "품절" }
]);

const displayStatusOptions = ref([
  { title: "노출", value: "노출" },
  { title: "비노출", value: "비노출" }
]);

/************************************************************************
|    유효성 검사
************************************************************************/
const rules = {
  required: (value) => !!value || "필수 입력 항목입니다.",
  maxLength: (max) => (value) => {
    if (!value) return true;
    return value.length <= max || `최대 ${max}자까지 입력 가능합니다.`;
  }
};

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

// 파일 업로드 트리거
const triggerFileUpload = () => {
  fileInput.value.click();
};

// 파일 업로드 처리
const handleFileUpload = (event) => {
  const file = event.target.files[0];
  if (file) {
    if (file.type !== 'application/zip' && !file.name.endsWith('.zip')) {
      $toast.error('ZIP 파일만 업로드 가능합니다.');
      return;
    }
    
    form.value.detailFile = file;
    $toast.success('파일이 선택되었습니다.');
  }
};

// 파일 제거
const removeFile = () => {
  form.value.detailFile = null;
  if (fileInput.value) {
    fileInput.value.value = '';
  }
};

// 파일 크기 포맷팅
const formatFileSize = (bytes) => {
  if (bytes === 0) return '0 Bytes';
  const k = 1024;
  const sizes = ['Bytes', 'KB', 'MB', 'GB'];
  const i = Math.floor(Math.log(bytes) / Math.log(k));
  return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i];
};

// 뒤로가기
const goBack = () => {
  router.push('/view/vendor');
};

// 제품 저장
const saveProduct = async () => {
  // 유효성 검사
  if (!form.value.productName) {
    $toast.error('제품명을 입력하세요.');
    return;
  }
  
  if (!form.value.supplyPrice) {
    $toast.error('공급가를 입력하세요.');
    return;
  }
  
  if (!form.value.sellPrice) {
    $toast.error('판매가를 입력하세요.');
    return;
  }
  
  if (!form.value.shippingCost) {
    $toast.error('배송비를 입력하세요.');
    return;
  }

  loading.value = true;
  
  try {
    // FormData 생성 (파일 업로드용)
    const formData = new FormData();
    formData.append('productName', form.value.productName);
    formData.append('supplyPrice', form.value.supplyPrice);
    formData.append('sellPrice', form.value.sellPrice);
    formData.append('shippingCost', form.value.shippingCost);
    formData.append('subtitle', form.value.subtitle);
    formData.append('detailContent', form.value.detailContent);
    formData.append('status', form.value.status);
    formData.append('displayStatus', form.value.displayStatus);
    formData.append('updateHistory', form.value.updateHistory);
    formData.append('compId', useAuthStore().getCompanyId);
    
    if (form.value.detailFile) {
      formData.append('detailFile', form.value.detailFile);
    }

    await useAxios()
      .post("/product/register", formData, {
        headers: {
          'Content-Type': 'multipart/form-data'
        }
      })
      .then((res) => {
        if (res.data.success) {
          $toast.success('제품이 등록되었습니다.');
          router.push('/view/vendor');
        } else {
          $toast.error('제품 등록에 실패했습니다.');
        }
      });
  } catch (error) {
    $log.error('제품 등록 오류:', error);
    $toast.error('제품 등록 중 오류가 발생했습니다.');
  } finally {
    loading.value = false;
  }
};
</script>

<style scoped>
.product-register--form {
  margin: 20px 0;
}

.form--container {
  background: white;
  border-radius: 8px;
  padding: 30px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.1);
}

.form--section {
  margin-bottom: 40px;
}

.form--section h3 {
  font-size: 18px;
  font-weight: 600;
  color: #333;
  margin-bottom: 20px;
  padding-bottom: 10px;
  border-bottom: 2px solid #f0f0f0;
}

.form--row {
  display: flex;
  gap: 20px;
  margin-bottom: 20px;
}

.form--field {
  flex: 1;
}

.form--field.full-width {
  width: 100%;
}

.form--label {
  display: block;
  font-size: 14px;
  font-weight: 500;
  color: #333;
  margin-bottom: 8px;
}

.form--label .required {
  color: #f44336;
}

.custom-input {
  width: 100%;
}

.custom-select {
  width: 100%;
}

/* 에디터 스타일 */
.editor--container {
  border: 1px solid #ddd;
  border-radius: 4px;
  overflow: hidden;
}

.editor--toolbar {
  background: #f8f9fa;
  padding: 10px;
  border-bottom: 1px solid #ddd;
}

.editor--mode-toggle {
  background: white;
  border-radius: 4px;
}

.html-textarea {
  width: 100%;
  border: none;
  outline: none;
  padding: 15px;
  font-family: 'Courier New', monospace;
  font-size: 14px;
  resize: vertical;
  min-height: 400px;
}

.text-editor {
  padding: 15px;
}

/* 파일 업로드 스타일 */
.file-upload--container {
  width: 100%;
}

.file-upload--area {
  border: 2px dashed #ddd;
  border-radius: 8px;
  padding: 30px;
  text-align: center;
  cursor: pointer;
  transition: all 0.3s ease;
}

.file-upload--area:hover {
  border-color: #3f51b5;
  background: #f8f9ff;
}

.file-upload--placeholder {
  color: #666;
}

.file-upload--placeholder .upload-icon {
  font-size: 48px;
  display: block;
  margin-bottom: 10px;
}

.file-upload--placeholder p {
  font-size: 16px;
  margin: 10px 0;
}

.file-upload--placeholder small {
  color: #999;
}

.file-upload--selected {
  display: flex;
  align-items: center;
  gap: 15px;
  padding: 15px;
  background: #f8f9fa;
  border-radius: 4px;
}

.file-upload--selected .file-icon {
  font-size: 24px;
}

.file-info {
  flex: 1;
  text-align: left;
}

.file-name {
  font-weight: 500;
  margin: 0;
}

.file-size {
  color: #666;
}

.file-remove {
  margin-left: auto;
}

/* 액션 버튼 스타일 */
.form--actions {
  display: flex;
  justify-content: center;
  gap: 15px;
  margin-top: 40px;
  padding-top: 20px;
  border-top: 1px solid #e0e0e0;
}

.custom-btn {
  padding: 12px 30px;
  font-size: 14px;
  font-weight: 500;
  border-radius: 4px;
  min-width: 120px;
}

.btn-white {
  background: white;
  color: #666;
  border: 1px solid #ddd;
}

.btn-blue {
  background: #3f51b5;
  color: white;
}

/* 반응형 */
@media (max-width: 768px) {
  .form--row {
    flex-direction: column;
    gap: 15px;
  }
  
  .form--container {
    padding: 20px;
  }
  
  .form--actions {
    flex-direction: column;
  }
  
  .custom-btn {
    width: 100%;
  }
}
</style>