<template>
  <div>
    <!-- 查询条件 -->
    <el-form ref="queryParamsRef" :model="queryParams" inline label-width="0" @submit.prevent>
      <el-form-item>
        <el-input
          v-model="queryParams.chapterName"
          placeholder="请输入章节名称"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleQuery"> 搜索 </el-button>
        <el-button type="primary" @click="handleAdd" v-hasPermi="['Question:UAVChapter:add']">
          新增
        </el-button>
      </el-form-item>
    </el-form>
    <el-table
      v-loading="loading"
      :data="tableList"
      highlight-current-row
      max-height="calc(100vh - 180px)"
    >
      <el-table-column type="index" width="55" align="center" />
      <el-table-column label="章节名称" prop="chapterName" />
      <el-table-column label="排序" prop="sort" />
      <el-table-column label="创建时间" prop="createTime" />
      <el-table-column label="创建人" prop="creator" />
      <el-table-column label="最近修改时间" prop="updateTime" />
      <el-table-column label="修改人" prop="updater" />

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

    <Dialog v-model="showDialog" :title="dialogTitle" style="width: 880px">
      <el-form ref="dialogFormRef" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="章节名称" prop="chapterName">
              <el-input v-model="form.chapterName" placeholder="请输入章节名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="排序" prop="sort">
              <el-input v-model="form.sort" placeholder="请输入排序" type="number" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="知识总结" prop="summary">
          <Editor v-model="form.summary" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="showDialog = false">取 消</el-button>
      </template>
    </Dialog>
  </div>
</template>

<script setup name="UAVChapter">
import Editor from '@/components/Editor/src/Editor.vue'
import * as ChapterApi from '@/api/uav/chapter'

const message = useMessage()

const loading = ref(false)
const tableList = ref([])
const queryParams = ref({
  chapterName: ''
})

onMounted(() => {
  handleQuery()
})

function getList() {
  loading.value = true
  // 查询无人机章节列表
  ChapterApi.searchChapter(queryParams.value).then((response) => {
    tableList.value = response
    loading.value = false
  })
}

function handleQuery() {
  getList()
}

const form = ref({})
const showDialog = ref(false)
const rules = {
  chapterName: [{ required: true, message: '请输入章节名称', trigger: 'blur' }],
  sort: [{ required: true, message: '请输入排序', trigger: 'blur' }]
}

const dialogTitle = ref('新增章节')
const dialogFormRef = ref()
function handleAdd() {
  showDialog.value = true
  dialogTitle.value = '新增章节'
  resetForm()
}

function handleEdit(row) {
  showDialog.value = true
  resetForm()
  dialogTitle.value = '修改章节'
  form.value = {
    chapterName: row.chapterName,
    sort: row.sort,
    chapterId: row.chapterId,
    summary: row.summary
  }
}

function resetForm() {
  form.value = {
    chapterName: undefined,
    sort: 1,
    summary: undefined
  }
  if (dialogFormRef.value) {
    dialogFormRef.value.resetFields()
  }
}

function handleDelete(row) {
  message
    .confirm('是否确认删除该章节？')
    .then(function () {
      ChapterApi.deleteChapter(row.chapterId).then(() => {
        getList()
        message.success('删除成功')
      })
    })
    .catch((err) => {
      console.log(err)
    })
}

const submitForm = async () => {
  try {
    if (!dialogFormRef.value) return
    const valid = await dialogFormRef.value.validate()
    if (!valid) return

    if (form.value.chapterId) {
      await ChapterApi.updateChapter(form.value)
      message.success('修改成功')
      getList()
      showDialog.value = false
    } else {
      await ChapterApi.addChapter(form.value)
      message.success('新增成功')
      showDialog.value = false
      getList()
    }
  } catch (error) {
    console.log(error)
  }
}
</script>

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