import { createElement, lazy } from 'react'

const PurchaseOrderInfo = lazy(
  () => import('../../components/Dashboard/Inventory/PurchaseOrders/PurchaseOrderInfo')
)
const NewOrderItem = lazy(
  () => import('../../components/Dashboard/Inventory/OrderItems/NewOrderItem')
)
const NewShipment = lazy(
  () => import('../../components/Dashboard/Inventory/Shipments/NewShipment')
)
const NewInvoice = lazy(
  () => import('../../components/Dashboard/Finance/Invoices/NewInvoice')
)
const PostPurchaseOrder = lazy(
  () => import('../../components/Dashboard/Inventory/PurchaseOrders/PostPurchaseOrder')
)
const AcknowledgePurchaseOrder = lazy(
  () => import('../../components/Dashboard/Inventory/PurchaseOrders/AcknowledgePurchaseOrder')
)
const CancelPurchaseOrder = lazy(
  () => import('../../components/Dashboard/Inventory/PurchaseOrders/CancelPurchaseOrder')
)
import PurchaseOrderIcon from '../../components/Icons/PurchaseOrderIcon'
import InfoCircleIcon from '../../components/Icons/InfoCircleIcon'
import PlusIcon from '../../components/Icons/PlusIcon'
import CheckIcon from '../../components/Icons/CheckIcon'
import EditIcon from '../../components/Icons/EditIcon'
import XMarkIcon from '../../components/Icons/XMarkIcon'
import BinIcon from '../../components/Icons/BinIcon'

