import { createElement, lazy } from 'react'

const DocumentJobInfo = lazy(
  () => import('../../components/Dashboard/Management/DocumentJobs/DocumentJobInfo')
)
import InfoCircleIcon from '../../components/Icons/InfoCircleIcon'
import EditIcon from '../../components/Icons/EditIcon'
import CheckIcon from '../../components/Icons/CheckIcon'
import XMarkIcon from '../../components/Icons/XMarkIcon'
import DocumentJobIcon from '../../components/Icons/DocumentJobIcon'
import dayjs from 'dayjs'

export const DocumentJob = {
  name: 'documentJob',
  label: 'Document Job',
  labelPlural: 'Document Jobs',
  url: '/dashboard/management/documentjobs',
  prefix: 'DJB',
  icon: DocumentJobIcon,
  actions: [
    {
      name: 'info',
      type: 'page',
      pageName: 'info',
      label: 'Info',
      default: true,
      row: true,
      icon: InfoCircleIcon
    },
    {
      name: 'edit',
      type: 'page',
      pageName: 'info',
      label: 'Edit',
      row: true,
      icon: EditIcon,
      visible: (objectData) => {
        return !(objectData?._isEditing && objectData?._isEditing == true)
      }
    },
    {
      name: 'cancelEdit',
      label: 'Cancel Edits',
      type: 'page',
      pageName: 'info',
      icon: XMarkIcon,
      visible: (objectData) => {
        return objectData?._isEditing && objectData?._isEditing == true
      }
    },
    {
      name: 'finishEdit',
      label: 'Save Edits',
      type: 'page',
      pageName: 'info',
      icon: CheckIcon,
      visible: (objectData) => {
        return objectData?._isEditing && objectData?._isEditing == true
      }
    },
  ],
  pages: [
    {
      name: 'info',
      content: () => createElement(DocumentJobInfo)
    }
  ],
  columns: ['_reference', 'name', 'state', 'createdAt', 'updatedAt'],
  filters: [
    'name',
    'width',
    'height',
    'state',
    'createdAt',
    'updatedAt',
    '_reference'
  ],
  sorters: ['name', 'state', 'createdAt', 'updatedAt'],
  properties: [
    {
      name: '_id',
      label: 'ID',
      type: 'id',
      objectType: 'documentJob',
      showCopy: true,
      columnWidth: 140
    },
    {
      name: 'createdAt',
      label: 'Created At',
      type: 'dateTime',
      readOnly: true,
      columnWidth: 175
    },
    {
      name: '_reference',
      label: 'Reference',
      type: 'reference',
      columnFixed: 'left',
      objectType: 'documentJob',
      showCopy: true,
      readOnly: true,
      columnWidth: 180
    },
    {
      name: 'updatedAt',
      label: 'Updated At',
      type: 'dateTime',
      readOnly: true,
      columnWidth: 175
    },
    {
      name: 'name',
      label: 'Name',
      required: true,
      type: 'text',
      columnWidth: 200,
      columnFixed: 'left',
      value: (objectData) => {
        if (objectData?.createdAt == undefined) {
          return `${objectData?.documentTemplate?.name || 'No template'} ${dayjs(new Date()).format('YYYY-MM-DD HH:mm:ss')} (${objectData?.object?.name || objectData?.object?._id})`
        }
      }
    },

    {
      name: 'state',
      label: 'Status',
      type: 'state',
      objectType: 'printer',
      showName: false,
      readOnly: true,
      columnWidth: 260
    },
    {
      name: 'objectType',
      label: 'Object Type',
      required: true,
      columnWidth: 150,
      type: 'objectType'
    },
    {
      name: 'object',
      label: 'Object',
      required: true,
      columnWidth: 150,
      type: 'object',
      objectType: (objectData) => {
        return objectData?.objectType
      },
      showHyperlink: true
    },
    {
      name: 'documentTemplate',
      label: 'Template',
      required: true,
      columnWidth: 150,
      type: 'object',
      objectType: 'documentTemplate',
      showHyperlink: true,
      masterFilter: (objectData) => {
        return {
          active: true,
          global: false,
          objectType: objectData.objectType
        }
      }
    },
    {
      name: 'documentPrinter',
      label: 'Printer',
      required: true,
      columnWidth: 150,
      type: 'object',
      objectType: 'documentPrinter',
      showHyperlink: true,
      masterFilter: () => {
        return {
          active: true,
          online: true
        }
      }
    }
  ]
}
