import { h, ref, reactive } from 'vue'
import TableTags from '@/components/DataTable/tools/Tags.vue'
import TableAction from '@/components/DataTable/tools/Action.vue'
import { uvaDelete } from '@/api/equipment/uva.js'
import { POWER_TYPE, FILE_TYPE } from '@/utils/dictionary.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) {
  uvaDelete(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: 'code',
      align: 'center'
    },
    {
      title: '设备名称',
      key: 'name',
      align: 'center'
    },
    {
      title: '动力类型',
      key: 'powerType',
      align: 'center',
      render(row) {
        return h(TableTags, {
          data: row.powerType,
          filters: POWER_TYPE
        })
      }
    },
    {
      title: '文件类型',
      key: 'srtType',
      align: 'center',
      render(row) {
        return h(TableTags, {
          data: row.srtType,
          filters: FILE_TYPE
        })
      }
    },
    {
      title: '定位精度',
      key: 'accuracy',
      align: 'center'
    },
    {
      title: '生产厂家',
      key: 'manufacturer',
      align: 'center'
    },
    {
      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:uva:edit'
            },
            {
              label: '删除',
              type: 'popconfirm',
              auth: 'equipment:uva:delete',
              tip: '确定删除这条数据吗?',
              props: {
                onPositiveClick: deleteData.bind(null, [row.id])
              },
              ButtonProps: {
                text: true,
                type: 'primary'
              }
            }
          ],
          align: 'center'
        })
      }
    }
  ]
})

export default data
