import React, { useState } from 'react'
import { OrderList, useLanguage, useOrder, ToastType, useToast } from 'ordering-components/native'
import { useTheme } from 'styled-components/native';
import { OText } from '../shared'
import { NotFoundSource } from '../NotFoundSource'
import { ItemWrap } from './styles'
import { OrdersOptionParams } from '../../types'
import {
Placeholder,
PlaceholderLine,
Fade
} from "rn-placeholder";
import { View } from 'react-native'
import moment from 'moment'
const OrdersOptionUI = (props: OrdersOptionParams) => {
const {
navigation,
activeOrders,
preOrders,
orderList,
pagination,
titleContent,
customArray,
onNavigationRedirect,
orderStatus,
loadMoreOrders,
loadOrders
} = props
const [, t] = useLanguage()
const [, { reorder }] = useOrder()
const [, { showToast }] = useToast()
const { loading, error, orders: values } = orderList
const theme = useTheme();
const imageFails = activeOrders
? theme.images.general.emptyActiveOrders
: theme.images.general.emptyPastOrders
const orders = customArray || values || []
const [reorderLoading, setReorderLoading] = useState(false)
const handleReorder = async (orderId: number) => {
setReorderLoading(true)
try {
const { error, result } = await reorder(orderId)
if (!error) {
onNavigationRedirect && onNavigationRedirect('CheckoutNavigator', { cartUuid: result.uuid })
setReorderLoading(false)
return
}
setReorderLoading(false)
} catch (err) {
showToast(ToastType.Error, t('ERROR', err.message))
setReorderLoading(false)
}
}
const formatDate = (dateStr: string) => {
return moment(dateStr).format('MMMM DD,YYYY - hh:mm a');
}
const getOrderStatus = (s: string) => {
const status = parseInt(s)
const orderStatus = [
{ key: 0, value: t('PENDING', 'Pending') },
{ key: 1, value: t('COMPLETED', 'Completed') },
{ key: 2, value: t('REJECTED', 'Rejected') },
{ key: 3, value: t('DRIVER_IN_BUSINESS', 'Driver in business') },
{ key: 4, value: t('PREPARATION_COMPLETED', 'Preparation Completed') },
{ key: 5, value: t('REJECTED_BY_BUSINESS', 'Rejected by business') },
{ key: 6, value: t('REJECTED_BY_DRIVER', 'Rejected by Driver') },
{ key: 7, value: t('ACCEPTED_BY_BUSINESS', 'Accepted by business') },
{ key: 8, value: t('ACCEPTED_BY_DRIVER', 'Accepted by driver') },
{ key: 9, value: t('PICK_UP_COMPLETED_BY_DRIVER', 'Pick up completed by driver') },
{ key: 10, value: t('PICK_UP_FAILED_BY_DRIVER', 'Pick up Failed by driver') },
{ key: 11, value: t('DELIVERY_COMPLETED_BY_DRIVER', 'Delivery completed by driver') },
{ key: 12, value: t('DELIVERY_FAILED_BY_DRIVER', 'Delivery Failed by driver') },
{ key: 13, value: t('PREORDER', 'PreOrder') },
{ key: 14, value: t('ORDER_NOT_READY', 'Order not ready') },
{ key: 15, value: t('ORDER_PICKEDUP_COMPLETED_BY_CUSTOMER', 'Order picked up completed by customer') },
{ key: 16, value: t('CANCELLED_BY_CUSTOMER', 'Cancelled by customer') },
{ key: 17, value: t('ORDER_NOT_PICKEDUP_BY_CUSTOMER', 'Order not picked up by customer') },
{ key: 18, value: t('DRIVER_ALMOST_ARRIVED_TO_BUSINESS', 'Driver almost arrived to business') },
{ key: 19, value: t('DRIVER_ALMOST_ARRIVED_TO_CUSTOMER', 'Driver almost arrived to customer') },
{ key: 20, value: t('ORDER_CUSTOMER_ALMOST_ARRIVED_BUSINESS', 'Customer almost arrived to business') },
{ key: 21, value: t('ORDER_CUSTOMER_ARRIVED_BUSINESS', 'Customer arrived to business') },
{ key: 22, value: t('ORDER_LOOKING_FOR_DRIVER', 'Looking for driver') },
{ key: 23, value: t('ORDER_DRIVER_ON_WAY', 'Driver on way') }
]
const objectStatus = orderStatus.find((o) => o.key === status)
return objectStatus && objectStatus
}
return (
<>
{/* {(orders.length > 0) && (
<>
{titleContent || (activeOrders
? t('ACTIVE', 'Active')
: preOrders
? t('PREORDERS', 'Preorders')
: t('PAST', 'Past'))}
>
)} */}
{!loading && orders.length === 0 && (
)}
{loading && (
<>
{[...Array(3)].map((item, i) => (
))}
>
)}
{!loading && !error && orders.length > 0 && (
orders.map((order: any) =>
{order?.business?.name}
{`${getOrderStatus(order.status)?.value} on ${formatDate(order?.created_at)}`}
)
)}
>
)
}
export const LastOrder = (props: OrdersOptionParams) => {
const MyOrdersProps = {
...props,
UIComponent: OrdersOptionUI,
orderStatus: [1, 2, 5, 6, 10, 11, 12, 16, 17],
useDefualtSessionManager: true,
paginationSettings: {
initialPage: 1,
pageSize: 3,
controlType: 'infinity'
}
}
return
}