<template>
  <div>
    <!-- 搜索工作栏 -->
    <el-form :model="searchForm" ref="queryFormRef" inline label-width="0">
      <el-form-item>
        <el-input v-model="searchForm.examineTarget" placeholder="请输入考核指标" clearable />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery" v-hasPermi="['kpi:appraise:search']"> 搜索</el-button>
        <el-button @click="resetQuery" v-hasPermi="['kpi:appraise:reset']"> 重置</el-button>
        <el-button
          type="primary"
          plain
          @click="openForm('create')"
          v-hasPermi="['kpi:appraise:add']"
        >
          新增
        </el-button>
      </el-form-item>
    </el-form>

    <!-- 列表 -->
    <el-table v-loading="loading" :data="list" border>
      <el-table-column prop="examineTarget" label="考核指标" width="180" />
      <el-table-column prop="typeName" label="分值模式" width="90" :formatter="getKpiModeLabel" />
      <el-table-column prop="weight" label="权重%" width="90" />
      <el-table-column label="考核内容">
        <template #default="{ row }">
          <div v-dompurify-html="row.examineContent"></div>
        </template>
      </el-table-column>
      <el-table-column label="考核规则" prop="examineRule">
        <template #default="{ row }">
          <div v-dompurify-html="row.examineRule"></div>
        </template>
      </el-table-column>
      <el-table-column label="评分上限" prop="examineScore" width="90" />
      <el-table-column label="考核人数" width="90">
        <template #default="{ row }">
          {{ row.examinedUserIdList?.length || 0 }}
        </template>
      </el-table-column>
      <el-table-column label="生效日期" prop="effectiveDate" width="120" />
      <el-table-column label="操作" class-name="fixed-width" width="120">
        <template #default="{ row }">
          <el-button
            link
            type="primary"
            @click="openForm('update', row.id)"
            v-hasPermi="['kpi:appraise:update']"
          >
            修改
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(row.id)"
            v-hasPermi="['kpi:appraise:remove']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <Pagination
      v-model:limit="searchForm.pageSize"
      v-model:page="searchForm.pageNo"
      :total="total"
      @pagination="getList"
    />

    <DialogAppraise ref="formRef" :kpiModeOoptions="kpiModeOoptions" @success="handleQuery" />
  </div>
</template>

<script setup name="KpiContent">
import { removeNullField } from '@/utils'
import DialogAppraise from './Components/DialogAppraise.vue'
import * as KpiApi from '@/api/kpi/index.js'
import { getGeneralSysDictData } from '@/api/system/dict/dict.data'

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗
const loading = ref(false) // 列表的加载中
const list = ref([]) // 列表的数据
const searchForm = ref({
  examineTarget: undefined,
  pageNo: 1,
  pageSize: 20
})
const total = ref(0)

/** 初始化 **/
onMounted(() => {
  getOptions()
  handleQuery()
})

const kpiModeOoptions = ref([])
function getOptions() {
  getGeneralSysDictData('examine_type').then((data) => {
    kpiModeOoptions.value = data
  })
}

function getKpiModeLabel(row) {
  return kpiModeOoptions.value.find((it) => it.value == row.type)?.label
}

const getList = async () => {
  loading.value = true
  try {
    const data = await KpiApi.getKpiPage(removeNullField(searchForm.value))
    list.value = data.list
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  searchForm.value.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  searchForm.value = {
    examineTarget: undefined,
    pageNo: 1,
    pageSize: 20
  }
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type, id) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await KpiApi.deleteKpi(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}
</script>

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