<template>
  <div class="app-container">
    <el-tabs v-model="activeTab">
      <el-tab-pane label="考核配置" name="config">
        <div ref="containerRef" class="table-page-wrap">
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" plain icon="Plus" @click="handleAdd">新增</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate">修改</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete">删除</el-button>
          </el-col>
        </el-row>

        <div class="table-wrapper">
        <el-table :height="tableHeight" v-loading="loading" :data="assessList" @selection-change="handleSelectionChange" stripe>
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column label="考核项目" align="center" prop="assessItem" :show-overflow-tooltip="true" />
          <el-table-column label="考核标准" align="center" prop="assessStd" :show-overflow-tooltip="true" />
          <el-table-column label="分值" align="center" prop="assessScore" width="100" />
          <el-table-column label="考核周期" align="center" prop="assessCycle" width="120" />
          <el-table-column label="启用状态" align="center" prop="status" width="100">
            <template #default="scope">
              <dict-tag :options="sys_normal_disable" :value="scope.row.status" />
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" width="180">
            <template #default="scope">
              <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)">修改</el-button>
              <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        </div>

        <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList" />
        </div>
      </el-tab-pane>

      <el-tab-pane label="考核评分" name="score">
        <el-empty description="功能开发中" />
      </el-tab-pane>

      <el-tab-pane label="排名查询" name="rank">
        <el-empty description="功能开发中" />
      </el-tab-pane>
    </el-tabs>

    <!-- 添加或修改考核配置对话框 -->
    <el-dialog :title="title" v-model="open" width="700px" append-to-body>
      <el-form :model="form" :rules="rules" ref="formRef" label-width="100px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="考核项目" prop="assessItem">
              <el-input v-model="form.assessItem" placeholder="请输入考核项目" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="分值" prop="assessScore">
              <el-input-number v-model="form.assessScore" placeholder="请输入分值" :min="0" :precision="1" style="width: 100%" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="考核周期" prop="assessCycle">
              <el-select v-model="form.assessCycle" placeholder="请选择考核周期" style="width: 100%">
                <el-option label="月度" value="month" />
                <el-option label="季度" value="quarter" />
                <el-option label="年度" value="year" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="启用状态" prop="status">
              <el-radio-group v-model="form.status">
                <el-radio v-for="dict in sys_normal_disable" :key="dict.value" :value="dict.value">{{ dict.label }}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="考核标准" prop="assessStd">
              <el-input v-model="form.assessStd" type="textarea" :rows="3" placeholder="请输入考核标准" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" type="textarea" :rows="2" placeholder="请输入备注" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="AcciAssess">
import { getCurrentInstance, onMounted, reactive, ref, toRefs } from 'vue'
import { useTableHeight } from '@/hooks/useTableHeight'
import { listBisAcciAssess, getBisAcciAssess, addBisAcciAssess, updateBisAcciAssess, delBisAcciAssess } from "@/api/slaj/acci"

const { tableHeight, containerRef } = useTableHeight()

const { proxy } = getCurrentInstance()
const { sys_normal_disable } = proxy.useDict("sys_normal_disable")

const activeTab = ref('config')
const assessList = ref([])
const open = ref(false)
const loading = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10
  },
  rules: {
    assessItem: [{ required: true, message: "考核项目不能为空", trigger: "blur" }],
    assessScore: [{ required: true, message: "分值不能为空", trigger: "blur" }],
    assessCycle: [{ required: true, message: "考核周期不能为空", trigger: "change" }],
    status: [{ required: true, message: "启用状态不能为空", trigger: "change" }]
  }
})

const { form, queryParams, rules } = toRefs(data)

function getList() {
  loading.value = true
  listBisAcciAssess(queryParams.value).then(res => {
    loading.value = false
    assessList.value = res.rows || []
    total.value = res.total || 0
  }).catch(() => {
    loading.value = false
  })
}

function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

function handleAdd() {
  reset()
  open.value = true
  title.value = "添加考核配置"
}

function handleUpdate(row) {
  reset()
  const id = row.id || ids.value[0]
  getBisAcciAssess(id).then(res => {
    const item = res.rows?.[0] || res.data || res
    form.value = item
    open.value = true
    title.value = "修改考核配置"
  })
}

function submitForm() {
  proxy.$refs["formRef"].validate(valid => {
    if (!valid) return
    if (form.value.id != undefined) {
      updateBisAcciAssess(form.value).then(() => {
        proxy.$modal.msgSuccess("修改成功")
        open.value = false
        getList()
      })
    } else {
      addBisAcciAssess(form.value).then(() => {
        proxy.$modal.msgSuccess("新增成功")
        open.value = false
        getList()
      })
    }
  })
}

function handleDelete(row) {
  const idsStr = row.id || ids.value
  proxy.$modal.confirm('是否确认删除所选考核配置？').then(() => {
    return delBisAcciAssess(idsStr)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

function cancel() {
  open.value = false
  proxy.resetForm("formRef")
}

function reset() {
  form.value = {
    id: undefined,
    assessItem: undefined,
    assessStd: undefined,
    assessScore: undefined,
    assessCycle: undefined,
    status: '0',
    remark: undefined
  }
  proxy.resetForm("formRef")
}

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