<template>
  <div class="ma-content-block">
    <sa-table ref="crudRef" :options="options" :columns="columns" :searchForm="searchForm">
      <!-- 搜索区 tableSearch -->
      <template #tableSearch>
        <a-col :sm="8" :xs="24">
          <a-form-item label="渠道类型" field="media_id">
            <a-select v-model="searchForm.media_id" placeholder="请选择渠道类型" allow-clear>
              <a-option
                v-for="item in mediaOptions"
                :key="item.id"
                :value="item.id"
                :label="`${item.id}:${item.name}`" />
            </a-select>
          </a-form-item>
        </a-col>
        <a-col :sm="8" :xs="24">
          <a-form-item label="渠道名称" field="name">
            <a-input v-model="searchForm.name" placeholder="请输入渠道名称" allow-clear />
          </a-form-item>
        </a-col>
        <a-col :sm="8" :xs="24">
          <a-form-item label="渠道ID" field="id">
            <a-input v-model="searchForm.id" placeholder="请输入渠道ID" allow-clear />
          </a-form-item>
        </a-col>
        <a-col :sm="8" :xs="24">
          <a-form-item label="后台归属人" field="auth_id">
            <auth-select v-model="searchForm.auth_id" multiple />
          </a-form-item>
        </a-col>
      </template>

      <!-- Table 自定义渲染 -->
      <template #operationBeforeExtend="{ record }">
        <a-button type="text" @click="addAdvert(record)"> <icon-plus /> 添加广告位 </a-button>
      </template>
    </sa-table>

    <!-- 编辑表单 -->
    <edit-form ref="editRef" @success="refresh" />

    <!-- 添加广告位表单 -->
    <add-site-form ref="addSiteRef" @success="refresh" />
  </div>
</template>

<script setup>
import { onMounted, ref, reactive } from 'vue'
import { Message } from '@arco-design/web-vue'
import EditForm from './edit.vue'
import addSiteForm from './addSite.vue'
import api from '../../api/advert/agenList'
import advertCommonApi from '../../api/advert/common'
import AuthSelect from '@/components/auth-select/index.vue'

// 引用定义
const crudRef = ref()
const editRef = ref()
const viewRef = ref()
const addSiteRef = ref()

const authOptions = ref([])
const mediaOptions = ref([])
// 搜索表单
const searchForm = ref({
  auth_id: '',
  name: '',
})

// 添加广告位
const addAdvert = (record) => {
  addSiteRef.value?.open()
  addSiteRef.value?.setFormData(record)
}

// SaTable 基础配置
const options = reactive({
  api: api.getPageList,
  rowSelection: { showCheckedAll: true },
  add: {
    show: true,
    auth: ['/v1/advert/AgentList/save'],
    func: async () => {
      editRef.value?.open()
    },
  },
  edit: {
    show: true,
    auth: ['/v1/advert/AgentList/update'],
    func: async (record) => {
      editRef.value?.open('edit')
      editRef.value?.setFormData(record)
    },
  },
  delete: {
    show: true,
    auth: ['/v1/advert/AgentList/destroy'],
    func: async (params) => {
      const resp = await api.destroy(params)
      if (resp.code === 200) {
        Message.success(`删除成功！`)
        crudRef.value?.refresh()
      }
    },
  },
})

// SaTable 列配置
const columns = reactive([
  { title: 'ID', dataIndex: 'id', width: 80 },

  { title: '渠道名称', dataIndex: 'name', width: 120 },
  { title: '媒体ID', dataIndex: 'media_id', width: 80 },
  { title: '媒体', dataIndex: 'media_name', width: 120 },
  { title: '后台归属人', dataIndex: 'auth_name', width: 120 },
])

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

const getAuthOptions = async () => {
  const resp = await advertCommonApi.getAuthOptionsApi()
  if (resp.code === 200) {
    authOptions.value = resp.data
  }
}

// 获取媒体列表Options
const getMediaOptions = async () => {
  const res = await advertCommonApi.getMediaOptionsApi()
  if (res.code == 200) {
    mediaOptions.value = res.data
  }
}

// SaTable 数据请求
const refresh = async () => {
  crudRef.value?.refresh()
}

// 页面加载完成执行
onMounted(async () => {
  initPage()
  refresh()
})
</script>

<script>
export default { name: 'v1/advert/agenList' }
</script>
