<template>
  <div class="quota-control-container">
    <div class="page-header">
      <PageTitle title="额度控制" />
      <el-button type="danger" @click="handleApplyQuota">额度申请</el-button>
    </div>

    <!-- 额度统计卡片 -->
    <div class="quota-cards">
      <div class="quota-card">
        <div class="card-icon">
          <el-icon><CreditCard /></el-icon>
        </div>
        <div class="card-info">
          <div class="card-label">当前额度 ></div>
          <div class="card-value">32</div>
        </div>
      </div>
      <div class="quota-card">
        <div class="card-info">
          <div class="card-label">剩余可用额度 ></div>
          <div class="card-value">8,831.00</div>
        </div>
      </div>
      <div class="quota-card">
        <div class="card-info">
          <div class="card-label">已使用额度 ></div>
          <div class="card-value">8,831.00</div>
        </div>
      </div>
    </div>

    <!-- 信用额度申请记录 -->
    <div class="section-title">信用额度申请记录</div>
    <el-table :data="recordList" border>
      <el-table-column prop="createTime" label="申请时间" min-width="120" align="center" />
      <el-table-column prop="monthPurchase" label="月度采购金额" min-width="120" align="center" />
      <el-table-column prop="yearPurchase" label="预估年度采购金额" min-width="140" align="center" />
      <el-table-column prop="applyAmount" label="申请信用金额" min-width="120" align="center" />
      <el-table-column prop="category" label="主要采购类目" min-width="120" align="center">
        <template #default="{ row }">
          {{ getDictLabel(purchase_item, row.mainPurchase) }}
        </template>
      </el-table-column>
      <el-table-column prop="approvalStatus" label="申请状态" min-width="100" align="center">
        <template #default="{ row }">
          <span :class="['status-text', getStatusClass(row.approvalStatus)]">{{
            row.approvalStatus == '0' ? '待审核' : row.approvalStatus == '1' ? '已通过' : '已拒绝'
          }}</span>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue';
import { useRouter } from 'vue-router';
import { CreditCard } from '@element-plus/icons-vue';
import { PageTitle } from '@/components';
import { getCreditApplyList } from '@/api/pc/cost/creditApply';
const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { purchase_item } = toRefs<any>(proxy?.useDict('purchase_item'));
const router = useRouter();

const recordList = ref([]);

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

const getDictLabel = (dictOptions: any[], value: string) => {
  if (!dictOptions || !value) return value;
  const dict = dictOptions.find((item) => item.value === value);
  return dict ? dict.label : value;
};

const getStatusClass = (status: string) => {
  if (status === '已通过') return 'success';
  if (status === '待审批') return 'warning';
  return '';
};

const handleApplyQuota = () => {
  router.push('/cost/quotaControl/apply');
};

onMounted(() => {
  loadCreditApply();
});
</script>

<style scoped lang="scss">
.quota-control-container {
  padding: 20px;
  background: #fff;
  min-height: 100%;
  flex: 1;
}

.page-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
  :deep(.page-title) {
    margin-bottom: 0;
  }
}

.quota-cards {
  display: flex;
  gap: 20px;
  margin-bottom: 30px;

  .quota-card {
    flex: 1;
    display: flex;
    align-items: center;
    gap: 15px;
    padding: 20px;
    background: #f9f9f9;
    border-radius: 4px;

    .card-icon {
      width: 40px;
      height: 40px;
      background: #409eff;
      border-radius: 4px;
      display: flex;
      align-items: center;
      justify-content: center;
      color: #fff;
      font-size: 20px;
    }

    .card-info {
      .card-label {
        font-size: 13px;
        color: #999;
        margin-bottom: 5px;
      }
      .card-value {
        font-size: 24px;
        font-weight: bold;
        color: #333;
      }
    }
  }
}

.section-title {
  font-size: 14px;
  color: #333;
  margin-bottom: 15px;
}

:deep(.el-table) {
  th.el-table__cell {
    background: #fafafa;
    font-weight: 500;
    color: #333;
  }
  .el-table__cell {
    padding: 10px 0;
  }
}

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