<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"-->
<!--          allow-search-->
<!--          placeholder="请选择投放游戏"-->
<!--        >-->
<!--          <a-option-->
<!--            v-for="item in gameOptions"-->
<!--            :key="item.value"-->
<!--            :value="item.value"-->
<!--            :label="item.label"-->
<!--          />-->
<!--        </a-select>-->
        <game-select v-model="formData.game_id"  />
      </a-form-item>
      <a-form-item label="广告位ID" field="site_id">
        <a-input v-model="formData.site_id" placeholder="请输入广告位ID" />
      </a-form-item>
      <a-form-item label="消耗日期" field="tdate">
        <a-date-picker
          v-model="formData.tdate"
          :show-time="false"
          mode="date"
          placeholder="请选择消耗日期"
          style="width: 100%"
        />
      </a-form-item>

      <a-form-item label="消耗金额" field="money">
        <a-input v-model="formData.money" placeholder="请输入消耗金额" />
      </a-form-item>
      <a-form-item label="备注" field="memo">
        <a-input v-model="formData.memo" 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/advert/mediaCost";
import centerCommonApi from "../../api/center/common";
import GameSelect from "@cps/game-select/index.vue";

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 "媒体消耗" + (mode.value == "add" ? "-新增" : "-编辑");
});

// 表单初始值
const initialFormData = {
  id: null,
  tdate: "",
  game_id: null,
  site_id: null,
  money: "0.00",
  memo: "",
  add_type: 1,
};

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

// 验证规则
const rules = {
  game_id: [{ required: true, message: "投放游戏必需填写" }],
  site_id: [{ required: true, message: "广告位ID必需填写" }],
  tdate: [{ required: true, message: "消耗日期必需填写" }],
  money: [{ 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 getGameOptions();
};

// 获取投放游戏选项
const getGameOptions = async () => {
  const res = await centerCommonApi.getGameOptionsApi();
  if (res.code === 200) {
    gameOptions.value = res.data;
  }
};

// 设置数据
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, add_type: 1 });
    } else {
      // 修改数据
      result = await api.update(data.id, { ...data, add_type: 1 });
    }
    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>
