<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" label-width="100px">
            <el-form-item label="供应商名称" prop="supplierName">
              <el-input v-model="queryParams.supplierName" placeholder="请输入供应商名称" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="助记名称" prop="mnemonicName">
              <el-input v-model="queryParams.mnemonicName" placeholder="请输入助记名称" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="供应商类型" prop="supplierType">
              <el-select v-model="queryParams.supplierType" placeholder="请选择供应商类型" clearable>
                <el-option v-for="dict in supplier_type" :key="dict.value" :label="dict.label" :value="dict.value" />
              </el-select>
            </el-form-item>
            <!--                                    <el-form-item label="注册资本" prop="registeredCapital">
                                                    <el-input v-model="queryParams.registeredCapital" placeholder="请输入注册资本" clearable @keyup.enter="handleQuery" />
                                                </el-form-item>
                                                <el-form-item label="成立日期" prop="establishmentDate">
                                                    <el-date-picker clearable
                                                                    v-model="queryParams.establishmentDate"
                                                                    type="date"
                                                                    value-format="YYYY-MM-DD"
                                                                    placeholder="请选择成立日期"
                                                    />
                                                </el-form-item>
                                                <el-form-item label="经营范围" prop="businessScope">
                                                    <el-input v-model="queryParams.businessScope" placeholder="请输入经营范围" clearable @keyup.enter="handleQuery" />
                                                </el-form-item>-->
            <el-form-item label="主营行业" prop="mainIndustry">
              <el-input v-model="queryParams.mainIndustry" placeholder="请输入主营行业" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <!--                                    <el-form-item label="法定代表人" prop="legalRepresentative">
                                                    <el-input v-model="queryParams.legalRepresentative" placeholder="请输入法定代表人" clearable @keyup.enter="handleQuery" />
                                                </el-form-item>-->
            <!--                                    <el-form-item label="营业执照号码" prop="businessLicenseNumber">
                                                    <el-input v-model="queryParams.businessLicenseNumber" placeholder="请输入营业执照号码" clearable @keyup.enter="handleQuery" />
                                                </el-form-item>
                                                <el-form-item label="税号" prop="taxNumber">
                                                    <el-input v-model="queryParams.taxNumber" placeholder="请输入税号" clearable @keyup.enter="handleQuery" />
                                                </el-form-item>
                                                <el-form-item label="开户银行" prop="bankAccountOpening">
                                                    <el-input v-model="queryParams.bankAccountOpening" placeholder="请输入开户银行" clearable @keyup.enter="handleQuery" />
                                                </el-form-item>
                                                <el-form-item label="银行账号" prop="bankNumber">
                                                    <el-input v-model="queryParams.bankNumber" placeholder="请输入银行账号" clearable @keyup.enter="handleQuery" />
                                                </el-form-item>-->
            <!--                                    <el-form-item label="注册地址" prop="registeredAddress">
                                                    <el-input v-model="queryParams.registeredAddress" placeholder="请输入注册地址" clearable @keyup.enter="handleQuery" />
                                                </el-form-item>
                                                <el-form-item label="经营地址" prop="businessAddress">
                                                    <el-input v-model="queryParams.businessAddress" placeholder="请输入经营地址" clearable @keyup.enter="handleQuery" />
                                                </el-form-item>-->
            <!--                                    <el-form-item label="联系人" prop="contactPerson">
                                                    <el-input v-model="queryParams.contactPerson" placeholder="请输入联系人" clearable @keyup.enter="handleQuery" />
                                                </el-form-item>
                                                <el-form-item label="联系电话" prop="contactPhone">
                                                    <el-input v-model="queryParams.contactPhone" placeholder="请输入联系电话" clearable @keyup.enter="handleQuery" />
                                                </el-form-item>
                                                <el-form-item label="联系手机" prop="contactMobile">
                                                    <el-input v-model="queryParams.contactMobile" placeholder="请输入联系手机" clearable @keyup.enter="handleQuery" />
                                                </el-form-item>
                                                <el-form-item label="联系邮箱" prop="contactEmail">
                                                    <el-input v-model="queryParams.contactEmail" placeholder="请输入联系邮箱" clearable @keyup.enter="handleQuery" />
                                                </el-form-item>
                                                <el-form-item label="传真" prop="contactFax">
                                                    <el-input v-model="queryParams.contactFax" placeholder="请输入传真" clearable @keyup.enter="handleQuery" />
                                                </el-form-item>-->
            <!-- 注释掉不合理的搜索条件 -->
            <!-- <el-form-item label="网址" prop="website">
                <el-input v-model="queryParams.website" placeholder="请输入网址" clearable @keyup.enter="handleQuery" />
            </el-form-item> -->
            <!-- <el-form-item label="详细地址" prop="detailedAddress">
                <el-input v-model="queryParams.detailedAddress" placeholder="请输入详细地址" clearable @keyup.enter="handleQuery" />
            </el-form-item> -->
            <!-- <el-form-item label="附件ID" prop="ossId">
                <el-input v-model="queryParams.ossId" placeholder="请输入附件ID" clearable @keyup.enter="handleQuery" />
            </el-form-item> -->
            <el-form-item label="激活标识" prop="activeFlag">
              <el-select v-model="queryParams.activeFlag" placeholder="请选择激活标识" clearable>
                <el-option v-for="dict in active_flag" :key="dict.value" :label="dict.label" :value="dict.value" />
              </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="1.5">
            <el-button type="primary" plain icon="Plus" @click="handleAdd" v-hasPermi="['oa/crm:crmSupplierInfo:add']"> 新增 </el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate()" v-hasPermi="['oa/crm:crmSupplierInfo:edit']"
              >修改
            </el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete()" v-hasPermi="['oa/crm:crmSupplierInfo:remove']"
              >删除
            </el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['oa/crm:crmSupplierInfo:export']">导出 </el-button>
          </el-col>
          <right-toolbar v-model:showSearch="showSearch" :columns="columns" :search="true" @queryTable="getList"></right-toolbar>
        </el-row>
      </template>

      <el-table v-loading="loading" border :data="crmSupplierInfoList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="供应商ID" align="center" prop="supplierId" v-if="columns[0].visible" />
        <el-table-column label="供应商名称" align="center" prop="supplierName" v-if="columns[2].visible" />
        <el-table-column label="助记名称" align="center" prop="mnemonicName" v-if="columns[3].visible" />
        <el-table-column label="供应商类型" align="center" prop="supplierType" v-if="columns[4].visible">
          <template #default="scope">
            <dict-tag :options="supplier_type" :value="scope.row.supplierType" />
          </template>
        </el-table-column>
        <el-table-column label="注册资本" align="center" prop="registeredCapital" v-if="columns[5].visible" />
        <el-table-column label="成立日期" align="center" prop="establishmentDate" width="180" v-if="columns[6].visible">
          <template #default="scope">
            <span>{{ parseTime(scope.row.establishmentDate, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="经营范围" align="center" prop="businessScope" v-if="columns[7].visible" />
        <el-table-column label="主营行业" align="center" prop="mainIndustry" v-if="columns[8].visible" />
        <el-table-column label="法定代表人" align="center" prop="legalRepresentative" v-if="columns[9].visible" />
        <el-table-column label="营业执照号码" align="center" prop="businessLicenseNumber" v-if="columns[10].visible" />
        <el-table-column label="税号" align="center" prop="taxNumber" v-if="columns[11].visible" />
        <el-table-column label="开户银行" align="center" prop="bankAccountOpening" v-if="columns[12].visible" />
        <el-table-column label="银行账号" align="center" prop="bankNumber" v-if="columns[13].visible" />
        <el-table-column label="注册地址" align="center" prop="registeredAddress" v-if="columns[14].visible" />
        <el-table-column label="经营地址" align="center" prop="businessAddress" v-if="columns[15].visible" />
        <el-table-column label="联系人" align="center" prop="contactPerson" v-if="columns[16].visible" />
        <el-table-column label="联系电话" align="center" prop="contactPhone" v-if="columns[17].visible" />
        <el-table-column label="联系手机" align="center" prop="contactMobile" v-if="columns[18].visible" />
        <el-table-column label="联系邮箱" align="center" prop="contactEmail" v-if="columns[19].visible" />
        <el-table-column label="传真" align="center" prop="contactFax" v-if="columns[20].visible" />
        <el-table-column label="网址" align="center" prop="website" v-if="columns[21].visible" />
        <el-table-column label="归属人员" align="center" prop="ownerName" v-if="columns[22].visible" />
        <el-table-column label="详细地址" align="center" prop="detailedAddress" v-if="columns[23].visible" />
        <el-table-column label="附件ID" align="center" prop="ossId" v-if="columns[24].visible" />
        <el-table-column label="备注" align="center" prop="remark" v-if="columns[25].visible" />
        <el-table-column label="激活标识" align="center" prop="activeFlag" v-if="columns[26].visible">
          <template #default="scope">
            <dict-tag :options="active_flag" :value="scope.row.activeFlag" />
          </template>
        </el-table-column>
        <el-table-column label="创建时间" align="center" prop="createTime" width="180" v-if="columns[27].visible">
          <template #default="scope">
            <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="创建人" align="center" prop="createName" v-if="columns[28].visible" />
        <el-table-column label="操作" align="center" fixed="right" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-tooltip content="修改" placement="top">
              <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['oa/crm:crmSupplierInfo:edit']"></el-button>
            </el-tooltip>
            <el-tooltip content="删除" placement="top">
              <el-button
                link
                type="primary"
                icon="Delete"
                @click="handleDelete(scope.row)"
                v-hasPermi="['oa/crm:crmSupplierInfo:remove']"
              ></el-button>
            </el-tooltip>
          </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="dialog.title" v-model="dialog.visible" width="760px" append-to-body>
      <el-form ref="crmSupplierInfoFormRef" :model="form" :rules="rules" label-width="110px" class="supplier-form">
        <el-card shadow="never" class="mb-12" body-class="pb-2">
          <template #header>
            <span class="section-title">基础信息</span>
          </template>
          <el-row :gutter="16">
            <el-col :md="12" :sm="24">
              <el-form-item label="供应商名称" prop="supplierName">
                <el-input v-model="form.supplierName" placeholder="请输入供应商名称" />
              </el-form-item>
            </el-col>
            <el-col :md="12" :sm="24">
              <el-form-item label="助记名称" prop="mnemonicName">
                <el-input v-model="form.mnemonicName" placeholder="请输入助记名称" />
              </el-form-item>
            </el-col>
            <el-col :md="12" :sm="24">
              <el-form-item label="供应商类型" prop="supplierType">
                <el-select v-model="form.supplierType" placeholder="请选择供应商类型">
                  <el-option v-for="dict in supplier_type" :key="dict.value" :label="dict.label" :value="dict.value" />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :md="12" :sm="24">
              <el-form-item label="成立日期" prop="establishmentDate">
                <el-date-picker v-model="form.establishmentDate" type="date" value-format="YYYY-MM-DD" placeholder="请选择成立日期" clearable />
              </el-form-item>
            </el-col>
            <el-col :md="12" :sm="24">
              <el-form-item label="主营行业" prop="mainIndustry">
                <el-input v-model="form.mainIndustry" placeholder="请输入主营行业" />
              </el-form-item>
            </el-col>
            <el-col :md="12" :sm="24">
              <el-form-item label="注册资本" prop="registeredCapital">
                <el-input-number v-model="form.registeredCapital" placeholder="请输入注册资本" />
              </el-form-item>
            </el-col>
            <el-col :md="24">
              <el-form-item label="经营范围" prop="businessScope">
                <el-input v-model="form.businessScope" placeholder="请输入经营范围" maxlength="500" show-word-limit type="textarea" />
              </el-form-item>
            </el-col>
          </el-row>
        </el-card>

        <el-card shadow="never" class="mb-12" body-class="pb-2">
          <template #header>
            <span class="section-title">联系方式</span>
          </template>
          <el-row :gutter="16">
            <el-col :md="12" :sm="24">
              <el-form-item label="联系人" prop="contactPerson">
                <el-input v-model="form.contactPerson" placeholder="请输入联系人" />
              </el-form-item>
            </el-col>
            <el-col :md="12" :sm="24">
              <el-form-item label="联系电话" prop="contactPhone">
                <el-input v-model="form.contactPhone" placeholder="请输入联系电话" />
              </el-form-item>
            </el-col>
            <el-col :md="12" :sm="24">
              <el-form-item label="联系手机" prop="contactMobile">
                <el-input v-model="form.contactMobile" placeholder="请输入联系手机" />
              </el-form-item>
            </el-col>
            <el-col :md="12" :sm="24">
              <el-form-item label="联系邮箱" prop="contactEmail">
                <el-input v-model="form.contactEmail" placeholder="请输入联系邮箱" />
              </el-form-item>
            </el-col>
            <el-col :md="12" :sm="24">
              <el-form-item label="传真" prop="contactFax">
                <el-input v-model="form.contactFax" placeholder="请输入传真" />
              </el-form-item>
            </el-col>
            <el-col :md="12" :sm="24">
              <el-form-item label="网址" prop="website">
                <el-input v-model="form.website" placeholder="请输入网址" />
              </el-form-item>
            </el-col>
            <el-col :md="24">
              <el-form-item label="归属人员" prop="ownerId">
                <el-select v-model="form.ownerId" placeholder="请选择归属人员" clearable filterable>
                  <el-option v-for="user in userList" :key="user.userId" :label="user.nickName" :value="user.userId" />
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
        </el-card>

        <el-card shadow="never" class="mb-12" body-class="pb-2">
          <template #header>
            <span class="section-title">办公与财务信息</span>
          </template>
          <el-row :gutter="16">
            <el-col :md="12" :sm="24">
              <el-form-item label="法定代表人" prop="legalRepresentative">
                <el-input v-model="form.legalRepresentative" placeholder="请输入法定代表人" />
              </el-form-item>
            </el-col>
            <el-col :md="12" :sm="24">
              <el-form-item label="营业执照号码" prop="businessLicenseNumber">
                <el-input v-model="form.businessLicenseNumber" placeholder="请输入营业执照号码" />
              </el-form-item>
            </el-col>
            <el-col :md="12" :sm="24">
              <el-form-item label="税号" prop="taxNumber">
                <el-input v-model="form.taxNumber" placeholder="请输入税号" />
              </el-form-item>
            </el-col>
            <el-col :md="12" :sm="24">
              <el-form-item label="开户银行" prop="bankAccountOpening">
                <el-input v-model="form.bankAccountOpening" placeholder="请输入开户银行" />
              </el-form-item>
            </el-col>
            <el-col :md="12" :sm="24">
              <el-form-item label="银行账号" prop="bankNumber">
                <el-input v-model="form.bankNumber" placeholder="请输入银行账号" />
              </el-form-item>
            </el-col>
            <el-col :md="12" :sm="24">
              <el-form-item label="注册地址" prop="registeredAddress">
                <el-input v-model="form.registeredAddress" type="textarea" placeholder="请输入注册地址" />
              </el-form-item>
            </el-col>
            <el-col :md="12" :sm="24">
              <el-form-item label="经营地址" prop="businessAddress">
                <el-input v-model="form.businessAddress" type="textarea" placeholder="请输入经营地址" />
              </el-form-item>
            </el-col>
          </el-row>
        </el-card>

        <el-card shadow="never" body-class="pb-2">
          <template #header>
            <span class="section-title">附件与其他</span>
          </template>
          <el-row :gutter="16">
            <!--                        <el-col :md="12" :sm="24">-->
            <el-form-item label="附件" prop="ossId">
              <FileUpload v-model="form.ossId" :limit="1" :is-show-tip="true" :file-size="10" />
              <!--                                <span class="upload-tip">仅支持单文件上传，大小不超过10MB</span>-->
              <!--                                <div v-if="form.attachmentList?.length" class="mt-2">-->
              <!--                                    <el-tag-->
              <!--                                        v-for="item in form.attachmentList"-->
              <!--                                        :key="item.ossId"-->
              <!--                                        size="small"-->
              <!--                                        class="mr-2 mb-2"-->
              <!--                                    >-->
              <!--                                        <el-link :href="item.url" target="_blank" type="primary">-->
              <!--                                            {{ item.originalName || item.name }}-->
              <!--                                        </el-link>-->
              <!--                                    </el-tag>-->
              <!--                                </div>-->
            </el-form-item>
            <!--                        </el-col>-->
            <!--                        <el-col :md="12" :sm="24">-->
            <!--                            <el-form-item label="激活标识" prop="activeFlag">-->
            <!--                                <el-radio-group v-model="form.activeFlag">-->
            <!--                                    <el-radio-->
            <!--                                        v-for="dict in active_flag"-->
            <!--                                        :key="dict.value"-->
            <!--                                        :value="dict.value"-->
            <!--                                    >{{ dict.label }}</el-radio>-->
            <!--                                </el-radio-group>-->
            <!--                            </el-form-item>-->
            <!--                        </el-col>-->
            <el-col :md="24">
              <el-form-item label="激活标识" prop="activeFlag">
                <el-radio-group v-model="form.activeFlag">
                  <el-radio v-for="dict in active_flag" :key="dict.value" :value="dict.value">{{ dict.label }} </el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
            <el-col :md="24">
              <el-form-item label="备注" prop="remark">
                <el-input v-model="form.remark" type="textarea" placeholder="请输入备注" maxlength="300" show-word-limit />
              </el-form-item>
            </el-col>
          </el-row>
        </el-card>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="CrmSupplierInfo" lang="ts">
import { listCrmSupplierInfo, getCrmSupplierInfo, delCrmSupplierInfo, addCrmSupplierInfo, updateCrmSupplierInfo } from '@/api/oa/crm/crmSupplierInfo';
import { CrmSupplierInfoVO, CrmSupplierInfoQuery, CrmSupplierInfoForm } from '@/api/oa/crm/crmSupplierInfo/types';
import { listUser } from '@/api/system/user';
import { UserVO } from '@/api/system/user/types';
import FileUpload from '@/components/FileUpload/index.vue';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { active_flag, supplier_type } = toRefs<any>(proxy?.useDict('active_flag', 'supplier_type'));

const crmSupplierInfoList = ref<CrmSupplierInfoVO[]>([]);
const userList = ref<UserVO[]>([]);
const buttonLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref<Array<string | number>>([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);

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

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

// 列显隐信息
const columns = ref<FieldOption[]>([
  { key: 0, label: `供应商ID`, visible: false },
  { key: 1, label: `租户编号`, visible: false },
  { key: 2, label: `供应商名称`, visible: true },
  { key: 3, label: `助记名称`, visible: true },
  { key: 4, label: `供应商类型`, visible: true },
  { key: 5, label: `注册资本`, visible: false },
  { key: 6, label: `成立日期`, visible: false },
  { key: 7, label: `经营范围`, visible: false },
  { key: 8, label: `主营行业`, visible: true },
  { key: 9, label: `法定代表人`, visible: false },
  { key: 10, label: `营业执照号码`, visible: false },
  { key: 11, label: `税号`, visible: false },
  { key: 12, label: `开户银行`, visible: false },
  { key: 13, label: `银行账号`, visible: false },
  { key: 14, label: `注册地址`, visible: false },
  { key: 15, label: `经营地址`, visible: false },
  { key: 16, label: `联系人`, visible: false },
  { key: 17, label: `联系电话`, visible: false },
  { key: 18, label: `联系手机`, visible: false },
  { key: 19, label: `联系邮箱`, visible: false },
  { key: 20, label: `传真`, visible: false },
  { key: 21, label: `网址`, visible: false },
  { key: 22, label: `归属人员`, visible: true },
  { key: 23, label: `详细地址`, visible: false },
  { key: 24, label: `附件ID`, visible: false },
  { key: 25, label: `备注`, visible: true },
  { key: 26, label: `激活标识`, visible: true },
  { key: 27, label: `创建时间`, visible: true },
  { key: 28, label: `创建人`, visible: true }
]);

const initFormData: CrmSupplierInfoForm = {
  supplierId: undefined,
  supplierName: undefined,
  mnemonicName: undefined,
  supplierType: undefined,
  registeredCapital: undefined,
  establishmentDate: undefined,
  businessScope: undefined,
  mainIndustry: undefined,
  legalRepresentative: undefined,
  businessLicenseNumber: undefined,
  taxNumber: undefined,
  bankAccountOpening: undefined,
  bankNumber: undefined,
  registeredAddress: undefined,
  businessAddress: undefined,
  contactPerson: undefined,
  contactPhone: undefined,
  contactMobile: undefined,
  contactEmail: undefined,
  contactFax: undefined,
  website: undefined,
  ownerId: undefined,
  detailedAddress: undefined,
  ossId: undefined,
  remark: undefined,
  activeFlag: undefined,
  ownerName: undefined,
  createName: undefined
};
const data = reactive<PageData<CrmSupplierInfoForm, CrmSupplierInfoQuery>>({
  form: { ...initFormData },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    supplierName: undefined,
    mnemonicName: undefined,
    supplierType: undefined,
    registeredCapital: undefined,
    establishmentDate: undefined,
    businessScope: undefined,
    mainIndustry: undefined,
    legalRepresentative: undefined,
    businessLicenseNumber: undefined,
    taxNumber: undefined,
    bankAccountOpening: undefined,
    bankNumber: undefined,
    registeredAddress: undefined,
    businessAddress: undefined,
    contactPerson: undefined,
    contactPhone: undefined,
    contactMobile: undefined,
    contactEmail: undefined,
    contactFax: undefined,
    website: undefined,
    ownerId: undefined,
    detailedAddress: undefined,
    ossId: undefined,
    activeFlag: undefined,
    ownerName: undefined,
    createName: undefined,
    params: {}
  },
  rules: {
    supplierName: [{ required: true, message: '供应商名称不能为空', trigger: 'blur' }],
    mnemonicName: [{ required: true, message: '助记名称不能为空', trigger: 'blur' }],
    supplierType: [{ required: true, message: '供应商类型不能为空', trigger: 'change' }],
    mainIndustry: [{ required: true, message: '主营行业不能为空', trigger: 'blur' }]
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询供应商信息列表 */
const getList = async () => {
  loading.value = true;
  const res = await listCrmSupplierInfo(queryParams.value);
  crmSupplierInfoList.value = res.rows;
  total.value = res.total;
  loading.value = false;
};

/** 获取用户列表 */
const getUserList = async () => {
  const res = await listUser({ pageNum: 1, pageSize: 1000 });
  userList.value = res.rows;
};

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

/** 表单重置 */
const reset = () => {
  form.value = { ...initFormData };
  crmSupplierInfoFormRef.value?.resetFields();
};

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

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

/** 多选框选中数据 */
const handleSelectionChange = (selection: CrmSupplierInfoVO[]) => {
  ids.value = selection.map((item) => item.supplierId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
};

/** 新增按钮操作 */
const handleAdd = () => {
  reset();
  form.value.activeFlag = '1';
  dialog.visible = true;
  dialog.title = '添加供应商信息';
};

/** 修改按钮操作 */
const handleUpdate = async (row?: CrmSupplierInfoVO) => {
  reset();
  const _supplierId = row?.supplierId || ids.value[0];
  const res = await getCrmSupplierInfo(_supplierId);
  Object.assign(form.value, res.data);
  dialog.visible = true;
  dialog.title = '修改供应商信息';
};

/** 提交按钮 */
const submitForm = () => {
  crmSupplierInfoFormRef.value?.validate(async (valid: boolean) => {
    if (valid) {
      buttonLoading.value = true;
      if (form.value.supplierId) {
        await updateCrmSupplierInfo(form.value).finally(() => (buttonLoading.value = false));
      } else {
        await addCrmSupplierInfo(form.value).finally(() => (buttonLoading.value = false));
      }
      proxy?.$modal.msgSuccess('操作成功');
      dialog.visible = false;
      await getList();
    }
  });
};

/** 删除按钮操作 */
const handleDelete = async (row?: CrmSupplierInfoVO) => {
  const _supplierIds = row?.supplierId || ids.value;
  await proxy?.$modal.confirm('是否确认删除供应商信息编号为"' + _supplierIds + '"的数据项？').finally(() => (loading.value = false));
  await delCrmSupplierInfo(_supplierIds);
  proxy?.$modal.msgSuccess('删除成功');
  await getList();
};

/** 导出按钮操作 */
const handleExport = () => {
  proxy?.download(
    'oa/crm/crmSupplierInfo/export',
    {
      ...queryParams.value
    },
    `crmSupplierInfo_${new Date().getTime()}.xlsx`
  );
};

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