<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Devis {{ $quote->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%;
        }
        .quote-info {
            float: right;
            width: 45%;
            text-align: right;
        }
        .client-info {
            margin: 30px 0;
            clear: both;
        }
        .table {
            width: 100%;
            border-collapse: collapse;
            margin: 20px 0;
        }
        .table th, .table td {
            border: 1px solid #ddd;
            padding: 8px;
            text-align: left;
        }
        .table th {
            background-color: #f8f9fa;
            font-weight: bold;
        }
        .text-right {
            text-align: right;
        }
        .text-center {
            text-align: center;
        }
        .total {
            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;
        }
        .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>Devis N°:</strong> {{ $quote->number }}<br>
                <strong>Date devis:</strong> {{ $quote->date->format('d/m/Y') }}
            </p>
        </div>
        <div class="quote-info">
            <p>
                <strong>Devis pour:</strong><br>
                <strong>{{ $quote->client->name }}</strong><br>
                {{ $quote->client->address }}<br>
                @if($quote->client->ice)ICE: {{ $quote->client->ice }}<br>@endif
                @if($quote->client->phone)Tél: {{ $quote->client->phone }}<br>@endif
                @if($quote->client->email)Email: {{ $quote->client->email }}@endif
            </p>
        </div>
    </div>

    <table class="table">
        <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($quote->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">
        <div class="total-row">
            <span>Sous-total HT:</span>
            <span>{{ number_format($quote->subtotal_ht, 2) }} MAD</span>
        </div>
        <div class="total-row">
            <span>TVA:</span>
            <span>{{ number_format($quote->total_vat, 2) }} MAD</span>
        </div>
        <div class="total-row final">
            <span>Total TTC:</span>
            <span>{{ number_format($quote->total_ttc, 2) }} MAD</span>
        </div>
    </div>

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

    @if($quote->notes || $quote->terms)
    <div class="notes" style="margin-top: 40px; clear: both;">
        @if($quote->notes)
        <h4>Notes:</h4>
        <p>{{ $quote->notes }}</p>
        @endif
        
        @if($quote->terms)
        <h4>Conditions:</h4>
        <p>{{ $quote->terms }}</p>
        @endif
    </div>
    @endif

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