<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="120px"
      v-loading="formLoading"
    >
      <el-form-item label="集团名称" prop="group_name_zh">
        <el-input v-model="formData.group_name_zh" placeholder="请输入集团名称" />
      </el-form-item>
      <el-form-item label="集团英文名称" prop="group_name_en">
        <el-input v-model="formData.group_name_en" placeholder="请输入集团英文名称" />
      </el-form-item>
      <el-form-item label="品牌名称" prop="brand_name_zh">
        <el-input v-model="formData.brand_name_zh" placeholder="请输入品牌名称" />
      </el-form-item>
      <el-form-item label="品牌英文名称" prop="brand_name_en">
        <el-input v-model="formData.brand_name_en" placeholder="请输入品牌英文名称" />
      </el-form-item>
      <el-form-item label="定位级别(中)" prop="positioning_level_zh">
        <el-input v-model="formData.positioning_level_zh" placeholder="请输入定位级别（中）" />
      </el-form-item>
      <el-form-item label="定位级别(英)" prop="positioning_level_en">
        <el-input v-model="formData.positioning_level_en" placeholder="请输入定位级别（英）" />
      </el-form-item>
      <el-form-item label="标签状态" prop="status">
        <el-radio-group v-model="formData.status">
          <el-radio
            v-for="dict in statusOptions"
            :key="dict.value"
            :value="dict.value"
          >
            {{ dict.label }}
          </el-radio>
        </el-radio-group>
      </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>
import { talentHotelGroupBrandApi } from '@/api/menduner/system/talentMap/hotel'
import _ from 'lodash-es'

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

const statusOptions = [
	{ label: '启用', value: 'active' },
	{ label: '禁用', value: 'inactive' },
]

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formRef = ref() // 表单 Ref
const formData = ref({
  group_name_zh: undefined,
  group_name_en: undefined,
  brand_name_zh: undefined,
  brand_name_en: undefined,
  positioning_level_zh: undefined,
  positioning_level_en: undefined,
  status: 'active'
})
const formRules = reactive({
  status: [{ required: true, message: '酒店状态不能为空', trigger: 'blur' }],
  group_name_zh: [{ required: true, message: '集团名称不能为空', trigger: 'blur' }],
  group_name_en: [{ required: true, message: '集团英文名称不能为空', trigger: 'blur' }],
  brand_name_zh: [{ required: true, message: '品牌名称不能为空', trigger: 'blur' }],
  brand_name_en: [{ required: true, message: '品牌英文名称不能为空', trigger: 'blur' }],
  positioning_level_zh: [{ required: true, message: '定位级别不能为空', trigger: 'blur' }],
  positioning_level_en: [{ required: true, message: '定位级别英文名称不能为空', trigger: 'blur' }]
})

/** 打开弹窗 */
const valueKeys = ['group_name_zh', 'group_name_en', 'brand_name_zh', 'brand_name_en', 'positioning_level_zh', 'positioning_level_en', 'status']
const editItemData = ref({})
const open = async (type, data) => {
  editItemData.value = {}
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()

  // 修改时，设置数据
  if (data && Object.keys(data).length) {
    formData.value = { ...data }
    valueKeys.forEach(key => editItemData.value[key] = data[key])
    if (data.status === 'disable') formData.value.status = 'inactive'
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  if (!formRef) return
  await formRef.value.validate()

  const params = { ...formData.value }
  // 更新标签时，至少需要提供一个要更新的项
  if (formType.value === 'update') {
    const isEqual = _.isEqual(params, editItemData.value)
    if (isEqual) return message.warning('请修改任意项后再提交！')
  }

  // 提交请求
  formLoading.value = true
  try {
    if (formType.value === 'create') {
      await talentHotelGroupBrandApi.create(params)
      message.success(t('common.createSuccess'))
    } else {
      await talentHotelGroupBrandApi.update(formData.value.id, params)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  Object.keys(formData.value).forEach(key => {
    formData.value[key] = undefined
  }),
  formData.value.status = 'active'
  formRef.value?.resetFields()
}
</script>