import React, { useState, useEffect, useCallback, useRef } from 'react';
import { useParams, useNavigate, useSearchParams } from 'react-router-dom';
import {
  FaArrowLeft, FaSave, FaEdit, FaTimes, FaPlus, FaTrash,
  FaSpinner, FaShoppingBag, FaPaperPlane,
  FaChevronDown, FaChevronRight, FaPencilAlt,
  FaFileInvoice,
} from 'react-icons/fa';
import { toast, ToastContainer } from 'react-toastify';
import 'react-toastify/dist/ReactToastify.css';
import purchaseOrderService, { PurchaseOrder, PurchaseOrderItem, PurchaseTaxCharge } from '../services/purchaseOrderService';
import LinkField from '../components/LinkField';
import ActivityLog from '../components/ActivityLog';
import { DEFAULT_COMPANY, DEFAULT_CURRENCY, displayTxnCurrency } from '../constants/orgDefaults';

// ── Shared helpers ────────────────────────────────────────────────────────────
const FL: React.FC<{ children: React.ReactNode; required?: boolean }> = ({ children, required }) => (
  <label className="block text-[11px] font-semibold text-gray-500 dark:text-gray-400 uppercase tracking-wide mb-1">
    {children}{required && <span className="text-red-500 ml-0.5">*</span>}
  </label>
);
const RV: React.FC<{ children?: React.ReactNode }> = ({ children }) => (
  <div className="px-3 py-2 text-sm text-gray-800 dark:text-gray-200 bg-gray-50 dark:bg-gray-800/60 rounded min-h-[34px] flex items-center">
    {children || <span className="text-gray-400">-</span>}
  </div>
);
const inputCls  = 'w-full px-3 py-2 text-sm border border-gray-300 dark:border-gray-600 rounded bg-white dark:bg-gray-700 text-gray-900 dark:text-white focus:outline-none focus:ring-1 focus:ring-amber-400';
const numCls    = inputCls + ' text-right';
const inlineNum = 'w-full px-2 py-1 text-sm text-right border border-gray-300 dark:border-gray-600 rounded bg-white dark:bg-gray-700 focus:outline-none focus:ring-1 focus:ring-amber-400';
const inlineTxt = 'w-full px-2 py-1 text-sm border border-gray-300 dark:border-gray-600 rounded bg-white dark:bg-gray-700 focus:outline-none focus:ring-1 focus:ring-amber-400';

// ── Create Dropdown ───────────────────────────────────────────────────────────
const CreateDropdown: React.FC<{
  items: { label: string; icon: React.ReactNode; onClick: () => void }[];
}> = ({ items }) => {
  const [open, setOpen] = useState(false);
  const ref = useRef<HTMLDivElement>(null);

  useEffect(() => {
    const handler = (e: MouseEvent) => {
      if (ref.current && !ref.current.contains(e.target as Node)) setOpen(false);
    };
    document.addEventListener('mousedown', handler);
    return () => document.removeEventListener('mousedown', handler);
  }, []);

  return (
    <div className="relative" ref={ref}>
      <button
        onClick={() => setOpen(o => !o)}
        className="flex items-center gap-1.5 px-4 py-2 bg-amber-500 text-white rounded-lg hover:bg-amber-600 text-sm font-medium shadow-sm"
      >
        Create
        <FaChevronDown size={10} className={`transition-transform ${open ? 'rotate-180' : ''}`} />
      </button>
      {open && (
        <div className="absolute right-0 mt-1 w-52 bg-white dark:bg-gray-800 border border-gray-200 dark:border-gray-700 rounded-xl shadow-xl z-50 py-1.5 overflow-hidden">
          <div className="px-3 py-1.5 text-[10px] font-bold text-gray-400 dark:text-gray-500 uppercase tracking-wider border-b border-gray-100 dark:border-gray-700 mb-1">
            Create from this order
          </div>
          {items.map(({ label, icon, onClick }) => (
            <button
              key={label}
              onClick={() => { onClick(); setOpen(false); }}
              className="w-full flex items-center gap-3 px-4 py-2.5 text-sm text-gray-700 dark:text-gray-300 hover:bg-amber-50 dark:hover:bg-amber-900/20 hover:text-amber-700 dark:hover:text-amber-300 transition-colors text-left"
            >
              <span className="text-gray-400">{icon}</span>
              {label}
            </button>
          ))}
        </div>
      )}
    </div>
  );
};

// ── Collapsible group inside row editor ───────────────────────────────────────
const RGroup: React.FC<{ title: string; children: React.ReactNode; defaultOpen?: boolean }> = ({ title, children, defaultOpen = false }) => {
  const [open, setOpen] = useState(defaultOpen);
  return (
    <div className="border-t border-gray-200 dark:border-gray-600 mt-3 pt-1">
      <button type="button" onClick={() => setOpen(o => !o)}
        className="flex items-center gap-2 py-1 text-xs font-semibold text-amber-600 dark:text-amber-400 hover:underline">
        {open ? <FaChevronDown size={9} /> : <FaChevronRight size={9} />}{title}
      </button>
      {open && <div className="mt-2">{children}</div>}
    </div>
  );
};

