<template>
  <div class="main-container">
    <el-card class="box-card !border-none" shadow="never">
      <div class="flex justify-between items-center">
        <span class="text-lg">{{ pageName }}</span>
        <el-button type="primary" @click="addEvent">
          {{ t('addExamQuestions') }}
        </el-button>
      </div>

      <el-card
        class="box-card !border-none my-[10px] table-search-wrap"
        shadow="never"
      >
        <el-form
          :inline="true"
          :model="examQuestionsTable.searchParam"
          ref="searchFormRef"
        >
          <el-form-item :label="t('questionType')" prop="question_type">
            <el-input
              v-model="examQuestionsTable.searchParam.question_type"
              :placeholder="t('questionTypePlaceholder')"
            />
          </el-form-item>
          <el-form-item
            :label="t('questionContentType')"
            prop="question_content_type"
          >
            <el-input
              v-model="examQuestionsTable.searchParam.question_content_type"
              :placeholder="t('questionContentTypePlaceholder')"
            />
          </el-form-item>
          <el-form-item :label="t('questionContent')" prop="question_content">
            <el-input
              v-model="examQuestionsTable.searchParam.question_content"
              :placeholder="t('questionContentPlaceholder')"
            />
          </el-form-item>
          <el-form-item
            :label="t('optionAContentType')"
            prop="option_a_content_type"
          >
            <el-input
              v-model="examQuestionsTable.searchParam.option_a_content_type"
              :placeholder="t('optionAContentTypePlaceholder')"
            />
          </el-form-item>
          <el-form-item :label="t('optionAContent')" prop="option_a_content">
            <el-input
              v-model="examQuestionsTable.searchParam.option_a_content"
              :placeholder="t('optionAContentPlaceholder')"
            />
          </el-form-item>
          <el-form-item
            :label="t('optionBContentType')"
            prop="option_b_content_type"
          >
            <el-input
              v-model="examQuestionsTable.searchParam.option_b_content_type"
              :placeholder="t('optionBContentTypePlaceholder')"
            />
          </el-form-item>
          <el-form-item :label="t('optionBContent')" prop="option_b_content">
            <el-input
              v-model="examQuestionsTable.searchParam.option_b_content"
              :placeholder="t('optionBContentPlaceholder')"
            />
          </el-form-item>
          <el-form-item
            :label="t('optionCContentType')"
            prop="option_c_content_type"
          >
            <el-input
              v-model="examQuestionsTable.searchParam.option_c_content_type"
              :placeholder="t('optionCContentTypePlaceholder')"
            />
          </el-form-item>
          <el-form-item :label="t('optionCContent')" prop="option_c_content">
            <el-input
              v-model="examQuestionsTable.searchParam.option_c_content"
              :placeholder="t('optionCContentPlaceholder')"
            />
          </el-form-item>
          <el-form-item
            :label="t('optionDContentType')"
            prop="option_d_content_type"
          >
            <el-input
              v-model="examQuestionsTable.searchParam.option_d_content_type"
              :placeholder="t('optionDContentTypePlaceholder')"
            />
          </el-form-item>
          <el-form-item :label="t('optionDContent')" prop="option_d_content">
            <el-input
              v-model="examQuestionsTable.searchParam.option_d_content"
              :placeholder="t('optionDContentPlaceholder')"
            />
          </el-form-item>
          <el-form-item :label="t('correctAnswer')" prop="correct_answer">
            <el-input
              v-model="examQuestionsTable.searchParam.correct_answer"
              :placeholder="t('correctAnswerPlaceholder')"
            />
          </el-form-item>

          <el-form-item>
            <el-button type="primary" @click="loadExamQuestionsList()">{{
              t('search')
            }}</el-button>
            <el-button @click="resetForm(searchFormRef)">{{
              t('reset')
            }}</el-button>
          </el-form-item>
        </el-form>
      </el-card>

      <div class="mt-[10px]">
        <el-table
          :data="examQuestionsTable.data"
          size="large"
          v-loading="examQuestionsTable.loading"
        >
          <template #empty>
            <span>{{ !examQuestionsTable.loading ? t('emptyData') : '' }}</span>
          </template>
          <el-table-column
            prop="question_type"
            :label="t('questionType')"
            min-width="120"
            :show-overflow-tooltip="true"
          />

          <el-table-column
            prop="question_content_type"
            :label="t('questionContentType')"
            min-width="120"
            :show-overflow-tooltip="true"
          />

          <el-table-column
            prop="question_content"
            :label="t('questionContent')"
            min-width="120"
            :show-overflow-tooltip="true"
          />

          <el-table-column
            prop="option_a_content_type"
            :label="t('optionAContentType')"
            min-width="120"
            :show-overflow-tooltip="true"
          />

          <el-table-column
            prop="option_a_content"
            :label="t('optionAContent')"
            min-width="120"
            :show-overflow-tooltip="true"
          />

          <el-table-column
            prop="option_b_content_type"
            :label="t('optionBContentType')"
            min-width="120"
            :show-overflow-tooltip="true"
          />

          <el-table-column
            prop="option_b_content"
            :label="t('optionBContent')"
            min-width="120"
            :show-overflow-tooltip="true"
          />

          <el-table-column
            prop="option_c_content_type"
            :label="t('optionCContentType')"
            min-width="120"
            :show-overflow-tooltip="true"
          />

          <el-table-column
            prop="option_c_content"
            :label="t('optionCContent')"
            min-width="120"
            :show-overflow-tooltip="true"
          />

          <el-table-column
            prop="option_d_content_type"
            :label="t('optionDContentType')"
            min-width="120"
            :show-overflow-tooltip="true"
          />

          <el-table-column
            prop="option_d_content"
            :label="t('optionDContent')"
            min-width="120"
            :show-overflow-tooltip="true"
          />

          <el-table-column
            prop="correct_answer"
            :label="t('correctAnswer')"
            min-width="120"
            :show-overflow-tooltip="true"
          />

          <el-table-column
            :label="t('operation')"
            fixed="right"
            min-width="120"
          >
            <template #default="{ row }">
              <el-button type="primary" link @click="editEvent(row)">{{
                t('edit')
              }}</el-button>
              <el-button type="primary" link @click="deleteEvent(row.id)">{{
                t('delete')
              }}</el-button>
            </template>
          </el-table-column>
        </el-table>
        <div class="mt-[16px] flex justify-end">
          <el-pagination
            v-model:current-page="examQuestionsTable.page"
            v-model:page-size="examQuestionsTable.limit"
            layout="total, sizes, prev, pager, next, jumper"
            :total="examQuestionsTable.total"
            @size-change="loadExamQuestionsList()"
            @current-change="loadExamQuestionsList"
          />
        </div>
      </div>
    </el-card>
  </div>
