<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('addExamAnswers') }}
        </el-button>
      </div>

      <el-card
        class="box-card !border-none my-[10px] table-search-wrap"
        shadow="never"
      >
        <el-form
          :inline="true"
          :model="examAnswersTable.searchParam"
          ref="searchFormRef"
        >
          <el-form-item :label="t('campusId')" prop="campus_id">
            <el-input
              v-model="examAnswersTable.searchParam.campus_id"
              :placeholder="t('campusIdPlaceholder')"
            />
          </el-form-item>
          <el-form-item :label="t('userId')" prop="user_id">
            <el-input
              v-model="examAnswersTable.searchParam.user_id"
              :placeholder="t('userIdPlaceholder')"
            />
          </el-form-item>
          <el-form-item :label="t('questionId')" prop="question_id">
            <el-input
              v-model="examAnswersTable.searchParam.question_id"
              :placeholder="t('questionIdPlaceholder')"
            />
          </el-form-item>
          <el-form-item :label="t('answer')" prop="answer">
            <el-input
              v-model="examAnswersTable.searchParam.answer"
              :placeholder="t('answerPlaceholder')"
            />
          </el-form-item>
          <el-form-item :label="t('isCorrect')" prop="is_correct">
            <el-input
              v-model="examAnswersTable.searchParam.is_correct"
              :placeholder="t('isCorrectPlaceholder')"
            />
          </el-form-item>

          <el-form-item>
            <el-button type="primary" @click="loadExamAnswersList()">{{
              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="examAnswersTable.data"
          size="large"
          v-loading="examAnswersTable.loading"
        >
          <template #empty>
            <span>{{ !examAnswersTable.loading ? t('emptyData') : '' }}</span>
          </template>
          <el-table-column
            prop="campus_id"
            :label="t('campusId')"
            min-width="120"
            :show-overflow-tooltip="true"
          />

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

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

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

          <el-table-column
            prop="is_correct"
            :label="t('isCorrect')"
            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="examAnswersTable.page"
            v-model:page-size="examAnswersTable.limit"
            layout="total, sizes, prev, pager, next, jumper"
            :total="examAnswersTable.total"
            @size-change="loadExamAnswersList()"
            @current-change="loadExamAnswersList"
          />
        </div>
      </div>

      <edit ref="editExamAnswersDialog" @complete="loadExamAnswersList" />
    </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 { getExamAnswersList, deleteExamAnswers } from '@/app/api/exam_answers'
import { img } from '@/utils/common'
import { ElMessageBox, FormInstance } from 'element-plus'
import Edit from '@/app/views/exam_answers/components/exam-answers-edit.vue'
import { useRoute } from 'vue-router'
const route = useRoute()
const pageName = route.meta.title

let examAnswersTable = reactive({
  page: 1,
  limit: 10,
  total: 0,
  loading: true,
  data: [],
  searchParam: {
    campus_id: '',
    user_id: '',
    question_id: '',
    answer: '',
    is_correct: '',
  },
})

const searchFormRef = ref<FormInstance>()

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

// 字典数据

/**
 * 获取答题记录列表
 */
const loadExamAnswersList = (page: number = 1) => {
  examAnswersTable.loading = true
  examAnswersTable.page = page

  getExamAnswersList({
    page: examAnswersTable.page,
    limit: examAnswersTable.limit,
    ...examAnswersTable.searchParam,
  })
    .then((res) => {
      examAnswersTable.loading = false
      examAnswersTable.data = res.data.data
      examAnswersTable.total = res.data.total
    })
    .catch(() => {
      examAnswersTable.loading = false
    })
}
loadExamAnswersList()

const editExamAnswersDialog: Record<string, any> | null = ref(null)

/**
 * 添加答题记录
 */
const addEvent = () => {
  editExamAnswersDialog.value.setFormData()
  editExamAnswersDialog.value.showDialog = true
}

/**
 * 编辑答题记录
 * @param data
 */
const editEvent = (data: any) => {
  editExamAnswersDialog.value.setFormData(data)
  editExamAnswersDialog.value.showDialog = true
}

/**
 * 删除答题记录
 */
const deleteEvent = (id: number) => {
  ElMessageBox.confirm(t('examAnswersDeleteTips'), t('warning'), {
    confirmButtonText: t('confirm'),
    cancelButtonText: t('cancel'),
    type: 'warning',
  }).then(() => {
    deleteExamAnswers(id)
      .then(() => {
        loadExamAnswersList()
      })
      .catch(() => {})
  })
}

const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.resetFields()
  loadExamAnswersList()
}
</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>
