<template>
  <div class="page-container">
    <div class="page-header">
      <PageTitle title="部门管理" />
      <el-button type="danger" @click="handleAdd(null)">+ 新增部门</el-button>
    </div>

    <el-table :data="deptList" row-key="id" default-expand-all :tree-props="{ children: 'children' }" :indent="24">
      <el-table-column prop="name" label="部门" min-width="250" />
      <el-table-column prop="status" label="状态" width="100" align="center" />
      <el-table-column prop="currentQuota" label="现有额度(年)" width="130" align="center" />
      <el-table-column prop="usedQuota" label="已用额度(年)" width="130" align="center" />
      <el-table-column prop="remainQuota" label="剩余额度(年)" width="130" align="center" />
      <el-table-column label="操作" width="180" align="center">
        <template #default="{ row }">
          <el-button type="primary" link size="small" @click="handleEdit(row)">编辑</el-button>
          <el-button type="danger" link size="small" @click="handleAdd(row)">新增</el-button>
          <el-button type="danger" link size="small" @click="handleDelete(row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 新增/编辑部门弹窗 -->
    <el-dialog v-model="dialogVisible" :title="dialogTitle" width="500px" destroy-on-close>
      <el-form ref="formRef" :model="formData" :rules="formRules" label-width="100px">
        <el-form-item label="上级部门">
          <el-input :value="formData.parentName || '无（顶级部门）'" disabled />
        </el-form-item>
        <el-form-item label="部门名称" prop="name">
          <el-input v-model="formData.name" placeholder="请输入部门名称" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="formData.status">
            <el-radio label="启用">启用</el-radio>
            <el-radio label="停用">停用</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="年度额度">
          <el-input-number v-model="formData.currentQuota" :min="0" :precision="2" controls-position="right" style="width: 200px" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="danger" @click="handleSubmit">确定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, computed } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { PageTitle } from '@/components'

const dialogVisible = ref(false)
const formRef = ref()
const editingRow = ref<any>(null)
const parentRow = ref<any>(null)

const formData = reactive({
  name: '',
  status: '启用',
  currentQuota: 0,
  parentName: ''
})

const formRules = {
  name: [{ required: true, message: '请输入部门名称', trigger: 'blur' }]
}

const dialogTitle = computed(() => editingRow.value ? '编辑部门' : '新增部门')

const deptList = ref([
  {
    id: 1, name: '技术部', status: '启用', currentQuota: '0.00', usedQuota: '0.00', remainQuota: '0.00',
    children: [
      { id: 11, name: '前端组', status: '启用', currentQuota: '0.00', usedQuota: '0.00', remainQuota: '0.00' },
      { id: 12, name: '后端组', status: '启用', currentQuota: '0.00', usedQuota: '0.00', remainQuota: '0.00' }
    ]
  },
  { id: 2, name: '财务部', status: '启用', currentQuota: '0.00', usedQuota: '0.00', remainQuota: '0.00' },
  { id: 3, name: '设计部', status: '启用', currentQuota: '0.00', usedQuota: '0.00', remainQuota: '0.00' },
  { id: 4, name: '财务部', status: '启用', currentQuota: '0.00', usedQuota: '0.00', remainQuota: '0.00' },
  {
    id: 5, name: '人事部...', status: '启用', currentQuota: '0.00', usedQuota: '0.00', remainQuota: '0.00',
    children: [
      { id: 51, name: '人事部01', status: '启用', currentQuota: '0.00', usedQuota: '0.00', remainQuota: '0.00' },
      { id: 52, name: '人事部02', status: '启用', currentQuota: '0.00', usedQuota: '0.00', remainQuota: '0.00' }
    ]
  }
])

const handleAdd = (parent: any) => {
  editingRow.value = null
  parentRow.value = parent
  formData.name = ''
  formData.status = '启用'
  formData.currentQuota = 0
  formData.parentName = parent ? parent.name : ''
  dialogVisible.value = true
}

const handleEdit = (row: any) => {
  editingRow.value = row
  parentRow.value = null
  formData.name = row.name
  formData.status = row.status
  formData.currentQuota = parseFloat(row.currentQuota) || 0
  formData.parentName = ''
  dialogVisible.value = true
}

const handleDelete = (row: any) => {
  ElMessageBox.confirm(`确定要删除部门"${row.name}"吗？`, '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    ElMessage.success('删除成功')
  })
}

const handleSubmit = async () => {
  const valid = await formRef.value?.validate()
  if (!valid) return
  if (editingRow.value) {
    ElMessage.success('编辑成功')
  } else {
    ElMessage.success('新增成功')
  }
  dialogVisible.value = false
}
</script>

<style scoped lang="scss">
.page-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
  
  :deep(.page-title) {
    margin-bottom: 0;
  }
}

:deep(.el-table) {
  th.el-table__cell {
    background: #fafafa;
    font-weight: 500;
    color: #333;
  }
  
  .el-table__expand-icon {
    color: #666;
    font-size: 12px;
  }
}
</style>