// ── Item Row Editor ───────────────────────────────────────────────────────────
const POItemRowEditor: React.FC<{
  item: Partial<PurchaseOrderItem>; rowNo: number;
  onChange: (k: keyof PurchaseOrderItem, v: any) => void;
  onClose: () => void; onDelete: () => void; onInsertBelow: () => void;
}> = ({ item, rowNo, onChange, onClose, onDelete, onInsertBelow }) => (
  <tr>
    <td colSpan={7} className="p-0">
      <div className="border border-amber-300 dark:border-amber-600 rounded-lg mx-2 my-1 bg-white dark:bg-gray-800 shadow-md">
        {/* Editor header */}
        <div className="flex items-center justify-between px-4 py-2 bg-amber-50 dark:bg-amber-900/30 rounded-t-lg border-b border-amber-200 dark:border-amber-700">
          <span className="text-sm font-semibold text-amber-700 dark:text-amber-300">Editing Row #{rowNo}</span>
          <div className="flex items-center gap-2">
            <button onClick={onInsertBelow} className="px-2 py-1 text-xs border border-gray-300 rounded hover:bg-gray-50 dark:hover:bg-gray-700 text-gray-600">Insert Below</button>
            <button onClick={onDelete} className="px-2 py-1 text-xs border border-red-300 rounded text-red-500 hover:bg-red-50">Delete</button>
            <button onClick={onClose} className="p-1 text-gray-400 hover:text-gray-600"><FaTimes size={13} /></button>
          </div>
        </div>

        <div className="p-4 space-y-4">
          {/* Row 1: Item Code + Schedule Date */}
          <div className="grid grid-cols-2 gap-4">
            <div><FL required>Item Code</FL>
              <LinkField label="Item" hideLabel doctype="Item" value={item.item_code || ''} onChange={v => onChange('item_code', v)} placeholder="Select item…" />
            </div>
            <div><FL>Schedule Date</FL>
              <input type="date" value={item.schedule_date || ''} onChange={e => onChange('schedule_date', e.target.value)} className={inputCls} />
            </div>
          </div>

          {/* Item Name */}
          <div><FL required>Item Name</FL>
            <input value={item.item_name || ''} onChange={e => onChange('item_name', e.target.value)} className={inputCls} placeholder="Item name…" />
          </div>

          {/* Description */}
          <RGroup title="Description" defaultOpen={!!(item.description)}>
            <textarea rows={2} value={item.description || ''} onChange={e => onChange('description', e.target.value)} className={inputCls} placeholder="Description…" />
          </RGroup>

          {/* Quantity and Rate */}
          <RGroup title="Quantity and Rate" defaultOpen>
            <div className="grid grid-cols-3 gap-3">
              <div><FL required>Quantity</FL>
                <input type="number" min={0} step="1" value={item.qty ?? 0} onChange={e => onChange('qty', parseFloat(e.target.value) || 0)} className={numCls} />
              </div>
              <div><FL required>UOM</FL>
                <LinkField label="UOM" hideLabel doctype="UOM" value={item.uom || ''} onChange={v => onChange('uom', v)} placeholder="UOM…" />
              </div>
              <div><FL>Stock UOM</FL>
                <LinkField label="Stock UOM" hideLabel doctype="UOM" value={item.stock_uom || ''} onChange={v => onChange('stock_uom', v)} placeholder="Stock UOM…" />
              </div>
              <div><FL>UOM Conversion Factor</FL>
                <input type="number" min={0} step="0.001" value={item.conversion_factor ?? 1} onChange={e => onChange('conversion_factor', parseFloat(e.target.value) || 1)} className={numCls} />
              </div>
              <div><FL>Stock Qty (auto)</FL>
                <div className="px-3 py-2 text-sm text-gray-600 bg-gray-50 dark:bg-gray-700 rounded text-right">
                  {((item.qty || 0) * (item.conversion_factor || 1)).toFixed(3)}
                </div>
              </div>
              <div><FL>Price List Rate</FL>
                <input type="number" min={0} step="0.01" value={item.price_list_rate ?? 0} onChange={e => onChange('price_list_rate', parseFloat(e.target.value) || 0)} className={numCls} />
              </div>
              <div><FL>Last Purchase Rate</FL>
                <div className="px-3 py-2 text-sm text-right bg-gray-50 dark:bg-gray-700 rounded">{(item.last_purchase_rate ?? 0).toFixed(2)}</div>
              </div>
            </div>
          </RGroup>

          {/* Discount and Margin */}
          <RGroup title="Discount and Margin">
            <div className="grid grid-cols-3 gap-3">
              <div><FL required>Rate</FL>
                <input type="number" min={0} step="0.01" value={item.rate ?? 0} onChange={e => onChange('rate', parseFloat(e.target.value) || 0)} className={numCls} />
              </div>
              <div><FL>Amount (auto)</FL>
                <div className="px-3 py-2 text-sm font-semibold text-right bg-gray-50 dark:bg-gray-700 rounded">
                  {((item.qty || 0) * (item.rate || 0)).toFixed(2)}
                </div>
              </div>
              <div className="flex flex-col justify-end gap-2 pb-1">
                <label className="flex items-center gap-2 cursor-pointer">
                  <input type="checkbox" checked={!!(item.is_free_item)} onChange={e => onChange('is_free_item', e.target.checked ? 1 : 0)} className="rounded" />
                  <span className="text-xs text-gray-600 dark:text-gray-400">Is Free Item</span>
                </label>
              </div>
            </div>
          </RGroup>

          {/* Warehouse and Reference */}
          <RGroup title="Warehouse and Reference">
            <div className="grid grid-cols-2 gap-3">
              <div><FL>Warehouse</FL>
                <LinkField label="Warehouse" hideLabel doctype="Warehouse" value={item.warehouse || ''} onChange={v => onChange('warehouse', v)} placeholder="Warehouse…" />
              </div>
              <div className="flex items-end pb-2">
                <label className="flex items-center gap-2 cursor-pointer">
                  <input type="checkbox" checked={!!(item.against_blanket_order)} onChange={e => onChange('against_blanket_order', e.target.checked ? 1 : 0)} className="rounded" />
                  <span className="text-xs text-gray-600 dark:text-gray-400">Against Blanket Order</span>
                </label>
              </div>
              {item.material_request && (
                <div><FL>Material Request</FL>
                  <RV>{item.material_request}</RV>
                </div>
              )}
            </div>
          </RGroup>

          {/* Available Quantity */}
          <RGroup title="Available Quantity">
            <div className="grid grid-cols-2 gap-3">
              <div><FL>Actual Qty (Warehouse)</FL>
                <div className="px-3 py-2 text-sm text-right bg-gray-50 dark:bg-gray-700 rounded">{item.actual_qty ?? 0}</div>
              </div>
              <div><FL>Company Total Stock</FL>
                <div className="px-3 py-2 text-sm text-right bg-gray-50 dark:bg-gray-700 rounded">{item.company_total_stock ?? 0}</div>
              </div>
            </div>
          </RGroup>

          {/* Accounting Details */}
          <RGroup title="Accounting Details">
            <div><FL>Expense Account</FL>
              <LinkField label="Account" hideLabel doctype="Account" value={item.expense_account || ''} onChange={v => onChange('expense_account', v)} placeholder="Expense account…" />
            </div>
          </RGroup>

          {/* Accounting Dimensions */}
          <RGroup title="Accounting Dimensions">
            <div className="grid grid-cols-2 gap-3">
              <div><FL>Cost Center</FL>
                <LinkField label="Cost Center" hideLabel doctype="Cost Center" value={item.cost_center || ''} onChange={v => onChange('cost_center', v)} placeholder="Cost center…" />
              </div>
              <div><FL>Project</FL>
                <LinkField label="Project" hideLabel doctype="Project" value={item.project || ''} onChange={v => onChange('project', v)} placeholder="Project…" />
              </div>
            </div>
          </RGroup>

          {/* Item Weight Details */}
          <RGroup title="Item Weight Details">
            <div className="grid grid-cols-2 gap-3">
              <div><FL>Weight Per Unit</FL>
                <input type="number" min={0} step="0.001" value={item.weight_per_unit ?? 0} onChange={e => onChange('weight_per_unit', parseFloat(e.target.value) || 0)} className={numCls} />
              </div>
              <div><FL>Total Weight (auto)</FL>
                <div className="px-3 py-2 text-sm text-right bg-gray-50 dark:bg-gray-700 rounded">
                  {((item.qty || 0) * (item.weight_per_unit || 0)).toFixed(3)}
                </div>
              </div>
            </div>
          </RGroup>
        </div>
      </div>
    </td>
  </tr>
);

