<template>
  <div>
    <el-form :model="searchForm" inline label-width="0">
      <el-form-item>
        <el-input
          v-model="searchForm.question"
          placeholder="请输入问题"
          clearable
          style="width: 200px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery" v-hasPermi="['clue:skill:search']">搜索</el-button>
        <el-button @click="resetQuery" v-hasPermi="['clue:skill:reset']">重置</el-button>
        <el-button type="primary" plain @click="handleAdd" v-hasPermi="['clue:skill:add']">
          新增
        </el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="loading" :data="tableList">
      <el-table-column type="index" width="55" />
      <el-table-column label="问题" prop="question" />
      <el-table-column label="答案" prop="content">
        <template #default="scope">
          <p v-dompurify-html="scope.row.content"></p>
        </template>
      </el-table-column>
      <el-table-column label="关键词" prop="skillKey" />
      <el-table-column label="操作">
        <template #default="scope">
          <el-button
            type="primary"
            text
            @click="handleUpdate(scope.row)"
            v-hasPermi="['clue:skill:update']"
          >
            修改
          </el-button>
          <el-button
            type="primary"
            text
            @click="handleDelete(scope.row)"
            v-hasPermi="['clue:skill:delete']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <Pagination
      v-model:limit="searchForm.pageSize"
      v-model:page="searchForm.pageNo"
      :total="total"
      @pagination="handleQuery"
    />
    <DialogSkill ref="skillDialog" @success="handleQuery" />
  </div>
</template>

<script setup name="ClueSkill">
import * as SkillApi from '@/api/clue/skill'
import DialogSkill from './Comp/DialogSkill.vue'
import { removeNullField } from '@/utils'

const skillDialog = ref()

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(false)
const searchForm = ref({
  question: '',
  pageSize: 20,
  pageNo: 1
})

const total = ref(0)

const tableList = ref([])

function resetQuery() {
  searchForm.value = {
    question: '',
    pageSize: 20,
    pageNo: 1
  }
  getList()
}

function handleQuery() {
  searchForm.value.pageNo = 1
  getList()
}

async function getList() {
  loading.value = true
  try {
    const data = await SkillApi.getSkillPage(removeNullField(searchForm.value))
    tableList.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

function handleAdd() {
  skillDialog.value.open('create', null)
}

function handleUpdate(row) {
  skillDialog.value.open('update', row.skillId)
}

async function handleDelete(row) {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await SkillApi.deleteSkill(row.skillId)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

onMounted(() => {
  handleQuery()
})
</script>

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