<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-item label="游戏名称" field="game_id">
        <a-select
          v-model="formData.game_id"
          placeholder="请选择游戏"
          search
          allow-clear
        >
          <a-option
            v-for="item in gameOptions"
            :key="item.id"
            :value="item.id"
            :label="`${item.id}:${item.name}`"
          />
        </a-select>
      </a-form-item>
      <a-form-item label="开启时间" field="pay_stime">
        <a-time-picker
          style="width: 100%"
          v-model="formData.pay_stime"
          placeholder="请选择开启时间"
        />
      </a-form-item>
      <a-form-item label="结束时间" field="pay_etime">
        <a-time-picker
          style="width: 100%"
          v-model="formData.pay_etime"
          placeholder="请选择结束时间"
        />
      </a-form-item>
      <a-form-item label="充值金额" field="pay_money">
        <a-input v-model="formData.pay_money" placeholder="请输入充值金额" />
      </a-form-item>
      <a-form-item label="充值次数" field="pay_num">
        <a-input v-model="formData.pay_num" placeholder="请输入充值次数" />
      </a-form-item>
      <a-form-item label="注册天数" field="reg_day">
        <a-input v-model="formData.reg_day" placeholder="请输入注册天数" />
      </a-form-item>
      <a-form-item label="屏蔽地区" field="city">
        <a-input v-model="formData.city" placeholder="请输入屏蔽地区" />
      </a-form-item>
      <a-form-item label="充值渠道开关" field="pay_way">
        <sa-radio v-model="formData.pay_way" dict="ios_pay_way" />
      </a-form-item>
    </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/center/iosPayWay";
import centerCommonApi from "../../api/center/common";

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

let title = computed(() => {
  return "ios切支付" + (mode.value == "add" ? "-新增" : "-编辑");
});

// 表单初始值
const initialFormData = {
  id: null,
  game_id: null,
  pay_stime: "",
  pay_etime: "",
  pay_money: 45,
  pay_num: 2,
  reg_day: 1,
  city: "",
  apple_pay: 1,
  pay_way: 1,
};

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

// 验证规则
const rules = {
  pay_stime: [{ required: true, message: "开启时间必需填写" }],
  pay_etime: [{ required: true, message: "结束时间必需填写" }],
  game_id: [{ required: true, message: "游戏名称必需填写" }],
  pay_money: [{ required: true, message: "充值金额必需填写" }],
  pay_num: [{ required: true, message: "充值次数必需填写" }],
  reg_day: [{ required: true, message: "注册天数必需填写" }],
  apple_pay: [{ required: true, message: "苹果支付必需填写" }],
  pay_way: [{ required: true, message: "充值渠道开关必需填写" }],
};

// 获取游戏列表
const getGameOptionsList = async () => {
  const res = await centerCommonApi.getGameOptionsApi();
  if (res.code == 200) {
    gameOptions.value = res.data;
  }
};

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

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

// 设置数据
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>
