<template>
  <div>
    <!-- 搜索工作栏 -->
    <el-form :model="queryParams" ref="queryFormRef" inline label-width="0">
      <el-form-item>
        <el-input v-model="queryParams.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="leaderUserName" label="权重%" width="90" />
      <el-table-column prop="sort" label="考核内容" />
      <el-table-column label="考核规则" prop="createTime" />
      <el-table-column label="评分上限" prop="createTime" width="90" />
      <el-table-column label="考核人数" prop="createTime" width="90" />
      <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>
  </div>
</template>

<script setup name="KpiContent">
const message = useMessage() // 消息弹窗
const loading = ref(false) // 列表的加载中
const list = ref() // 列表的数据
const queryParams = reactive({
  name: undefined,
  pageNo: 1,
  pageSize: 20
})

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

const getList = async () => {
  loading.value = true
  try {
    const data = await DeptApi.getDeptPage(queryParams)
    list.value = handleTree(data)
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  getList()
}

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

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

/** 删除按钮操作 */
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>
