<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="主播id" prop="hostsId">
        <el-input v-model="formData.hostsId" placeholder="请输入主播id" />
      </el-form-item>
      <el-form-item label="用户 Id" prop="userId">
        <el-input v-model="formData.userId" placeholder="请输入用户 Id" />
      </el-form-item>
      <el-form-item label="主播等级" prop="hostsLevel">
        <el-input v-model="formData.hostsLevel" placeholder="请输入主播等级" />
      </el-form-item>
      <el-form-item label="主播金币" prop="hostsCoins">
        <el-input v-model="formData.hostsCoins" placeholder="请输入主播金币" />
      </el-form-item>
      <el-form-item label="邀请类型" prop="invitationType">
        <el-select v-model="formData.invitationType" placeholder="请选择邀请类型">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="在线人数" prop="onlineFans">
        <el-input v-model="formData.onlineFans" placeholder="请输入在线人数" />
      </el-form-item>
      <el-form-item label="粉丝数量" prop="fans">
        <el-input v-model="formData.fans" placeholder="请输入粉丝数量" />
      </el-form-item>
      <el-form-item label="关注数量" prop="fllowernum">
        <el-input v-model="formData.fllowernum" placeholder="请输入关注数量" />
      </el-form-item>
      <el-form-item label="昨日金币" prop="yesterdayCoins">
        <el-input v-model="formData.yesterdayCoins" placeholder="请输入昨日金币" />
      </el-form-item>
      <el-form-item label="主播国家" prop="country">
        <el-input v-model="formData.country" placeholder="请输入主播国家" />
      </el-form-item>
      <el-form-item label="直播类型 " prop="hostsKind">
        <el-input v-model="formData.hostsKind" placeholder="请输入直播类型 娱乐，游戏 " />
      </el-form-item> -->
      <el-form-item label="旗帜" prop="flag">
        <el-select v-model="formData.flag" placeholder="请选择状态" clearable class="!w-240px" style="width: 50px">
          <el-option label="请选择" :value="''" />
          <el-option v-for="dict in getStrDictOptions(DICT_TYPE.FLAG_TYPE)" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="备注" prop="remake">
        <el-input v-model="formData.remake" placeholder="请输入备注" />
      </el-form-item>
      <el-form-item label="操作状态" prop="operationStatus">
        <el-select v-model="formData.operationStatus" placeholder="请选择状态" clearable class="!w-240px">
          <el-option v-for="dict in getStrDictOptions(DICT_TYPE.OPERATION_STATE)" :key="dict.value"
            :label="$t(dict.label)" :value="dict.value" />
        </el-select>
      </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 { EmployeeHostsApi, EmployeeHostsVO } from '@/api/server/employeehosts'
import { DICT_TYPE, getStrDictOptions } from '@/utils/dict'

/** 员工分配主播 表单 */
defineOptions({ name: 'EmployeeHostsForm' })

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,
  hostsId: undefined,
  userId: undefined,
  hostsLevel: undefined,
  hostsCoins: undefined,
  invitationType: undefined,
  onlineFans: undefined,
  fans: undefined,
  flag: undefined,
  fllowernum: undefined,
  yesterdayCoins: undefined,
  country: undefined,
  hostsKind: undefined,
  operationStatus: '',
  remake: undefined
})
const formRules = reactive({
  hostsId: [{ required: true, message: '主播id不能为空', trigger: 'blur' }]
})
const formRef = ref() // 表单 Ref
let indexData = -1 // 当前修改的索引：新增时值为 -1
/** 打开弹窗 */
const open = async (type: string, id?: number, index?: number) => {
  dialogVisible.value = true
  if (index) {
    indexData = index
  }
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await EmployeeHostsApi.getEmployeeHosts(id)
      getStrDictOptions(DICT_TYPE.OPERATION_STATE).forEach(item => {
        if (item.value == formData.value.operationStatus) {
          console.log("item", item)
          formData.value.operationStatus = item.value
        }

      })
      formLoading.value = false
    } 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 EmployeeHostsVO
    if (formType.value === 'create') {
      await EmployeeHostsApi.createEmployeeHosts(data)
      message.success(t('common.createSuccess'))
    } else {
      await EmployeeHostsApi.updateEmployeeHosts(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success', data, indexData)
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    hostsId: undefined,
    userId: undefined,
    hostsLevel: undefined,
    hostsCoins: undefined,
    invitationType: undefined,
    onlineFans: undefined,
    fans: undefined,
    fllowernum: undefined,
    yesterdayCoins: undefined,
    country: undefined,
    hostsKind: undefined,
    operationStatus: '',
    remake: undefined

  }
  formRef.value?.resetFields()
}
</script>