<template>
  <component
    is="a-modal"
    :width="tool.getDevice() === 'mobile' ? '100%' : '600px'"
    v-model:visible="visible"
    title="CP对接信息"
    :mask-closable="false"
    @before-ok="handleOk"
    okText="复制"
    cancelText="关闭"
    @cancel="close"
  >
    <a-space direction="vertical" fill>
      <a-form>
        <a-form-item label="游戏名称">
          <a-input v-model="cp_info.name" readonly />
        </a-form-item>
        <a-form-item label="appid">
          <a-input v-model="cp_info.appid" readonly />
        </a-form-item>
        <a-form-item label="appkey">
          <a-input v-model="cp_info.appkey" readonly />
        </a-form-item>
        <a-form-item label="login_key">
          <a-input v-model="cp_info.login_key" readonly />
        </a-form-item>
        <a-form-item label="pay_key">
          <a-input v-model="cp_info.pay_key" readonly />
        </a-form-item>
        <a-form-item label="h5地址" v-if="cp_info.os == '5'">
          <a-input v-model="cp_info.channel_h5_url" readonly />
        </a-form-item>
      </a-form>
    </a-space>
  </component>
</template>

<script setup>
import { ref } from "vue";
import tool from "@/utils/tool";
import { Modal, Message } from "@arco-design/web-vue";
// 引用定义
const visible = ref(false);
const cp_info = ref({});

const emit = defineEmits(["success"]);

// 打开弹框
const open = async () => {
  visible.value = true;
  await initPage();
};

// 设置表单数据
const setFormData = (data) => {
  cp_info.value = data;
};

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

// 确定
const handleOk = async () => {
  // 复制
  const copyText = `游戏名称：${cp_info.value.name}
appid：${cp_info.value.appid}
appkey：${cp_info.value.appkey}
login_key：${cp_info.value.login_key}
pay_key：${cp_info.value.pay_key}
${cp_info.value.os == "5" ? `h5地址：${cp_info.value.channel_h5_url}` : ""}`;

  try {
    await tool.copy(copyText);
    Message.success({
      title: "提示",
      content: "复制成功",
    });
  } catch (error) {
    Message.error({
      title: "提示",
      content: "复制失败，请手动复制",
    });
    console.error("复制失败:", error);
  }

  close();
  emit("success");
};

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

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