<!-- eslint-disable unicorn/prefer-switch -->
<script lang="ts" setup>
import { useTabs } from '@vben/hooks'

import AnswerData from './components/answer-data/index.vue'
import BasicData from './components/basic-data/index.vue'
import OptionButtons from './components/option-buttons/index.vue'
import StemData from './components/stem-data/index.vue'
import { useQuestionAdd } from './hooks'
// https://vueup.github.io/vue-quill/
// https://www.wangeditor.com/
// https://github.com/tinymce/tinymce-vue
// https://ckeditor.com/docs/ckeditor5/latest/getting-started/installation/cloud/vuejs-v3.html

// 表单数据
const sharedData = useQuestionAdd()
const {
  formRef,
  formData,
  formDisabled,
  baseDataRef,
  stemDataRef,
  answerDataRef,
  initData,
} = sharedData
provide('sharedData', sharedData)

const { id } = useRoute().query

// 表单验证规则
const formRules = {
  channelTypeId: [{ required: true, message: '请选择题型', trigger: 'change' }],
  difficulty: [{ required: true, message: '请选择难度', trigger: 'change' }],
  province: [{ required: true, message: '请选择地区', trigger: 'change' }],
  year: [{ required: true, message: '请选择年份', trigger: 'change' }],
  textbookId: [{ required: true, message: '请选择教材', trigger: 'change' }],
}

const { setTabTitle } = useTabs()

const loading = ref(false)

onMounted(async () => {
  loading.value = true
  try {
    await initData()
    setTabTitle(id ? '查看题目' : '新增题目')
  } finally {
    loading.value = false
  }
})
</script>

<template>
  <div class="question-page g-container" v-loading="loading">
    <div class="form-box">
      <el-form
        ref="formRef"
        label-width="100px"
        :model="formData"
        :rules="formRules"
        :disabled="formDisabled"
        class="question-form"
      >
        <!-- 基本信息 -->
        <BasicData ref="baseDataRef" />

        <!-- 题干 -->
        <StemData ref="stemDataRef" />

        <!-- 答案设置 -->
        <AnswerData ref="answerDataRef" />
      </el-form>
    </div>

    <!-- 操作按钮 -->
    <OptionButtons />
  </div>
</template>

<style lang="scss">
.question-page {
  display: flex;
  flex-direction: column;
  height: calc(100vh - 88px) !important;

  .section-title {
    font-size: 20px;
  }

  .el-input-tag__input,
  .el-input__inner,
  .el-select__wrapper,
  .el-form-item__label {
    font-size: 16px;
  }

  .el-form-item__content {
    font-size: 16px;

    .el-tree-node__content {
      margin-bottom: 5px;
    }

    .el-text {
      display: inline-block;
      padding: 5px;
      font-size: 16px;
    }
  }

  .form-box {
    overflow: auto;

    .tree {
      width: 100%;
      max-height: 500px;
      margin-top: 10px;
      overflow: auto;
    }

    .question-form {
      .form-section {
        margin-bottom: 24px;

        .section-title {
          font-weight: 600;
          color: #303133;
        }
      }

      .options-container {
        .option-item {
          display: flex;
          align-items: center;
          margin-bottom: 12px;

          .option-label {
            min-width: 24px;
            margin-right: 8px;
            font-weight: 600;
          }

          .el-input {
            flex: 1;
            margin-right: 8px;
          }
        }
      }

      .fill-answers {
        .fill-answer-item {
          display: flex;
          align-items: center;
          margin-bottom: 12px;

          .fill-label {
            min-width: 60px;
            margin-right: 8px;
            font-weight: 500;
          }

          .el-input {
            flex: 1;
            margin-right: 8px;
          }
        }
      }
    }
  }

  .form-actions {
    padding: 16px 0 0;
    text-align: center;
    border-top: 1px solid #e4e7ed;

    .el-button {
      margin: 0 8px;
    }
  }

  .question-preview {
    .options {
      margin-top: 16px;

      .option {
        padding: 8px 0;
        border-bottom: 1px solid #f0f0f0;
      }
    }
  }

  .import-content {
    .import-actions {
      margin-top: 16px;
      text-align: center;

      .el-button {
        margin: 0 8px;
      }
    }
  }
}
</style>
