import { h, ref, reactive } from 'vue'
import TableImage from '@/components/DataTable/tools/Image.vue'
import TableTags from '@/components/DataTable/tools/Tags.vue'
import TableSwitch from '@/components/DataTable/tools/Switch.vue'
import TableAction from '@/components/DataTable/tools/Action.vue'
import { NOTICE_SOURCE } from '@/utils/dictionary.js'
import { noticeDelete, isTopUpdate, statusUpdate } from '@/api/setting/notice.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) {
  noticeDelete(id)
    .then((res) => {
      if (res.code === 0) {
        handleSearch()
      }
    })
    .catch((e) => {
      console.log(e)
    })
}

/**
 * @description: 改变状态
 * @param {*} row 选中数据
 * @return {*}
 */
function handleIsTopChange(row) {
  isTopUpdate({ id: row.data.id, isTop: row.value })
    .then((res) => {
      if (res.code === 0) {
        handleSearch()
      }
    })
    .catch((e) => {
      console.log(e)
    })
}

/**
 * @description: 改变状态
 * @param {*} row 选中数据
 * @return {*}
 */
function handleStatusChange(row) {
  statusUpdate({ id: row.data.id, status: row.value })
    .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',
      ellipsis: {
        tooltip: true
      },
      width: 500
    },
    {
      title: '公告图片',
      key: 'image',
      render(row) {
        return h(TableImage, {
          images: {
            width: 36,
            height: 36,
            src: row.image
          }
        })
      },
      align: 'center',
      width: 100
    },
    {
      title: '通知来源',
      key: 'source',
      align: 'center',
      width: 100,
      render(row) {
        return h(TableTags, {
          data: row.source,
          filters: NOTICE_SOURCE,
          tags: {
            bordered: true
          }
        })
      }
    },
    {
      title: '是否置顶',
      key: 'isTop',
      align: 'center',
      width: 100,
      render(row) {
        return h(TableSwitch, {
          data: { id: row.id, isTop: row.isTop },
          rowKey: 'isTop',
          checkedValue: 1,
          uncheckedValue: 2,
          onChange: handleIsTopChange.bind(row)
        })
      }
    },
    {
      title: '状态',
      key: 'status',
      align: 'center',
      width: 100,
      render(row) {
        return h(TableSwitch, {
          data: { id: row.id, status: row.status },
          rowKey: 'status',
          checkedValue: 1,
          uncheckedValue: 2,
          onChange: handleStatusChange.bind(row)
        })
      }
    },
    {
      title: '浏览量',
      key: 'browse',
      align: 'center',
      width: 100
    },
    {
      title: '创建时间',
      key: 'createTime',
      align: 'center',
      width: 200
    },
    {
      title: '更新时间',
      key: 'updateTime',
      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
