<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Inventory Stock Level Report</title>
    <style>
        @page { 
            size: A4; 
            margin: 15px 15px 30px 15px;
        }
        html, body {
            font-family: 'DejaVu Sans', sans-serif;
            font-size: 9pt;
            margin: 0; padding: 0;
            color: #333;
            position: relative;
        }
        body { 
            padding: 15px; 
            box-sizing: border-box; 
            padding-bottom: 40px;
        }

        .pdf-page { 
            page-break-after: always; 
            position: relative;
            min-height: 100%;
        }
        .pdf-page:last-child { page-break-after: auto; }

        .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; 
        }

        .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; 
        }

        .logo-full { text-align: left; margin-bottom: 1px; }
        .logo-full img { max-width: 100px; }

        .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; }

        .total-row { 
            font-weight: bold; 
            background-color: #1a365d;
            color: #fff;
        }
        
        .item-total-row {
            font-weight: bold;
            background-color: #e2e8f0;
        }

        .negative { color: red; }
        .text-right { text-align: right; }
        
        /* Footer styles */
        .footer {
            position: absolute;
            bottom: -25px;
            left: 0;
            right: 0;
            text-align: center;
            font-size: 8pt;
            color: #666;
            border-top: 1px solid #e2e8f0;
            padding-top: 5px;
        }
        .footer table {
            width: 100%;
        }
        .footer td {
            padding: 0;
        }
        .footer .text-right {
            text-align: right;
        }
    </style>
</head>
<body>
    <div class="pdf-page">
        <div class="document-title">
            Inventory Stock Level Report
        </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="file://{{ public_path($company->logo_path) }}" alt="Company Logo">
                            @endif
                        </div>
                        <div><span class="label">Name: </span><span class="value">{{ $company->name ?? 'Company Name' }}</span></div>
                        <div><span class="label">Address: </span><span class="value">{{ $company->address ?? 'Company Address' }}</span></div>
                        <div><span class="label">Phone: </span><span class="value">{{ $company->phone ?? 'Company Phone' }}</span></div>
                        <div><span class="label">Email: </span><span class="value">{{ $company->email ?? 'Company Email' }}</span></div>
                        @if($company && $company->vat_no)
                            <div><span class="label">VAT No: </span><span class="value">{{ $company->vat_no }}</span></div>
                        @endif
                        @if($company && $company->tin_no)
                            <div><span class="label">TIN No: </span><span class="value">{{ $company->tin_no }}</span></div>
                        @endif
                    </td>
                    <td class="info-right">
                        <span class="section-title">Report Information</span>
                        <div><span class="label">Period:</span> <span class="value">{{ $dateFrom }} to {{ $dateTo }}</span></div>
                        <div><span class="label">Report Date:</span> <span class="value">{{ date('d M Y') }}</span></div>
                    </td>
                </tr>
            </table>
        </div>

        <table class="items-table">
            <thead>
                <tr>
                    <th>Item Code</th>
                    <th>Item Description</th>
                    <th>Warehouse</th>
                    <th>Category</th>
                    <th>Qty Available</th>
                </tr>
            </thead>
            <tbody>
                @php
                    $currentItem = null;
                @endphp
                
                @foreach($stockData as $data)
                    @if($currentItem != $data['item']->id)
                        @if($currentItem !== null)
                            <tr class="item-total-row">
                                <td colspan="4" class="text-right">Total for {{ $currentItemCode }} - {{ $currentItemDescription }}</td>
                                <td class="text-right">{{ number_format($itemTotals[$currentItem], 2) }}</td>
                            </tr>
                        @endif
                        
                        @php
                            $currentItem = $data['item']->id;
                            $currentItemCode = $data['item']->code;
                            $currentItemDescription = $data['item']->description1;
                        @endphp
                    @endif
                    
                    <tr>
                        <td>{{ $data['item']->code }}</td>
                        <td>{{ $data['item']->description1 }}</td>
                        <td>{{ $data['warehouse']->description ?? 'N/A' }}</td>
                        <td>{{ $data['category']->name ?? 'N/A' }}</td>
                        <td class="text-right">{{ number_format($data['available_quantity'], 2) }}</td>
                    </tr>
                @endforeach
                
                @if($currentItem !== null)
                    <tr class="item-total-row">
                        <td colspan="4" class="text-right">Total for {{ $currentItemCode }} - {{ $currentItemDescription }}</td>
                        <td class="text-right">{{ number_format($itemTotals[$currentItem], 2) }}</td>
                    </tr>
                @endif
            </tbody>
        </table>
        
        <div class="footer">
            <table>
                <tr>
                    <td>&copy; {{ date('Y') }} Melsoft (Private) Limited. 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>