<template>
  <div class="app-container">
    <el-card shadow="never" class="search-card">
      <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch">
        <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="clientName">
          <el-input v-model="queryParams.clientName" placeholder="请输入名称" clearable style="width: 180px" @keyup.enter="handleQuery" />
        </el-form-item>
        <el-form-item label="行业条线" prop="industryLine">
          <el-select v-model="queryParams.industryLine" placeholder="请选择" clearable style="width: 160px">
          </el-select>
        </el-form-item>
        <el-form-item label="部门" prop="deptId">
          <el-select v-model="queryParams.deptId" placeholder="请选择部门" clearable style="width: 160px">
          </el-select>
        </el-form-item>
        <el-form-item label="业务人员" prop="salesUserId">
           <el-input v-model="queryParams.salesUserId" 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">客户目标信息列表</span>
          <div class="action-buttons">
            <el-button type="primary" icon="Plus" @click="handleAdd">新增数据</el-button>
          </div>
        </div>
      </template>

      <el-table v-loading="loading" :data="clientTaskList" border stripe highlight-current-row class="custom-table">
        <el-table-column type="index" label="序号" width="60" align="center" />
        <el-table-column label="客户编号" align="center" prop="clientCode" width="120" />
        <el-table-column label="客户名称" align="left" prop="clientName" show-overflow-tooltip />
        <el-table-column label="行业条线" align="center" prop="industryLine" width="100" />
        <el-table-column label="部门" align="center" prop="deptName" width="100" />
        <el-table-column label="业务人员" align="center" prop="salesUserName" width="100" />
        <el-table-column label="年度任务" align="center" prop="annualTask" width="120">
          <template #default="scope">
             {{ scope.row.annualTask ? scope.row.annualTask + '万' : '0万' }}
          </template>
        </el-table-column>
        <el-table-column label="实绩" align="center" prop="actualAchievement" width="100" />
        <el-table-column label="差额" align="center" prop="diffAmount" width="100" />
        <el-table-column label="达成比" align="center" prop="achievementRate" width="100" />
        
        <el-table-column label="操作" align="center" width="160" fixed="right">
          <template #default="scope">
            <el-button link type="primary" icon="View" @click="handleView(scope.row)">查看</el-button>
            <el-button link type="primary" icon="Edit" @click="handleUpdate(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="ClientTask">
import { ref, reactive, onMounted, getCurrentInstance } from 'vue';
import { listClientTask, delClientTask } from '@/api/sale/clientTask/index';

const { proxy } = getCurrentInstance();

const loading = ref(false);
const showSearch = ref(true);
const total = ref(0);
const clientTaskList = ref([]);

const queryParams = reactive({
  pageNum: 1,
  pageSize: 10,
  year: new Date().getFullYear().toString(),
  clientName: undefined,
  clientCode: undefined,
  industryLine: undefined,
  deptId: undefined,
  salesUserId: undefined
});

const getList = async () => {
  loading.value = true;
  try {
    const res = await listClientTask(queryParams);
    clientTaskList.value = res.rows || [];
    total.value = res.total || 0;
  } finally {
    loading.value = false;
  }
};

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

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

const handleAdd = () => {
  // 跳转到新增详情
  proxy.$modal.msg("跳转功能开发中...");
};

const handleUpdate = (row) => {
  proxy.$modal.msg("编辑 ID:" + row.id);
};

const handleView = (row) => {
  proxy.$modal.msg("查看 ID:" + row.id);
};

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-radius: 8px;
  border: none;
}

.content-card {
  border-radius: 8px;
  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>
