<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="main_id">
        <a-select
          v-model="formData.main_id"
          :options="mainGameOptions"
          placeholder="请选择产品归属"
          :disabled="mode == 'edit'"
          allow-clear
        />
      </a-form-item>
      <a-form-item label="游戏名称" field="name">
        <a-input v-model="formData.name" placeholder="请输入游戏名称" />
      </a-form-item>
      <a-form-item label="游戏平台" field="os">
        <sa-select
          v-model="formData.os"
          dict="os"
          placeholder="请选择平台"
          allow-clear
        />
      </a-form-item>

      <a-form-item label="分成比例" field="divide">
        <a-input-number
          v-model="formData.divide"
          placeholder="请输入分成比例"
        />
      </a-form-item>
      <a-form-item
        label="内购分成比例"
        field="ios_divide"
        v-if="formData.os == '2'"
      >
        <a-input-number
          v-model="formData.ios_divide"
          placeholder="请输入苹果内购分成比例"
        />
      </a-form-item>
      <a-form-item label="游戏实名" field="realname_switch">
        <sa-select
          v-model="formData.realname_switch"
          dict="realname_switch"
          placeholder="请选择游戏实名"
          allow-clear
        />
      </a-form-item>
      <a-divider />
      <a-form-item label="游戏KEY" field="main_game_id">
        <a-select
          v-model="formData.main_game_id"
          :options="gameOptions"
          placeholder="请选择主游戏"
          allow-clear
          @change="handleMainGameChange"
        />
      </a-form-item>
      <a-form-item label="游戏发货规则" field="cp_callback_type" hidden>
        <sa-radio v-model="formData.cp_callback_type" dict="cp_callback_type" />
      </a-form-item>
      <a-form-item label="游戏包名" field="package_name">
        <a-input
          v-model="formData.package_name"
          placeholder="安装包的包名，IOS与过包技术确认后填写"
        />
        <template #extra>
          <div>格式：com.gamename.newname</div>
        </template>
      </a-form-item>
      <a-form-item
        label="分包参数"
        field="wxgamepro"
        v-if="formData.os == '3' || formData.os == '4'"
      >
        <a-input
          v-model="formData.wxgamepro"
          placeholder="请输入小游戏分包参数"
        />
        <template #extra>
          <div>格式：wxgamepro=PkgCBgAAoxxxx</div>
        </template>
      </a-form-item>
      <a-form-item
        label="ISO商店APPID"
        field="ios_appid"
        v-if="formData.os == '2'"
      >
        <a-input v-model="formData.ios_appid" placeholder="请输入IOS APPID" />
      </a-form-item>
      <a-form-item
        label="iOS线上版本"
        field="online_version"
        v-if="formData.os == '2'"
      >
        <a-input
          v-model="formData.online_version"
          placeholder="请输入线上版本"
        />
        <template #extra>
          <div>iOS包内传参大于该版本则接口会返回审核服</div>
        </template>
      </a-form-item>
      <a-form-item label="充值回调" field="cp_callback_url">
        <a-input
          v-model="formData.cp_callback_url"
          placeholder="研发发货地址提供"
        />
      </a-form-item>
      <a-form-item
        label="研发登录地址"
        field="cp_h5_url"
        v-if="formData.os == '5'"
      >
        <a-input v-model="formData.cp_h5_url" placeholder="研发H5登录地址" />
        <template #extra>
          <div>H5或iOS-H5壳包必填</div>
        </template>
      </a-form-item>
      <a-form-item label="状态" field="status">
        <sa-switch v-model="formData.status" />
      </a-form-item>
      <!-- <a-form-item label="排序" field="sort">
        <a-input v-model="formData.sort" placeholder="请输入排序" />
      </a-form-item>
      <a-form-item label="描述" field="remark">
        <a-input v-model="formData.remark" placeholder="请输入描述" />
      </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/game";
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 mainGameOptions = ref([]);
const gameOptions = ref([]);

let title = computed(() => {
  return "游戏列表" + (mode.value == "add" ? "-新增" : "-编辑");
});

// 表单初始值
const initialFormData = {
  id: null,
  main_id: null,
  name: "",
  os: 1,
  cp_h5_url: "",
  divide: "50",
  ios_divide: "50",
  main_game_id: "",
  cp_callback_type: 0,
  wxgamepro: "",
  online_version: "",
  status: 1,
  sort: null,
  remark: "",
  package_name: "",
  cp_callback_url: "",
  realname_switch: 2,
  ios_appid: "",
};

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

// 验证规则
const rules = {
  main_id: [{ required: true, message: "产品归属必需填写" }],
  name: [{ required: true, message: "游戏名称必需填写" }],
  os: [{ required: true, message: "平台必需选择" }],
  divide: [{ required: true, message: "分成比例必需填写" }],
  cp_callback_type: [{ required: true, message: "游戏发货规则必需选择" }],
  realname_switch: [{ 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 () => {
  await getMainGameOptionsList();
  await getGameOptionsList();
};

// 获取主游戏列表
const getMainGameOptionsList = async () => {
  const { data } = await centerCommonApi.getMainGameOptionsApi();
  mainGameOptions.value = data;
};

// 获取子游戏列表
const getGameOptionsList = async () => {
  const { data } = await centerCommonApi.getGameOptionsApi();
  console.log(formData.name);
  const newData = data.filter((item) => {
    return item.label !== formData.name;
  });
  newData.unshift({
    label: "默认生成独立key",
    value: 0,
  });
  gameOptions.value = newData;
};

// 设置数据
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,
      realname_switch: Number(formData.realname_switch),
    };

    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);
};

// handleMainGameChange
const handleMainGameChange = (value) => {
  console.log(value);
  if (value == "") {
    formData.cp_callback_type = "0";
  } else {
    formData.cp_callback_type = "1";
  }
};

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

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