<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item label="企业名称" prop="name">
        <el-input v-model="formData.name" placeholder="请输入企业名称" />
      </el-form-item>
      <el-form-item label="企业法定代表人" prop="legalPerson">
        <el-input v-model="formData.legalPerson" placeholder="请输入企业法定代表人" />
      </el-form-item>
      <el-form-item label="统一信用代码" prop="unifiedCreditCode">
        <el-input v-model="formData.unifiedCreditCode" placeholder="请输入统一信用代码" />
      </el-form-item>
      <el-form-item label="电话" prop="telephone">
        <el-input v-model="formData.telephone" placeholder="请输入电话" />
      </el-form-item>
      <el-form-item label="邮箱" prop="mailbox">
        <el-input v-model="formData.mailbox" placeholder="请输入邮箱" />
      </el-form-item>
      <el-form-item label="注册地址" prop="registerAddress">
        <el-input v-model="formData.registerAddress" placeholder="请输入注册地址" />
      </el-form-item>
      <el-form-item label="通信地址" prop="communicationAddress">
        <el-input v-model="formData.communicationAddress" placeholder="请输入通信地址" />
      </el-form-item>
      <el-form-item label="经营状态" prop="operateState">
        <el-select v-model="formData.operateState" placeholder="请选择经营状态">
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.ENTERPRISE_OPERATE_STATE)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="成立日期" prop="establishDate">
        <el-date-picker
          v-model="formData.establishDate"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="选择成立日期"
        />
      </el-form-item>
      <el-form-item label="企业类型" prop="enterpriseType">
        <el-select v-model="formData.enterpriseType" placeholder="请选择企业类型">
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.ENTERPRISE_TYPE)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="企业所属地区" prop="enterpriseBelongingToRegion">
        <el-input v-model="formData.enterpriseBelongingToRegion" placeholder="请输入企业所属地区" />
      </el-form-item>
      <el-form-item label="企业所属行业" prop="enterpriseBelongingToIndustry">
        <el-input v-model="formData.enterpriseBelongingToIndustry" placeholder="请输入企业所属行业" />
      </el-form-item>
      <el-form-item label="登记机关" prop="registerOffice">
        <el-input v-model="formData.registerOffice" placeholder="请输入登记机关" />
      </el-form-item>
      <el-form-item label="经营许可范围" prop="operatePermitRange">
        <el-input v-model="formData.operatePermitRange" placeholder="请输入经营许可范围" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { DICT_TYPE, getIntDictOptions} from '@/utils/dict'
import { EnterpriseInformationApi, EnterpriseInformationVO } from '@/api/fta/enterpriseinformation'

/** 企业信息 表单 */
defineOptions({ name: 'EnterpriseInformationForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  name: undefined,
  legalPerson: undefined,
  unifiedCreditCode: undefined,
  telephone: undefined,
  mailbox: undefined,
  registerAddress: undefined,
  communicationAddress: undefined,
  operateState: undefined,
  establishDate: undefined,
  enterpriseType: undefined,
  enterpriseBelongingToRegion: undefined,
  enterpriseBelongingToIndustry: undefined,
  registerOffice: undefined,
  operatePermitRange: undefined,
})
const formRules = reactive({
  name: [{ required: true, message: '企业名称不能为空', trigger: 'blur' }],
  legalPerson: [{ required: true, message: '企业法定代表人不能为空', trigger: 'blur' }],
  unifiedCreditCode: [{ required: true, message: '统一信用代码不能为空', trigger: 'blur' }],
  telephone: [{ required: true, message: '电话不能为空', trigger: 'blur' }],
  mailbox: [{ required: true, message: '邮箱不能为空', trigger: 'blur' }],
  registerAddress: [{ required: true, message: '注册地址不能为空', trigger: 'blur' }],
  communicationAddress: [{ required: true, message: '通信地址不能为空', trigger: 'blur' }],
  operateState: [{ required: true, message: '经营状态不能为空', trigger: 'change' }],
  establishDate: [{ required: true, message: '成立日期不能为空', trigger: 'blur' }],
  enterpriseType: [{ required: true, message: '企业类型不能为空', trigger: 'change' }],
  enterpriseBelongingToRegion: [{ required: true, message: '企业所属地区不能为空', trigger: 'blur' }],
  enterpriseBelongingToIndustry: [{ required: true, message: '企业所属行业不能为空', trigger: 'blur' }],
  registerOffice: [{ required: true, message: '登记机关不能为空', trigger: 'blur' }],
  operatePermitRange: [{ required: true, message: '经营许可范围不能为空', trigger: 'blur' }],
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await EnterpriseInformationApi.getEnterpriseInformation(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as EnterpriseInformationVO
    if (formType.value === 'create') {
      await EnterpriseInformationApi.createEnterpriseInformation(data)
      message.success(t('common.createSuccess'))
    } else {
      await EnterpriseInformationApi.updateEnterpriseInformation(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    name: undefined,
    legalPerson: undefined,
    unifiedCreditCode: undefined,
    telephone: undefined,
    mailbox: undefined,
    registerAddress: undefined,
    communicationAddress: undefined,
    operateState: undefined,
    establishDate: undefined,
    enterpriseType: undefined,
    enterpriseBelongingToRegion: undefined,
    enterpriseBelongingToIndustry: undefined,
    registerOffice: undefined,
    operatePermitRange: undefined,
  }
  formRef.value?.resetFields()
}
</script>
