<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form ref="formRef" :model="formData" :rules="formRules" v-loading="formLoading">
      <el-form-item label="邀请人用户ID" prop="inviterUserId">
        <el-input v-model="formData.inviterUserId" placeholder="请输入邀请人用户ID" />
      </el-form-item>
      <el-form-item label="被邀请人用户ID" prop="inviteeUserId">
        <el-input v-model="formData.inviteeUserId" placeholder="请输入被邀请人用户ID" />
      </el-form-item>
      <el-form-item label="邀请码ID" prop="inviteCodeId">
        <el-input v-model="formData.inviteCodeId" placeholder="请输入邀请码ID" />
      </el-form-item>
      <el-form-item label="点击Token" prop="clickToken">
        <el-input v-model="formData.clickToken" placeholder="请输入点击Token" />
      </el-form-item>
      <el-form-item label="绑定方式" prop="bindType">
        <el-select v-model="formData.bindType" placeholder="请选择绑定方式">
          <el-option v-for="item in bindTypeOptions" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="绑定时间" prop="boundAt">
        <el-date-picker v-model="formData.boundAt" type="date" value-format="x" placeholder="选择绑定时间" />
      </el-form-item>
      <el-form-item label="绑定IP" prop="bindIp">
        <el-input v-model="formData.bindIp" placeholder="请输入绑定IP" />
      </el-form-item>
      <el-form-item label="UserAgent" prop="bindUserAgent">
        <el-input v-model="formData.bindUserAgent" placeholder="请输入UserAgent" />
      </el-form-item>
      <el-form-item label="邀请码类型" prop="inviteType">
        <el-select v-model="formData.inviteType" placeholder="请选择邀请码类型">
          <el-option v-for="item in inviteTypeOptions" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="收益租户ID" prop="profitTenantId">
        <el-input v-model="formData.profitTenantId" placeholder="请输入收益租户ID" />
      </el-form-item>
      <el-form-item label="收益员工ID" prop="profitEmployeeId">
        <el-input v-model="formData.profitEmployeeId" placeholder="请输入收益员工ID" />
      </el-form-item>
      <el-form-item label="邀请人租户ID" prop="inviterTenantId">
        <el-input v-model="formData.inviterTenantId" placeholder="请输入邀请人租户ID" />
      </el-form-item>
      <el-form-item label="邀请码快照" prop="inviteCode">
        <el-input v-model="formData.inviteCode" 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 { UserInvitesApi, UserInvites } from '@/api/keyboard/userinvites'

/** 用户邀请关系绑定台账表，记录新用户最终归属的邀请人 表单 */
defineOptions({ name: 'UserInvitesForm' })

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,
  inviterUserId: undefined,
  inviteeUserId: undefined,
  inviteCodeId: undefined,
  clickToken: undefined,
  bindType: undefined,
  boundAt: undefined,
  bindIp: undefined,
  bindUserAgent: undefined,
  inviteType: undefined,
  profitTenantId: undefined,
  profitEmployeeId: undefined,
  inviterTenantId: undefined,
  inviteCode: undefined
})
const formRules = reactive({
  inviterUserId: [{ required: true, message: '邀请人用户ID不能为空', trigger: 'blur' }],
  inviteeUserId: [{ required: true, message: '被邀请人用户ID不能为空', trigger: 'blur' }],
  inviteCodeId: [{ required: true, message: '邀请码ID不能为空', trigger: 'blur' }],
  bindType: [{ required: true, message: '绑定方式不能为空', trigger: 'change' }],
  boundAt: [{ required: true, message: '绑定时间不能为空', trigger: 'blur' }],
  inviteType: [{ required: true, message: '邀请码类型不能为空', trigger: 'change' }]
})
const formRef = ref() // 表单 Ref
const bindTypeOptions = [
  { label: '手动填写邀请码', value: 1 },
  { label: '邀请链接自动绑定', value: 2 },
  { label: '其他方式', value: 3 }
]
const inviteTypeOptions = [
  { label: '普通用户邀请', value: 'USER' },
  { label: '代理邀请', value: 'AGENT' }
]

/** 打开弹窗 */
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 UserInvitesApi.getUserInvites(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 UserInvites
    if (formType.value === 'create') {
      await UserInvitesApi.createUserInvites(data)
      message.success(t('common.createSuccess'))
    } else {
      await UserInvitesApi.updateUserInvites(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    inviterUserId: undefined,
    inviteeUserId: undefined,
    inviteCodeId: undefined,
    clickToken: undefined,
    bindType: undefined,
    boundAt: undefined,
    bindIp: undefined,
    bindUserAgent: undefined,
    inviteType: undefined,
    profitTenantId: undefined,
    profitEmployeeId: undefined,
    inviterTenantId: undefined,
    inviteCode: undefined
  }
  formRef.value?.resetFields()
}
</script>
