<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Professional Invoice</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
    <style>
        :root {
            --primary: #2c3e50;
            --secondary: #3498db;
            --accent: #e74c3c;
            --light: #f8f9fa;
            --dark: #2c3e50;
            --success: #27ae60;
            --warning: #f39c12;
            --danger: #e74c3c;
            --border-radius: 8px;
            --box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
        }
        
        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background-color: #f5f7fa;
            color: #333;
            line-height: 1.5;
        }
        
        .invoice-container {
            max-width: 1200px;
            margin: 2rem auto;
            background: white;
            border-radius: var(--border-radius);
            box-shadow: var(--box-shadow);
            overflow: hidden;
        }
        
        .invoice-header {
            background: white;
            padding: 1.5rem 2rem;
            border-bottom: 1px solid #e9ecef;
        }
        
        .invoice-title {
            font-weight: 700;
            font-size: 1.8rem;
            color: var(--primary);
        }
        
        .status-badge {
            font-size: 0.85rem;
            padding: 0.5rem 1rem;
            border-radius: 50px;
            font-weight: 600;
        }
        
        .id-container {
            margin-bottom: 1rem; /* Reduced from 1.5rem */
        }
        
        .id-box {
            background: white;
            border-radius: var(--border-radius);
            padding: 1rem;
            border: 1px solid #e9ecef;
            box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
            transition: all 0.3s ease;
            height: 100%;
        }
        
        .id-box:hover {
            transform: translateY(-1px);
            box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
        }
        
        .id-box-primary {
            border-left: 4px solid var(--secondary);
        }
        
        .id-box-secondary {
            border-left: 4px solid var(--success);
        }
        
        .id-box-accent {
            border-left: 4px solid var(--warning);
        }
        
        .id-icon {
            width: 36px;
            height: 36px;
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            margin-bottom: 0.5rem; /* Reduced from 0.75rem */
            font-size: 1rem;
        }
        
        .id-icon-primary {
            background: rgba(52, 152, 219, 0.1);
            color: var(--secondary);
        }
        
        .id-icon-secondary {
            background: rgba(39, 174, 96, 0.1);
            color: var(--success);
        }
        
        .id-icon-accent {
            background: rgba(243, 156, 18, 0.1);
            color: var(--warning);
        }
        
        .id-label {
            font-size: 0.75rem;
            color: #6c757d;
            font-weight: 600;
            margin-bottom: 0.25rem;
            text-transform: uppercase;
            letter-spacing: 0.5px;
        }
        
        .id-value {
            font-size: 0.95rem;
            font-weight: 700;
            color: var(--primary);
            margin-bottom: 0;
        }
        
        .date-container {
            background: white;
            border-radius: var(--border-radius);
            padding: 1rem; /* Reduced from 1.25rem */
            margin-bottom: 1rem; /* Reduced from 1.5rem */
            border: 1px solid #e9ecef;
            box-shadow: var(--box-shadow);
        }
        
        .date-card {
            text-align: center;
            padding: 0.75rem;
            background: linear-gradient(135deg, #f8f9fa 0%, #e9ecef 100%);
            border-radius: var(--border-radius);
            border: 1px solid rgba(0,0,0,0.05);
        }
        
        .date-icon {
            width: 40px;
            height: 40px;
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            margin: 0 auto 0.5rem; /* Reduced from 0.75rem */
            background: var(--secondary);
            color: white;
            font-size: 1rem;
        }
        
        .date-label {
            font-size: 0.8rem;
            color: #6c757d;
            font-weight: 600;
            margin-bottom: 0.5rem;
            text-transform: uppercase;
            letter-spacing: 0.5px;
        }
        
        .date-value {
            font-size: 1rem;
            font-weight: 700;
            color: var(--primary);
            padding: 0.5rem;
            background: white;
            border-radius: 4px;
            border: 1px solid #e9ecef;
        }
        
        .date-connector {
            display: flex;
            align-items: center;
            justify-content: center;
            height: 100%;
        }
        
        .date-connector i {
            background: var(--light);
            padding: 10px;
            border-radius: 50%;
            color: var(--secondary);
            border: 2px solid #e9ecef;
        }
        
        .card {
            border: 1px solid #e9ecef;
            border-radius: var(--border-radius);
            margin-bottom: 1rem; /* Reduced from 1.5rem */
            box-shadow: var(--box-shadow);
        }
        
        .card-header {
            background: var(--light);
            border-bottom: 1px solid #e9ecef;
            padding: 0.75rem 1.25rem;
            font-weight: 600;
            color: var(--primary);
        }
        
        .table {
            margin-bottom: 0;
            font-size: 0.9rem;
        }
        
        .table > :not(caption) > * > * {
            padding: 10px 8px;
        }
        
        .table thead th {
            background-color: var(--light);
            color: var(--primary);
            font-weight: 600;
            border-bottom: 1px solid #dee2e6;
            font-size: 0.85rem;
        }
        
        .table tbody tr:hover {
            background-color: rgba(52, 152, 219, 0.03);
        }
        
        .summary-card {
            background: var(--light);
            border-left: 4px solid var(--secondary);
        }
        
        .summary-header {
            background: var(--primary);
            color: white;
        }
        
        .amount-row {
            border-bottom: 1px solid #e9ecef;
            padding: 0.75rem 0;
        }
        
        .total-row {
            border-top: 2px solid #dee2e6;
            font-size: 1.1rem;
            font-weight: 700;
        }
        
        .text-primary {
            color: var(--primary) !important;
        }
        
        .text-success {
            color: var(--success) !important;
        }
        
        .text-danger {
            color: var(--danger) !important;
        }
        
        .badge {
            font-size: 0.75rem;
            padding: 0.35rem 0.65rem;
        }
        
        /* COMPACT HEADER STYLES */
        .compact-header {
            margin-bottom: 0.75rem; /* Reduced from default */
        }
        
        .compact-header .invoice-title {
            margin-bottom: 0.25rem; /* Reduced gap */
        }
        
        .compact-header .status-badge {
            margin-top: 0.25rem; /* Reduced gap */
        }
        
        @media (max-width: 768px) {
            .invoice-container {
                margin: 1rem;
            }
            
            .date-connector {
                margin: 1rem 0;
            }
            
            .table-responsive {
                font-size: 0.8rem;
            }
            
            .id-box {
                margin-bottom: 1rem;
            }
        }
    </style>
</head>
<body>
    <div class="invoice-container">
        <div class="p-4">
            <!-- ============================
                 INVOICE HEADER - COMPACT
            ============================ -->
            @php
            $showActions = $showActions ?? true;
            @endphp

            <div class="compact-header">
                <div class="row align-items-center">
                    <div class="col-md-6">
                        <h2 class="invoice-title mb-1">
                            <i class="fas fa-file-invoice me-2"></i> INVOICE
                        </h2>
                        <h4 class="fw-bold text-dark mb-0">{{ $invoice->invoice_number }}</h4>
                    </div>

                    <div class="col-md-6 text-end">
                        <span class="status-badge 
                            @if($invoice->status=='paid') bg-success 
                            @elseif($invoice->status=='overdue') bg-danger
                            @elseif($invoice->status=='pending') bg-warning text-dark
                            @else bg-secondary @endif">
                            <i class="fas 
                                @if($invoice->status=='paid') fa-check-circle 
                                @elseif($invoice->status=='overdue') fa-exclamation-circle
                                @elseif($invoice->status=='pending') fa-clock
                                @else fa-question-circle @endif me-1"></i>
                            {{ ucfirst($invoice->status) }}
                        </span>
                    </div>
                </div>
            </div>

            <!-- Three ID Boxes in One Row -->
            <div class="id-container">
                <div class="row">
                    <!-- Invoice ID Box -->
                    <div class="col-md-4 mb-3">
                        <div class="id-box id-box-primary">
                            <div class="id-icon id-icon-primary">
                                <i class="fas fa-receipt"></i>
                            </div>
                            <div class="id-label">Invoice ID</div>
                            <div class="id-value">{{ $invoice->invoice_number }}</div>
                        </div>
                    </div>
                    
                    <!-- Order ID Box -->
                    <div class="col-md-4 mb-3">
                        <div class="id-box id-box-secondary">
                            <div class="id-icon id-icon-secondary">
                                <i class="fas fa-shopping-cart"></i>
                            </div>
                            <div class="id-label">Order ID</div>
                            <div class="id-value">
                                @if($invoice->order && $invoice->order->order_id)
                                    {{ $invoice->order->order_id }}
                                @elseif($invoice->order_id)
                                    {{ $invoice->order_id }}
                                @else
                                    N/A
                                @endif
                            </div>
                        </div>
                    </div>
                    
                    <!-- Shipment ID Box -->
                    <div class="col-md-4 mb-3">
                        <div class="id-box id-box-accent">
                            <div class="id-icon id-icon-accent">
                                <i class="fas fa-shipping-fast"></i>
                            </div>
                            <div class="id-label">Shipment ID</div>
                            <div class="id-value">
                                @php
                                    $shipmentId = 'N/A';
                                    // Try multiple ways to get shipment ID
                                    if($invoice->shipment && $invoice->shipment->shipment_id) {
                                        $shipmentId = $invoice->shipment->shipment_id;
                                    } elseif($invoice->shipment_id) {
                                        $shipmentId = $invoice->shipment_id;
                                    } elseif(isset($shipment) && $shipment && $shipment->shipment_id) {
                                        $shipmentId = $shipment->shipment_id;
                                    }
                                @endphp
                                {{ $shipmentId }}
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <!-- ============================
                 DATES SECTION
            ============================ -->
            <div class="date-container">
                <div class="row align-items-center">
                    <div class="col-md-5">
                        <div class="date-card">
                            <div class="date-icon">
                                <i class="fas fa-calendar-alt"></i>
                            </div>
                            <div class="date-label">INVOICE DATE</div>
                            <div class="date-value">{{ \Carbon\Carbon::parse($invoice->invoice_date)->format('M d, Y') }}</div>
                        </div>
                    </div>
                    <div class="col-md-2">
                        <div class="date-connector">
                            <i class="fas fa-arrow-right"></i>
                        </div>
                    </div>
                    <div class="col-md-5">
                        <div class="date-card">
                            <div class="date-icon">
                                <i class="fas fa-clock"></i>
                            </div>
                            <div class="date-label">DUE DATE</div>
                            <div class="date-value @if($invoice->status == 'overdue') text-danger @endif">
                                {{ \Carbon\Carbon::parse($invoice->due_date)->format('M d, Y') }}
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <!-- ============================
                 CUSTOMER DETAILS
            ============================ -->
            <div class="card">
                <div class="card-header">
                    <h6 class="mb-0 fw-bold">
                        <i class="fas fa-user me-2"></i> Customer Details
                    </h6>
                </div>
                <div class="card-body">
                    <div class="row">
                        <div class="col-md-6">
                            <h6 class="fw-bold text-primary mb-1">{{ $invoice->customer_name }}</h6>
                            @if($invoice->company_name)
                            <p class="mb-1">
                                <strong>Company:</strong> {{ $invoice->company_name }}
                            </p>
                            @endif
                            <p class="mb-1">
                                <strong>Mobile:</strong> {{ $invoice->customer_mobile }}
                            </p>
                            <p class="mb-1">
                                <strong>Email:</strong> {{ $invoice->customer_email }}
                            </p>
                        </div>
                        <div class="col-md-6">
                            <p class="mb-1">
                                <strong>Address:</strong><br>
                                {{ $invoice->customer_address }}
                            </p>
                            <p class="mb-1">
                                <strong>Pincode:</strong> {{ $invoice->pincode }}
                            </p>
                        </div>
                    </div>
                </div>
            </div>

            <!-- ============================
                 INVOICE ITEMS
            ============================ -->
            <div class="card">
                <div class="card-header">
                    <h6 class="mb-0 fw-bold">
                        <i class="fas fa-list me-2"></i> Invoice Items
                    </h6>
                </div>
                <div class="card-body p-0">
                    <div class="table-responsive">
                        <table class="table table-bordered table-hover align-middle mb-0">
                            <thead class="table-light">
                                <tr>
                                    <th class="text-center">#</th>
                                    <th>Description</th>
                                    <th class="text-center">CTN</th>
                                    <th class="text-center">QTY</th>
                                    <th class="text-center">TTL/QTY</th>
                                    <th class="text-center">Unit</th>
                                    <th class="text-center">Price</th>
                                    <th class="text-center">TTL Amount</th>
                                    <th class="text-center">CBM</th>
                                    <th class="text-center">TTL CBM</th>
                                    <th class="text-center">KG</th>
                                    <th class="text-center">TTL KG</th>
                                    <th class="text-center">Shop No</th>
                                </tr>
                            </thead>
                            <tbody>
                                @foreach($invoice->items as $i => $item)
                                <tr>
                                    <td class="text-center fw-bold text-muted">{{ $i+1 }}</td>
                                    <td class="fw-semibold">{{ $item->description }}</td>
                                    <td class="text-center">{{ $item->ctn }}</td>
                                    <td class="text-center">{{ $item->qty }}</td>
                                    <td class="text-center fw-bold">{{ $item->ttl_qty }}</td>
                                    <td class="text-center">{{ $item->unit }}</td>
                                    <td class="text-center text-success fw-bold">₹{{ number_format($item->price,2) }}</td>
                                    <td class="text-center text-primary fw-bold">₹{{ number_format($item->ttl_amount,2) }}</td>
                                    <td class="text-center">{{ $item->cbm }}</td>
                                    <td class="text-center">{{ $item->ttl_cbm }}</td>
                                    <td class="text-center">{{ $item->kg }}</td>
                                    <td class="text-center">{{ $item->ttl_kg }}</td>
                                    <td class="text-center">
                                        <span class="badge bg-light text-dark border">{{ $item->shop_no }}</span>
                                    </td>
                                </tr>
                                @endforeach
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>

            <!-- ============================
                 FINAL SUMMARY
            ============================ -->
            <div class="row">
                <div class="col-md-6 offset-md-6">
                    <div class="card summary-card">
                        <div class="card-header summary-header">
                            <h6 class="mb-0 fw-bold">
                                <i class="fas fa-calculator me-2"></i> Final Summary
                            </h6>
                        </div>
                        <div class="card-body">
                            <div class="d-flex justify-content-between align-items-center mb-2 pb-1 border-bottom">
                                <span class="fw-semibold">Amount:</span>
                                <span class="fw-bold text-dark">₹{{ number_format($invoice->final_amount,2) }}</span>
                            </div>

                            @if($invoice->tax_type === 'gst')
                                {{-- CGST --}}
                                <div class="d-flex justify-content-between align-items-center mb-2 pb-1 border-bottom">
                                    <span class="fw-semibold">CGST ({{ $invoice->cgst_percent ?? ($invoice->gst_percent/2) }}%):</span>
                                    <span class="fw-bold text-danger">₹{{ number_format($invoice->gst_amount/2, 2) }}</span>
                                </div>

                                {{-- SGST --}}
                                <div class="d-flex justify-content-between align-items-center mb-2 pb-1 border-bottom">
                                    <span class="fw-semibold">SGST ({{ $invoice->sgst_percent ?? ($invoice->gst_percent/2) }}%):</span>
                                    <span class="fw-bold text-danger">₹{{ number_format($invoice->gst_amount/2, 2) }}</span>
                                </div>

                            @elseif($invoice->tax_type === 'igst')
                                {{-- IGST --}}
                                <div class="d-flex justify-content-between align-items-center mb-2 pb-1 border-bottom">
                                    <span class="fw-semibold">IGST ({{ $invoice->igst_percent ?? $invoice->gst_percent }}%):</span>
                                    <span class="fw-bold text-danger">₹{{ number_format($invoice->gst_amount, 2) }}</span>
                                </div>
                            @else
                                {{-- Default GST --}}
                                <div class="d-flex justify-content-between align-items-center mb-2 pb-1 border-bottom">
                                    <span class="fw-semibold">GST ({{ $invoice->gst_percent }}%):</span>
                                    <span class="fw-bold text-danger">₹{{ number_format($invoice->gst_amount, 2) }}</span>
                                </div>
                            @endif

                            <div class="d-flex justify-content-between align-items-center pt-1">
                                <span class="fw-bold text-dark">Total Payable:</span>
                                <span class="fw-bold text-success">₹{{ number_format($invoice->final_amount_with_gst,2) }}</span>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            
            <!-- ============================
                 FOOTER — DOWNLOAD & SHARE
            ============================ -->
            <div class="mt-4 pt-3 border-top text-center">
                @if($invoice->pdf_path)
                <a href="{{ asset($invoice->pdf_path) }}" class="btn btn-primary me-2" download>
                    <i class="fas fa-download me-1"></i> Download PDF
                </a>

                <button class="btn btn-success" onclick="shareInvoice()">
                    <i class="fas fa-share me-1"></i> Share
                </button>
                @endif
            </div>

            <!-- Footer Message -->
            <div class="mt-4 pt-3 border-top text-center text-muted">
                <p class="mb-1">Thank you for your business!</p>
                <p class="mb-0">For any inquiries, contact us at support@Kent Logistic</p>
            </div>
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/js/bootstrap.bundle.min.js"></script>
    
    <!-- ============================
         SHARE SCRIPT
    ============================ -->
    <script>
    function shareInvoice() {
        const shareData = {
            title: "Invoice {{ $invoice->invoice_number }}",
            text: "Sharing invoice {{ $invoice->invoice_number }}",
            url: "{{ asset($invoice->pdf_path) }}"
        };

        if (navigator.share) {
            navigator.share(shareData).catch(() => {});
        } else {
            navigator.clipboard.writeText(shareData.url);
            alert("Link copied! Sharing not supported on this browser.");
        }
    }
    </script>
</body>
</html>