<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="母包ID" field="package_id">
        <a-select
          v-model="formData.package_id"
          placeholder="请选择母包"
          allow-clear
          @change="getPackageDetail"
        >
          <a-option
            v-for="item in packageOptions"
            :key="item.id"
            :value="item.id"
            :label="`${item.id}:${item.name}`"
          />
        </a-select>
      </a-form-item>
      <a-form-item label="游戏ID" field="game_id" hidden>
        <a-select
          v-model="formData.game_id"
          placeholder="请选择游戏"
          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="letter" hidden>
        <a-input v-model="formData.letter" placeholder="请输入目录" />
      </a-form-item>
      <a-form-item label="渠道" field="agent_id" hidden>
        <a-input
          v-model="formData.agent_id"
          placeholder="请选择渠道"
          allow-clear
        />
      </a-form-item>
      <a-form-item label="广告位" field="site_id" hidden>
        <a-input
          v-model="formData.site_id"
          placeholder="请输入广告位"
          allow-clear
        />
      </a-form-item>
      <a-form-item label="icon" field="icon">
        <sa-upload-image v-model="formData.icon" :limit="1" :multiple="false" />
      </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/advert/gamePackageLog";
import gamePackageApi from "../../api/advert/gamePackage";
import commonApi from "../../api/common";
const emit = defineEmits(["success"]);
// 引用定义
const visible = ref(false);
const loading = ref(false);
const formRef = ref();
const mode = ref("");
const packageOptions = ref([]);
const gameOptions = ref([]);

let title = computed(() => {
  return "打包记录" + (mode.value == "add" ? "-新增" : "-编辑");
});

// 表单初始值
const initialFormData = {
  id: null,
  package_id: null,
  letter: "",
  icon: "",
};

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

// 批量打包数据
const batchData = ref([]);

// 验证规则
const rules = {
  package_id: [{ required: true, message: "母包ID必需填写" }],
};

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

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

// 获取母包列表Options
const getPackageOptions = async () => {
  const resp = await commonApi.getPackageOptionsApi();
  if (resp.code === 200) {
    packageOptions.value = resp.data;
  }
};

// 获取游戏列表Options
const getGameOptions = async () => {
  const resp = await commonApi.getGameOptionsApi();
  if (resp.code === 200) {
    gameOptions.value = resp.data;
  }
};

// 设置批量打包数据
const setBatchData = async (data) => {
  batchData.value = data;
};

// 获取母包详情
const getPackageDetail = async (id) => {
  let row = packageOptions.value.find((item) => item.id == id);
  formData.letter = row.letter;
  formData.game_id = row.game_id;
  formData.package_id = row.id;
};

// 数据保存
const submit = async (done) => {
  const validate = await formRef.value?.validate();
  if (!validate) {
    loading.value = true;
    let data = { ...formData };
    let result = {};
    if (mode.value === "add") {
      let postData = [];
      // 添加数据
      data.id = undefined;
      // 批量打包
      if (batchData.value.length > 0) {
        for (let i = 0; i < batchData.value.length; i++) {
          let item = batchData.value[i];
          postData.push({
            ...data,
            ...item,
          });
        }
        result = await api.batchSave(postData);
      }
    } 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, setBatchData });
</script>
