<template>
  <component
    is="a-modal"
    v-model:visible="visible"
    :width="tool.getDevice() === 'mobile' ? '100%' : '600px'"
    :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="请选择推送游戏" @change="handleGameChange">
          <a-option v-for="item in gamePackageOptions" :key="item.game_id" :value="item.game_id" :label="item.name" />
        </a-select>
      </a-form-item>

      <a-form-item label="推送头条分包" field="fb">
        <a-switch v-model="formData.fb" />
      </a-form-item>

      <a-form-item label="推送转化跟踪" field="zh">
        <a-switch v-model="formData.zh" />
      </a-form-item>

      <a-form-item label="头条账号" field="advertiser_id">
        <a-select v-model="formData.advertiser_id" placeholder="请选择头条账号">
          <a-option
            v-for="item in ttAccountOptions"
            :key="item.advertiser_id"
            :value="item.advertiser_id"
            :label="item.advertiser_name" />
        </a-select>
      </a-form-item>
    </a-form>
    <!-- 表单信息 end -->
  </component>
</template>

<script setup>
import { ref, reactive, computed } from 'vue'
import { Message, Modal } from '@arco-design/web-vue'
import tool from '@/utils/tool'
import api from '@/api/system/post'
import { request } from '@/utils/request'
import commonApi from '../../api/common'
import agentSiteApi from '../../api/advert/agentSite'

const emit = defineEmits(['success'])

// 引用定义
const formRef = ref()
const mode = ref('')
const visible = ref(false)
const loading = ref(false)
const siteIds = ref()
const ttAccountOptions = ref([])
const gamePackageOptions = ref([])

let title = computed(() => {
  return '推送头条分包和转化'
})

// 表单初始值
const initialFormData = {
  id: '',
  game_id: '',
  fb: true,
  zh: true,
  remark: '',
}

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

// 设置批量数据
const setSiteIds = async (value) => {
  siteIds.value = value
}

// 验证规则
const rules = {
  game_id: [{ required: true, message: '推送游戏不能为空' }],
  advertiser_id: [{ required: true, message: '头条账号不能为空' }],
}

// 打开弹框
const open = async (type = 'ks') => {
  mode.value = type
  // 重置表单数据
  Object.assign(formData, initialFormData)
  formRef.value.clearValidate()
  formRef.value.resetFields()

  visible.value = true
  await initPage()
}

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

// 游戏ID改变
const handleGameChange = (value) => {
  const gameData = gamePackageOptions.value.find((item) => item.game_id === value)

  formData.id = gameData.id
}

// 获取母包列表
const getGamePackageOptions = async () => {
  const res = await commonApi.getPackageOptionsApi({ media_id: 1 })
  if (res.code === 200) {
    gamePackageOptions.value = res.data
  }
}

// 获取头条账号Options
const getTtAccountOptions = async () => {
  const res = await agentSiteApi.getTtAccountOptionsApi()
  if (res.code === 200) {
    ttAccountOptions.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 }
    console.log(data)
    let result = {}

    // 推送
    result = await agentSiteApi.ttPushNewEventApi({
      site_ids: siteIds.value,
      game_package_id: data.id,
      geme_id: data.game_id,
      fb: data.fb,
      zh: data.zh,
      advertiser_id: data.advertiser_id,
    })

    if (result.code === 200) {
      // Message.success("操作成功");
      Modal.info({
        title: '推送结果',
        content: result.data.message,
      })
      emit('success')
      done(true)
    }
    // 防止连续点击提交
    setTimeout(() => {
      loading.value = false
    }, 500)
  }
  done(false)
}

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

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