/*
 * @Author: ZhaoYing 
 * @Date: 2026-02-03 17:35:49 
 * @Last Modified by:   ZhaoYing 
 * @Last Modified time: 2026-02-03 17:35:49 
 */
/**
 * Order Detail Component
 * Modal displaying detailed order information including payment details
 */

import { forwardRef, useImperativeHandle, useState, useCallback } from 'react';
import { Descriptions } from 'antd';
import { useTranslation } from 'react-i18next';
import dayjs from 'dayjs';

import type { Order, OrderDetailRef } from '../types'
import RbModal from '@/components/RbModal'
import { getOrderDetail } from '@/api/order'
import { STATUS } from '../index';


const OrderDetail = forwardRef<OrderDetailRef, { getProductType: (type: string) => void; }>(({ getProductType }, ref) => {
  const { t } = useTranslation();
  const [visible, setVisible] = useState(false);
  const [data, setData] = useState({})

  /** Close modal */
  const handleClose = () => {
    setVisible(false);
  };

  /** Open modal and fetch order details */
  const handleOpen = (order: Order) => {
    setVisible(true);
    getOrderDetail(order.order_no)
      .then(res => {
        setData(res as Order)
      })
  };
  /** Format order information items */
  const formatItems = useCallback(() => {
    if (!data) return []
    return ['order_no', 'product_type', 'payable_amount', 'status', 'pay_time', 'create_time'].map(key => {
      const value = (data as any)[key]
      return {
        key,
        label: t(`pricing.${key}`),
        children: ['pay_time', 'create_time'].includes(key) && value
          ? dayjs(value).format('YYYY-MM-DD HH:mm:ss')
          : key === 'status' && value
            ? t(`pricing.${STATUS[value as keyof typeof STATUS].key}`)
            : key === 'product_type' && value
              ? t(`pricing.${getProductType(value)}.type`)
              : value
      }
    })
  }, [data])
  /** Format payment information items */
  const formatPayItems = useCallback(() => {
    if (!data) return []
    return ['pay_txn_id', 'payer'].map(key => ({
      key,
      label: t(`pricing.${key}`),
      children: (data as any)[key]
    }))
  }, [data])

  /** Expose methods to parent component */
  useImperativeHandle(ref, () => ({
    handleOpen,
    handleClose
  }));

  return (
    <RbModal
      title={t('pricing.orderDetail')}
      open={visible}
      footer={null}
      onCancel={handleClose}
      width={1000}
    >
      <Descriptions title={t('pricing.orderInfo')} column={2} items={formatItems()} classNames={{ label: 'rb:w-50' }} />
      <Descriptions title={t('pricing.orderPayInfo')} column={2} items={formatPayItems()} classNames={{ label: 'rb:w-50' }} className="rb:mt-6!" />
    </RbModal>
  );
});

export default OrderDetail;