<template>
  <div class="app-container" style="text-align: center">
    <el-tabs v-model="queryParams.source" @tab-click="handleChangeSource">
      <el-tab-pane
        v-for="item in sourceOptions"
        :key="item.key"
        :label="item.label"
        :name="item.key"
      >
        <el-form :inline="true" label-width="68px" @submit.prevent>
          <el-row :gutter="20">
            <el-form-item label="车型">
              <el-radio-group v-model="queryParams.carTypeId" @change="getQuestionChapter">
                <el-radio label="1001" value="1001">小车</el-radio>
                <el-radio label="1002" value="1002">摩托车</el-radio>
              </el-radio-group>
            </el-form-item>
            <el-form-item label="科目">
              <el-radio-group v-model="queryParams.subject" @change="getQuestionChapter">
                <el-radio label="1" value="1">科一</el-radio>
                <el-radio label="4" value="4">科四</el-radio>
              </el-radio-group>
            </el-form-item>

            <el-form-item label="有图片">
              <el-radio-group v-model="queryParams.isPic">
                <el-radio :label="true" :value="true">有</el-radio>
                <el-radio :label="false" :value="false">无</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-row>
          <el-row :gutter="20">
            <el-form-item label="题目">
              <el-input
                v-model="queryParams.question"
                placeholder="请输入题目"
                clearable
                style="width: 400px"
                @keyup.enter="handleQuery"
              />
            </el-form-item>
            <el-form-item>
              <el-button
                type="primary"
                @click="handleQuery"
                v-hasPermi="['question:database:search']"
              >
                搜索
              </el-button>
              <el-button type="primary" @click="handleAdd" v-hasPermi="['question:database:add']">
                新增
              </el-button>
            </el-form-item>
          </el-row>
        </el-form>

        <el-table
          v-loading="loading"
          :data="tableList"
          highlight-current-row
          max-height="calc(100vh - 320px)"
        >
          <el-table-column type="index" width="55" align="center" />
          <el-table-column label="题目" align="left" prop="question" min-width="140" />
          <el-table-column label="选项" align="left" min-width="140">
            <template #default="{ row }">
              <p v-if="row.chooseA">A:{{ row.chooseA }}</p>
              <p v-if="row.chooseB">B:{{ row.chooseB }}</p>
              <p v-if="row.chooseC">C:{{ row.chooseC }}</p>
              <p v-if="row.chooseD">D:{{ row.chooseD }}</p>
            </template>
          </el-table-column>
          <el-table-column label="答案" align="center" prop="trueAnswer" width="100" />
          <!-- <el-table-column label="科目" align="center" prop="subject" width="100">
            <template #default="{ row }">
              <span v-if="row.subject == '1'">科一</span>
              <span v-if="row.subject == '4'">科四</span>
            </template>
          </el-table-column> -->
          <el-table-column label="章节" align="center" prop="chapterName" min-width="100" />
          <el-table-column label="图片" align="center" width="100">
            <template #default="{ row }">
              <el-image
                v-if="row.imageUrl"
                :src="getShowImg(row)"
                :preview-src-list="[getShowImg(row)]"
                :lazy="true"
                style="width: 90px"
                preview-teleported
              />
            </template>
          </el-table-column>
          <!-- <el-table-column label="状态" width="100">
            <template #default="{ row }">
              <el-tag v-if="row.isActive == 0" type="success">使用中</el-tag>
              <el-tag v-else type="danger">已删除</el-tag>
            </template>
          </el-table-column> -->

          <el-table-column label="操作" align="center" width="140">
            <template #default="scope">
              <el-button
                type="primary"
                link
                @click="handleEdit(scope.row)"
                v-hasPermi="['question:database:edit']"
              >
                修改
              </el-button>
              <el-button
                type="primary"
                link
                @click="handleDelete(scope.row)"
                v-hasPermi="['question:database:remove']"
              >
                删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination
          style="margin-bottom: 0"
          v-show="total > 0"
          :total="total"
          v-model:page="queryParams.pageNo"
          v-model:limit="queryParams.pageSize"
          @pagination="getList"
        />
      </el-tab-pane>
    </el-tabs>

    <QuestionAddForm ref="dialogAddForm" @update="getList" />
  </div>
</template>

<script setup name="XjDatabase">
import { searchQuestion, deleteQuestion, getQuestionSort } from '@/api/xjapplet/xjdatabase'
import QuestionAddForm from './components/QuestionAddForm.vue'

const message = useMessage()

const loading = ref(false)
const total = ref(0)
const tableList = ref([])
const queryParams = ref({
  source: '1',
  question: '',
  carTypeId: '1001',
  subject: '1',
  isPic: undefined,
  pageNo: 1,
  pageSize: 100
})

const sourceOptions = [
  {
    key: '1',
    label: '驾考精灵'
  },
  {
    key: '2',
    label: '驾校一点通'
  }
]

onMounted(() => {
  getQuestionChapter()
})

const chapterOptions = ref([])
const getQuestionChapter = () => {
  getQuestionSort({
    carTypeId: queryParams.value.carTypeId,
    subject: queryParams.value.subject,
    source: queryParams.value.source
  }).then((data) => {
    chapterOptions.value = data
  })
}

function getShowImg(row) {
  return row.imageUrl.includes('http')
    ? row.imageUrl
    : `https://ss-cloud.ahduima.com/xjxc/pic/${row.imageUrl}`
}

function getList() {
  loading.value = true
  searchQuestion(queryParams.value).then((response) => {
    tableList.value = response.list
    total.value = response.total
    loading.value = false
  })
}

function handleQuery() {
  getList()
}

const dialogAddForm = ref(null)
function handleEdit(item) {
  dialogAddForm.value.open(item, chapterOptions.value)
}

function handleAdd() {
  dialogAddForm.value.open(
    {
      subject: queryParams.value.subject,
      carTypeId: queryParams.value.carTypeId,
      source: queryParams.value.source
    },
    chapterOptions.value
  )
}

function handleDelete(row) {
  message
    .confirm('是否确认删除该题？')
    .then(function () {
      deleteQuestion(row.questionId, row.source).then(() => {
        getList()
        message.success('删除题目成功')
      })
    })
    .catch((err) => {
      console.log(err)
    })
}

function handleChangeSource() {
  const obj = {
    question: '',
    carTypeId: '1001',
    subject: '1',
    isPic: undefined,
    pageNo: 1
  }
  queryParams.value = { ...obj, source: queryParams.value.source }
  getQuestionChapter()
  getList()
}
</script>

<style lang="scss" scoped></style>
