<template>
  <div class="page-container">
    <div class="page-header">
      <PageTitle title="分项费用" />
      <el-button type="danger" @click="handleAdd">+ 新建</el-button>
    </div>

    <el-table :data="expenseList" border>
      <el-table-column prop="expenseName" label="分项费用名称" min-width="200" />
      <el-table-column prop="currentQuota" label="现有额度(年)" min-width="150" align="center">
        <template #default="{ row }">
          <span>{{ row.currentQuota || '0.00' }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="usedQuota" label="已用额度(年)" min-width="150" align="center">
        <template #default="{ row }">
          <span>{{ row.usedQuota || '0.00' }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="status" label="状态" min-width="100" align="center">
        <template #default="{ row }">
          <span :class="['status-text', row.status === '启用' ? 'active' : '']">{{ row.status == '0' ? '启用' : '禁用' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="100" 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="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="120px">
        <el-form-item label="分项费用名称" prop="expenseName">
          <el-input v-model="formData.expenseName" placeholder="请输入分项费用名称" />
        </el-form-item>
        <!-- <el-form-item label="现有额度(年)">
          <el-input-number v-model="formData.currentQuota" :min="0" :precision="2" controls-position="right" style="width: 100%" />
        </el-form-item> -->
        <el-form-item label="状态">
          <el-radio-group v-model="formData.status">
            <el-radio label="0">启用</el-radio>
            <el-radio label="1">停用</el-radio>
          </el-radio-group>
        </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 { getItemExpenseList, addItemExpense, updateItemExpense, deleteItemExpense } from '@/api/pc/cost/itemExpense';
import { PageTitle } from '@/components';
import { id } from 'element-plus/es/locale/index.mjs';

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

const formData = reactive({
  id: 0,
  expenseName: '',
  currentQuota: 0,
  type: '',
  status: '0'
});

const formRules = {
  name: [{ required: true, message: '请输入分项费用名称', trigger: 'blur' }]
};

const dialogTitle = computed(() => (editingRow.value ? '编辑分项费用' : '新建分项费用'));

const expenseList = ref([]);

const loadItemExpense = async () => {
  try {
    const res = await getItemExpenseList();
    if (res.code === 200 && res.rows) {
      expenseList.value = res.rows;
    }
  } catch (error) {
    console.error('获取分项费用失败:', error);
    ElMessage.error('获取分项费用失败');
  }
};

const handleAdd = () => {
  editingRow.value = null;
  formData.expenseName = '';
  formData.currentQuota = 0;
  formData.status = '0';
  dialogVisible.value = true;
};

const handleEdit = (row: any) => {
  editingRow.value = row;
  formData.id = row.id;
  formData.expenseName = row.expenseName;
  formData.currentQuota = parseFloat(row.currentQuota) || 0;
  formData.status = row.status;
  dialogVisible.value = true;
};

const handleDelete = (row: any) => {
  ElMessageBox.confirm(`确定要删除"${row.name}"吗？`, '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    const index = expenseList.value.findIndex((item) => item.id === row.id);
    if (index > -1) {
      expenseList.value.splice(index, 1);
    }
    ElMessage.success('删除成功');
  });
};

const handleSubmit = async () => {
  const valid = await formRef.value?.validate();
  if (!valid) return;
  if (editingRow.value) {
    await updateItemExpense(formData);
    ElMessage.success('编辑成功');
    loadItemExpense();
  } else {
    await addItemExpense(formData);
    ElMessage.success('新建成功');
    loadItemExpense();
  }
  dialogVisible.value = false;
};
onMounted(() => {
  loadItemExpense();
});
</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__cell {
    padding: 10px 0;
  }
}

.status-text {
  color: #999;
  &.active {
    color: #67c23a;
  }
}
</style>
