<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Indirect Goods Incoming Receipt - {{ $incoming->receipt_number }}</title>
    <style>
        @media print {
            body { margin: 0; }
            .no-print { display: none !important; }
            @page { margin: 15mm; }
        }

        body {
            font-family: Arial, sans-serif;
            font-size: 12px;
            line-height: 1.4;
            color: #000;
            margin: 20px;
        }

        .header {
            text-align: center;
            margin-bottom: 30px;
            display: flex;
            align-items: center;
            justify-content: center;
            gap: 20px;
        }

        .logo-container {
            flex-shrink: 0;
        }

        .logo-container img {
            height: 80px;
            width: auto;
        }

        .company-info {
            flex: 1;
            text-align: center;
        }

        .company-name {
            font-size: 14px;
            font-weight: bold;
            margin-bottom: 2px;
            text-transform: uppercase;
        }

        .company-address {
            font-size: 10px;
            margin-bottom: 1px;
        }

        .document-title {
            font-size: 14px;
            font-weight: bold;
            margin: 20px 0;
            text-transform: uppercase;
            text-align: center;
            background-color: #000;
            color: #fff;
            padding: 10px;
        }

        .info-section {
            margin-bottom: 20px;
        }

        .info-table {
            width: 100%;
            margin-bottom: 15px;
        }

        .info-table td {
            padding: 3px 8px;
            font-size: 12px;
            vertical-align: top;
        }

        .info-table .label {
            font-weight: bold;
            width: 130px;
        }

        .items-table {
            width: 100%;
            border-collapse: collapse;
            margin-bottom: 20px;
            font-size: 11px;
        }

        .items-table th,
        .items-table td {
            border: 1px solid #000;
            padding: 5px 8px;
            text-align: left;
        }

        .items-table th {
            font-weight: bold;
            background-color: #f0f0f0;
        }

        .items-table td.text-center {
            text-align: center;
        }

        .items-table td.text-right {
            text-align: right;
        }

        .totals-section {
            width: 100%;
            margin-bottom: 30px;
        }

        .totals-section table {
            width: 300px;
            margin-left: auto;
            border-collapse: collapse;
        }

        .totals-section td {
            padding: 4px 8px;
            font-size: 12px;
        }

        .totals-section .total-label {
            text-align: right;
            font-weight: bold;
        }

        .totals-section .total-value {
            text-align: right;
            width: 120px;
        }

        .totals-section .grand-total td {
            border-top: 2px solid #000;
            font-size: 13px;
            font-weight: bold;
            padding-top: 6px;
        }

        .signatures-section {
            margin-top: 50px;
            display: flex;
            justify-content: space-between;
        }

        .signature-block {
            width: 30%;
            text-align: center;
        }

        .signature-line {
            border-top: 1px solid #000;
            margin-top: 50px;
            padding-top: 5px;
            font-size: 11px;
        }

        .signature-name {
            font-weight: bold;
            margin-top: 3px;
            font-size: 11px;
        }

        .footer {
            margin-top: 40px;
            text-align: center;
            font-size: 10px;
            color: #666;
        }

        .print-btn {
            background-color: #007bff;
            color: white;
            border: none;
            padding: 10px 20px;
            cursor: pointer;
            border-radius: 4px;
            margin-bottom: 20px;
            font-size: 14px;
        }

        .print-btn:hover {
            background-color: #0056b3;
        }

        .close-btn {
            background-color: #6c757d;
            color: white;
            border: none;
            padding: 10px 20px;
            cursor: pointer;
            border-radius: 4px;
            margin-bottom: 20px;
            margin-left: 10px;
            font-size: 14px;
        }

        .close-btn:hover {
            background-color: #545b62;
        }

        .status-badge {
            display: inline-block;
            padding: 2px 10px;
            border: 1px solid #000;
            font-size: 11px;
            font-weight: bold;
            text-transform: uppercase;
        }
    </style>
