import { h, ref, reactive } from 'vue'
import TableAction from '@/components/DataTable/tools/Action.vue'
import { boxDelete } from '@/api/equipment/box.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(data) {
  boxDelete(data)
    .then((res) => {
      if (res.code === 0) {
        handleSearch()
      }
    })
    .catch((e) => {
      console.log(e)
    })
}

const data = reactive({
  tableRef,
  searchParams,
  rowData: {},
  modalType: 'create',
  modalShow: false,
  configModalShow: false,
  handleSearch,
  deleteData,
  columns: [
    { type: 'selection' },
    {
      title: '设备编号',
      key: 'boxSn',
      align: 'center'
    },
    {
      title: '盒子名称',
      key: 'boxName',
      align: 'center'
    },
    {
      title: '通道编码',
      key: 'streamCode',
      align: 'center',
      Minwidth: 160
    },
    {
      title: '创建时间',
      key: 'createTime',
      align: 'center',
      Minwidth: 160
    },
    {
      title: '创建人',
      key: 'createUser',
      align: 'center',
      width: 100
    },
    {
      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, 'update')
              },
              auth: 'equipment:box:edit'
            },
            {
              label: '删除',
              type: 'popconfirm',
              auth: 'equipment:box:delete',
              tip: '确定删除这条数据吗?',
              props: {
                onPositiveClick: deleteData.bind(null, [row.id])
              },
              ButtonProps: {
                text: true,
                type: 'primary'
              }
            }
          ],
          align: 'center'
        })
      }
    }
  ]
})

export default data
