<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="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="app_id">
            <a-input
              v-model="searchForm.app_id"
              placeholder="请输入应用ID"
              allow-clear
            />
          </a-form-item>
        </a-col>
      </template>

      <!-- Table 自定义渲染 -->
    </sa-table>

    <!-- 编辑表单 -->
    <edit-form ref="editRef" @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 api from "../../api/advert/mediaAuth/app";

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

// 搜索表单
const searchForm = ref({
  name: "",
  app_id: "",
});

// SaTable 基础配置
const options = reactive({
  api: api.getPageList,
  rowSelection: { showCheckedAll: true },
  add: {
    show: true,
    auth: ["/v1/advert/mediaAuth/AdAdvertiserApp/save"],
    func: async () => {
      editRef.value?.open();
    },
  },
  edit: {
    show: true,
    auth: ["/v1/advert/mediaAuth/AdAdvertiserApp/update"],
    func: async (record) => {
      editRef.value?.open("edit");
      editRef.value?.setFormData(record);
    },
  },
  delete: {
    show: true,
    auth: ["/v1/advert/mediaAuth/AdAdvertiserApp/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: "media_id", width: 180 },
  { title: "应用名称", dataIndex: "name", width: 180 },
  { title: "应用ID", dataIndex: "app_id", width: 180 },
  { title: "应用密钥", dataIndex: "app_secret", width: 180 },
  { title: "OAuth授权地址", dataIndex: "auth_url", width: 180 },
  { title: "Token获取地址", dataIndex: "token_url", width: 180 },
  { title: "Token刷新地址", dataIndex: "refresh_url", width: 180 },
]);

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

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

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