import { QUESTION_TYPE, ORDER_STATUS, QUES_STATUS } from '@/utils/dictionary.js'
import TableImage from '@/components/DataTable/tools/Image.vue'
import TableTags from '@/components/DataTable/tools/Tags.vue'
import TableAction from '@/components/DataTable/tools/Action.vue'
import { orderDetail } from '@/api/order/index.js'
import { h, ref, reactive } from 'vue'

/* 注册table */
const tableRef = ref()
const searchParams = ref()

function handleSearch(params) {
  searchParams.value = { ...params }
  if (params?.time?.length) {
    searchParams.value = {
      ...params,
      startTime: params.time[0],
      endTime: params.time[1]
    }
  }
  delete searchParams.value.time
  tableRef.value.reFetch({ searchParams })
}

async function handlePreview(row) {
  if (row.handleStatus === 0) {
    data.rowData = {
      ...row,
      status: 0
    }
  }
  if (row.handleStatus === 1) {
    const res = await orderDetail({ questionId: row.id })
    data.rowData = {
      ...row,
      handlerImage: res.data.questionHandle.handlerImage,
      handlerResult: res.data.questionHandle.handlerResult
    }
  }
  data.modalShow = true
}

/* 位置 */
function handlePositionDrawer(row) {
  data.rowData = row
  data.mapDrawer = true
}

function handleImgPreview(row) {
  data.rowData = row
  data.previewModal = true
}

const data = reactive({
  tableRef,
  searchParams,
  rowData: {},
  modalShow: false,
  mapDrawer: false,
  previewModal: false,
  handleSearch,

  columns: [
    {
      type: 'selection',
      disabled(row) {
        return row.wordOrderStatus === 1
      }
    },
    {
      title: '序号',
      key: 'key',
      render: (_, index) => {
        return `${index + 1}`
      },
      align: 'center'
    },
    {
      title: '问题类型',
      key: 'type',
      align: 'center',
      render(row) {
        return h(TableTags, {
          data: row.type,
          filters: QUESTION_TYPE.value
        })
      }
    },
    {
      title: '问题图片',
      key: 'fileMarkerUrl',
      align: 'center',
      render(row) {
        return h(TableImage, {
          images: {
            width: 36,
            height: 36,
            src: row.fileMarkerUrl,
            previewDisabled: true,
            onClick: handleImgPreview.bind(null, row)
          }
        })
      }
    },
    {
      title: '所属任务',
      key: 'missionName',
      align: 'center'
    },
    {
      title: '经纬度',
      key: 'name',
      align: 'center',
      render(row) {
        return h(TableTags, {
          data: [{ name: row.lng }, { name: row.lat }]
        })
      }
    },
    {
      title: '位置',
      key: 'position',
      align: 'center',
      render(row) {
        return h(TableAction, {
          actions: [
            {
              label: '图片位置',
              type: 'button',
              props: {
                type: 'primary',
                text: true,
                onClick: handlePositionDrawer.bind(null, row)
              },
              auth: 'basic_list'
            }
          ],
          align: 'center'
        })
      }
    },
    {
      title: '工单生成状态',
      key: 'wordOrderStatus',
      align: 'center',
      render(row) {
        return h(TableTags, {
          data: row.wordOrderStatus,
          filters: ORDER_STATUS
        })
      }
    },
    {
      title: '处理状态',
      key: 'handleStatus',
      align: 'center',
      render(row) {
        return h(TableTags, {
          data: row.handleStatus,
          filters: QUES_STATUS
        })
      }
    },
    {
      title: '操作',
      align: 'center',
      width: 150,
      fixed: 'right',
      render(row) {
        return h(TableAction, {
          actions: [
            {
              label: '查看',
              type: 'button',
              props: {
                type: 'primary',
                text: true,
                onClick: handlePreview.bind(null, row)
              },
              auth: 'basic_list'
            }
          ],
          align: 'center'
        })
      }
    }
  ]
})

export default data
