import { 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 { 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 })
}

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

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

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

  columns: [
    {
      title: '序号',
      key: 'key',
      render: (_, index) => {
        return `${index + 1}`
      },
      align: 'center'
    },
    {
      title: '问题类型',
      key: 'questionName',
      align: 'center'
    },
    {
      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: '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: 'status',
      align: 'center',
      render(row) {
        return h(TableTags, {
          data: row.status,
          filters: QUES_STATUS
        })
      }
    },
    {
      title: '处理人员',
      key: 'handlerUserName',
      align: 'center',
      width: 100
    },
    {
      title: '处理时间',
      key: 'handlerTime',
      align: 'center',
      width: 200
    },
    {
      title: '处理后图片',
      key: 'handlerImage',
      align: 'center',
      render(row) {
        return h(TableImage, {
          images: {
            width: 36,
            height: 36,
            src: row.handlerImage,
            previewDisabled: true
            // onClick: handleImgPreview.bind(null, row)
          }
        })
      }
    },
    {
      title: '备注',
      key: 'handlerResult',
      align: 'center',
      width: 200
    }

  ]
})

export default data
