<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    @php use Illuminate\Support\Str; @endphp
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>{{ Str::title(str_replace('_', ' ', $document->type)) }} - {{ $document->document_number }}</title>
    <style>
        html, body {
            font-family: 'DejaVu Sans', sans-serif;
            font-size: 9pt;
            margin: 0; padding: 0;
            color: #333;
        }
        body { 
            padding: 15px; 
            box-sizing: border-box;
            background-color: #f9f9f9;
        }
        .preview-container {
            width: 210mm;
            min-height: 297mm;
            margin: 0 auto;
            padding: 15mm;
            background: white;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
        }
        .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 {
            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; 
            text-align: left;
        }

        .bottom-footer { 
            width: 100%; 
            font-size: 9pt; 
            margin-top: 30px;
        }
        .bottom-footer table { width: 100%; border-collapse: collapse; }
        .bottom-footer td { width: 50%; padding-right: 20px; }
        .text-right { text-align: right; }
        
        /* Print button styling */
        .print-actions {
            text-align: center;
            margin: 20px 0;
        }
        .btn-print {
            background-color: #1a365d;
            color: white;
            border: none;
            padding: 10px 20px;
            border-radius: 4px;
            cursor: pointer;
            font-size: 12pt;
            margin-right: 10px;
        }
        .btn-download {
            background-color: #2dce89;
            color: white;
            border: none;
            padding: 10px 20px;
            border-radius: 4px;
            cursor: pointer;
            font-size: 12pt;
        }
        
        @media print {
            body {
                background: white;
                padding: 0;
            }
            .preview-container {
                width: 100%;
                padding: 0;
                margin: 0;
                box-shadow: none;
            }
            .print-actions {
                display: none;
            }
        }
    </style>
</head>
<body>
    <div class="print-actions">
        <button onclick="window.print()" class="btn-print"><i class="fas fa-print"></i> Print</button>
        <a href="{{ route('documents.download-pdf', ['type' => $document->type, 'id' => $document->id]) }}" class="btn-download"><i class="fas fa-file-pdf"></i> Download PDF</a>
    </div>
    
    <div class="preview-container">
        <div class="document-title">
            @if(isset($isReprint) && $isReprint)
                COPY - {{ Str::title(str_replace('_', ' ', $document->type)) }}
            @else
                {{ Str::title(str_replace('_', ' ', $document->type)) }}
            @endif
        </div>

        <div class="header-container">
            <table class="info-row compact-info">
                <tr>
                    <td class="info-left">
                        <span class="section-title">Company Information</span>
                        <div class="logo-full">
                            @if($company && $company->logo_path)
                                <img src="{{ asset($company->logo_path) }}" alt="Company Logo">
                            @endif
                        </div>
                        <div><span class="label">Name:</span> <span class="value">{{ $company->name ?? 'Melsoft Zim Pvt Ltd' }}</span></div>
                        <div><span class="label">Address:</span> <span class="value">{{ $company->address ?? 'Harare, Zimbabwe' }}</span></div>
                        <div><span class="label">Phone:</span> <span class="value">{{ $company->phone ?? '+263 242 700000' }}</span></div>
                        <div><span class="label">Email:</span> <span class="value">{{ $company->email ?? 'info@melsoft.co.zw' }}</span></div>
                        <div><span class="label">VAT No:</span> <span class="value">{{ $company->vat_no ?? 'N/A' }}</span></div>
                        <div><span class="label">TIN No:</span> <span class="value">{{ $company->tin_no ?? 'N/A' }}</span></div>
                        <div><span class="label">Currency:</span> <span class="value">{{ $document->currency_code }}</span></div>
                    </td>
                    <td class="info-right">
                        <span class="section-title">Supplier Information</span>
                        <div><span class="label">Name:</span> <span class="value">{{ $document->supplier->supplier_account ?? '—' }}</span></div>
                        <div><span class="label">Address:</span> <span class="value">{{ $document->supplier->address ?? '—' }}</span></div>
                        <div><span class="label">Email:</span> <span class="value">{{ $document->supplier->email ?? '—' }}</span></div>
                        <div><span class="label">Phone:</span> <span class="value">{{ $document->supplier->contact_1 ?? '—' }}</span></div>
                        <div><span class="label">Contact Person:</span> <span class="value">{{ $document->contact_person }}</span></div>
                        <div><span class="label">City:</span> <span class="value">{{ $document->supplier->city ?? '—' }}</span></div>
                        <div><span class="label">Province:</span> <span class="value">{{ $document->supplier->province ?? '—' }}</span></div>
                        <div><span class="label">VAT:</span> <span class="value">{{ $document->supplier->vat ?? 'N/A' }}</span></div>
                        <div><span class="label">TIN:</span> <span class="value">{{ $document->supplier->tin ?? 'N/A' }}</span></div>
                    </td>
                </tr>
            </table>
        </div>

        <div class="doc-info">
            <div>
                <div>Document #: <strong>{{ $document->document_number }}</strong></div>
                <div>Document Date: <strong>{{ $document->document_date->format('d M Y') }}</strong></div>
            </div>
            <div>
                @if($document->delivery_date)
                    <div>Delivery Date: <strong>{{ $document->delivery_date->format('d M Y') }}</strong></div>
                @endif
                @if($document->order_number)
                    <div>Order #: <strong>{{ $document->order_number }}</strong></div>
                @endif
            </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; width:100%;">
            <tr>
                <td class="info-left" style="padding-right:20px; vertical-align: top;">
                    <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>

        <hr style="border: none; border-bottom: 1px solid #1a365d; margin-top: 10px; margin-bottom: 20px;">

        <div class="notes-banking-row">
            <div class="notes" style="margin-bottom:20px;">
                <div class="section-title">Terms &amp; Conditions</div>
                <div>{{ $document->notes ?? 'No terms provided' }}</div>
            </div>
            <div class="signature-block" style="margin-bottom:20px; text-align: left;">
                <div><strong>Sales Representative: ________________________________________</strong></div>
            </div>
            <div class="banking-details">
                <div class="section-title">Banking Details</div>
                <div><span class="label">Bank Name:</span> {{ $company->bank_name ?? 'Standard Chartered Bank Zimbabwe' }}</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 style="width:100%; font-size:9pt;">
                <tr>
                    <td>&copy; {{ date('Y') }} Melsoft Pvt Ltd. All rights reserved.</td>
                    <td class="text-right">Printed: {{ now()->setTimezone('Africa/Harare')->format('d M Y H:i') }}</td>
                </tr>
            </table>
        </div>
    </div>
</body>
</html>
