import { MENU_TYPE, MENU_STATUS, MENU_VISIBLE } from '@/utils/dictionary.js'
import { h, ref, reactive } from 'vue'
import TableTags from '@/components/DataTable/tools/Tags.vue'
import TableAction from '@/components/DataTable/tools/Action.vue'
import { deleteMenu } from '@/api/system/menu/index.js'

/* 注册table */
const tableRef = ref()
const searchParams = ref()

function handleSearch(params) {
  searchParams.value = { ...params }
  tableRef.value.reFetch({ searchParams })
}

/**
 * @description: 获取数据及操作
 * @param {*} row 单行数据
 * @param {*} type 操作类型 create:创建，preview:预览，edit:编辑
 * @return {*}
 */
function getRowData(row, type) {
  data.rowData = type === 'create' ? { pid: row.id } : row
  data.modalType = type
  data.modalShow = true
}

// 删除方法
function deleteData(id) {
  deleteMenu(id)
    .then((res) => {
      if (res.code === 0) {
        handleSearch()
      }
    })
    .catch((e) => {
      console.log(e)
    })
}

const data = reactive({
  tableRef,
  searchParams,
  rowData: {},
  modalType: 'create',
  modalShow: false,
  handleSearch,

  columns: [
    {
      title: '菜单标题',
      key: 'title',
      align: 'center',
      width: 200
    },
    {
      title: '菜单类型',
      key: 'type',
      align: 'center',
      width: 100,
      render(row) {
        return h(TableTags, {
          data: row.type,
          filters: MENU_TYPE
        })
      }
    },
    {
      title: '路由地址',
      key: 'path',
      align: 'center',
      width: 200
    },
    {
      title: '组件路径',
      key: 'component',
      align: 'center',
      width: 200
    },
    {
      title: '状态',
      key: 'status',
      align: 'center',
      width: 100,
      render(row) {
        return h(TableTags, {
          data: row.status,
          filters: MENU_STATUS
        })
      }
    },
    {
      title: '排序',
      key: 'sort',
      align: 'center',
      width: 100
    },
    {
      title: '是否可见',
      key: 'hide',
      align: 'center',
      width: 100,
      render(row) {
        return h(TableTags, {
          data: row.hide,
          filters: MENU_VISIBLE
        })
      }
    },
    {
      title: '创建时间',
      key: 'createTime',
      align: 'center',
      width: 160
    },
    {
      title: '操作',
      align: 'center',
      width: 150,
      fixed: 'right',
      render(row) {
        return h(TableAction, {
          actions: [
            {
              label: '添加',
              type: 'button',
              props: {
                type: 'primary',
                text: true,
                onClick: getRowData.bind(null, row, 'create')
              },
              auth: 'basic_list'
            },
            {
              label: '修改',
              type: 'button',
              props: {
                type: 'primary',
                text: true,
                onClick: getRowData.bind(null, row, 'update')
              },
              auth: 'basic_list'
            },
            {
              label: '删除',
              type: 'popconfirm',
              auth: 'basic_list',
              tip: '确定删除这条数据吗?',
              props: {
                onPositiveClick: deleteData.bind(null, [row.id])
              },
              ButtonProps: {
                text: true,
                type: 'primary'
              }
            }
          ],
          align: 'center'
        })
      }
    }
  ]
})

export default data