// ── Tax Row Editor ────────────────────────────────────────────────────────────
const POTaxRowEditor: React.FC<{
  tax: Partial<PurchaseTaxCharge>; rowNo: number;
  onChange: (k: keyof PurchaseTaxCharge, v: any) => void;
  onClose: () => void; onDelete: () => void; onInsertBelow: () => void;
}> = ({ tax, rowNo, onChange, onClose, onDelete, onInsertBelow }) => (
  <tr>
    <td colSpan={6} className="p-0">
      <div className="border border-amber-300 dark:border-amber-600 rounded-lg mx-2 my-1 bg-white dark:bg-gray-800 shadow-md">
        <div className="flex items-center justify-between px-4 py-2 bg-amber-50 dark:bg-amber-900/30 rounded-t-lg border-b border-amber-200">
          <span className="text-sm font-semibold text-amber-700 dark:text-amber-300">Editing Row #{rowNo}</span>
          <div className="flex items-center gap-2">
            <button onClick={onInsertBelow} className="px-2 py-1 text-xs border border-gray-300 rounded hover:bg-gray-50 text-gray-600">Insert Below</button>
            <button onClick={onDelete} className="px-2 py-1 text-xs border border-red-300 rounded text-red-500 hover:bg-red-50">Delete</button>
            <button onClick={onClose} className="p-1 text-gray-400 hover:text-gray-600"><FaTimes size={13} /></button>
          </div>
        </div>
        <div className="p-4 space-y-4">
          <div className="grid grid-cols-2 gap-4">
            <div><FL required>Type</FL>
              <select value={tax.charge_type || ''} onChange={e => onChange('charge_type', e.target.value)} className={inputCls}>
                <option value="">Select type…</option>
                <option value="Actual">Actual</option>
                <option value="On Net Total">On Net Total</option>
                <option value="On Previous Row Amount">On Previous Row Amount</option>
              </select>
            </div>
            <div><FL>Description</FL>
              <textarea rows={3} value={tax.description || ''} onChange={e => onChange('description', e.target.value)} className={inputCls} placeholder="Description…" />
            </div>
          </div>
          <div><FL required>Account Head</FL>
            <LinkField label="Account Head" hideLabel doctype="Account" value={tax.account_head || ''} onChange={v => onChange('account_head', v)} placeholder="Account…" />
          </div>
          <div className="flex items-center gap-2">
            <input type="checkbox" checked={!!(tax.included_in_print_rate)} onChange={e => onChange('included_in_print_rate', e.target.checked ? 1 : 0)} className="rounded" />
            <span className="text-xs text-gray-600 dark:text-gray-400">Is this Tax included in Basic Rate?</span>
          </div>
          <p className="text-xs text-amber-500 dark:text-amber-400">If checked, the tax amount will be considered as already included in the Print Rate / Print Amount</p>

          <RGroup title="Accounting Dimensions" defaultOpen>
            <div><FL>Cost Center</FL>
              <LinkField label="Cost Center" hideLabel doctype="Cost Center" value={tax.cost_center || ''} onChange={v => onChange('cost_center', v)} placeholder="Cost center…" />
            </div>
          </RGroup>

          <div className="grid grid-cols-2 gap-4">
            <div><FL required>Tax Rate</FL>
              <input type="number" min={0} step="0.01" value={tax.rate ?? 0} onChange={e => onChange('rate', parseFloat(e.target.value) || 0)} className={numCls} />
            </div>
            <div><FL>Account Currency</FL>
              <input value={tax.account_currency || DEFAULT_CURRENCY} onChange={e => onChange('account_currency', e.target.value)} className={inputCls} />
            </div>
          </div>
        </div>
      </div>
    </td>
  </tr>
);

// ── Page ──────────────────────────────────────────────────────────────────────
const emptyItem = (): Partial<PurchaseOrderItem> => ({ item_code: '', item_name: '', qty: 1, rate: 0, amount: 0, uom: '', conversion_factor: 1, is_free_item: 0 });
const emptyTax  = (): Partial<PurchaseTaxCharge>  => ({ charge_type: '', account_head: '', rate: 0, account_currency: DEFAULT_CURRENCY, included_in_print_rate: 0 });

