<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="year">
              <el-date-picker v-model="queryParams.year" type="year" value-format="YYYY" placeholder="请选择年份" style="width: 100%" />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="选择月份" prop="month">
              <el-date-picker v-model="queryParams.month" type="month" value-format="MM" format="M月" placeholder="请选择" style="width: 100%" />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="客户名称" prop="customerName">
              <el-input v-model="queryParams.customerName" placeholder="请输入客户名称" clearable />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="部门" prop="deptId">
              <el-input v-model="queryParams.deptId" placeholder="请输入部门" clearable />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="24" style="margin-top: 15px;">
          <el-col :span="6">
            <el-form-item label="行业条线" prop="industryLine">
              <el-input v-model="queryParams.industryLine" 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="12" 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">客户目标信息列表</span>
          <div class="action-buttons">
            <el-button type="primary" icon="Plus" @click="handleAdd">新增数据</el-button>
          </div>
        </div>
      </template>

      <el-table v-loading="loading" :data="customerTargetList" 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="customerNo" width="120" show-overflow-tooltip />
        <el-table-column label="客户名称" align="center" prop="customerName" min-width="180" 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="salesName" width="100" />
        <el-table-column label="年度任务" align="center" prop="annualTaskValue" width="100">
          <template #default="scope">
            {{ scope.row.annualTaskValue ? scope.row.annualTaskValue + '万' : '-' }}
          </template>
        </el-table-column>
        <el-table-column label="实绩" align="center" prop="performance" width="100" />
        <el-table-column label="差额" align="center" prop="difference" width="100" />
        <el-table-column label="达成比" align="center" prop="achievementRate" width="100" />
        <el-table-column label="操作" align="center" width="150" fixed="right">
          <template #default="scope">
            <el-button type="primary" link icon="View" @click="handleView(scope.row)">查看</el-button>
            <el-button type="primary" link icon="Edit" @click="handleEdit(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>

    <!-- 新增引导弹窗 -->
    <el-dialog title="新增客户任务制度" v-model="dialogVisible" width="500px" center append-to-body>
      <div class="dialog-body">
        <el-form label-width="100px">
          <el-form-item label="任务制定：" required>
            <el-radio-group v-model="targetType">
              <el-radio value="year">年度任务制定</el-radio>
              <el-radio value="month">月度任务制定</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-form>
      </div>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="confirmTargetType">确认</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="CustomerTarget">
import { ref, reactive, onMounted, getCurrentInstance } from 'vue';
import { useRouter } from 'vue-router';
import { listCustomerTarget } from '@/api/sale/customerTarget/index';

const router = useRouter();
const { proxy } = getCurrentInstance();

const loading = ref(false);
const total = ref(0);
const customerTargetList = ref([]);
const dialogVisible = ref(false);
const targetType = ref('year');

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

const getList = async () => {
  loading.value = true;
  try {
    const res = await listCustomerTarget(queryParams);
    customerTargetList.value = res.data || [];
    total.value = res.total || 0;
  } catch (err) {
    // 设计师模拟数据还原
    customerTargetList.value = [
      { id: 1, customerNo: 'GZYH001', customerName: '广发银行某信支行', industryLine: '服务商', deptName: '黄石', salesName: '喻欣源', annualTaskValue: 300, achievementRate: '0%' },
      { id: 2, customerNo: 'YZ002', customerName: '中国邮政储蓄...', industryLine: '服务商', deptName: '黄石', salesName: '唐某某', annualTaskValue: 300, achievementRate: '0%' },
      { id: 3, customerNo: 'NY003', customerName: '中国农业银行...', industryLine: '服务商', deptName: '黄石', salesName: '孔某某', annualTaskValue: 300, achievementRate: '0%' }
    ];
    total.value = 3;
  } finally {
    loading.value = false;
  }
};

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

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

const handleAdd = () => {
  dialogVisible.value = true;
};

const confirmTargetType = () => {
  dialogVisible.value = false;
  if (targetType.value === 'year') {
    router.push('/sales/year-detail');
  } else {
    router.push('/sales/month-detail');
  }
};

const handleView = (row) => {};
const handleEdit = (row) => {};

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;
  }
}

.dialog-body {
  padding: 30px 20px;
}

.dialog-footer {
  display: flex;
  justify-content: center;
  gap: 20px;
}
</style>
