<template>
  <Dialog title="企业注册详情" v-model="dialogVisible" width="70%">
    <el-descriptions title="" border :column="2">
      <el-descriptions-item label="企业名称">{{ formatName(info?.name) }}</el-descriptions-item>
      <el-descriptions-item label="营业执照图片">
        <el-image style="width: 80px; height: 80px" :src="info?.businessLicenseUrl || ''" hide-on-click-modal :preview-src-list="[info?.businessLicenseUrl]"/>
      </el-descriptions-item>
      <el-descriptions-item label="企业统一社会信用代码">{{ info?.code || '-' }}</el-descriptions-item>
      <el-descriptions-item label="管理员姓名">{{ info?.contactName || '-' }}</el-descriptions-item>
      <el-descriptions-item label="管理员电话">{{ info?.phone || '-' }}</el-descriptions-item>
      <el-descriptions-item label="管理员邮箱">{{ info?.email || '-' }}</el-descriptions-item>
      <el-descriptions-item label="申请备注/说明">{{ info?.description || '-' }}</el-descriptions-item>
      <el-descriptions-item label="审批状态">
        <dict-tag :type="DICT_TYPE.MENDUNER_ENTERPRISE_REGISTER_AUDIT_STATUS" :value="info?.status" />
      </el-descriptions-item>
      <el-descriptions-item label="审核原因">{{ info?.reason || '-' }}</el-descriptions-item>
      <el-descriptions-item label="创建时间">{{ info?.createTime ? formatDate(info?.createTime) : '-' }}</el-descriptions-item>
      <el-descriptions-item label="审核备注">{{ info?.remark || '-' }}</el-descriptions-item>
    </el-descriptions>

    <!-- 多联系人 -->
    <div v-if="info?.contacts && info?.contacts?.length">
      <el-descriptions v-for="(item, index) in info?.contacts" :key="index" :title="'联系人' + (index+1)" border :column="3" style="margin-top: 10px;">
        <el-descriptions-item label="姓名">{{ item.contactName || '-' }}</el-descriptions-item>
        <el-descriptions-item label="电话">{{ item.phone || '-' }}</el-descriptions-item>
        <el-descriptions-item label="邮箱">{{ item.email || '-' }}</el-descriptions-item>
      </el-descriptions>
    </div>

    <!-- 企业发布职位类型权限设置 -->
    <el-descriptions v-if="dialogType ==='pass'" title="企业职位发布类型权限设置" border :column="1" style="margin-top: 30px;">
      <el-descriptions-item label="企业职位发布权限 *">
        <el-checkbox-group v-model="checkList">
          <el-checkbox v-for="k in getIntDictOptions(DICT_TYPE.MENDUNER_JOB_TYPE)" :key="k.value" :label="k.label" :value="k.value" />
        </el-checkbox-group>
      </el-descriptions-item>
    </el-descriptions>

    <template #footer>
      <el-button @click="dialogVisible = false">取 消</el-button>
      <el-button v-if="dialogType ==='pass'" type="primary" @click="handleSubmitConfirm">提 交</el-button>
    </template>
  </Dialog>
</template>

<script setup lang="ts">
import { DICT_TYPE, getIntDictOptions } from '@/utils/dict'
import { EnterpriseRegisterApi } from '@/api/menduner/system/enterprise/register'
import { formatDate } from '@/utils/formatTime'
import { formatName } from '@/utils'

defineOptions({ name: 'EnterpriseRegisterDetails' })
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const message = useMessage() // 消息弹窗
const dialogVisible = ref(false) // 弹窗的是否展示
const dialogType = ref('') // 弹窗的类型

// 职位发布权限
const checkList = ref()

/** 打开弹窗 */
const open = async (id: Number, type: string) => {
  dialogType.value = type
  checkList.value = [0, 3] // 默认选中
  dialogVisible.value = true
  getData(id)
}

const info = ref({})
const getData = async (id) => {
  if (!id) return
  const data = await EnterpriseRegisterApi.getEnterpriseRegister(id)
  info.value = data
}

const handleSubmitConfirm = async () => {
  if (!checkList.value?.length) {
    // 权限设置提示
    await message.delConfirm('未给企业设置发布职位权限，企业将无法发布职位。是否确定继续提交？')
    handleSubmit()
  } else {
    handleSubmit()
  }
}

const handleSubmit = async () => {
  try {
    await EnterpriseRegisterApi.passEnterpriseRegister({ id: info.value?.id, reason: '', pubJobTypePerm: checkList.value })
    message.success('操作成功')
    emit('success')
  } finally {
    dialogVisible.value = false
    dialogType.value = ''
    info.value = {}
    checkList.value = []
  }
}


defineExpose({ open }) // 提供 open 方法，用于打开弹窗
</script>