import { h, ref, reactive } from 'vue'
import TableImage from '@/components/DataTable/tools/Image.vue'
import TableAction from '@/components/DataTable/tools/Action.vue'
import { bannerDelete } from '@/api/system/banner.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 = row
  data.modalType = type
  data.modalShow = true
}

/**
 * @description: 删除用户接口
 * @param {Array} ids 用户id集合
 * @return {*}
 */
function deleteData(ids) {
  bannerDelete(ids).then((res) => {
    if (res.code === 0) {
      handleSearch()
    }
  }).catch(e => {
    console.log(e)
  })
}

const data = reactive({
  tableRef,
  searchParams,
  rowData: {},
  modalType: 'create',
  modalShow: false,
  handleSearch,
  deleteData,
  columns: [
    {
      title: '序号',
      key: 'key',
      width: 180,
      render: (_, index) => {
        return `${index + 1}`
      },
      align: 'center'
    },
    {
      title: '横幅图片',
      key: 'cover',
      align: 'center',
      render(row) {
        return h(TableImage, {
          images: {
            width: 119,
            height: 51,
            src: row.cover
            // previewDisabled: true  // 禁止点击预览
          }
        })
      }
    },
    {
      title: '上传人',
      key: 'updateUser',
      align: 'center',
      width: 200
    },
    {
      title: '上传时间',
      key: 'createTime',
      align: 'center',
      width: 220
    },
    {
      title: '操作',
      align: 'center',
      width: 220,
      fixed: 'right',
      render(row) {
        return h(TableAction, {
          actions: [
            {
              label: '详情',
              type: 'button',
              props: {
                type: 'primary',
                text: true,
                onClick: getRowData.bind(null, row, 'preview')
              },
              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: 'error'
              }
            }
          ],
          align: 'center'
        })
      }
    }
  ]
})

export default data
