<!-- 编辑弹窗 -->
<template>
  <ele-modal
    width="80%"
    :visible="visible"
    :maskClosable="false"
    :maxable="maxable"
    :title="isUpdate ? '主体企业信息' : '添加企业'"
    :body-style="{ paddingBottom: '28px' }"
    @update:visible="updateVisible"
    @ok="save"
  >
    <a-form
      ref="formRef"
      :model="form"
      :rules="rules"
      :label-col="styleResponsive ? { md: 4, sm: 5, xs: 24 } : { flex: '90px' }"
      :wrapper-col="
        styleResponsive ? { md: 19, sm: 19, xs: 24 } : { flex: '1' }
      "
    >
      <a-form-item label="原文件导入名称" name="name">
        <a-input
          allow-clear
          placeholder="请输入原文件导入名称"
          v-model:value="form.name"
        />
      </a-form-item>
      <a-form-item label="系统匹配企业名称" name="matchName">
        <a-input
          allow-clear
          placeholder="请输入系统匹配企业名称"
          v-model:value="form.matchName"
        />
      </a-form-item>
      <a-form-item label="统一社会信用代码" name="code">
        <a-input
          allow-clear
          placeholder="请输入统一社会信用代码"
          v-model:value="form.code"
        />
      </a-form-item>
      <!--      <a-form-item label="类型" name="type">-->
      <!--        <a-input-->
      <!--          allow-clear-->
      <!--          placeholder="请输入类型"-->
      <!--          v-model:value="form.type"-->
      <!--        />-->
      <!--      </a-form-item>-->
      <!--      <a-form-item label="上级id, 0是顶级" name="parentId">-->
      <!--        <a-input-->
      <!--          allow-clear-->
      <!--          placeholder="请输入上级id, 0是顶级"-->
      <!--          v-model:value="form.parentId"-->
      <!--        />-->
      <!--      </a-form-item>-->
      <a-form-item label="登记状态" name="registrationStatus">
        <a-input
          allow-clear
          placeholder="请输入登记状态"
          v-model:value="form.registrationStatus"
        />
      </a-form-item>
      <a-form-item label="法定代表人" name="legalPerson">
        <a-input
          allow-clear
          placeholder="请输入法定代表人"
          v-model:value="form.legalPerson"
        />
      </a-form-item>
      <a-form-item label="注册资本" name="registeredCapital">
        <a-input
          allow-clear
          placeholder="请输入注册资本"
          v-model:value="form.registeredCapital"
        />
      </a-form-item>
      <a-form-item label="实缴资本" name="paidinCapital">
        <a-input
          allow-clear
          placeholder="请输入实缴资本"
          v-model:value="form.paidinCapital"
        />
      </a-form-item>
      <a-form-item label="成立日期" name="establishDate">
        <a-input
          allow-clear
          placeholder="请输入成立日期"
          v-model:value="form.establishDate"
        />
      </a-form-item>
      <a-form-item label="企业地址" name="address">
        <a-input
          allow-clear
          placeholder="请输入企业地址"
          v-model:value="form.address"
        />
      </a-form-item>
      <a-form-item label="电话" name="tel">
        <a-input
          allow-clear
          placeholder="请输入电话"
          v-model:value="form.tel"
        />
      </a-form-item>
      <a-form-item label="更多电话" name="moreTel">
        <a-input
          allow-clear
          placeholder="请输入更多电话"
          v-model:value="form.moreTel"
        />
      </a-form-item>
      <a-form-item label="邮箱" name="email">
        <a-input
          allow-clear
          placeholder="请输入邮箱"
          v-model:value="form.email"
        />
      </a-form-item>
      <a-form-item label="更多邮箱" name="moreEmail">
        <a-input
          allow-clear
          placeholder="请输入更多邮箱"
          v-model:value="form.moreEmail"
        />
      </a-form-item>
      <a-form-item label="所在国家" name="country">
        <a-input
          allow-clear
          placeholder="请输入所在国家"
          v-model:value="form.country"
        />
      </a-form-item>
      <a-form-item label="所属省份" name="province">
        <a-input
          allow-clear
          placeholder="请输入所属省份"
          v-model:value="form.province"
        />
      </a-form-item>
      <a-form-item label="所属城市" name="city">
        <a-input
          allow-clear
          placeholder="请输入所属城市"
          v-model:value="form.city"
        />
      </a-form-item>
      <a-form-item label="所属区县" name="region">
        <a-input
          allow-clear
          placeholder="请输入所属区县"
          v-model:value="form.region"
        />
      </a-form-item>
      <a-form-item label="企业（机构）类型" name="institutionType">
        <a-input
          allow-clear
          placeholder="请输入企业（机构）类型"
          v-model:value="form.institutionType"
        />
      </a-form-item>
      <a-form-item label="纳税人识别号" name="taxpayerCode">
        <a-input
          allow-clear
          placeholder="请输入纳税人识别号"
          v-model:value="form.taxpayerCode"
        />
      </a-form-item>
      <a-form-item label="注册号" name="registrationNumber">
        <a-input
          allow-clear
          placeholder="请输入注册号"
          v-model:value="form.registrationNumber"
        />
      </a-form-item>
      <a-form-item label="组织机构代码" name="organizationalCode">
        <a-input
          allow-clear
          placeholder="请输入组织机构代码"
          v-model:value="form.organizationalCode"
        />
      </a-form-item>
      <a-form-item label="参保人数" name="numberOfInsuredPersons">
        <a-input
          allow-clear
          placeholder="请输入参保人数"
          v-model:value="form.numberOfInsuredPersons"
        />
      </a-form-item>
      <a-form-item label="参保人数所属年报" name="annualReport">
        <a-input
          allow-clear
          placeholder="请输入参保人数所属年报"
          v-model:value="form.annualReport"
        />
      </a-form-item>
      <a-form-item label="营业期限" name="businessTerm">
        <a-input
          allow-clear
          placeholder="请输入营业期限"
          v-model:value="form.businessTerm"
        />
      </a-form-item>
      <a-form-item
        label="国标行业门类"
        name="nationalStandardIndustryCategories"
      >
        <a-input
          allow-clear
          placeholder="请输入国标行业门类"
          v-model:value="form.nationalStandardIndustryCategories"
        />
      </a-form-item>
      <a-form-item
        label="国标行业大类"
        name="nationalStandardIndustryCategories2"
      >
        <a-input
          allow-clear
          placeholder="请输入国标行业大类"
          v-model:value="form.nationalStandardIndustryCategories2"
        />
      </a-form-item>
      <a-form-item
        label="国标行业中类"
        name="nationalStandardIndustryCategories3"
      >
        <a-input
          allow-clear
          placeholder="请输入国标行业中类"
          v-model:value="form.nationalStandardIndustryCategories3"
        />
      </a-form-item>
      <a-form-item
        label="国标行业小类"
        name="nationalStandardIndustryCategories4"
      >
        <a-input
          allow-clear
          placeholder="请输入国标行业小类"
          v-model:value="form.nationalStandardIndustryCategories4"
        />
      </a-form-item>
      <a-form-item
        label="企查查行业门类"
        name="nationalStandardIndustryCategories5"
      >
        <a-input
          allow-clear
          placeholder="请输入企查查行业门类"
          v-model:value="form.nationalStandardIndustryCategories5"
        />
      </a-form-item>
      <a-form-item
        label="企查查行业大类"
        name="nationalStandardIndustryCategories6"
      >
        <a-input
          allow-clear
          placeholder="请输入企查查行业大类"
          v-model:value="form.nationalStandardIndustryCategories6"
        />
      </a-form-item>
      <a-form-item
        label="企查查行业中类"
        name="nationalStandardIndustryCategories7"
      >
        <a-input
          allow-clear
          placeholder="请输入企查查行业中类"
          v-model:value="form.nationalStandardIndustryCategories7"
        />
      </a-form-item>
      <a-form-item
        label="企查查行业小类"
        name="nationalStandardIndustryCategories8"
      >
        <a-input
          allow-clear
          placeholder="请输入企查查行业小类"
          v-model:value="form.nationalStandardIndustryCategories8"
        />
      </a-form-item>
      <a-form-item label="企业规模" name="companySize">
        <a-input
          allow-clear
          placeholder="请输入企业规模"
          v-model:value="form.companySize"
        />
      </a-form-item>
      <a-form-item label="曾用名" name="formerName">
        <a-input
          allow-clear
          placeholder="请输入曾用名"
          v-model:value="form.formerName"
        />
      </a-form-item>
      <a-form-item label="英文名" name="englishName">
        <a-input
          allow-clear
          placeholder="请输入英文名"
          v-model:value="form.englishName"
        />
      </a-form-item>
      <a-form-item label="官网" name="domain">
        <a-input
          allow-clear
          placeholder="请输入官网"
          v-model:value="form.domain"
        />
      </a-form-item>
      <a-form-item label="通信地址" name="mailingAddress">
        <a-input
          allow-clear
          placeholder="请输入通信地址"
          v-model:value="form.mailingAddress"
        />
      </a-form-item>
      <a-form-item label="企业简介" name="companyProfile">
        <a-input
          allow-clear
          placeholder="请输入企业简介"
          v-model:value="form.companyProfile"
        />
      </a-form-item>
      <a-form-item label="经营范围" name="natureOfBusiness">
        <a-input
          allow-clear
          placeholder="请输入经营范围"
          v-model:value="form.natureOfBusiness"
        />
      </a-form-item>
      <a-form-item label="登记机关" name="registrationAuthority">
        <a-input
          allow-clear
          placeholder="请输入登记机关"
          v-model:value="form.registrationAuthority"
        />
      </a-form-item>
      <a-form-item label="纳税人资质" name="taxpayerQualification">
        <a-input
          allow-clear
          placeholder="请输入纳税人资质"
          v-model:value="form.taxpayerQualification"
        />
      </a-form-item>
      <a-form-item label="最新年报年份" name="latestAnnualReportYear">
        <a-input
          allow-clear
          placeholder="请输入最新年报年份"
          v-model:value="form.latestAnnualReportYear"
        />
      </a-form-item>
      <a-form-item
        label="最新年报营业收入"
        name="latestAnnualReportOnOperatingRevenue"
      >
        <a-input
          allow-clear
          placeholder="请输入最新年报营业收入"
          v-model:value="form.latestAnnualReportOnOperatingRevenue"
        />
      </a-form-item>
      <a-form-item label="企查分" name="enterpriseScoreCheck">
        <a-input
          allow-clear
          placeholder="请输入企查分"
          v-model:value="form.enterpriseScoreCheck"
        />
      </a-form-item>
      <a-form-item label="信用等级" name="creditRating">
        <a-input
          allow-clear
          placeholder="请输入信用等级"
          v-model:value="form.creditRating"
        />
      </a-form-item>
      <a-form-item label="科创分" name="cechnologyScore">
        <a-input
          allow-clear
          placeholder="请输入科创分"
          v-model:value="form.cechnologyScore"
        />
      </a-form-item>
      <a-form-item label="科创等级" name="cechnologyLevel">
        <a-input
          allow-clear
          placeholder="请输入科创等级"
          v-model:value="form.cechnologyLevel"
        />
      </a-form-item>
      <a-form-item label="是否小微企业" name="smallEnterprise">
        <a-input
          allow-clear
          placeholder="请输入是否小微企业"
          v-model:value="form.smallEnterprise"
        />
      </a-form-item>
      <a-form-item label="备注" name="comments">
        <a-textarea
          :rows="4"
          :maxlength="200"
          placeholder="请输入描述"
          v-model:value="form.comments"
        />
      </a-form-item>
      <a-form-item label="排序" name="sortNumber">
        <a-input-number
          :min="0"
          :max="9999"
          class="ele-fluid"
          placeholder="请输入排序号"
          v-model:value="form.sortNumber"
        />
      </a-form-item>
      <a-form-item label="状态" name="status">
        <a-radio-group v-model:value="form.status">
          <a-radio :value="0">显示</a-radio>
          <a-radio :value="1">隐藏</a-radio>
        </a-radio-group>
      </a-form-item>
    </a-form>
  </ele-modal>
