<template>
  <div class="app-container">
    <el-card shadow="never" class="search-card">
      <el-form :model="queryParams" ref="queryRef" label-width="80px">
        <el-row :gutter="24">
          <el-col :span="6">
            <el-form-item label="部门" prop="deptId">
              <el-input v-model="queryParams.deptId" placeholder="请输入部门" clearable />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="业务员" prop="salesName">
              <el-input v-model="queryParams.salesName" placeholder="请输入业务人员" clearable />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="客服人员" prop="csName">
              <el-input v-model="queryParams.csName" placeholder="请输入客服人员" clearable />
            </el-form-item>
          </el-col>
          <el-col :span="6" class="search-btns">
            <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
            <el-button icon="Refresh" @click="resetQuery">重置</el-button>
          </el-col>
        </el-row>
      </el-form>
    </el-card>

    <el-card shadow="never" class="content-card">
      <template #header>
        <div class="header-container">
          <span class="title-text">KPI考核信息列表</span>
          <div class="action-buttons">
            <el-button type="info" plain icon="Download" @click="handleExport">导出</el-button>
          </div>
        </div>
      </template>

      <el-table v-loading="loading" :data="kpiList" border stripe highlight-current-row class="custom-table">
        <el-table-column type="index" label="序号" width="60" align="center" fixed />
        <el-table-column label="部门" align="center" prop="deptName" width="100" />
        <el-table-column label="业务人员" align="center" prop="salesName" width="100" />
        <el-table-column label="客服人员" align="center" prop="csName" width="100" />
        
        <el-table-column label="商机管理" align="center">
          <el-table-column label="销售线索" align="center" prop="leads" width="120" show-overflow-tooltip />
          <el-table-column label="项目商机" align="center" prop="opportunities" width="120" show-overflow-tooltip />
          <el-table-column label="招标入围" align="center" prop="biddingIn" width="120" show-overflow-tooltip />
        </el-table-column>

        <el-table-column label="客户开发" align="center">
          <el-table-column label="原有客户数" align="center" prop="existingCustomers" width="100" />
          <el-table-column label="新增客户数" align="center" prop="newCustomers" width="100" />
        </el-table-column>

        <el-table-column label="客户拜访" align="center">
          <el-table-column label="客户数" align="center" prop="visitedCustomers" width="100" />
          <el-table-column label="已跟进客户数" align="center" prop="followedCustomers" width="120" />
        </el-table-column>

        <el-table-column label="销售目标(月)" align="center" prop="monthlyGoal" width="110">
          <template #default="scope">
            {{ scope.row.monthlyGoal ? scope.row.monthlyGoal + '万' : '-' }}
          </template>
        </el-table-column>
        <el-table-column label="冲刺目标(月)" align="center" prop="monthlySprintGoal" width="110">
          <template #default="scope">
            {{ scope.row.monthlySprintGoal ? scope.row.monthlySprintGoal + '万' : '-' }}
          </template>
        </el-table-column>

        <el-table-column label="销售情况" align="center">
          <el-table-column label="销售金额" align="center" prop="salesAmount" width="100">
             <template #default="scope">
              {{ scope.row.salesAmount ? scope.row.salesAmount + '万' : '-' }}
            </template>
          </el-table-column>
          <el-table-column label="退货金额" align="center" prop="returnAmount" width="100" />
          <el-table-column label="实际销售金额" align="center" prop="actualAmount" width="120">
             <template #default="scope">
              {{ scope.row.actualAmount ? scope.row.actualAmount + '万' : '-' }}
            </template>
          </el-table-column>
        </el-table-column>
      </el-table>

      <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList" />
    </el-card>
  </div>
</template>

<script setup name="KpiAssessment">
import { ref, reactive, onMounted, getCurrentInstance } from 'vue';
import { listKpiAssessment } from '@/api/sale/kpi/index';

const { proxy } = getCurrentInstance();

const loading = ref(false);
const total = ref(0);
const kpiList = ref([]);

const queryParams = reactive({
  pageNum: 1,
  pageSize: 10,
  deptId: undefined,
  salesName: undefined,
  csName: undefined
});

const getList = async () => {
  loading.value = true;
  try {
    const res = await listKpiAssessment(queryParams);
    kpiList.value = res.data || [];
    total.value = res.total || 0;
  } catch (err) {
    // Designer Preview Mock Data
    kpiList.value = [
      { id: 1, deptName: '咸宁', salesName: '柏忠林', csName: '郝淑淼', leads: '黄梅农行', opportunities: '黄梅农行', biddingIn: '邮储武汉...', existingCustomers: 10000, newCustomers: 1000, visitedCustomers: 10000, followedCustomers: 3600, monthlyGoal: 100, monthlySprintGoal: 100, salesAmount: 100, returnAmount: 0, actualAmount: 100 },
      { id: 2, deptName: '咸宁', salesName: '岑丽', csName: '戚淑基', leads: '麻城农行', opportunities: '麻城农行', biddingIn: '送变电...', existingCustomers: 10000, newCustomers: 1000, visitedCustomers: 10000, followedCustomers: 3600, monthlyGoal: 100, monthlySprintGoal: 100, salesAmount: 100, returnAmount: 0, actualAmount: 100 },
      { id: 3, deptName: '咸宁', salesName: '孙建林', csName: '章慧英', leads: '麻城农行', opportunities: '麻城农行', biddingIn: '送变电...', existingCustomers: 10000, newCustomers: 1000, visitedCustomers: 10000, followedCustomers: 3600, monthlyGoal: 100, monthlySprintGoal: 100, salesAmount: 100, returnAmount: 0, actualAmount: 100 }
    ];
    total.value = 3;
  } finally {
    loading.value = false;
  }
};

const handleQuery = () => {
  queryParams.pageNum = 1;
  getList();
};

const resetQuery = () => {
  proxy.resetForm("queryRef");
  handleQuery();
};

const handleExport = () => {};

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

<style scoped lang="scss">
.app-container {
  padding: 20px;
  background-color: #f7f9fb;
  min-height: calc(100vh - 84px);
}

.search-card {
  margin-bottom: 20px;
  border: none;
  background: #fff;
  border-radius: 8px;
  padding: 10px;
  :deep(.el-form-item) {
    margin-bottom: 0px;
  }
}

.search-btns {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 12px;
}

.content-card {
  border: none;
  .header-container {
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
  .title-text {
    font-size: 16px;
    font-weight: 600;
    color: #333;
    position: relative;
    padding-left: 12px;
    &::before {
      content: "";
      position: absolute;
      left: 0;
      top: 50%;
      transform: translateY(-50%);
      width: 4px;
      height: 16px;
      background: #409eff;
      border-radius: 2px;
    }
  }
}

.custom-table {
  border-radius: 8px;
  overflow: hidden;
  :deep(th.el-table__cell) {
    background-color: #f8fafc !important;
    color: #475569;
    font-weight: 600;
  }
}
</style>
