<template>
  <div class="app-container">
    <el-card shadow="never" class="search-card">
      <el-form :model="queryParams" ref="queryRef" :inline="true">
        <el-form-item label="年度" prop="year">
          <el-date-picker v-model="queryParams.year" type="year" value-format="YYYY" placeholder="选择年" style="width: 140px" />
        </el-form-item>
        <el-form-item label="月份" prop="month">
           <el-select v-model="queryParams.month" placeholder="请选择月" clearable style="width: 100px">
              <el-option v-for="i in 12" :key="i" :label="i + '月'" :value="i" />
           </el-select>
        </el-form-item>
        <el-form-item label="部门" prop="deptId">
          <el-select v-model="queryParams.deptId" placeholder="部门" clearable style="width: 150px">
          </el-select>
        </el-form-item>
        <el-form-item label="业务人员" prop="salesUserId">
          <el-input v-model="queryParams.salesUserName" placeholder="姓名" clearable style="width: 150px" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
          <el-button icon="Refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </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="success" plain icon="Download" @click="handleExport">导出</el-button>
            <el-button type="primary" icon="Plus" @click="handleAdd">新增考核</el-button>
          </div>
        </div>
      </template>

      <el-table v-loading="loading" :data="kpiList" border stripe class="kpi-table" height="600">
        <!-- 基础列 Fixed Left -->
        <el-table-column type="index" label="序号" width="60" align="center" fixed="left" />
        <el-table-column label="部门" align="center" prop="deptName" width="100" fixed="left" />
        <el-table-column label="行业条线" align="center" prop="industryLine" width="100" fixed="left" />
        <el-table-column label="业务人员" align="center" prop="salesUserName" width="100" fixed="left" />
        <el-table-column label="客服人员" align="center" prop="serviceUserName" width="100" fixed="left" />
        <el-table-column label="核心客户名称" align="left" prop="coreCustomerName" width="180" show-overflow-tooltip fixed="left" />

        <!-- 商机管理 -->
        <el-table-column label="商机管理" align="center">
          <el-table-column label="销售线索" align="center" prop="salesLeads" width="150" />
          <el-table-column label="项目商机" align="center" prop="projectOpportunities" width="150" />
          <el-table-column label="招标入围" align="center" prop="tenderShortlist" width="150" />
        </el-table-column>

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

        <!-- 客户拜访 -->
        <el-table-column label="客户拜访" align="center">
          <el-table-column label="客户数" align="center" prop="visitCustomerCount" width="100" />
          <el-table-column label="已跟进客户数" align="center" prop="followedCustomerCount" width="110" />
        </el-table-column>

        <!-- 销售指标 -->
        <el-table-column label="销售目标(月)" align="center" prop="monthlyTarget" width="120" />
        <el-table-column label="冲刺目标(月)" align="center" prop="sprintTarget" width="120" />

        <!-- 销售情况 -->
        <el-table-column label="销售情况" align="center">
          <el-table-column label="销售金额" align="center" prop="salesAmount" width="110" />
          <el-table-column label="退货金额" align="center" prop="returnAmount" width="110" />
          <el-table-column label="实际销售金额" align="center" prop="actualSalesAmount" width="120" />
          <el-table-column label="毛利系数" align="center" prop="grossMarginRate" width="100">
             <template #default="scope">
                {{ scope.row.grossMarginRate ? scope.row.grossMarginRate + '%' : '0%' }}
             </template>
          </el-table-column>
          <el-table-column label="基础销量" align="center" prop="baseSalesVolume" width="110">
             <template #default="scope">
                {{ scope.row.baseSalesVolume ? scope.row.baseSalesVolume + '万' : '0万' }}
             </template>
          </el-table-column>
          <el-table-column label="项目销量" align="center" prop="projectSalesVolume" width="110">
             <template #default="scope">
                {{ scope.row.projectSalesVolume ? scope.row.projectSalesVolume + '万' : '0万' }}
             </template>
          </el-table-column>
        </el-table-column>

        <!-- 考勤管理 -->
        <el-table-column label="考勤管理" align="center">
          <el-table-column label="应出勤天数" align="center" prop="shouldAttendanceDays" width="90" />
          <el-table-column label="实际出勤天数" align="center" prop="actualAttendanceDays" width="90" />
          <el-table-column label="异常天数" align="center" prop="abnormalDays" width="70" />
        </el-table-column>

        <el-table-column label="操作" align="center" width="120" fixed="right">
          <template #default="scope">
            <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)">修改</el-button>
            <el-button link type="danger" icon="Delete" @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </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="KpiTask">