</template>

<script lang="ts" setup>
  import { ref, reactive, watch } from 'vue';
  import { Form, message } from 'ant-design-vue';
  import { assignObject, uuid } from 'ele-admin-pro';
  import {
    addCreditCompany,
    updateCreditCompany
  } from '@/api/credit/creditCompany';
  import { CreditCompany } from '@/api/credit/creditCompany/model';
  import { useThemeStore } from '@/store/modules/theme';
  import { storeToRefs } from 'pinia';
  import { ItemType } from 'ele-admin-pro/es/ele-image-upload/types';
  import { FormInstance } from 'ant-design-vue/es/form';
  import { FileRecord } from '@/api/system/file/model';

  // 是否是修改
  const isUpdate = ref(false);
  const useForm = Form.useForm;
  // 是否开启响应式布局
  const themeStore = useThemeStore();
  const { styleResponsive } = storeToRefs(themeStore);

  const props = defineProps<{
    // 弹窗是否打开
    visible: boolean;
    // 修改回显的数据
    data?: CreditCompany | null;
  }>();

  const emit = defineEmits<{
    (e: 'done'): void;
    (e: 'update:visible', visible: boolean): void;
  }>();

  // 提交状态
  const loading = ref(false);
  // 是否显示最大化切换按钮
  const maxable = ref(true);
  // 表格选中数据
  const formRef = ref<FormInstance | null>(null);
  const images = ref<ItemType[]>([]);

  // 用户信息
  const form = reactive<CreditCompany>({
    id: undefined,
    name: undefined,
    matchName: undefined,
    code: undefined,
    type: undefined,
    parentId: undefined,
    registrationStatus: undefined,
    legalPerson: undefined,
    registeredCapital: undefined,
    paidinCapital: undefined,
    establishDate: undefined,
    address: undefined,
    tel: undefined,
    moreTel: undefined,
    email: undefined,
    moreEmail: undefined,
    country: undefined,
    province: undefined,
    city: undefined,
    region: undefined,
    institutionType: undefined,
    taxpayerCode: undefined,
    registrationNumber: undefined,
    organizationalCode: undefined,
    numberOfInsuredPersons: undefined,
    annualReport: undefined,
    businessTerm: undefined,
    nationalStandardIndustryCategories: undefined,
    nationalStandardIndustryCategories2: undefined,
    nationalStandardIndustryCategories3: undefined,
    nationalStandardIndustryCategories4: undefined,
    nationalStandardIndustryCategories5: undefined,
    nationalStandardIndustryCategories6: undefined,
    nationalStandardIndustryCategories7: undefined,
    nationalStandardIndustryCategories8: undefined,
    companySize: undefined,
    formerName: undefined,
    englishName: undefined,
    domain: undefined,
    mailingAddress: undefined,
    companyProfile: undefined,
    natureOfBusiness: undefined,
    registrationAuthority: undefined,
    taxpayerQualification: undefined,
    latestAnnualReportYear: undefined,
    latestAnnualReportOnOperatingRevenue: undefined,
    enterpriseScoreCheck: undefined,
    creditRating: undefined,
    cechnologyScore: undefined,
    cechnologyLevel: undefined,
    smallEnterprise: undefined,
    recommend: undefined,
    sortNumber: undefined,
    deleted: undefined,
    userId: undefined,
    tenantId: undefined,
    createTime: undefined,
    updateTime: undefined,
    status: 0,
    comments: ''
  });

  /* 更新visible */
  const updateVisible = (value: boolean) => {
    emit('update:visible', value);
  };

  // 表单验证规则
  const rules = reactive({
    creditCompanyName: [
      {
        required: true,
        type: 'string',
        message: '请填写企业名称',
        trigger: 'blur'
      }
    ]
  });

  const chooseImage = (data: FileRecord) => {
    images.value.push({
      uid: data.id,
      url: data.path,
      status: 'done'
    });
    form.image = data.path;
  };

  const onDeleteItem = (index: number) => {
    images.value.splice(index, 1);
    form.image = '';
  };

  const { resetFields } = useForm(form, rules);

  /* 保存编辑 */
  const save = () => {
    if (!formRef.value) {
      return;
    }
    formRef.value
      .validate()
      .then(() => {
        loading.value = true;
        const formData = {
          ...form
        };
        const saveOrUpdate = isUpdate.value
          ? updateCreditCompany
          : addCreditCompany;
        saveOrUpdate(formData)
          .then((msg) => {
            loading.value = false;
            message.success(msg);
            updateVisible(false);
            emit('done');
          })
          .catch((e) => {
            loading.value = false;
            message.error(e.message);
          });
      })
      .catch(() => {});
  };

  watch(
    () => props.visible,
    (visible) => {
      if (visible) {
        images.value = [];
        if (props.data) {
          assignObject(form, props.data);
          isUpdate.value = true;
        } else {
          isUpdate.value = false;
        }
      } else {
        resetFields();
      }
    },
    { immediate: true }
  );
</script>
