import { TASK_MODE, TASK_TYPE, TASK_STATUS } from '@/utils/dictionary.js'
import TableTags from '@/components/DataTable/tools/Tags.vue'
import TableAction from '@/components/DataTable/tools/Action.vue'
import { h, ref, reactive } from 'vue'
import { taskDelete, implement } from '@/api/task/index.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 handleRowDelete(row) {
  taskDelete(row.id)
    .then(res => {
      if (res.code === 0) {
        handleSearch()
      }
    })
}

function handleImplement(row) {
  $message.info('机场设备开始自检，请稍等')

  implement(row.id)
    .then(res => {
      if (res.code === 0) {
        handleSearch()
      }
    })
}

/* 直播 */
function handleTaskLive(row) {
  data.rowData = row
  data.liveDrawer = true
}

/* 回放 */
function handleTaskDemand(row) {
  data.rowData = row
  data.demandDrawer = true
}

/* 问题核实 */
function handleTaskVerify(row) {
  data.rowData = row
  data.verifyDrawer = true
}

const data = reactive({
  tableRef,
  searchParams,
  rowData: {},
  modalType: 'create',
  modalShow: false,
  liveDrawer: false,
  demandDrawer: false,
  verifyDrawer: false,
  handleSearch,

  columns: [
    {
      title: '序号',
      key: 'key',
      render: (_, index) => {
        return `${index + 1}`
      },
      align: 'center'
    },
    {
      title: '任务编号',
      key: 'code',
      align: 'center'
    },
    {
      title: '任务名称',
      key: 'name',
      align: 'center'
    },
    {
      title: '巡检方式',
      key: 'inspectionType',
      align: 'center',
      render(row) {
        return h(TableTags, {
          data: row.inspectionType,
          filters: TASK_MODE
        })
      }
    },
    {
      title: '巡检机场',
      key: 'airportName',
      align: 'center'
    },
    {
      title: '巡检路线',
      key: 'inspectionLineName',
      align: 'center'
    },
    {
      title: '任务类型',
      key: 'type',
      align: 'center',
      render(row) {
        return h(TableTags, {
          data: row.type,
          filters: TASK_TYPE
        })
      }
    },
    {
      title: '巡检时间',
      key: 'executionStartTime',
      align: 'center'
    },
    {
      title: '状态',
      key: 'status',
      align: 'center',
      render(row) {
        return h(TableTags, {
          data: row.status,
          filters: TASK_STATUS
        })
      }
    },

    {
      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: 'popconfirm',
              tip: '是否删除该数据？',
              props: {
                onPositiveClick: handleRowDelete.bind(null, row)
              },
              ButtonProps: {
                text: true,
                type: 'primary'
              },
              auth: 'basic_list',
              show: row.status === 1
            },
            {
              label: '立即执行',
              type: 'popconfirm',
              tip: '是否立即开始执行任务？',
              props: {
                onPositiveClick: handleImplement.bind(null, row)
              },
              ButtonProps: {
                text: true,
                type: 'primary'
              },
              auth: 'basic_list',
              show: row.status === 1
            },
            {
              label: '直播',
              type: 'button',
              props: {
                type: 'primary',
                text: true,
                onClick: handleTaskLive.bind(null, row)
              },
              auth: 'basic_list',
              show: row.status === 2
            },
            {
              label: '轨迹',
              type: 'button',
              props: {
                type: 'primary',
                text: true,
                onClick: handleTaskDemand.bind(null, row)
              },
              auth: 'basic_list',
              show: row.status === 4
            },
            {
              label: '问题核实',
              type: 'button',
              props: {
                type: 'primary',
                text: true,
                onClick: handleTaskVerify.bind(null, row)
              },
              auth: 'basic_list',
              show: row.status !== 1 && row.status !== 3
            }
          ],
          align: 'center'
        })
      }
    }

  ]
})

export default data
