<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Facture {{ $invoice->number }}</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            font-size: 12px;
            line-height: 1.4;
            color: #333;
        }
        .header {
            border-bottom: 2px solid #007bff;
            padding-bottom: 20px;
            margin-bottom: 30px;
        }
        .company-info {
            float: left;
            width: 50%;
        }
        .invoice-info {
            float: right;
            width: 45%;
            text-align: right;
        }
        .client-info {
            margin: 30px 0;
            clear: both;
        }
        .invoice-details {
            width: 100%;
            border-collapse: collapse;
            margin: 20px 0;
        }
        .invoice-details th,
        .invoice-details td {
            border: 1px solid #ddd;
            padding: 8px;
            text-align: left;
        }
        .invoice-details th {
            background-color: #f8f9fa;
            font-weight: bold;
        }
        .text-right {
            text-align: right;
        }
        .text-center {
            text-align: center;
        }
        .total-section {
            float: right;
            width: 300px;
            margin-top: 20px;
        }
        .total-row {
            display: flex;
            justify-content: space-between;
            padding: 5px 0;
        }
        .total-row.final {
            border-top: 2px solid #333;
            font-weight: bold;
            font-size: 14px;
        }
        .notes {
            margin-top: 40px;
            clear: both;
        }
        .footer {
            margin-top: 50px;
            padding-top: 20px;
            border-top: 1px solid #ddd;
            clear: both;
            font-size: 11px;
            color: #666;
        }
        .footer-fixed {
            position: fixed;
            bottom: 0;
            left: 0;
            right: 0;
            height: 60px;
            background: white;
            border-top: 1px solid #ddd;
            padding: 10px 20px;
            font-size: 10px;
            color: #666;
            z-index: 1000;
        }
        @page {
            margin-bottom: 80px;
        }
        body {
            margin-bottom: 80px;
        }
        .clearfix::after {
            content: "";
            display: table;
            clear: both;
        }
    </style>
</head>
<body>
    <div class="header clearfix">
        <div class="company-info">
            <p>
                <strong>{{ $company ? $company->name : 'Mon Entreprise' }}</strong><br>
                @if($company && $company->address){{ $company->address }}@else Ville @endif<br>
                @if($company && $company->phone){{ $company->phone }}@else Téléphone @endif<br>
                <strong>Facture N°:</strong> {{ $invoice->number }}<br>
                <strong>Date facture:</strong> {{ $invoice->date->format('d/m/Y') }}
            </p>
        </div>
        <div class="invoice-info">
            <p>
                <strong>{{ $invoice->type === 'sale' ? 'Facturé à:' : 'Facturé par:' }}</strong><br>
                <strong>{{ $invoice->client ? $invoice->client->name : $invoice->supplier->name }}</strong><br>
                {{ $invoice->client ? $invoice->client->address : $invoice->supplier->address }}<br>
                @if($invoice->client)
                    @if($invoice->client->ice)ICE: {{ $invoice->client->ice }}<br>@endif
                    @if($invoice->client->phone)Tél: {{ $invoice->client->phone }}<br>@endif
                    @if($invoice->client->email)Email: {{ $invoice->client->email }}@endif
                @else
                    @if($invoice->supplier->ice)ICE: {{ $invoice->supplier->ice }}<br>@endif
                    @if($invoice->supplier->phone)Tél: {{ $invoice->supplier->phone }}<br>@endif
                    @if($invoice->supplier->email)Email: {{ $invoice->supplier->email }}@endif
                @endif
            </p>
        </div>
    </div>

    <table class="invoice-details">
        <thead>
            <tr>
                <th>Produit/Service</th>
                <th>Description</th>
                <th class="text-center">Qté</th>
                <th class="text-right">Prix unitaire</th>
                <th class="text-center">TVA</th>
                <th class="text-right">Total HT</th>
                <th class="text-right">Total TTC</th>
            </tr>
        </thead>
        <tbody>
            @foreach($invoice->items as $item)
            <tr>
                <td>{{ $item->product_name }}</td>
                <td>{{ $item->description }}</td>
                <td class="text-center">{{ $item->quantity }} {{ $item->unit }}</td>
                <td class="text-right">{{ number_format($item->unit_price, 2) }} MAD</td>
                <td class="text-center">{{ $item->vat_rate }}%</td>
                <td class="text-right">{{ number_format($item->total_ht, 2) }} MAD</td>
                <td class="text-right">{{ number_format($item->total_ttc, 2) }} MAD</td>
            </tr>
            @endforeach
        </tbody>
    </table>

    <div class="total-section">
        <div class="total-row">
            <span>Sous-total HT:</span>
            <span>{{ number_format($invoice->subtotal_ht, 2) }} MAD</span>
        </div>
        <div class="total-row">
            <span>TVA:</span>
            <span>{{ number_format($invoice->total_vat, 2) }} MAD</span>
        </div>
        <div class="total-row final">
            <span>Total TTC:</span>
            <span>{{ number_format($invoice->total_ttc, 2) }} MAD</span>
        </div>
        @if($invoice->paid_amount > 0)
        <div class="total-row">
            <span>Montant payé:</span>
            <span>{{ number_format($invoice->paid_amount, 2) }} MAD</span>
        </div>
        <div class="total-row">
            <span>Reste à payer:</span>
            <span>{{ number_format($invoice->remaining_amount, 2) }} MAD</span>
        </div>
        @endif
    </div>

    <div class="amount-in-words" style="margin-top: 20px; clear: both; border: 1px solid #ddd; padding: 10px; background-color: #f9f9f9;">
        <strong>ARRÊTÉE LA PRÉSENTE FACTURE À LA SOMME DE :</strong><br>
        <strong>{{ \App\Helpers\NumberToWords::convert($invoice->total_ttc) }}</strong>
    </div>

    @if($invoice->notes || $invoice->terms)
    <div class="notes">
        @if($invoice->notes)
        <h4>Notes:</h4>
        <p>{{ $invoice->notes }}</p>
        @endif
        
        @if($invoice->terms)
        <h4>Conditions:</h4>
        <p>{{ $invoice->terms }}</p>
        @endif
    </div>
    @endif

    @if($company && $company->footer_template)
    <div class="footer-fixed">
        {!! nl2br(e($company->footer_template)) !!}
    </div>
    @endif
</body>
</html>