import { h, ref, reactive } from 'vue'
import TableImage from '@/components/DataTable/tools/Image.vue'
import TableTags from '@/components/DataTable/tools/Tags.vue'
import TableAction from '@/components/DataTable/tools/Action.vue'
import { ADVERTISING_TYPE, ADVERTISING_PLATFORM, ADVERTISING_STATUS } from '@/utils/dictionary.js'
import { advertisingDelete } from '@/api/setting/advertising.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
}

// 删除方法
function deleteData(id) {
  advertisingDelete(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: [
    {
      type: 'selection'
    },
    {
      title: '编号',
      key: 'key',
      render: (_, index) => {
        return `${index + 1}`
      },
      align: 'center',
      width: 50
    },
    {
      title: '广告名称',
      key: 'title',
      align: 'center',
      width: 200
    },
    {
      title: '广告封面',
      key: 'cover',
      render(row) {
        return h(TableImage, {
          images: {
            width: 36,
            height: 36,
            src: row.cover
          }
        })
      },
      align: 'center',
      width: 100
    },
    {
      title: '广告类型',
      key: 'type',
      align: 'center',
      width: 100,
      render(row) {
        return h(TableTags, {
          data: row.type,
          filters: ADVERTISING_TYPE,
          tags: {
            bordered: true
          }
        })
      }
    },
    {
      title: '投放平台',
      key: 'platform',
      align: 'center',
      width: 100,
      render(row) {
        return h(TableTags, {
          data: row.platform,
          filters: ADVERTISING_PLATFORM,
          tags: {
            bordered: true
          }
        })
      }
    },
    {
      title: '广告描述',
      key: 'description',
      align: 'center',
      ellipsis: {
        tooltip: true
      },
      width: 250
    },
    {
      title: '广告地址',
      key: 'url',
      align: 'center',
      ellipsis: {
        tooltip: true
      },
      width: 200
    },
    {
      title: '广告尺寸',
      key: 'size',
      align: 'center',
      render(row) {
        return h(TableTags, {
          data: `${row.width} x ${row.height}`
        })
      },
      width: 100
    },
    {
      title: '开始时间',
      key: 'startTime',
      align: 'center',
      width: 200
    },
    {
      title: '结束时间',
      key: 'endTime',
      align: 'center',
      width: 200
    },
    {
      title: '浏览量',
      key: 'viewNum',
      align: 'center',
      width: 100
    },
    {
      title: '状态',
      key: 'status',
      align: 'center',
      width: 50,
      render(row) {
        return h(TableTags, {
          data: row.status,
          filters: ADVERTISING_STATUS,
          tags: {
            bordered: true
          }
        })
      }
    },
    {
      title: '排序',
      key: 'sort',
      align: 'center',
      width: 50
    },
    {
      title: '创建时间',
      key: 'createTime',
      align: 'center',
      width: 200
    },
    {
      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, '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: 'primary'
              }
            }
          ],
          align: 'center'
        })
      }
    }
  ]
})

export default data
