<template>
  <div class="app-page appeal-workbench">
    <div class="page-hero">
      <div class="hero-info">
        <h2>绩效申诉</h2>
        <p>员工异议处理与结果回执</p>
      </div>
      <el-select v-model="status" style="width:160px" @change="load">
        <el-option label="待处理" value="OPEN" />
        <el-option label="已接受" value="ACCEPTED" />
        <el-option label="已驳回" value="REJECTED" />
        <el-option label="全部" value="" />
      </el-select>
    </div>

    <el-table :data="appeals" stripe v-loading="loading" class="data-table" style="width:100%">
      <el-table-column prop="id" label="ID" width="70" />
      <el-table-column prop="periodId" label="周期" width="90" />
      <el-table-column prop="userId" label="员工ID" width="90" />
      <el-table-column prop="reason" label="申诉原因" min-width="260" show-overflow-tooltip />
      <el-table-column label="状态" width="110">
        <template #default="{ row }">
          <el-tag :type="statusType(row.status)" effect="plain" round>{{ statusLabel(row.status) }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="reply" label="处理意见" min-width="180" show-overflow-tooltip />
      <el-table-column prop="submittedAt" label="提交时间" min-width="160" />
      <el-table-column label="操作" width="150" fixed="right">
        <template #default="{ row }">
          <el-button v-if="row.status === 'OPEN'" link type="primary" @click="openResolve(row, 'ACCEPTED')">接受</el-button>
          <el-button v-if="row.status === 'OPEN'" link type="danger" @click="openResolve(row, 'REJECTED')">驳回</el-button>
          <span v-else class="muted">已处理</span>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { scoreAppealApi } from '../../api'
import { ElMessage, ElMessageBox } from 'element-plus'

const status = ref('OPEN')
const appeals = ref([])
const loading = ref(false)

onMounted(() => load())

async function load() {
  loading.value = true
  try {
    appeals.value = await scoreAppealApi.list({ status: status.value })
  } finally {
    loading.value = false
  }
}

async function openResolve(row, result) {
  const { value } = await ElMessageBox.prompt('请输入处理意见', result === 'ACCEPTED' ? '接受申诉' : '驳回申诉', {
    inputType: 'textarea',
    inputPlaceholder: result === 'ACCEPTED' ? '说明后续修正或沟通安排' : '说明驳回原因',
    confirmButtonText: '提交处理',
    cancelButtonText: '取消'
  })
  await scoreAppealApi.resolve(row.id, { status: result, reply: value })
  ElMessage.success('申诉已处理')
  await load()
}

function statusLabel(s) { return { OPEN: '待处理', ACCEPTED: '已接受', REJECTED: '已驳回' }[s] || s }
function statusType(s) { return { OPEN: 'warning', ACCEPTED: 'success', REJECTED: 'info' }[s] || 'info' }
</script>

<style scoped>
.page-hero { display: flex; justify-content: space-between; align-items: flex-start; margin-bottom: 24px; }
.hero-info h2 { font-size: 28px; font-weight: 700; color: #1a1a2e; margin: 0; }
.hero-info p { font-size: 14px; color: #888; margin: 4px 0 0; }
.data-table { border-radius: 8px; overflow: hidden; }
.muted { color: #a0a7b2; font-size: 12px; }
</style>
