<template>
  <div>
    <!-- 搜索工作栏 -->
    <el-form :model="searchForm" ref="queryFormRef" inline label-width="0">
      <el-form-item>
        <el-input v-model="searchForm.name" 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="name" label="考核指标" width="180" />
      <el-table-column prop="rate" label="权重%" width="90" />
      <el-table-column label="考核内容">
        <template #default="{ row }">
          <div v-dompurify-html="row.kaoheneirong"></div>
        </template>
      </el-table-column>
      <el-table-column label="考核规则" prop="kaoheguize">
        <template #default="{ row }">
          <div v-dompurify-html="row.kaoheguize"></div>
        </template>
      </el-table-column>
      <el-table-column label="评分上限" prop="maxScore" width="90" />
      <el-table-column label="考核人数" width="90">
        <template #default="{ row }">
          {{ row.checkEmployees?.length || 0 }}
        </template>
      </el-table-column>
      <el-table-column label="操作" class-name="fixed-width" width="120">
        <template #default="{ row }">
          <el-button
            link
            type="primary"
            @click="openForm('update', row)"
            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" @success="handleQuery" />
  </div>
</template>

<script setup name="KpiContent">
import DialogAppraise from './Components/DialogAppraise.vue'

const message = useMessage() // 消息弹窗
const loading = ref(false) // 列表的加载中
const list = ref() // 列表的数据
const searchForm = ref({
  name: undefined,
  pageNo: 1,
  pageSize: 20
})
const total = ref(0)

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

const getList = async (info) => {
  loading.value = true
  try {
    const data = [
      {
        name: '纳税申报',
        rate: 35,
        kaoheneirong:
          '<p>1、每个月13号之前在柠檬云财税把上个月的财务数据录入系统出具财务报表；</p><p>2、社保和公积金及时增减人员，做好核定工作，及时申报缴纳；</p><p>3、按照税务局规定时间把自己所负责的公司增指税、附加税、所得税以及其他税种申报完成</p>',
        kaoheguize: `当天处理完今日线索，连续2天未处理完，一次扣2分，最多扣5分`,
        maxScore: 5,
        checkEmployees: [1, 3, 4]
      }
    ]
    if (info) {
      data.push(info)
    }
    list.value = data
  } finally {
    loading.value = false
  }
}

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

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

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

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

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