const PurchaseOrderDetail: React.FC = () => {
  const { poName } = useParams<{ poName: string }>();
  const [searchParams] = useSearchParams();
  const navigate = useNavigate();
  const isNew = poName === 'new';

  const contextProject  = searchParams.get('project')  || '';
  const contextSupplier = searchParams.get('supplier') || '';
  const contextCompany  = searchParams.get('company')  || DEFAULT_COMPANY;
  const contextMR       = searchParams.get('mr')       || '';

  const [doc, setDoc]             = useState<PurchaseOrder | null>(null);
  const [loading, setLoading]     = useState(!isNew);
  const [saving, setSaving]       = useState(false);
  const [submitting, setSubmitting] = useState(false);
  const [isEditing, setIsEditing] = useState(isNew);
  const [expandedItem, setExpandedItem] = useState<number | null>(null);
  const [expandedTax,  setExpandedTax]  = useState<number | null>(null);

  const today = new Date().toISOString().split('T')[0];

  const [form, setForm] = useState<Partial<PurchaseOrder>>({
    supplier: contextSupplier, supplier_name: contextSupplier,
    company: contextCompany, project: contextProject,
    transaction_date: today, schedule_date: today, currency: DEFAULT_CURRENCY,
    items: [], taxes: [],
  });

  const syncForm = useCallback((d: PurchaseOrder) => {
    setForm({
      supplier: d.supplier || '', supplier_name: d.supplier_name || d.supplier || '',
      company: d.company || DEFAULT_COMPANY, project: d.project || '',
      transaction_date: d.transaction_date || today,
      schedule_date: d.schedule_date || '',
      currency: d.currency || DEFAULT_CURRENCY,
      cost_center: d.cost_center || '',
      set_warehouse: d.set_warehouse || '',
      tax_category: (d as any).tax_category || '',
      taxes_and_charges: (d as any).taxes_and_charges || '',
      items: d.items || [], taxes: d.taxes || [],
    });
    setExpandedItem(null); setExpandedTax(null);
  }, [today]);

  // Pre-fill from MR when creating a new PO from Material Request context
  useEffect(() => {
    if (!isNew || !contextMR) return;
    fetch(`/api/resource/Material Request/${encodeURIComponent(contextMR)}`, { credentials: 'include' })
      .then(r => r.json()).then(body => {
        const mr = body.data;
        if (!mr) return;
        setForm(f => ({
          ...f,
          company:  mr.company  || f.company,
          project:  mr.project  || f.project,
          schedule_date: mr.transaction_date || f.schedule_date,
          items: (mr.items || []).map((it: any) => ({
            item_code: it.item_code, item_name: it.item_name,
            description: it.description,
            qty: it.qty, uom: it.uom, stock_uom: it.stock_uom,
            rate: 0, amount: 0,
            schedule_date: it.required_by || f.schedule_date,
            material_request: contextMR,
            material_request_item: it.name,
            warehouse: it.warehouse,
            project: it.project || mr.project,
            cost_center: it.cost_center,
          })),
        }));
      }).catch(() => {});
  }, [isNew, contextMR]);

  useEffect(() => {
    if (isNew) return;
    setLoading(true);
    purchaseOrderService.getPurchaseOrder(poName!)
      .then(d => { setDoc(d); syncForm(d); })
      .catch(e => toast.error(e.message))
      .finally(() => setLoading(false));
  }, [poName, isNew, syncForm]);

  // Auto-fetch company default currency for new documents
  useEffect(() => {
    const company = form.company;
    if (!isNew || !company) return;
    fetch(`/api/resource/Company/${encodeURIComponent(company)}`, { credentials: 'include' })
      .then(r => r.json()).then(b => {
        if (b.data?.default_currency) {
          const cur = displayTxnCurrency(b.data.default_currency);
          setForm(f => ({ ...f, currency: cur,
            buying_price_list: (f as any).buying_price_list || 'Standard Buying',
            price_list_currency: (f as any).price_list_currency || cur,
          } as any));
        }
      }).catch(() => {});
  }, [form.company, isNew]);

  const set = (k: keyof PurchaseOrder, v: any) => setForm(f => ({ ...f, [k]: v }));

  // ── Item helpers ──────────────────────────────────────────────────────────
  const updateItem = (idx: number, k: keyof PurchaseOrderItem, v: any) =>
    setForm(f => {
      const items = [...(f.items || [])];
      const updated = { ...items[idx], [k]: v };
      if (k === 'qty' || k === 'rate') {
        const qty  = parseFloat(String(k === 'qty'  ? v : updated.qty))  || 0;
        const rate = parseFloat(String(k === 'rate' ? v : updated.rate)) || 0;
        updated.amount = parseFloat((qty * rate).toFixed(4));
      }
      if (k === 'qty' || k === 'conversion_factor') {
        const qty = parseFloat(String(k === 'qty' ? v : updated.qty)) || 0;
        const cf  = parseFloat(String(k === 'conversion_factor' ? v : updated.conversion_factor)) || 1;
        updated.stock_qty = parseFloat((qty * cf).toFixed(4));
      }
      items[idx] = updated;
      return { ...f, items };
    });

  const handleItemCode = async (idx: number, code: string) => {
    updateItem(idx, 'item_code', code);
    if (!code) return;
    try {
      const r = await fetch(`/api/resource/Item/${encodeURIComponent(code)}`, { credentials: 'include' });
      const body = await r.json(); const d = body.data; if (!d) return;
      setForm(f => {
        const items = [...(f.items || [])];
        items[idx] = {
          ...items[idx],
          item_code: code,
          item_name: d.item_name || code,
          description: d.description || d.item_name || code,
          stock_uom: d.stock_uom || '',
          uom: d.purchase_uom || d.stock_uom || '',
          price_list_rate: d.standard_rate ?? 0,
          rate: items[idx].rate || d.standard_rate || 0,
        };
        return { ...f, items };
      });
    } catch { /* ignore */ }
  };

  const addItem = (afterIdx?: number) => {
    setForm(f => {
      const items = [...(f.items || [])];
      const newItem = emptyItem();
      let newIdx: number;
      if (afterIdx !== undefined) { items.splice(afterIdx + 1, 0, newItem); newIdx = afterIdx + 1; }
      else { items.push(newItem); newIdx = items.length - 1; }
      setTimeout(() => setExpandedItem(newIdx), 0);
      return { ...f, items };
    });
  };
  const removeItem = (idx: number) => { setForm(f => { const items = [...(f.items || [])]; items.splice(idx, 1); return { ...f, items }; }); setExpandedItem(null); };

  // ── Tax helpers ───────────────────────────────────────────────────────────
  const updateTax = (idx: number, k: keyof PurchaseTaxCharge, v: any) =>
    setForm(f => { const taxes = [...(f.taxes || [])]; taxes[idx] = { ...taxes[idx], [k]: v }; return { ...f, taxes }; });

  const addTax = (afterIdx?: number) => {
    setForm(f => {
      const taxes = [...(f.taxes || [])];
      const newTax = emptyTax();
      let newIdx: number;
      if (afterIdx !== undefined) { taxes.splice(afterIdx + 1, 0, newTax); newIdx = afterIdx + 1; }
      else { taxes.push(newTax); newIdx = taxes.length - 1; }
      setTimeout(() => setExpandedTax(newIdx), 0);
      return { ...f, taxes };
    });
  };
  const removeTax = (idx: number) => { setForm(f => { const taxes = [...(f.taxes || [])]; taxes.splice(idx, 1); return { ...f, taxes }; }); setExpandedTax(null); };

  // ── Computed ──────────────────────────────────────────────────────────────
  const netTotal = (form.items || []).reduce((s, it) => s + ((it.qty || 0) * (it.rate || 0)), 0);
  const taxTotal = (form.taxes || []).reduce((s, tx) => {
    if (tx.charge_type === 'On Net Total') return s + ((tx.rate || 0) / 100) * netTotal;
    return s + (tx.tax_amount || 0);
  }, 0);
  const grandTotal = netTotal + taxTotal;

  // ── Payload ───────────────────────────────────────────────────────────────
  const loadTaxTemplate = async (templateName: string) => {
    if (!templateName) return;
    try {
      const r = await fetch(`/api/resource/Purchase Taxes and Charges Template/${encodeURIComponent(templateName)}`, { credentials: 'include' });
      const body = await r.json();
      const tmpl = body.data;
      if (tmpl?.taxes?.length) {
        setForm(f => ({ ...f, taxes: tmpl.taxes.map((tx: any) => ({
          charge_type: tx.charge_type, account_head: tx.account_head,
          description: tx.description, rate: tx.rate,
          cost_center: tx.cost_center, account_currency: tx.account_currency,
          included_in_print_rate: tx.included_in_print_rate ?? 0,
        })) }));
      }
    } catch { /* ignore */ }
  };

  const buildPayload = (): Partial<PurchaseOrder> => ({
    supplier: form.supplier, company: form.company || undefined,
    project: form.project || undefined, cost_center: form.cost_center || undefined,
    transaction_date: form.transaction_date,
    schedule_date: form.schedule_date || undefined,
    currency: form.currency || undefined,
    set_warehouse: form.set_warehouse || undefined,
    tax_category: (form as any).tax_category || undefined,
    taxes_and_charges: (form as any).taxes_and_charges || undefined,
    items: (form.items || []).filter(it => it.item_code).map((it, i) => ({
      item_code: it.item_code, item_name: it.item_name || it.item_code,
      description: it.description || it.item_name || it.item_code,
      qty: it.qty ?? 1, uom: it.uom || undefined, stock_uom: it.stock_uom || undefined,
      conversion_factor: it.conversion_factor ?? 1,
      rate: it.rate ?? 0, amount: (it.qty || 0) * (it.rate || 0),
      price_list_rate: it.price_list_rate ?? 0,
      schedule_date: it.schedule_date || form.schedule_date || undefined,
      is_free_item: it.is_free_item ?? 0,
      warehouse: it.warehouse || form.set_warehouse || undefined,
      expense_account: it.expense_account || undefined,
      against_blanket_order: it.against_blanket_order ?? 0,
      weight_per_unit: it.weight_per_unit || undefined,
      project: it.project || form.project || undefined,
      cost_center: it.cost_center || form.cost_center || undefined,
      idx: i + 1,
    })),
    taxes: (form.taxes || []).filter(tx => tx.charge_type).map((tx, i) => ({
      charge_type: tx.charge_type, account_head: tx.account_head || undefined,
      description: tx.description || undefined,
      included_in_print_rate: tx.included_in_print_rate ?? 0,
      cost_center: tx.cost_center || undefined, rate: tx.rate ?? 0,
      account_currency: tx.account_currency || DEFAULT_CURRENCY, idx: i + 1,
    })),
  });

  const handleSave = async () => {
    if (!form.supplier) { toast.error('Supplier is required'); return; }
    try {
      setSaving(true);
      if (isNew) {
        const created = await purchaseOrderService.createPurchaseOrder(buildPayload());
        toast.success('Purchase Order created');
        setIsEditing(false);
        navigate(`/purchase-orders/${created.name}`);
      } else {
        const updated = await purchaseOrderService.updatePurchaseOrder(poName!, buildPayload());
        setDoc(updated); syncForm(updated);
        toast.success('Purchase Order saved');
        setIsEditing(false);
      }
    } catch (e: any) { toast.error(e.message || 'Error saving'); }
    finally { setSaving(false); }
  };

  const handleSubmit = async () => {
    if (!poName || isNew) return;
    try {
      setSubmitting(true);
      const updated = await purchaseOrderService.submitPurchaseOrder(poName);
      setDoc(updated); syncForm(updated);
      toast.success('Purchase Order submitted');
    } catch (e: any) { toast.error(e.message || 'Error submitting'); }
    finally { setSubmitting(false); }
  };

  const createPR = () => {
    const p = new URLSearchParams();
    p.set('po', poName!);
    if (form.supplier) p.set('supplier', form.supplier);
    if (form.company)  p.set('company',  String(form.company));
    if (form.project)  p.set('project',  String(form.project));
    navigate(`/purchase-receipts/new?${p.toString()}`);
  };

  const editable = isNew || isEditing;
  const isSubmitted = !isNew && doc?.docstatus === 1;
  const title = isNew ? 'New Purchase Order' : (form.supplier_name || poName || '');

  if (loading) return <div className="flex items-center justify-center min-h-[400px]"><FaSpinner className="animate-spin text-amber-500 text-3xl" /></div>;

  return (
    <div className="min-h-screen bg-gray-50 dark:bg-gray-900 px-6 py-6">
      <ToastContainer position="top-right" autoClose={3500} />

      <div className="flex items-center gap-2 text-sm mb-6 text-gray-500">
        <button onClick={() => navigate('/purchase-orders')} className="hover:text-amber-600">Purchase Orders</button>
        <span>/</span>
        <span className="text-gray-700 dark:text-gray-300">{isNew ? 'New Purchase Order' : poName}</span>
      </div>

      <div className="bg-white dark:bg-gray-800 rounded-xl border border-gray-200 dark:border-gray-700 shadow-sm overflow-hidden">

        {/* Header */}
        <div className="px-6 py-4 border-b border-gray-100 dark:border-gray-700 flex items-center justify-between gap-4 flex-wrap">
          <div className="flex items-center gap-3">
            <button onClick={() => navigate('/purchase-orders')} className="text-gray-400 hover:text-gray-700"><FaArrowLeft /></button>
            <FaShoppingBag className="text-amber-500" />
            <div>
              <div className="flex items-center gap-2 flex-wrap">
                <h1 className="text-xl font-bold text-gray-900 dark:text-white">{title}</h1>
                {!isNew && poName && (
                  <span className="text-xs text-gray-400 font-mono">{poName}</span>
                )}
                {!isNew && (
                  <span className={`px-2 py-0.5 rounded text-xs font-semibold ${(() => { const s = doc?.status || ''; if (doc?.docstatus === 2 || s === 'Cancelled') return 'bg-red-100 text-red-700'; if (!doc || doc.docstatus === 0) return 'bg-yellow-100 text-yellow-800'; if (s === 'Completed') return 'bg-green-100 text-green-800'; if (s === 'To Receive and Bill' || s === 'To Bill' || s === 'To Receive') return 'bg-blue-100 text-blue-800'; if (s === 'Closed') return 'bg-gray-100 text-gray-700'; return 'bg-green-100 text-green-800'; })()}`}>
                    {doc?.docstatus === 2 ? 'Cancelled' : doc?.docstatus === 0 ? 'Draft' : (doc?.status || 'Submitted')}
                  </span>
                )}
              </div>
            </div>
          </div>
          <div className="flex items-center gap-2 flex-wrap">
            {isSubmitted && (
              <CreateDropdown items={[
                { label: 'Purchase Receipt', icon: <FaFileInvoice size={13} />, onClick: createPR },
              ]} />
            )}
            {!isNew && !isEditing && doc?.docstatus === 0 && (
              <button onClick={handleSubmit} disabled={submitting} className="flex items-center gap-2 px-4 py-2 bg-blue-600 text-white rounded-lg hover:bg-blue-700 disabled:opacity-50 text-sm font-medium">
                {submitting ? <FaSpinner className="animate-spin" /> : <FaPaperPlane size={12} />} Submit
              </button>
            )}
            {!isNew && !isEditing && !isSubmitted && (
              <button onClick={() => setIsEditing(true)} className="flex items-center gap-2 px-4 py-2 border border-amber-500 text-amber-600 rounded-lg hover:bg-amber-50 text-sm">
                <FaEdit /> Edit
              </button>
            )}
            {editable && (
              <>
                <button onClick={handleSave} disabled={saving} className="flex items-center gap-2 px-4 py-2 bg-amber-500 text-white rounded-lg hover:bg-amber-600 disabled:opacity-50 text-sm font-medium">
                  {saving ? <FaSpinner className="animate-spin" /> : <FaSave />}{saving ? 'Saving…' : 'Save'}
                </button>
                {!isNew && <button onClick={() => { if (doc) syncForm(doc); setIsEditing(false); }} className="px-3 py-2 border border-gray-300 rounded-lg text-gray-600 text-sm"><FaTimes /></button>}
              </>
            )}
          </div>
        </div>

        {/* Main fields */}
        <div className="px-6 pt-5 pb-4">
          <div className="grid grid-cols-1 sm:grid-cols-2 gap-x-8 gap-y-4">
            <div><FL required>Supplier</FL>
              {editable ? <LinkField label="Supplier" hideLabel doctype="Supplier" value={form.supplier || ''} onChange={v => { set('supplier', v); set('supplier_name', v); }} placeholder="Select supplier…" /> : <RV>{form.supplier_name || form.supplier}</RV>}
            </div>
            <div><FL required>Transaction Date</FL>
              {editable ? <input type="date" value={form.transaction_date || ''} onChange={e => set('transaction_date', e.target.value)} className={inputCls} /> : <RV>{form.transaction_date}</RV>}
            </div>
            <div><FL>Schedule Date</FL>
              {editable ? <input type="date" value={form.schedule_date || ''} onChange={e => set('schedule_date', e.target.value)} className={inputCls} /> : <RV>{form.schedule_date}</RV>}
            </div>
            <div><FL>Company</FL>
              {editable ? <LinkField label="Company" hideLabel doctype="Company" value={form.company || ''} onChange={v => set('company', v)} placeholder="Select company…" /> : <RV>{form.company}</RV>}
            </div>
            <div><FL>Project</FL>
              {editable ? <LinkField label="Project" hideLabel doctype="Project" value={form.project || ''} onChange={v => set('project', v)} placeholder="Select project…" /> : <RV>{form.project}</RV>}
            </div>
            <div><FL>Set Warehouse</FL>
              {editable ? <LinkField label="Warehouse" hideLabel doctype="Warehouse" value={form.set_warehouse || ''} onChange={v => set('set_warehouse', v)} placeholder="Set warehouse…" /> : <RV>{form.set_warehouse}</RV>}
            </div>
            <div><FL>Currency</FL>
              {editable
                ? <select value={form.currency || DEFAULT_CURRENCY} onChange={e => set('currency', e.target.value)} className={inputCls}>
                    <option value="SAR">SAR</option><option value="USD">USD</option><option value="EUR">EUR</option>
                  </select>
                : <RV>{form.currency}</RV>}
            </div>
            <div><FL>Cost Center</FL>
              {editable ? <LinkField label="Cost Center" hideLabel doctype="Cost Center" value={form.cost_center || ''} onChange={v => set('cost_center', v)} placeholder="Cost center…" /> : <RV>{form.cost_center}</RV>}
            </div>
          </div>
        </div>

        {/* ── Items ── */}
        <div className="border-t border-gray-100 dark:border-gray-700">
          <div className="px-6 py-3 bg-gray-50 dark:bg-gray-900/20 border-b border-gray-100 dark:border-gray-700">
            <span className="text-sm font-semibold text-gray-700 dark:text-gray-300">Items</span>
          </div>
          <div className="px-6 pb-5">
            <div className="overflow-x-auto -mx-2 mt-3">
              <table className="min-w-full text-sm">
                <thead>
                  <tr className="border-b border-gray-200 dark:border-gray-700 bg-gray-50 dark:bg-gray-900/40">
                    <th className="text-left text-[10px] font-semibold text-gray-500 uppercase py-2 px-3 w-8">No.</th>
                    <th className="text-left text-[10px] font-semibold text-gray-500 uppercase py-2 px-3 min-w-[180px]">Item Code <span className="text-red-400">*</span></th>
                    <th className="text-left text-[10px] font-semibold text-gray-500 uppercase py-2 px-3 w-32">Schedule Date</th>
                    <th className="text-right text-[10px] font-semibold text-gray-500 uppercase py-2 px-3 w-24">Qty <span className="text-red-400">*</span></th>
                    <th className="text-right text-[10px] font-semibold text-gray-500 uppercase py-2 px-3 w-28">Rate ({form.currency || DEFAULT_CURRENCY})</th>
                    <th className="text-right text-[10px] font-semibold text-gray-500 uppercase py-2 px-3 w-28">Amount ({form.currency || DEFAULT_CURRENCY})</th>
                    {editable && <th className="w-16 py-2 px-2" />}
                  </tr>
                </thead>
                <tbody>
                  {(form.items || []).map((it, idx) => (
                    <React.Fragment key={idx}>
                      <tr className={`border-b border-gray-100 dark:border-gray-700 align-middle ${expandedItem === idx ? 'bg-amber-50/60 dark:bg-amber-900/10' : ''}`}>
                        <td className="py-1.5 px-3 text-gray-400 text-xs">{idx + 1}</td>
                        <td className="py-1.5 px-2 min-w-[180px]">
                          {editable
                            ? <LinkField label="Item" hideLabel doctype="Item" value={it.item_code || ''} onChange={v => handleItemCode(idx, v)} placeholder="Item Code" />
                            : <span className="font-medium text-gray-800 dark:text-gray-200">{it.item_code || '-'}</span>}
                        </td>
                        <td className="py-1.5 px-2 w-32">
                          {editable
                            ? <input type="date" value={it.schedule_date || ''} onChange={e => updateItem(idx, 'schedule_date', e.target.value)} className={inlineTxt} />
                            : <span className="text-gray-500 text-sm">{it.schedule_date || '-'}</span>}
                        </td>
                        <td className="py-1.5 px-2 w-24">
                          {editable ? <input type="number" min={0} step="1" value={it.qty ?? 0} onChange={e => updateItem(idx, 'qty', parseFloat(e.target.value) || 0)} className={inlineNum} /> : <span className="block text-right text-gray-700 dark:text-gray-300 text-sm pr-1">{it.qty ?? 0}</span>}
                        </td>
                        <td className="py-1.5 px-2 w-28">
                          {editable ? <input type="number" min={0} step="0.01" value={it.rate ?? 0} onChange={e => updateItem(idx, 'rate', parseFloat(e.target.value) || 0)} className={inlineNum} /> : <span className="block text-right text-gray-700 dark:text-gray-300 text-sm pr-1">{(it.rate ?? 0).toFixed(2)}</span>}
                        </td>
                        <td className="py-1.5 px-3 text-right font-semibold text-gray-900 dark:text-white text-sm">{((it.qty || 0) * (it.rate || 0)).toFixed(2)}</td>
                        {editable && (
                          <td className="py-1.5 px-2">
                            <div className="flex items-center gap-1">
                              <button onClick={() => setExpandedItem(expandedItem === idx ? null : idx)}
                                className={`p-1.5 rounded text-xs ${expandedItem === idx ? 'bg-amber-500 text-white' : 'text-amber-600 hover:bg-amber-50'}`} title="More fields">
                                <FaPencilAlt size={11} />
                              </button>
                              <button onClick={() => removeItem(idx)} className="p-1.5 text-red-400 hover:text-red-600 hover:bg-red-50 rounded"><FaTrash size={11} /></button>
                            </div>
                          </td>
                        )}
                      </tr>
                      {editable && expandedItem === idx && (
                        <POItemRowEditor item={it} rowNo={idx + 1}
                          onChange={(k, v) => { if (k === 'item_code') handleItemCode(idx, v as string); else updateItem(idx, k, v); }}
                          onClose={() => setExpandedItem(null)} onDelete={() => removeItem(idx)} onInsertBelow={() => addItem(idx)} />
                      )}
                    </React.Fragment>
                  ))}
                  {editable && (
                    <tr><td colSpan={7} className="py-2 px-3">
                      <button onClick={() => addItem()} className="flex items-center gap-1.5 text-amber-600 hover:text-amber-700 text-sm font-medium"><FaPlus size={10} /> Add Row</button>
                    </td></tr>
                  )}
                </tbody>
              </table>
            </div>
            <div className="mt-3 flex justify-between text-sm border-t border-gray-100 dark:border-gray-700 pt-3">
              <span className="text-gray-500">Total Qty: <strong>{(form.items || []).reduce((s, it) => s + (it.qty || 0), 0)}</strong></span>
              <span className="text-gray-500">Net Total: <strong className="text-gray-900 dark:text-white">{form.currency || DEFAULT_CURRENCY} {netTotal.toFixed(2)}</strong></span>
            </div>
          </div>
        </div>

        {/* ── Taxes ── */}
        <div className="border-t border-gray-100 dark:border-gray-700">
          <div className="px-6 py-3 bg-gray-50 dark:bg-gray-900/20 border-b border-gray-100 dark:border-gray-700">
            <span className="text-sm font-semibold text-gray-700 dark:text-gray-300">Taxes and Charges</span>
          </div>
          <div className="px-6 pt-4 pb-2 grid grid-cols-1 sm:grid-cols-2 gap-x-8 gap-y-3">
            <div><FL>Tax Category</FL>
              {editable ? <LinkField label="Tax Category" hideLabel doctype="Tax Category" value={(form as any).tax_category || ''} onChange={v => set('tax_category' as any, v)} placeholder="Select tax category…" /> : <RV>{(form as any).tax_category}</RV>}
            </div>
            <div><FL>Purchase Taxes and Charges Template</FL>
              {editable
                ? <LinkField label="Purchase Taxes and Charges Template" hideLabel doctype="Purchase Taxes and Charges Template" value={(form as any).taxes_and_charges || ''} onChange={v => { set('taxes_and_charges' as any, v); loadTaxTemplate(v); }} placeholder="Select template…" />
                : <RV>{(form as any).taxes_and_charges}</RV>}
            </div>
          </div>
          <div className="px-6 pb-5">
            <div className="overflow-x-auto -mx-2 mt-3">
              <table className="min-w-full text-sm">
                <thead>
                  <tr className="border-b border-gray-200 dark:border-gray-700 bg-gray-50 dark:bg-gray-900/40">
                    <th className="text-left text-[10px] font-semibold text-gray-500 uppercase py-2 px-3 w-8">No.</th>
                    <th className="text-left text-[10px] font-semibold text-gray-500 uppercase py-2 px-3 w-44">Type <span className="text-red-400">*</span></th>
                    <th className="text-left text-[10px] font-semibold text-gray-500 uppercase py-2 px-3">Account Head <span className="text-red-400">*</span></th>
                    <th className="text-right text-[10px] font-semibold text-gray-500 uppercase py-2 px-3 w-24">Tax Rate</th>
                    <th className="text-right text-[10px] font-semibold text-gray-500 uppercase py-2 px-3 w-28">Amount</th>
                    <th className="text-right text-[10px] font-semibold text-gray-500 uppercase py-2 px-3 w-28">Total</th>
                    {editable && <th className="w-16 py-2 px-2" />}
                  </tr>
                </thead>
                <tbody>
                  {(form.taxes || []).map((tx, idx) => {
                    const calcAmt = tx.charge_type === 'On Net Total' ? ((tx.rate || 0) / 100) * netTotal : (tx.charge_type === 'Actual' ? (tx.tax_amount || 0) : ((tx.rate || 0) / 100) * netTotal);
                    const calcRunning = netTotal + (form.taxes || []).slice(0, idx + 1).reduce((s: number, t: any) => {
                      const a = t.charge_type === 'On Net Total' ? ((t.rate || 0) / 100) * netTotal : (t.charge_type === 'Actual' ? (t.tax_amount || 0) : ((t.rate || 0) / 100) * netTotal);
                      return s + a;
                    }, 0);
                    return (
                      <React.Fragment key={idx}>
                        <tr className={`border-b border-gray-100 dark:border-gray-700 align-middle ${expandedTax === idx ? 'bg-amber-50/60 dark:bg-amber-900/10' : ''}`}>
                          <td className="py-1.5 px-3 text-gray-400 text-xs">{idx + 1}</td>
                          <td className="py-1.5 px-2 w-44">
                            {editable
                              ? <select value={tx.charge_type || ''} onChange={e => updateTax(idx, 'charge_type', e.target.value)} className={inlineTxt}>
                                  <option value="">Select type…</option>
                                  <option value="Actual">Actual</option>
                                  <option value="On Net Total">On Net Total</option>
                                  <option value="On Previous Row Amount">On Previous Row Amount</option>
                                </select>
                              : <span className="text-gray-700 dark:text-gray-300">{tx.charge_type || '-'}</span>}
                          </td>
                          <td className="py-1.5 px-2">
                            {editable
                              ? <LinkField label="Account Head" hideLabel doctype="Account" value={tx.account_head || ''} onChange={v => updateTax(idx, 'account_head', v)} placeholder="Account Head" />
                              : <span className="text-gray-700 dark:text-gray-300">{tx.account_head || '-'}</span>}
                          </td>
                          <td className="py-1.5 px-2 w-24">
                            {editable
                              ? <input type="number" min={0} step="0.01" value={tx.rate ?? 0} onChange={e => updateTax(idx, 'rate', parseFloat(e.target.value) || 0)} className={inlineNum} />
                              : <span className="block text-right text-gray-700 dark:text-gray-300 pr-1">{tx.rate ?? 0}</span>}
                          </td>
                          <td className="py-1.5 px-3 text-right text-gray-700 dark:text-gray-300 text-sm">{calcAmt.toFixed(2)}</td>
                          <td className="py-1.5 px-3 text-right font-semibold text-gray-900 dark:text-white text-sm">{calcRunning.toFixed(2)}</td>
                          {editable && (
                            <td className="py-1.5 px-2">
                              <div className="flex items-center gap-1">
                                <button onClick={() => setExpandedTax(expandedTax === idx ? null : idx)}
                                  className={`p-1.5 rounded text-xs ${expandedTax === idx ? 'bg-amber-500 text-white' : 'text-amber-600 hover:bg-amber-50'}`} title="More fields">
                                  <FaPencilAlt size={11} />
                                </button>
                                <button onClick={() => removeTax(idx)} className="p-1.5 text-red-400 hover:text-red-600 hover:bg-red-50 rounded"><FaTrash size={11} /></button>
                              </div>
                            </td>
                          )}
                        </tr>
                        {editable && expandedTax === idx && (
                          <POTaxRowEditor tax={tx} rowNo={idx + 1} onChange={(k, v) => updateTax(idx, k, v)}
                            onClose={() => setExpandedTax(null)} onDelete={() => removeTax(idx)} onInsertBelow={() => addTax(idx)} />
                        )}
                      </React.Fragment>
                    );
                  })}
                  {editable && (
                    <tr><td colSpan={7} className="py-2 px-3">
                      <button onClick={() => addTax()} className="flex items-center gap-1.5 text-amber-600 hover:text-amber-700 text-sm font-medium"><FaPlus size={10} /> Add Row</button>
                    </td></tr>
                  )}
                </tbody>
              </table>
            </div>
            {(form.taxes || []).length > 0 && (
              <div className="mt-2 flex justify-end text-sm text-gray-500 pt-2 border-t border-gray-100 dark:border-gray-700">
                Total Taxes and Charges: <strong className="ml-2 text-gray-900 dark:text-white">{form.currency || DEFAULT_CURRENCY} {taxTotal.toFixed(2)}</strong>
              </div>
            )}
          </div>
        </div>

        {/* ── Totals ── */}
        <div className="border-t border-gray-100 dark:border-gray-700 px-6 py-4">
          <h3 className="text-sm font-semibold text-gray-700 dark:text-gray-300 mb-3">Totals</h3>
          <div className="space-y-2 max-w-xs ml-auto">
            {[
              { label: 'Net Total',           value: (doc?.net_total            ?? netTotal).toFixed(2) },
              { label: 'Total Taxes',         value: (doc?.total_taxes_and_charges ?? taxTotal).toFixed(2) },
              { label: 'Grand Total',         value: (doc?.grand_total           ?? grandTotal).toFixed(2) },
              { label: 'Rounded Total',       value: (doc?.rounded_total         ?? grandTotal).toFixed(2) },
              { label: 'Advance Paid',        value: (doc?.advance_paid          ?? 0).toFixed(2) },
            ].map(({ label, value }) => (
              <div key={label} className="flex justify-between text-sm border-b border-gray-100 dark:border-gray-700 pb-1.5 last:border-0">
                <span className="text-gray-500">{label}</span>
                <span className="font-semibold text-gray-900 dark:text-white">{form.currency || DEFAULT_CURRENCY} {value}</span>
              </div>
            ))}
          </div>
        </div>

        {/* Meta */}
        {!isNew && doc && (
          <div className="border-t border-gray-100 dark:border-gray-700 px-6 py-4 grid grid-cols-3 gap-4 text-sm bg-gray-50 dark:bg-gray-900/20">
            <div><FL>Created By</FL><RV>{doc.owner}</RV></div>
            <div><FL>Created</FL><RV>{doc.creation ? new Date(doc.creation).toLocaleString() : '-'}</RV></div>
            <div><FL>Modified</FL><RV>{doc.modified ? new Date(doc.modified).toLocaleString() : '-'}</RV></div>
          </div>
        )}

        {!isNew && (
          <ActivityLog
            doctype="Purchase Order"
            docname={doc?.name || poName || ''}
            creationDate={doc?.creation}
            createdBy={doc?.owner}
            compact={false}
            initialVisible={5}
            collapsible
            startCollapsed
          />
        )}
      </div>
    </div>
  );
};

export default PurchaseOrderDetail;