</template>

<script lang="ts" setup>
import { reactive, ref, watch } from 'vue'
import { t } from '@/lang'
import { useDictionary } from '@/app/api/dict'
import {
  getExamQuestionsList,
  deleteExamQuestions,
} from '@/app/api/exam_questions'
import { img } from '@/utils/common'
import { ElMessageBox, FormInstance } from 'element-plus'
import { useRouter } from 'vue-router'
import { useRoute } from 'vue-router'
const route = useRoute()
const pageName = route.meta.title

let examQuestionsTable = reactive({
  page: 1,
  limit: 10,
  total: 0,
  loading: true,
  data: [],
  searchParam: {
    question_type: '',
    question_content_type: '',
    question_content: '',
    option_a_content_type: '',
    option_a_content: '',
    option_b_content_type: '',
    option_b_content: '',
    option_c_content_type: '',
    option_c_content: '',
    option_d_content_type: '',
    option_d_content: '',
    correct_answer: '',
  },
})

const searchFormRef = ref<FormInstance>()

// 选中数据
const selectData = ref<any[]>([])

// 字典数据

/**
 * 获取试题列表
 */
const loadExamQuestionsList = (page: number = 1) => {
  examQuestionsTable.loading = true
  examQuestionsTable.page = page

  getExamQuestionsList({
    page: examQuestionsTable.page,
    limit: examQuestionsTable.limit,
    ...examQuestionsTable.searchParam,
  })
    .then((res) => {
      examQuestionsTable.loading = false
      examQuestionsTable.data = res.data.data
      examQuestionsTable.total = res.data.total
    })
    .catch(() => {
      examQuestionsTable.loading = false
    })
}
loadExamQuestionsList()

const router = useRouter()

/**
 * 添加试题
 */
const addEvent = () => {
  router.push('/exam_questions/exam_questions_edit')
}

/**
 * 编辑试题
 * @param data
 */
const editEvent = (data: any) => {
  router.push('/exam_questions/exam_questions_edit?id=' + data.id)
}

/**
 * 删除试题
 */
const deleteEvent = (id: number) => {
  ElMessageBox.confirm(t('examQuestionsDeleteTips'), t('warning'), {
    confirmButtonText: t('confirm'),
    cancelButtonText: t('cancel'),
    type: 'warning',
  }).then(() => {
    deleteExamQuestions(id)
      .then(() => {
        loadExamQuestionsList()
      })
      .catch(() => {})
  })
}

const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.resetFields()
  loadExamQuestionsList()
}
</script>

<style lang="scss" scoped>
/* 多行超出隐藏 */
.multi-hidden {
  word-break: break-all;
  text-overflow: ellipsis;
  overflow: hidden;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}
</style>