import { ref, reactive, onMounted, getCurrentInstance } from 'vue';
import { listKpiTask } from '@/api/sale/kpiTask/index';

const { proxy } = getCurrentInstance();

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

const queryParams = reactive({
  pageNum: 1,
  pageSize: 10,
  year: new Date().getFullYear().toString(),
  month: new Date().getMonth() + 1, // 设置默认当前月份
  deptId: undefined,
  salesUserName: undefined
});

/** 模拟数据深度补全 */
const getMockData = () => {
    return [
        {
            id: 1,
            deptName: '咸宁',
            industryLine: '服务商',
            salesUserName: '卫昊轩',
            serviceUserName: '陈贺详',
            coreCustomerName: '黄梅农村商业银行',
            salesLeads: '黄梅农行',
            projectOpportunities: '黄梅农行',
            tenderShortlist: '邮储武汉...',
            oldCustomerCount: 10000,
            newCustomerCount: 1000,
            visitCustomerCount: 10000,
            followedCustomerCount: 3600,
            monthlyTarget: '100万',
            sprintTarget: '100万',
            salesAmount: '100万',
            returnAmount: '0',
            actualSalesAmount: '100万',
            grossMarginRate: 80,
            baseSalesVolume: '100',
            projectSalesVolume: '100',
            shouldAttendanceDays: 31,
            actualAttendanceDays: 31,
            abnormalDays: 0
        },
        {
            id: 2,
            deptName: '咸宁',
            industryLine: '直销',
            salesUserName: '张洁晨',
            serviceUserName: '严冰枫',
            coreCustomerName: '宜城农村商业银行',
            salesLeads: '宜城农行...',
            projectOpportunities: '宜城农行...',
            tenderShortlist: '送变电局...',
            actualSalesAmount: '100万',
            grossMarginRate: 80,
            baseSalesVolume: '100万',
            projectSalesVolume: '100万',
            shouldAttendanceDays: 31,
            actualAttendanceDays: 31,
            abnormalDays: 0
        }
    ];
};

const getList = async () => {
  loading.value = true;
  try {
    // 实际联调启动
    // const res = await listKpiTask(queryParams);
    // kpiList.value = res.rows;
    // total.value = res.total;
    
    // 暂时使用模拟数据进行 UI 演示
    setTimeout(() => {
        kpiList.value = getMockData();
        total.value = 10;
        loading.value = false;
    }, 500);
  } catch (err) {
    loading.value = false;
  }
};

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

const resetQuery = () => {
  proxy.resetForm("queryRef");
  queryParams.year = new Date().getFullYear().toString();
  queryParams.month = new Date().getMonth() + 1;
  handleQuery();
};

const handleUpdate = (row) => {
  proxy.$modal.msg("该功能开发中...");
};

const handleAdd = () => {
    proxy.$modal.msg("正在前往录入页面...");
};

const handleExport = () => {
    proxy.$modal.msg("导出任务已提交...");
};

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

<style scoped lang="scss">
.app-container {
  padding: 10px 20px;
  background-color: #f7f9fb;
}

.search-card {
  margin-bottom: 12px;
  border: none;
  border-radius: 8px;
}

.content-card {
  border: none;
  border-radius: 8px;
  .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: #007aff;
      border-radius: 2px;
    }
  }
}

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