<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    @php use Illuminate\Support\Str; @endphp
    <title>DELIVERY NOTE - {{ $document->document_number }}</title>
    <style>
        @page { size: A4; margin: 15px; }
        html, body {
            font-family: 'DejaVu Sans', sans-serif;
            font-size: 9pt;
            margin: 0; padding: 0;
            color: #333;
        }
        body { padding: 15px; box-sizing: border-box; }
        .document-title { 
            text-align: center; 
            font-size: 15pt; 
            font-weight: bold; 
            color: #1a365d; 
            margin-bottom: 15px;
            text-transform: uppercase; 
        }
        .header-container { border-bottom: 1px solid #e2e8f0; padding-bottom: 15px; margin-bottom: 15px; }
        .logo-full { text-align: left; margin-bottom: 1px; }
        .logo-full img { max-width: 100px; }

        .info-row { width: 100%; border-collapse: collapse; }
        .info-row td { padding: 5px; vertical-align: top; }
        .info-left, .info-right { width: 50%; }
        .info-left { text-align: left; }
        .info-right { text-align: right; }
        .compact-info { font-size: 10pt; }

        .label, .value { 
            display: inline-block; 
            vertical-align: bottom; 
            font-size: 9pt; 
            font-family: 'DejaVu Sans', sans-serif; 
        }
    
        .label {
            display: inline-block;
            vertical-align: bottom;
            font-family: 'DejaVu Sans', sans-serif;
            font-size: 9pt;
            font-weight: normal;
            color: #1a365d;
        }

        .section-title { 
            font-size: 10pt; 
            font-weight: bold; 
            color: #1a365d; 
            text-transform: uppercase; 
            margin-bottom: 5px; 
            border-bottom: 1px solid #1a365d; 
            padding-bottom: 2px; 
            display: block; 
        }

        .doc-info { 
            display: flex; 
            justify-content: space-between; 
            font-size: 9pt; 
            padding: 10px; 
            background-color: #f8fafc; 
            border: 1px solid #e2e8f0; 
            border-radius: 4px; 
            margin-bottom: 15px; 
        }
        .doc-info div { line-height: 1.5; }
        .items-table { width: 100%; border-collapse: collapse; font-size: 9pt; margin-bottom: 15px; }
        .items-table th { background-color: #1a365d; color: #fff; padding: 8px; text-align: center; }
        .items-table td { border: 1px solid #e2e8f0; padding: 8px; text-align: center; }
        .items-table tr:nth-child(even) { background-color: #f8fafc; }

        .totals-panel table { width: auto; margin-left: auto; font-size: 9pt; }
        .totals-panel td { padding: 4px 0; }
        .total-row { border-top: 1px solid #333; font-weight: bold; }

        .signature-line { border-top: 1px dotted #333; margin: 20px 0 5px; }
        .signature-block { font-size: 9pt; }

        .notes-banking-row { 
            width: 100%; 
            display: flex; 
            justify-content: space-between; 
            gap: 10px; 
            margin-top: 20px; 
        }
        .notes, .banking-details { 
            width: 49%; 
            padding: 10px; 
            background-color: #f8fafc; 
            border: 1px solid #e2e8f0; 
            border-radius: 4px; 
            font-size: 9pt; 
        }

        .bottom-footer { 
            width: 100%; 
            font-size: 9pt; 
            position: fixed; 
            bottom: 15px; 
            left: 15px; 
            right: 15px; 
        }
        .bottom-footer table { width: 100%; border-collapse: collapse; }
        .bottom-footer td { width: 50%; padding-right: 20px; }
        .text-right { text-align: right; }
    </style>
</head>
<body>
    <div class="document-title">DELIVERY NOTE</div>

    <div class="header-container">
        <div class="logo-full">
            @if($company && $company->logo_path)
                <img src="file://{{ public_path($company->logo_path) }}" alt="Company Logo">
            @endif
        </div>
        <table class="info-row compact-info">
            <tr>
                <td class="info-left">
                    <span class="section-title">Company Information</span>
                    <div><span class="label">Name: </span><span class="value">{{ $company->company_name }}</span></div>
                    <div><span class="label">Address: </span><span class="value">{{ $company->address }}</span></div>
                    <div><span class="label">Phone: </span><span class="value">{{ $company->phone }}</span></div>
                    <div><span class="label">Email: </span><span class="value">{{ $company->email }}</span></div>
                    <div><span class="label">VAT No: </span><span class="value">{{ $company->vat_no }}</span></div>
                    <div><span class="label">TIN No: </span><span class="value">{{ $company->tin_no }}</span></div>
                    <div><span class="label">Customer Currency: </span><span class="value">{{ $document->currency_code }}</span></div>
                </td>
                <td class="info-right">
                    <span class="section-title">Customer Information</span>
                    <div><span class="label">Customer Name: </span><span class="value">{{ $document->customer->customer_account ?? '—' }}</span></div>
                    <div><span class="label">Customer Address: </span><span class="value">{{ $document->customer->address ?? '—' }}</span></div>
                    <div><span class="label">Customer Email: </span><span class="value">{{ $document->customer->email ?? '—' }}</span></div>
                    <div><span class="label">Customer Phone: </span><span class="value">{{ $document->customer->contact_1 ?? '—' }}</span></div>
                    <div><span class="label">Customer Contact Person: </span><span class="value">{{ $document->contact_person }}</span></div>
                    <div><span class="label">Customer City: </span><span class="value">{{ $document->customer->city ?? '—' }}</span></div>
                    <div><span class="label">Customer Province: </span><span class="value">{{ $document->customer->province ?? '—' }}</span></div>
                    <div><span class="label">Customer VAT: </span><span class="value">{{ $document->customer->vat }}</span></div>
                    <div><span class="label">Customer TIN: </span><span class="value">{{ $document->customer->tin }}</span></div>
                </td>
            </tr>
        </table>
    </div>

    <div class="doc-info">
        <div>
            <div>Delivery Note #: <strong>DN-{{ substr($document->document_number, 3) }}</strong></div>
            <div>Order Date: <strong>{{ $document->document_date->format('d M Y') }}</strong></div>
        </div>
        <div>
            <div>Order #: <strong>{{ $document->order_number }}</strong></div>
            <div>Delivery Date: <strong>{{ $document->delivery_date->format('d M Y') }}</strong></div>
        </div>
    </div>

    <table class="items-table">
        <thead>
            <tr>
                <th>HS Code</th>
                <th>Code</th>
                <th>Description</th>
                <th>Additional Description</th>
                <th>Qty</th>
                <th>UOM Description</th>
                <th>Price (incl)</th>
                <th>Disc %</th>
                <th>Tax</th>
                <th>Total (incl)</th>
            </tr>
        </thead>
        <tbody>
            @foreach($document->lines as $line)
                <tr>
                    <td>{{ $line->hs_code }}</td>
                    <td>
                        @if($line->module === 'GL')
                            {{ optional($line->account)->code }}
                        @else
                            {{ optional($line->item)->code }}
                        @endif
                    </td>
                    <td>
                        @if($line->module === 'GL')
                            {{ optional($line->account)->account_name }}
                        @else
                            {{ optional($line->item)->description1 }}
                        @endif
                    </td>
                    <td>{{ $line->description }}</td>
                    <td>{{ $line->confirmed_quantity }}</td>
                    <td>{{ optional($line->uom)->description ?? '' }}</td>
                    <td>{{ number_format($line->price, 2) }}</td>
                    <td>{{ number_format($line->discount_percent, 2) }}%</td>
                    <td>{{ number_format($line->line_tax, 2) }}</td>
                    <td>{{ number_format($line->line_total, 2) }}</td>
                </tr>
            @endforeach
        </tbody>
    </table>

    <table class="info-row compact-info" style="margin-top:20px;">
        <tr>
            <td class="info-left" style="padding-right:20px;">
                <div class="signature-block">
                    <div><strong>Received by: _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _  </strong></div>     
                </div>
                <div class="signature-block" style="margin-top:20px;">
                    <div><strong>Date: _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _</strong></div>
                </div>
                 <div class="signature-block" style="margin-top:20px;">
                    <div><strong>Signed:   _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ </strong></div>
                </div>
                
            </td>
            <td class="info-right" style="vertical-align:top;">
                <table style="width:auto; margin-left:auto; font-size:9pt;">
                    <tr>
                        <td><strong>Exclusive Total:</strong></td>
                        <td class="text-right">
                            {{ optional($document->currency)->symbol ?? $document->currency_code }}
                            {{ number_format($document->exclusive_total, 2) }}
                        </td>
                    </tr>
                    <tr>
                        <td><strong>Tax:</strong></td>
                        <td class="text-right">
                            {{ optional($document->currency)->symbol ?? $document->currency_code }}
                            {{ number_format($document->tax_amount, 2) }}
                        </td>
                    </tr>
                    <tr>
                        <td><strong>Discount:</strong></td>
                        <td class="text-right">
                            {{ optional($document->currency)->symbol ?? $document->currency_code }}
                            {{ number_format($document->discount_total, 2) }}
                        </td>
                    </tr>
                    <tr class="total-row">
                        <td><strong>Inclusive Total:</strong></td>
                        <td class="text-right">
                            {{ optional($document->currency)->symbol ?? $document->currency_code }}
                            {{ number_format($document->inclusive_total, 2) }}
                        </td>
                    </tr>
                </table>
            </td>
        </tr>
    </table>

    <div class="notes-banking-row">
        <div class="notes">
            <div class="section-title">Terms &amp; Conditions</div>
            <div>{{ $document->notes ?? 'No terms provided' }}</div>
        </div>
        <br>
        <div class="signature-block">
            <div><strong>Sales Representative:.....................................................</strong></div>
        </div>
        <br>
        <div class="banking-details">
            <div class="section-title">Banking Details</div>
            <div><span class="label">Bank Name:</span> {{ $company->bank_name }}</div>
            <div><span class="label">Account Name:</span> {{ $company->account_name }}</div>
            <div><span class="label">Account Number:</span> {{ $company->account_number }}</div>
            <div><span class="label">Branch:</span> {{ $company->branch_name }}</div>
        </div>
        
    </div>
    

    <div class="bottom-footer">
        <table>
            <tr>
                <td>&copy; {{ date('Y') }} {{ $company->company_name }}. All rights reserved.</td>
                <td class="text-right">Printed: {{ now()->setTimezone('Africa/Harare')->format('d M Y H:i') }}</td>
            </tr>
        </table>
    </div>
</body>
</html>
