<template>
  <Dialog title="企业信息编辑" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item label="企业全称" prop="name" required>
        <el-input v-model="formData.name" placeholder="请输入企业全称" class="!w-360px" />
      </el-form-item>
      <el-form-item label="企业别称" prop="anotherName">
        <el-input v-model="formData.anotherName" placeholder="请输入企业别称" class="!w-360px" />
      </el-form-item>
      <el-form-item label="职位发布类型" prop="pubJobTypePerm">
        <el-checkbox-group v-model="formData.pubJobTypePerm">
          <el-checkbox v-for="k in getIntDictOptions(DICT_TYPE.MENDUNER_JOB_TYPE)" :key="k.value" :label="k.label" :value="k.value.toString()" />
        </el-checkbox-group>
      </el-form-item>
      <el-form-item label="企业官网" prop="website">
        <el-input v-model="formData.website" placeholder="请输入企业官网" class="!w-360px"/>
      </el-form-item>
      <el-form-item label="联系人" prop="contact" required>
        <el-input v-model="formData.contact" placeholder="请输入联系人" class="!w-360px" />
      </el-form-item>
      <el-form-item label="联系电话" prop="phone" required>
        <el-input v-model="formData.phone" placeholder="请输入联系电话" class="!w-360px" />
      </el-form-item>
      <el-form-item label="企业logo" prop="logoUrl">
        <UploadImg v-model="formData.logoUrl" height="140px" />
      </el-form-item>
      <el-form-item label="营业执照图片" prop="businessUrl">
        <UploadImg v-model="formData.businessUrl" height="140px" />
      </el-form-item>
      <el-form-item label="所在行业" prop="industryId">
        <el-cascader v-model="formData.industryId" :options="industry" :props="{ emitPath: false, label: 'nameCn', value: 'id' }" class="!w-360px" />
      </el-form-item>
      <el-form-item label="融资阶段" prop="financingStatus">
        <el-select v-model="formData.financingStatus" placeholder="请选择融资阶段" class="!w-360px">
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.MENDUNER_FINANCING_STATUS)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value.toString()"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="人员规模" prop="scale">
        <el-select v-model="formData.scale" placeholder="请选择人员规模" class="!w-360px">
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.MENDUNER_SCALE)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value.toString()"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="是否筹备中" prop="prepare">
        <el-radio-group v-model="formData.prepare">
          <el-radio :value="true">是</el-radio>
          <el-radio :value="false">否</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="账号状态" prop="status" required>
        <el-select v-model="formData.status" placeholder="请选择帐号状态" class="!w-360px">
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.MENDUNER_STATUS)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value.toString()"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="开业时间" prop="openTime">
        <el-date-picker
          v-model="formData.openTime"
          type="date"
          value-format="x"
          placeholder="选择开业时间"
          class="!w-360px"
        />
      </el-form-item>
      <el-form-item label="公司简介" prop="introduce">
        <el-input type="textarea" :rows="8" v-model="formData.introduce" 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 { getIntDictOptions, DICT_TYPE } from '@/utils/dict'
import { EnterpriseApi } from '@/api/menduner/system/enterprise/message'
import { formatName } from '@/utils'

defineOptions({ name: 'EnterpriseForm' })

const { t } = useI18n()
const message = useMessage()

defineProps({ industry: Array })

const dialogVisible = ref(false)
const formLoading = ref(false)
const formData = ref({
  id: undefined,
  name: undefined,
  anotherName: undefined,
  website: undefined,
  introduce: undefined,
  contact: undefined,
  phone: undefined,
  logoUrl: undefined,
  industryId: undefined,
  financingStatus: undefined,
  scale: undefined,
  welfareList: undefined,
  albumList: undefined,
  openTime: undefined,
  developHistory: undefined,
  businessUrl: undefined,
  receivedHonors: undefined,
  parentId: undefined,
  tagList: undefined,
  prepare: undefined,
  status: undefined,
  pubJobTypePerm: undefined
})
const formRules = reactive({
  name: [{ required: true, message: '企业全称不能为空', trigger: 'blur' }],
  phone: [{ required: true, message: '联系电话不能为空', trigger: 'blur' }],
  contact: [{ required: true, message: '联系人不能为空', trigger: 'blur' }],
  status: [{ required: true, message: '账号状态不能为空', trigger: 'blur' }]
})
const formRef = ref()

/** 打开弹窗 */
const open = async (id: number) => {
  dialogVisible.value = true
  resetForm()
  if (id) {
    formLoading.value = true
    try {
      formData.value = await EnterpriseApi.getEnterprise(id)
      formData.value.anotherName = formatName(formData.value.anotherName)
      formData.value.name = formatName(formData.value.name)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

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

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    name: undefined,
    anotherName: undefined,
    website: undefined,
    introduce: undefined,
    contact: undefined,
    phone: undefined,
    logoUrl: undefined,
    industryId: undefined,
    financingStatus: undefined,
    businessUrl: undefined,
    scale: undefined,
    welfareList: undefined,
    albumList: undefined,
    openTime: undefined,
    developHistory: undefined,
    prepare: undefined,
    receivedHonors: undefined,
    parentId: undefined,
    tagList: undefined,
    status: undefined,
    pubJobTypePerm: undefined
  }
  formRef.value?.resetFields()
}
</script>