<template>
  <component
    is="a-modal"
    :width="tool.getDevice() === 'mobile' ? '100%' : '600px'"
    v-model:visible="visible"
    :title="title"
    :mask-closable="false"
    :ok-loading="loading"
    @cancel="close"
    @before-ok="submit">
    <!-- 表单信息 start -->
    <a-form ref="formRef" :model="formData" :rules="rules" :auto-label-width="true">
    </a-form>
    <!-- 表单信息 end -->
  </component>
</template>

<script setup>
import { ref, reactive, computed } from 'vue'
import tool from '@/utils/tool'
import { Message, Modal } from '@arco-design/web-vue'
import api from '../../api/customer/sdkOrder'

const emit = defineEmits(['success'])
// 引用定义
const visible = ref(false)
const loading = ref(false)
const formRef = ref()
const mode = ref('')

let title = computed(() => {
  return '用户订单记录' + (mode.value == 'add' ? '-新增' : '-编辑')
})

// 表单初始值
const initialFormData = {
  id: null,
}

// 表单信息
const formData = reactive({ ...initialFormData })

// 验证规则
const rules = {
  orderid: [{ required: true, message: '订单号必需填写' }],
  cp_orderid: [{ required: true, message: '研发订单号必需填写' }],
  pay_channel: [{ required: true, message: '充值方式必需填写' }],
  money: [{ required: true, message: '金额必需填写' }],
  paid_amount: [{ required: true, message: '净额必需填写' }],
  game_id: [{ required: true, message: '充值游戏必需填写' }],
  media_id: [{ required: true, message: '媒体ID必需填写' }],
  auth_id: [{ required: true, message: '负责人ID必需填写' }],
  agent_id: [{ required: true, message: '渠道ID必需填写' }],
  site_id: [{ required: true, message: '广告位ID必需填写' }],
  uid: [{ required: true, message: 'UID必需填写' }],
  user_name: [{ required: true, message: '充值账号必需填写' }],
  user_ip: [{ required: true, message: '充值IP必需填写' }],
  server_id: [{ required: true, message: '服务器ID必需填写' }],
  server_name: [{ required: true, message: '充值区服必需填写' }],
  role_name: [{ required: true, message: '角色名必需填写' }],
  payname: [{ required: true, message: '必需填写' }],
  pay_date: [{ required: true, message: '支付时间必需填写' }],
  sync_status: [{ required: true, message: '支付状态必需填写' }],
  send_status: [{ required: true, message: '发货状态必需填写' }],
  product_name: [{ required: true, message: '充值商品名必需填写' }],
}

// 打开弹框
const open = async (type = 'add') => {
  mode.value = type
  // 重置表单数据
  Object.assign(formData, initialFormData)
  formRef.value.clearValidate()
  visible.value = true
  await initPage()
}

// 初始化页面数据
const initPage = async () => {}

// 设置数据
const setFormData = async (data) => {
  for (const key in formData) {
    if (data[key] != null && data[key] != undefined) {
      formData[key] = data[key]
    }
  }
}

// 数据保存
const submit = async (done) => {
  const validate = await formRef.value?.validate()
  if (!validate) {
    loading.value = true
    let data = { ...formData }
    let result = {}
    if (mode.value === 'add') {
      // 添加数据
      data.id = undefined
      result = await api.save(data)
    } else {
      // 修改数据
      result = await api.update(data.id, data)
    }
    if (result.code === 200) {
      Message.success('操作成功')
      emit('success')
      done(true)
    }
    // 防止连续点击提交
    setTimeout(() => {
      loading.value = false
    }, 500)
  }
  done(false)
}

// 关闭弹窗
const close = () => (visible.value = false)

defineExpose({ open, setFormData })
</script>
