import { createElement, lazy } from 'react'

const SalesOrderInfo = lazy(
  () => import('../../components/Dashboard/Sales/SalesOrders/SalesOrderInfo')
)
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 PostSalesOrder = lazy(
  () => import('../../components/Dashboard/Sales/SalesOrders/PostSalesOrder')
)
const ConfirmSalesOrder = lazy(
  () => import('../../components/Dashboard/Sales/SalesOrders/ConfirmSalesOrder')
)
const CancelSalesOrder = lazy(
  () => import('../../components/Dashboard/Sales/SalesOrders/CancelSalesOrder')
)
import SalesOrderIcon from '../../components/Icons/SalesOrderIcon'
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 SalesOrder = {
  name: 'salesOrder',
  label: 'Sales Order',
  labelPlural: 'Sales Orders',
  url: '/dashboard/sales/salesorders',
  prefix: 'SOR',
  icon: SalesOrderIcon,
  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: 'salesOrder',
            syncAmount: 'itemPrice'
          },
          onOk,
          reset: true
        })
      }
    },
    {
      name: 'newShipment',
      type: 'modal',
      modalWidth: 700,
      modalCentered: false,
      label: 'New Shipment',
      icon: PlusIcon,
      disabled: (objectData) => {
        return objectData?.state?.type != 'draft'
      },
      content: (objectData, { onOk } = {}) => {
        return createElement(NewShipment, {
          defaultValues: {
            orderType: 'salesOrder',
            order: { _id: objectData._id }
          },
          onOk,
          reset: true
        })
      }
    },
    {
      name: 'newInvoice',
      type: 'modal',
      modalWidth: 700,
      modalCentered: false,
      label: 'New Invoice',
      icon: PlusIcon,
      disabled: (objectData) => {
        const allowedStates = [
          'delivered',
          'sent',
          'confirmed',
          'shipped',
          'partiallyDelivered',
          'partiallyShipped'
        ]
        return !allowedStates.includes(objectData?.state?.type)
      },
      content: (objectData, { onOk } = {}) => {
        return createElement(NewInvoice, {
          defaultValues: { orderType: 'salesOrder', order: objectData },
          onOk,
          reset: true
        })
      }
    },
    { type: 'divider' },
    {
      name: 'post',
      type: 'modal',
      modalWidth: 520,
      modalCentered: true,
      label: 'Post',
      icon: CheckIcon,
      visible: (objectData) => {
        return objectData?.state?.type == 'draft'
      },
      content: (objectData, { onOk } = {}) => {
        return createElement(PostSalesOrder, { objectData, onOk })
      }
    },
    {
      name: 'confirm',
      type: 'modal',
      modalWidth: 520,
      label: 'Confirm',
      modalCentered: true,
      icon: CheckIcon,
      visible: (objectData) => {
        return objectData?.state?.type == 'sent'
      },
      content: (objectData, { onOk } = {}) => {
        return createElement(ConfirmSalesOrder, { objectData, onOk })
      }
    },
    {
      name: 'complete',
      type: 'page',
      pageName: 'info',
      label: 'Complete',
      icon: CheckIcon,
      disabled: (objectData) => {
        return objectData?.state?.type != 'delivered'
      },
      visible: (objectData) => {
        return objectData?.state?.type == 'delivered'
      }
    },
    {
      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 != 'delivered'
        )
      },
      content: (objectData, { onOk } = {}) => {
        return createElement(CancelSalesOrder, { objectData, onOk })
      }
    }
  ],
  pages: [
    {
      name: 'info',
      content: () => createElement(SalesOrderInfo)
    }
  ],
  group: ['client'],
  filters: [
    'client',
    'state',
    'value',
    'client._id',
    'createdAt',
    'updatedAt',
    '_reference'
  ],
  sorters: ['createdAt', 'state', 'updatedAt'],
  columns: [
    '_reference',
    'state',
    'client',
    'marketplace',
    'totalAmount',
    'totalAmountWithTax',
    'totalTaxAmount',
    'shippingAmount',
    'shippingAmountWithTax',
    'grandTotalAmount',
    'createdAt',
    'updatedAt',
    'client'
  ],
  properties: [
    {
      name: '_id',
      label: 'ID',
      type: 'id',
      columnFixed: 'left',
      objectType: 'salesOrder',
      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: 'salesOrder',
      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: 'client',
      label: 'Client',
      required: true,
      type: 'object',
      objectType: 'client',
      showHyperlink: true,
      columnWidth: 200
    },
    {
      name: 'marketplace',
      label: 'Marketplace',
      type: 'object',
      objectType: 'marketplace',
      showHyperlink: true,
      readOnly: false,
      required: false,
      columnWidth: 200
    },
    {
      name: 'confirmedAt',
      label: 'Confirmed At',
      type: 'dateTime',
      readOnly: true,
      columnWidth: 175
    },
    {
      name: 'totalTaxAmount',
      label: 'Total Tax Amount',
      type: 'number',
      prefix: '£',
      roundNumber: 2,
      readOnly: true,
      columnWidth: 175
    },
    {
      name: 'completedAt',
      label: 'Completed At',
      type: 'dateTime',
      readOnly: true,
      columnWidth: 175
    },
    {
      name: 'totalAmountWithTax',
      label: 'Total Amount w/ Tax',
      type: 'number',
      prefix: '£',
      readOnly: true,
      columnWidth: 175,
      roundNumber: 2
    },
    {
      name: 'shippingAmount',
      label: 'Shipping Amount',
      type: 'number',
      prefix: '£',
      roundNumber: 2,
      readOnly: true,
      columnWidth: 150
    },
    {
      name: 'shippingAmountWithTax',
      label: 'Shipping Amount w/ Tax',
      type: 'number',
      prefix: '£',
      readOnly: true,
      roundNumber: 2,
      columnWidth: 200
    },
    {
      name: 'totalAmount',
      label: 'Total Amount',
      type: 'number',
      prefix: '£',
      roundNumber: 2,
      readOnly: true,
      columnWidth: 150
    },
    {
      name: 'grandTotalAmount',
      label: 'Grand Total Amount',
      type: 'number',
      prefix: '£',
      roundNumber: 2,
      columnWidth: 175,
      readOnly: true
    }
  ],
  stats: [
    {
      name: 'draft.count',
      label: 'Draft',
      type: 'number',
      color: 'default'
    },
    {
      name: 'sent.count',
      label: 'Sent',
      type: 'number',
      color: 'cyan'
    },
    {
      name: 'confirmed.count',
      label: 'Confirmed',
      type: 'number',
      color: 'purple'
    },
    {
      name: 'partiallyShipped.count',
      label: 'Partially Shipped',
      type: 'number',
      color: 'processing'
    },
    {
      name: 'shipped.count',
      label: 'Shipped',
      type: 'number',
      color: 'processing'
    },
    {
      name: 'partiallyDelivered.count',
      label: 'Partially Delivered',
      type: 'number',
      color: 'success'
    },
    {
      name: 'delivered.count',
      label: 'Delivered',
      type: 'number',
      color: 'success'
    }
  ]
}
