<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="package_name">
            <a-input v-model="searchForm.package_name" placeholder="请输入包名" allow-clear />
          </a-form-item>
        </a-col>
      </template>

      <!-- Table 自定义渲染 -->
      <template #qx="{ record }">
        <sa-switch
          checkedText="开启"
          uncheckedText="关闭"
          v-model="record.qx"
          @change="changeStatus($event, record, 'qx')" />
      </template>
      <template #ystc="{ record }">
        <sa-switch
          checkedText="开启"
          uncheckedText="关闭"
          v-model="record.ystc"
          @change="changeStatus($event, record, 'ystc')" />
      </template>
      <template #init="{ record }">
        <sa-switch
          checkedText="开启"
          uncheckedText="关闭"
          v-model="record.init"
          @change="changeStatus($event, record, 'init')" />
      </template>
    </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/center/qxSwitch'

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

// 搜索表单
const searchForm = ref({
  package_name: '',
})

// 修改状态
const changeStatus = async (status, record, field) => {
  const response = await api.update(record.id, {
    ...record,
    [field]: status,
  })
  if (response.code === 200) {
    Message.success(response.message)
    crudRef.value.refresh()
  }
}

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

// SaTable 列配置
const columns = reactive([
  { title: '包名', dataIndex: 'package_name', width: 180 },
  { title: '咨询允许获得权限', dataIndex: 'qx', width: 180 },
  { title: '隐私弹窗', dataIndex: 'ystc', width: 180 },
  { title: '登录框同意勾选文案', dataIndex: 'init', width: 180 },
])

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

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

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

<script>
export default { name: 'v1/center/qxSwitch' }
</script>
