<template>
  <ContentWrap>
    <el-form
      label-position="top"
      ref="formRef"
      v-loading="formLoading"
      status-icon
      :model="formData"
      :rules="formRules"
    >
      <el-form-item label="系统时区" prop="timezone">
        <el-input v-model="formData.timezone" placeholder="" />
        <span style="color: gray">时区会影响每日任务的重置时间，修改需要谨慎</span>
      </el-form-item>
      <el-form-item label="注册赠送金额" prop="registrationBonus">
        <el-input v-model="formData.registrationBonus" placeholder="" />
        <span style="color: gray">注册成功会体现在用户余额</span>
      </el-form-item>
      <el-form-item label="虚拟会员默认USDT地址" prop="registrationBonus">
        <el-input v-model="formData.virtualMemberUSDTAddress" placeholder="" />
        <span style="color: gray">创建虚拟会员时默认的取款地址</span>
      </el-form-item>
      <el-form-item label="默认顶级用户邀请码" prop="defaultTopParentInvitationCode">
        <el-input v-model="formData.defaultTopParentInvitationCode" placeholder="" />
        <span style="color: gray">没有填写邀请码的用户的默认上级</span>
      </el-form-item>
      <el-form-item label="客服链接" prop="serviceLink">
        <el-input v-model="formData.serviceLink" placeholder="" />
      </el-form-item>
      <el-form-item label="客服在线时间" prop="serviceOnlineTime">
        <el-input v-model="formData.serviceOnlineTime" placeholder="" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">保存</el-button>
        <el-button>取消</el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>
</template>

<script setup lang="ts">
import { onMounted, reactive, ref } from 'vue'
import * as ConfigApi from '@/api/infra/config'
import { ElMessage } from 'element-plus'
defineOptions({ name: 'SiteConfig' })

const formLoading = ref(false)
const formData = ref({
  timezone: '',
  serviceLink: '',
  registrationBonus: '0',
  serviceOnlineTime: '',
  defaultTopParentInvitationCode: '',
  virtualMemberUSDTAddress: ''
})
const formRules = reactive({
  timezone: [{ required: true, message: '系统时区不能为空', trigger: 'blur' }],
  serviceLink: [{ required: true, message: '客服链接', trigger: 'blur' }],
  serviceOnlineTime: [{ required: true, message: '客服在线时间不能为空', trigger: 'blur' }]
})

const getList = async () => {
  const params = {
    category: 'site'
  }
  const res = await ConfigApi.getConfigPage(params)
  if (res.code === 200) {
    ElMessage.success('更新成功')
  }
  const list = res.list
  list.forEach((item) => {
    if (item.key === 'timezone') {
      formData.value.timezone = item.value
    }
    if (item.key === 'serviceLink') {
      formData.value.serviceLink = item.value
    }
    if (item.key === 'serviceOnlineTime') {
      formData.value.serviceOnlineTime = item.value
    }
    if (item.key === 'registrationBonus') {
      formData.value.registrationBonus = item.value
    }
    if (item.key === 'defaultTopParentInvitationCode') {
      formData.value.defaultTopParentInvitationCode = item.value
    }
    if (item.key === 'virtualMemberUSDTAddress') {
      formData.value.virtualMemberUSDTAddress = item.value
    }
  })
}

const onSubmit = async () => {
  formLoading.value = true
  let list = [
    {
      key: 'timezone',
      value: formData.value.timezone,
      name: '系统时区'
    },
    {
      key: 'serviceLink',
      value: formData.value.serviceLink,
      name: '客服链接'
    },
    {
      key: 'serviceOnlineTime',
      value: formData.value.serviceOnlineTime,
      name: '客服在线时间'
    },
    {
      key: 'registrationBonus',
      value: formData.value.registrationBonus,
      name: '注册赠送金额'
    },
    {
      key: 'virtualMemberUSDTAddress',
      value: formData.value.virtualMemberUSDTAddress,
      name: '虚拟会员默认USDT地址'
    }
  ]
  try {
    const res = await ConfigApi.batchCreateSiteConfig({ list: list })
    console.log(res)
  } finally {
    formLoading.value = false
  }
}

// 初始化
onMounted(() => {
  getList()
})
</script>
