import { h, ref, reactive } from 'vue'
import TableTags from '@/components/DataTable/tools/Tags.vue'
import TableAction from '@/components/DataTable/tools/Action.vue'
import { TASK_STATUS } from '@/utils/dictionary.js'
import { taskDelete } from '@/api/inspection/alltask.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 = type === 'create' ? { pid: row.id } : row
  data.modalType = type
  data.modalShow = true
}

/**
 * 打开审核弹窗
 * @param {*} row 单行数据
 */
function approve(row) {
  data.rowData = row
  data.approveShow = true
}

/**
 * 飞行坐标
 * @param {*} row 单行数据
 */
function coordinate(row) {
  data.rowData = row
  data.coordinateShow = true
}
/**
 * 任务详情
 * @param {*} row 单行数据
 */
function detail(row) {
  data.rowData = row
  data.detailShow = true
}
/**
 * 直播
 * @param {*} row 单行数据
 */
function live(row) {
  data.rowData = row
  data.liveShow = true
}

/**
 * 巡河报告
 * @param {*} row 单行数据
 */
function streamReport(row) {
  data.rowData = row
  data.streamShow = true
}

/**
 * 处理结果
 * @param {*} row 单行数据
 */
function handleResult(row) {
  data.rowData = row
  data.handleShow = true
}

// 删除方法
function deleteData(id) {
  taskDelete([id])
    .then((res) => {
      if (res.code === 0) {
        handleSearch()
      }
    })
    .catch((e) => {
      console.log(e)
    })
}

const data = reactive({
  tableRef,
  searchParams,
  deleteData,
  handleSearch,
  rowData: {},
  modalType: 'create',
  modalShow: false,
  approveShow: false,
  coordinateShow: false,
  detailShow: false,
  streamShow: false,
  handleShow: false,
  liveShow: false,
  columns: [
    {
      title: '序号',
      key: 'key',
      render: (_, index) => {
        return `${index + 1}`
      },
      align: 'center',
      minWidth: 50
    },
    {
      title: '任务编码',
      key: 'code',
      align: 'center',
      minWidth: 100
    },
    {
      title: '任务名称',
      key: 'name',
      align: 'center',
      minWidth: 100
    },
    {
      title: '巡检河道',
      key: 'streamName',
      align: 'center',
      minWidth: 150
    },
    {
      title: '河道长度（公里）',
      key: 'streamLength',
      align: 'center',
      minWidth: 60
    },
    {
      title: '状态',
      key: 'status',
      align: 'center',
      width: 100,
      render(row) {
        return h(TableTags, {
          data: row.status,
          filters: TASK_STATUS,
          tags: {
            bordered: false
          }
        })
      }
    },
    {
      title: '巡检日期',
      key: 'inspectionTime',
      align: 'center',
      minWidth: 100
    },
    {
      title: '操作',
      align: 'center',
      minWidth: 80,
      fixed: 'right',
      render(row) {
        return h(TableAction, {
          actions: [
            {
              label: '详情',
              type: 'button',
              props: {
                type: 'primary',
                text: true,
                onClick: detail.bind(null, row)
              },
              auth: 'basic_list'
            },
            {
              label: '审核',
              type: 'button',
              props: {
                type: 'primary',
                text: true,
                onClick: approve.bind(null, row)
              },
              auth: 'basic_list'
            },
            {
              label: '修改',
              type: 'button',
              props: {
                type: 'primary',
                text: true,
                onClick: getRowData.bind(null, row, 'update')
              },
              auth: 'basic_list'
            },
            {
              label: '飞行坐标',
              type: 'button',
              props: {
                type: 'primary',
                text: true,
                onClick: coordinate.bind(null, row)
              },
              auth: 'basic_list'
            },
            {
              label: '直播',
              type: 'button',
              props: {
                type: 'primary',
                text: true,
                onClick: live.bind(null, row)
              },
              auth: 'basic_list'
            },
            {
              label: '巡河报告',
              type: 'button',
              props: {
                type: 'primary',
                text: true,
                onClick: streamReport.bind(null, row)
              },
              auth: 'basic_list'
            },
            {
              label: '处理结果',
              type: 'button',
              props: {
                type: 'primary',
                text: true,
                onClick: handleResult.bind(null, row)
              },
              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
