<template>
  <div class="score-rule-page">
    <!-- 搜索表单 -->
    <el-card shadow="never" class="search-card">
      <el-form ref="searchFormRef" :model="searchForm" label-width="100px">
        <el-row :gutter="20">
          <el-col :span="6">
            <el-form-item label="考核类别" prop="category">
              <el-select v-model="searchForm.category" placeholder="请选择" clearable>
                <el-option label="劳动改造" :value="1" />
                <el-option label="教育改造" :value="2" />
                <el-option label="日常行为" :value="3" />
                <el-option label="卫生纪律" :value="4" />
                <el-option label="加分项" :value="5" />
                <el-option label="扣分项" :value="6" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="项目名称" prop="itemName">
              <el-input v-model="searchForm.itemName" placeholder="请输入项目名称" clearable />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="状态" prop="status">
              <el-select v-model="searchForm.status" placeholder="请选择" clearable>
                <el-option label="启用" :value="1" />
                <el-option label="禁用" :value="2" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item>
              <el-button type="primary" @click="handleSearch">
                <Icon icon="ep:search" class="mr-5px" /> 搜索
              </el-button>
              <el-button @click="handleReset">
                <Icon icon="ep:refresh" class="mr-5px" /> 重置
              </el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-card>

    <!-- 数据表格 -->
    <el-card shadow="never">
      <template #header>
        <div class="card-header">
          <span>考核规则配置</span>
          <el-button type="primary" @click="handleCreate" v-hasPermi="'prison:score-rule:create'">
            <Icon icon="ep:plus" class="mr-5px" /> 新增规则
          </el-button>
        </div>
      </template>

      <el-table v-loading="loading" :data="tableData" border stripe>
        <el-table-column prop="categoryName" label="考核类别" width="120" />
        <el-table-column prop="itemName" label="项目名称" width="180" />
        <el-table-column prop="itemCode" label="项目编码" width="120" />
        <el-table-column prop="score" label="分值" width="100">
          <template #default="{ row }">
            <span :class="{ 'text-danger': row.score < 0, 'text-success': row.score > 0 }">
              {{ row.score > 0 ? '+' : '' }}{{ row.score }}
            </span>
          </template>
        </el-table-column>
        <el-table-column prop="maxDailyScore" label="日最高分" width="100" />
        <el-table-column prop="maxMonthlyScore" label="月最高分" width="100" />
        <el-table-column prop="description" label="规则说明" show-overflow-tooltip />
        <el-table-column prop="statusName" label="状态" width="100">
          <template #default="{ row }">
            <el-tag :type="row.status === 1 ? 'success' : 'danger'">
              {{ row.statusName }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="sort" label="排序" width="80" />
        <el-table-column label="操作" width="150" fixed="right">
          <template #default="{ row }">
            <el-button link type="primary" @click="handleEdit(row)" v-hasPermi="'prison:score-rule:update'">
              编辑
            </el-button>
            <el-button link type="danger" @click="handleDelete(row)" v-hasPermi="'prison:score-rule:delete'">
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <Pagination
        :total="total"
        v-model:page="searchForm.pageNo"
        v-model:limit="searchForm.pageSize"
        @pagination="getPage"
      />
    </el-card>

    <!-- 规则表单弹窗 -->
    <ScoreRuleForm ref="formRef" @success="getPage" />
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import ScoreRuleForm from './ScoreRuleForm.vue'
import { ScoreRuleApi, type ScoreRulePageReqVO } from '@/api/prison/score-rule'

defineOptions({ name: 'PrisonScoreRule' })

const loading = ref(false)
const tableData = ref([])
const total = ref(0)

const searchForm = reactive<ScoreRulePageReqVO>({
  category: undefined,
  itemName: '',
  status: undefined,
  pageNo: 1,
  pageSize: 20
})

const formRef = ref()

/** 获取分页数据 */
const getPage = async () => {
  loading.value = true
  try {
    const data = await ScoreRuleApi.getRulePage(searchForm)
    tableData.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索 */
const handleSearch = () => {
  searchForm.pageNo = 1
  getPage()
}

/** 重置 */
const handleReset = () => {
  searchForm.category = undefined
  searchForm.itemName = ''
  searchForm.status = undefined
  getPage()
}

/** 新增 */
const handleCreate = () => {
  formRef.value.open()
}

/** 编辑 */
const handleEdit = (row: any) => {
  formRef.value.open(row.id)
}

/** 删除 */
const handleDelete = (row: any) => {
  ElMessageBox.confirm(`确认要删除规则【${row.itemName}】吗？`, '操作确认', {
    type: 'warning'
  }).then(async () => {
    await ScoreRuleApi.deleteRule(row.id)
    ElMessage.success('删除成功')
    getPage()
  })
}

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

<style scoped>
.score-rule-page {
  padding: 20px;
}

.search-card {
  margin-bottom: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.text-danger {
  color: #f56c6c;
}

.text-success {
  color: #67c23a;
}
</style>