export const PurchaseOrder = {
  name: 'purchaseOrder',
  label: 'Purchase Order',
  labelPlural: 'Purchase Orders',
  url: '/dashboard/inventory/purchaseorders',
  prefix: 'POR',
  icon: PurchaseOrderIcon,
  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)
      },
      disabled: (objectData) => {
        return objectData?.state?.type != 'draft'
      }
    },
    {
      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
      }
    },
    {
      name: 'delete',
      type: 'page',
      pageName: 'info',
      label: 'Delete',
      icon: BinIcon,
      danger: true,
      visible: (objectData) => {
        return !(objectData?._isEditing && objectData?._isEditing == true)
      },
      disabled: (objectData) => {
        return objectData?.state?.type != 'draft'
      }
    },
    { type: 'divider' },
    {
      name: 'newOrderItem',
      type: 'modal',
      modalWidth: 700,
      label: 'New Order Item',
      icon: PlusIcon,
      disabled: (objectData) => {
        return objectData?.state?.type != 'draft'
      },
      content: (objectData, { onOk } = {}) => {
        return createElement(NewOrderItem, {
          defaultValues: {
            order: { _id: objectData._id },
            orderType: 'purchaseOrder',
            syncAmount: 'itemCost'
          },
          onOk,
          reset: true
        })
      }
    },
    {
      name: 'newShipment',
      type: 'modal',
      modalWidth: 700,
      label: 'New Shipment',
      icon: PlusIcon,
      disabled: (objectData) => {
        return objectData?.state?.type != 'draft'
      },
      content: (objectData, { onOk } = {}) => {
        return createElement(NewShipment, {
          defaultValues: { orderType: 'purchaseOrder', order: { _id: objectData._id } },
          onOk,
          reset: true
        })
      }
    },
    {
      name: 'newInvoice',
      type: 'modal',
      modalWidth: 700,
      label: 'New Invoice',
      icon: PlusIcon,
      disabled: (objectData) => {
        const allowedStates = [
          'received',
          'shipped',
          'partiallyReceived',
          'partiallyShipped'
        ]
        return !allowedStates.includes(objectData?.state?.type)
      },
      content: (objectData, { onOk } = {}) => {
        return createElement(NewInvoice, {
          defaultValues: { orderType: 'purchaseOrder', order: objectData },
          onOk,
          reset: true
        })
      }
    },
    {
      type: 'divider'
    },
    {
      name: 'post',
      type: 'modal',
      modalWidth: 520,
      label: 'Post',
      icon: CheckIcon,
      visible: (objectData) => {
        return objectData?.state?.type == 'draft'
      },
      content: (objectData, { onOk } = {}) => {
        return createElement(PostPurchaseOrder, { objectData, onOk })
      }
    },
    {
      name: 'acknowledge',
      type: 'modal',
      modalWidth: 520,
      label: 'Acknowledge',
      icon: CheckIcon,
      visible: (objectData) => {
        return objectData?.state?.type == 'sent'
      },
      content: (objectData, { onOk } = {}) => {
        return createElement(AcknowledgePurchaseOrder, { objectData, onOk })
      }
    },
    {
      name: 'complete',
      label: 'Complete',
      icon: CheckIcon,
      url: (_id) =>
        `/dashboard/inventory/shipments/info?shipmentId=${_id}&action=complete`,
      disabled: (objectData) => {
        return objectData?.state?.type != 'received'
      },
      visible: (objectData) => {
        return objectData?.state?.type == 'received'
      }
    },
    {
      name: 'cancel',
      type: 'modal',
      modalWidth: 520,
      label: 'Cancel',
      icon: XMarkIcon,
      danger: true,
      disabled: (objectData) => {
        return objectData?.state?.type == 'cancelled'
      },
      visible: (objectData) => {
        return (
          objectData?.state?.type != 'draft' &&
          objectData?.state?.type != 'completed' &&
          objectData?.state?.type != 'received'
        )
      },
      content: (objectData, { onOk } = {}) => {
        return createElement(CancelPurchaseOrder, { objectData, onOk })
      }
    }
  ],
  pages: [
    {
      name: 'info',
      content: () => createElement(PurchaseOrderInfo)
    }
  ],
  group: ['vendor'],
  filters: [
    'vendor',
    'state',
    'value',
    'vendor._id',
    'totalAmount',
    'totalAmountWithTax',
    'totalTaxAmount',
    'shippingAmount',
    'shippingAmountWithTax',
    'grandTotalAmount',
    'createdAt',
    'updatedAt',
    '_reference'
  ],
  sorters: [
    'createdAt',
    'state',
    'updatedAt',
    'totalAmount',
    'totalAmountWithTax',
    'totalTaxAmount',
    'shippingAmount',
    'shippingAmountWithTax',
    'grandTotalAmount'
  ],
  columns: [
    '_reference',
    'state',
    'vendor',
    'totalAmount',
    'totalAmountWithTax',
    'totalTaxAmount',
    'shippingAmount',
    'shippingAmountWithTax',
    'grandTotalAmount',
    'createdAt',
    'updatedAt',
    'vendor'
  ],
  properties: [
    {
      name: '_id',
      label: 'ID',
      type: 'id',
      columnFixed: 'left',
      objectType: 'purchaseOrder',
      columnWidth: 140,
      showCopy: true
    },
    {
      name: 'createdAt',
      label: 'Created At',
      type: 'dateTime',
      readOnly: true,
      columnWidth: 175
    },
    {
      name: '_reference',
      label: 'Reference',
      type: 'reference',
      columnFixed: 'left',
      required: true,
      objectType: 'purchaseOrder',
      showCopy: true,
      readOnly: true,
      columnWidth: 180
    },
    {
      name: 'updatedAt',
      label: 'Updated At',
      type: 'dateTime',
      readOnly: true,
      columnWidth: 175
    },
    {
      name: 'state',
      label: 'State',
      type: 'state',
      readOnly: true,
      columnWidth: 260
    },
    {
      name: 'postedAt',
      label: 'Posted At',
      type: 'dateTime',
      readOnly: true,
      columnWidth: 175
    },
    {
      name: 'vendor',
      label: 'Vendor',
      required: true,
      type: 'object',
      objectType: 'vendor',
      showHyperlink: true,
      columnWidth: 200
    },
    {
      name: 'acknowledgedAt',
      label: 'Acknowledged At',
      type: 'dateTime',
      readOnly: true,
      columnWidth: 175
    },
    {
      name: 'totalTaxAmount',
      label: 'Total Tax Amount',
      type: 'number',
      prefix: '£',
      roundNumber: 2,
      readOnly: true,
      columnWidth: 185
    },
    {
      name: 'completedAt',
      label: 'Completed At',
      type: 'dateTime',
      readOnly: true,
      columnWidth: 175
    },
    {
      name: 'totalAmountWithTax',
      label: 'Total Amount w/ Tax',
      type: 'number',
      prefix: '£',
      readOnly: true,
      columnWidth: 215,
      roundNumber: 2
    },
    {
      name: 'shippingAmount',
      label: 'Shipping Amount',
      type: 'number',
      prefix: '£',
      roundNumber: 2,
      readOnly: true,
      columnWidth: 190
    },
    {
      name: 'shippingAmountWithTax',
      label: 'Shipping Amount w/ Tax',
      type: 'number',
      prefix: '£',
      readOnly: true,
      roundNumber: 2,
      columnWidth: 240
    },
    {
      name: 'totalAmount',
      label: 'Total Amount',
      type: 'number',
      prefix: '£',
      roundNumber: 2,
      readOnly: true,
      columnWidth: 170
    },
    {
      name: 'grandTotalAmount',
      label: 'Grand Total Amount',
      type: 'number',
      prefix: '£',
      roundNumber: 2,
      columnWidth: 215,
      readOnly: true
    }
  ],
  stats: [
    {
      name: 'draft.count',
      label: 'Draft',
      type: 'number',
      color: 'default'
    },
    {
      name: 'sent.count',
      label: 'Sent',
      type: 'number',
      color: 'cyan'
    },
    {
      name: 'acknowledged.count',
      label: 'Acknowledged',
      type: 'number',
      color: 'purple'
    },
    {
      name: 'partiallyShipped.count',
      label: 'Partially Shipped',
      type: 'number',
      color: 'processing'
    },
    {
      name: 'shipped.count',
      label: 'Shipped',
      type: 'number',
      color: 'processing'
    },
    {
      name: 'partiallyReceived.count',
      label: 'Partially Received',
      type: 'number',
      color: 'success'
    },
    {
      name: 'received.count',
      label: 'Received',
      type: 'number',
      color: 'success'
    }
  ]
}
