<template>
  <div class="app-container">
    <el-form :inline="true" label-width="100px" @submit.prevent>
      <el-row :gutter="20">
        <el-col :span="12" :offset="0">
          <el-form-item label="无人机类型">
            <el-radio-group v-model="queryParams.modelId" @change="changeCarType">
              <el-radio
                v-for="item in cartypeOptions"
                :key="item.modelId"
                :label="item.modelId"
                :value="item.modelId"
              >
                {{ item.modelName }}
              </el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="12" :offset="0">
          <el-form-item label="驾驶员类型">
            <el-radio-group v-model="queryParams.typeId">
              <el-radio
                v-for="item in driverOptions"
                :key="item.typeId"
                :label="item.typeId"
                :value="item.typeId"
              >
                {{ item.typeName }}
              </el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-form-item label="章节">
          <el-select v-model="queryParams.chapter" placeholder="选择章节" clearable filterable>
            <el-option
              v-for="item in chapterOptions"
              :key="item.chapterId"
              :label="item.chapterName"
              :value="item.chapterId"
            />
          </el-select>
        </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-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"> 搜索 </el-button>
          <el-button plain @click="reset"> 重置 </el-button>
          <el-button type="primary" @click="handleAdd" v-hasPermi="['Question:UAV:add']">
            新增
          </el-button>
        </el-form-item>
      </el-row>
    </el-form>

    <el-table
      v-loading="loading"
      :data="tableList"
      highlight-current-row
      max-height="calc(100vh - 260px)"
    >
      <el-table-column label="序号" width="60" prop="showOrder" />
      <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:UAVVip:edit']"
          >
            修改
          </el-button>
          <el-button
            type="primary"
            link
            @click="handleDelete(scope.row)"
            v-hasPermi="['Question:UAVVip:delete']"
          >
            删除
          </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"
    />
    <QuestionAddForm ref="dialogAddForm" @update="getList" />
  </div>
</template>

<script setup name="UAV">
import QuestionAddForm from './components/QuestionAddForm.vue'
import { searchChapter } from '@/api/uav/chapter'
import {
  searchQuestion,
  deleteQuestion,
  getCarTypeOptions,
  getDriverTypeOptions
} from '@/api/uav/question.js'

const message = useMessage()

const loading = ref(false)
const total = ref(0)
const tableList = ref([])
const queryParams = ref({
  question: undefined,
  modelId: undefined,
  typeId: undefined,
  isPic: undefined,
  chapter: undefined,
  pageNo: 1,
  pageSize: 100
})

const cartypeOptions = ref([])
const driverOptions = ref([])

onMounted(() => {
  getQuestionChapter()
  getCarTypeOptions().then((res) => {
    cartypeOptions.value = res
    if (res.length) {
      queryParams.value.modelId = res[0].modelId
      changeCarType()
    }
  })
})

function changeCarType() {
  getDriverTypeOptions({ modelId: queryParams.value.modelId }).then((res) => {
    driverOptions.value = res
    if (res.length) {
      queryParams.value.typeId = res[0].typeId
    }
  })
}

function reset() {
  queryParams.value = {
    question: undefined,
    modelId: undefined,
    typeId: undefined,
    isPic: undefined,
    chapter: undefined,
    pageNo: 1,
    pageSize: 100
  }
}

const chapterOptions = ref([])
const getQuestionChapter = () => {
  searchChapter().then((res) => {
    chapterOptions.value = res
  })
}

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() {
  queryParams.value.pageNo = 1
  getList()
}

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

function handleAdd() {
  dialogAddForm.value.open(
    {
      chapter: queryParams.value.chapter,
      modelId: queryParams.value.modelId,
      typeId: queryParams.value.typeId
    },
    chapterOptions.value
  )
}

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

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