</head>
<body>
    <div class="no-print" style="margin-bottom: 20px;">
        <button onclick="window.print()" class="print-btn">Print</button>
        <button onclick="window.close()" class="close-btn">Close</button>
    </div>

    <!-- Header -->
    <div class="header">
        <div class="logo-container">
            <img src="{{ asset('images/site_logo.png') }}" alt="Company Logo">
        </div>
        <div class="company-info">
            <div class="company-name">ISAM KABBANI & PARTNERS FOR CONSTRUCTION & MAINT. CO. ( L.L.C )</div>
            <div class="company-address">Dubai: Dubai Industrial City, Jebel Ali Tel# 04 818 1900; Fax# 04 429 7031</div>
            <div class="company-address">E-mail : dubai@kcgemirates.ae; abudhabi@kcgemirates.ae   Website : www.ikkgroup.com</div>
        </div>
    </div>

    <!-- Document Title -->
    <div class="document-title">INDIRECT GOODS - INCOMING RECEIPT</div>

    <!-- Receipt Information -->
    <div class="info-section">
        <table class="info-table">
            <tr>
                <td class="label">Receipt Number:</td>
                <td>{{ $incoming->receipt_number }}</td>
                <td class="label">Status:</td>
                <td><span class="status-badge">{{ strtoupper($incoming->status) }}</span></td>
            </tr>
            <tr>
                <td class="label">Supplier:</td>
                <td>{{ $incoming->supplier->name ?? '-' }}</td>
                <td class="label">Receipt Date:</td>
                <td>{{ $incoming->receipt_date ? $incoming->receipt_date->format('d/m/Y') : $incoming->created_at->format('d/m/Y') }}</td>
            </tr>
            <tr>
                <td class="label">Invoice Number:</td>
                <td>{{ $incoming->invoice_number ?? '-' }}</td>
                <td class="label">Invoice Date:</td>
                <td>{{ $incoming->invoice_date ? $incoming->invoice_date->format('d/m/Y') : '-' }}</td>
            </tr>
            <tr>
                <td class="label">Project:</td>
                <td>{{ $incoming->project ? $incoming->project->project_name . ($incoming->project->project_number ? ' (' . $incoming->project->project_number . ')' : '') : '-' }}</td>
                <td class="label">Division:</td>
                <td>{{ $incoming->division ? $incoming->division->division_name : '-' }}</td>
            </tr>
            <tr>
                <td class="label">Received By:</td>
                <td>{{ $incoming->receiver->name ?? '-' }}</td>
                <td class="label">Approved By:</td>
                <td>{{ $incoming->approver->name ?? '-' }}{{ $incoming->approved_at ? ' (' . $incoming->approved_at->format('d/m/Y') . ')' : '' }}</td>
            </tr>
            @if($incoming->notes)
            <tr>
                <td class="label">Notes:</td>
                <td colspan="3">{{ $incoming->notes }}</td>
            </tr>
            @endif
        </table>
    </div>

    <!-- Items Table -->
    <table class="items-table">
        <thead>
            <tr>
                <th style="width: 4%;">Sr.</th>
                <th style="width: 10%;">Code</th>
                <th style="width: 22%;">Description</th>
                <th style="width: 8%;">Qty Ordered</th>
                <th style="width: 8%;">Qty Delivered</th>
                <th style="width: 8%;">Qty Accepted</th>
                <th style="width: 6%;">Unit</th>
                <th style="width: 10%;">Unit Price</th>
                <th style="width: 10%;">Total Price</th>
                <th style="width: 8%;">Batch</th>
                <th style="width: 6%;">Expiry</th>
            </tr>
        </thead>
        <tbody>
            @forelse($incoming->items as $index => $item)
            <tr>
                <td class="text-center">{{ $index + 1 }}</td>
                <td class="text-center">{{ $item->indirectGood->ig_code ?? '-' }}</td>
                <td>{{ $item->indirectGood->ig_description ?? 'Unknown Item' }}</td>
                <td class="text-center">{{ $item->quantity_ordered > 0 ? number_format($item->quantity_ordered, 2) : '-' }}</td>
                <td class="text-center">{{ number_format($item->quantity_delivered, 2) }}</td>
                <td class="text-center">{{ number_format($item->quantity_accepted, 2) }}</td>
                <td class="text-center">{{ strtoupper($item->unit_of_measure ?? 'PCS') }}</td>
                <td class="text-right">{{ number_format($item->unit_price, 2) }}</td>
                <td class="text-right">{{ number_format($item->total_price, 2) }}</td>
                <td class="text-center">{{ $item->batch_number ?: '-' }}</td>
                <td class="text-center">{{ $item->expiry_date ? $item->expiry_date->format('d/m/Y') : '-' }}</td>
            </tr>
            @empty
            <tr>
                <td colspan="11" class="text-center">No items found</td>
            </tr>
            @endforelse
        </tbody>
    </table>

    <!-- Totals Section -->
    <div class="totals-section">
        <table>
            <tr>
                <td class="total-label">Subtotal:</td>
                <td class="total-value">{{ number_format($incoming->items->sum('total_price'), 2) }}</td>
            </tr>
            @if($incoming->vat_amount > 0)
            <tr>
                <td class="total-label">VAT ({{ number_format($incoming->vat_rate, 2) }}%):</td>
                <td class="total-value">{{ number_format($incoming->vat_amount, 2) }}</td>
            </tr>
            @endif
            <tr class="grand-total">
                <td class="total-label">Grand Total:</td>
                <td class="total-value">{{ number_format($incoming->total_amount, 2) }}</td>
            </tr>
        </table>
    </div>

    <!-- Signature Section -->
    <div class="signatures-section">
        <div class="signature-block">
            <div class="signature-line">Received By</div>
            <div class="signature-name">{{ $incoming->receiver->name ?? '________________' }}</div>
        </div>
        <div class="signature-block">
            <div class="signature-line">Approved By</div>
            <div class="signature-name">{{ $incoming->approver->name ?? '________________' }}</div>
        </div>
        <div class="signature-block">
            <div class="signature-line">Warehouse Manager</div>
            <div class="signature-name">________________</div>
        </div>
    </div>

    <!-- Footer -->
    <div class="footer">
        <p>Generated on {{ now()->format('d/m/Y H:i') }} | {{ $incoming->receipt_number }}</p>
    </div>

    <script>
        // Auto-print when page loads
        window.addEventListener('load', function() {
            setTimeout(function() {
                if (window.opener) {
                    window.print();
                }
            }, 500);
        });

        // Close window after printing
        window.addEventListener('afterprint', function() {
            if (window.opener) {
                setTimeout(function() {
                    window.close();
                }, 500);
            }
        });
    </script>
</body>
</html>
