<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Prescription - {{ $prescription->prescription_number }}</title>
    <style>
        /* FONT REGISTRATION */
        @font-face {
            font-family: 'Dancing Script';
            src: url("{{ base_path('storage/fonts/HomemadeApple-Regular.ttf') }}") format('truetype');
            font-weight: 400;
            font-style: normal;
        }
        @font-face {
            font-family: 'Kalam';
            src: url("{{ base_path('storage/fonts/Kalam-Regular.ttf') }}") format('truetype');
            font-weight: 400;
            font-style: normal;
        }

        @page { margin: 15mm; size: A4; background: white; }
        
        body {
            font-family: Arial, sans-serif;
            font-size: 11px;
            margin: 0;
            padding: 0;
            line-height: 1.4;
            color: #333;
        }

        .container { width: 100%; }
        .table-full { width: 100%; border-collapse: collapse; }
        
        .handwritten {
            font-family: 'Dancing Script', cursive;
            font-size: 13px;
            color: #1a2a3a;
        }

        .header-section { margin-bottom: 20px; }
        .letterhead { width: 100%; max-height: 120px; object-fit: contain; }

        .info-box {
            border: 1px solid #ddd;
            padding: 8px;
            vertical-align: top;
        }

        .prescription-table th {
            border-bottom: 2px solid #333;
            padding: 8px 2px;
            text-align: left;
            font-size: 10px;
        }

        .prescription-table td {
            border-bottom: 1px solid #eee;
            padding: 6px 2px;
            vertical-align: middle;
        }

        .stamp-image {
            position: absolute;
            right: 15mm;
            bottom: 30mm;
            max-width: 120px;
            max-height: 80px;
            opacity: 0.8;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="header-section">
            @if($letterhead)
                <img src="{{ $letterhead }}" alt="Header" class="letterhead">
            @else
                <div style="height: 40px;"></div>
            @endif
        </div>

        <table class="table-full" style="margin-bottom: 15px;">
            <tr>
                <td class="info-box" style="width: 48%;">
                    <h3 style="margin: 0 0 5px 0; font-size: 12px;">Patient Information</h3>
                    <table class="table-full" style="font-size: 10px;">
                        <tr>
                            <td><strong>ID:</strong> {{ $patient->patient_id }}</td>
                            <td><strong>DOB:</strong> {{ $patient->date_of_birth ? \Carbon\Carbon::parse($patient->date_of_birth)->format('m/d/Y') : 'N/A' }}</td>
                        </tr>
                        <tr>
                            <td><strong>Name:</strong> {{ $patient->full_name }}</td>
                            <td><strong>Gender:</strong> {{ $patient->gender ?? 'N/A' }}</td>
                        </tr>
                        <tr>
                            <td><strong>Age:</strong> {{ $patient->date_of_birth ? \Carbon\Carbon::parse($patient->date_of_birth)->age : 'N/A' }} yrs</td>
                            <td><strong>Payment:</strong> {{ $patient->payment_mode }}</td>
                        </tr>
                    </table>
                </td>
                <td style="width: 4%;"></td>
                <td class="info-box" style="width: 48%;">
                    <h3 style="margin: 0 0 5px 0; font-size: 12px;">Patient Vitals</h3>
                    @if($vitals)
                    <table class="table-full" style="font-size: 10px;">
                        <tr><td><strong>BP:</strong> {{ $vitals->blood_pressure ?? 'N/A' }}</td><td><strong>Resp:</strong> {{ $vitals->resp_pattern ?? 'N/A' }}</td></tr>
                        <tr><td><strong>Pulse:</strong> {{ $vitals->pulse_rate ?? 'N/A' }}</td><td><strong>SpO2:</strong> {{ $vitals->spo2 ?? 'N/A' }}%</td></tr>
                        <tr><td><strong>RBS:</strong> {{ $vitals->rbs ?? 'N/A' }}%</td><td><strong>Temp:</strong> {{ $vitals->temperature ?? 'N/A' }}°C</td></tr>
                    </table>
                    @endif
                </td>
            </tr>
        </table>

        <div style="padding: 5px 0; margin-bottom: 10px;">
            <h3 style="margin: 0; font-size: 12px;">Diagnosis:</h3>
            <p class="handwritten" style="margin: 5px 0;">{{ $prescription->diagnosis ?? 'N/A' }}</p>
        </div>

        <table class="table-full prescription-table">
            <thead>
                <tr>
                    <th colspan="7" style="border: none; font-size: 26px; padding: 10px 0; font-weight: normal;">Rx</th>
                </tr>
              
            </thead>
            <tbody>
                @foreach($items as $item)
                <tr>
                    <td>{{ $item->item_number }}</td>
                    <td class="handwritten">{{ $item->medicine_name }}</td>
                    <td class="handwritten">{{ $item->quantity ?? 'N/A' }}</td>
                    <td class="handwritten">{{ $item->dosage ?? 'N/A' }}</td>
                    <td class="handwritten">{{ $item->frequency }}</td>
                    <td class="handwritten">{{ $item->instructions ?? 'N/A' }}</td>
                    <td class="handwritten">{{ $item->duration }}</td>
                </tr>
                @endforeach
            </tbody>
        </table>

        @if($prescription->additional_prescription_details)
        <div style="margin-top: 15px; font-size: 10px;">
            <h3 style="margin: 0; font-size: 11px;">Additional Details:</h3>
            <p>{{ $prescription->additional_prescription_details }}</p>
        </div>
        @endif

        <div style="margin-top: 50px;">
            @if($signature)
                <div style="text-align: left; margin-bottom: 5px;">
                    <img src="{{ $signature }}" alt="Signature" style="max-height: 60px;">
                </div>
            @endif
            
            <table class="table-full" style="border-top: 1px solid #333; padding-top: 10px;">
                <tr>
                    <td style="width: 60%; vertical-align: top;">
                        <strong style="font-size: 13px;">{{ $doctor->full_name }}</strong><br>
                        {{ $doctor->qualification ?? $doctor->email }} <br>
                        Practice Number: {{ $doctor->practice_number }}
                    </td>
                    <td style="width: 40%; text-align: right; vertical-align: top;">
                        <strong>Date:</strong> {{ $prescription->created_at->format('F d, Y') }}<br>
                        <strong>Prescription #:</strong> {{ $prescription->prescription_number }}
                    </td>
                </tr>
            </table>
        </div>

        @if($stamp)
            <img src="{{ $stamp }}" alt="Stamp" class="stamp-image">
        @endif
    </div>
</body>
</html>