<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div v-show="showSearch" class="mb-[10px]">
        <el-card shadow="hover">
          <el-form ref="queryFormRef" :model="queryParams" :inline="true">
            <el-form-item label="客户编号" prop="customerNo">
              <el-input v-model="queryParams.customerNo" placeholder="请输入客户编号" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="客户名称" prop="customerNo">
              <el-input v-model="queryParams.customerName" placeholder="请输入客户名称" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="归属部门" prop="belongingDepartmentId">
              <el-select v-model="queryParams.belongingDepartmentId" placeholder="请选择归属部门" clearable filterable style="width: 200px">
                <el-option label="销售部" value="1" />
                <el-option label="市场部" value="2" />
                <el-option label="客服部" value="3" />
                <el-option label="技术部" value="4" />
              </el-select>
            </el-form-item>
            <el-form-item label="所属行业" prop="industryCategoryId">
              <el-select v-model="queryParams.industryCategoryId" placeholder="请选择行业" clearable filterable style="width: 200px">
                <el-option v-for="industry in industryCategoryList" :key="industry.id" :label="industry.industryCategoryName" :value="industry.id" />
              </el-select>
            </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>
      </div>
    </transition>

    <el-card shadow="never">
      <template #header>
        <el-row :gutter="10" class="mb8">
          <el-col :span="20"><span>合同管理信息列表</span> </el-col>
        </el-row>
      </template>

      <el-table v-loading="loading" border :data="contractList">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="客户编号" align="center" prop="customerNo" />
        <el-table-column label="客户名称" align="center" prop="customerName" />
        <el-table-column label="归属部门" align="center" prop="deptName" />
        <el-table-column label="所属行业" align="center" prop="industryCategory" />
        <el-table-column label="合同数量" align="center" prop="contractCount" />
        <el-table-column label="生效合同" align="center" prop="effectiveContract" />
        <el-table-column label="失效合同" align="center" prop="invalidContract" />
        <el-table-column label="作废合同" align="center" prop="voidedContract" />

        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-button link type="primary" icon="Edit" @click="handleManage(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="Contracts" lang="ts">
import { listContractList } from '@/api/customer/customerFile/customerInfo';
import { CustomerInfoQuery, CustomerInfoForm } from '@/api/customer/customerFile/customerInfo/types';
import { listIndustryCategory } from '@/api/customer/customerCategory/industryCategory';
import { IndustryCategoryVO } from '@/api/customer/customerCategory/industryCategory/types';
import { info } from 'console';
const router = useRouter();
const { proxy } = getCurrentInstance() as ComponentInternalInstance;

const industryCategoryList = ref<IndustryCategoryVO[]>([]);
const contractList = ref<any[]>([]);
const loading = ref(true);
const showSearch = ref(true);
const total = ref(0);

const queryFormRef = ref<ElFormInstance>();
const maintainInfoFormRef = ref<ElFormInstance>();

const dialog = reactive<DialogOption>({
  visible: false,
  title: ''
});

const initFormData: CustomerInfoForm = {
  id: undefined,
  customerNo: undefined,
  belongCompanyId: undefined,
  companyName: undefined,
  customerName: undefined,
  businessCustomerName: undefined,
  shortName: undefined,
  invoiceTypeId: undefined,
  enterpriseScaleId: undefined,
  customerTypeId: undefined,
  industryCategoryId: undefined,
  customerLevelId: undefined,
  landline: undefined,
  fax: undefined,
  url: undefined,
  postCode: undefined,
  validityFromDate: undefined,
  validityToDate: undefined,
  invoiceTop: undefined,
  address: undefined,
  regProvincialNo: undefined,
  regCityNo: undefined,
  regCountyNo: undefined,
  provincialCityCounty: undefined,
  status: undefined,
  remark: undefined
};
const data = reactive<any>({
  form: { ...initFormData },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    customerNo: undefined,
    customerName: undefined,
    industryCategoryId: undefined,
    belongingDepartmentId: undefined
  }
}) as any;
const { queryParams, form, rules } = toRefs(data);

/** 查询维保记录列表 */
const getList = async () => {
  loading.value = true;
  const res = await listContractList(queryParams.value);
  contractList.value = res.rows;
  total.value = res.total;
  loading.value = false;
};

/** 取消按钮 */
const cancel = () => {
  reset();
  dialog.visible = false;
};

//管理按钮
const handleManage = (row: any) => {
  router.push({
    path: '/customer/customer-file/customer-contract-info',
    query: {
      customerId: row.customerId
    }
  });
};
/** 表单重置 */
const reset = () => {
  form.value = { ...initFormData };
  maintainInfoFormRef.value?.resetFields();
};

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
};

/** 加载行业分类列表 */
const loadIndustryCategories = async () => {
  try {
    const res = await listIndustryCategory(); // 只获取启用状态的行业
    industryCategoryList.value = res.rows;
  } catch (error) {
    console.error('加载行业分类失败:', error);
  }
};

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  queryParams.value = {};
  handleQuery();
};

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