<?php
session_start();
require_once 'config/database.php';
require_once 'includes/auth.php';
require_once 'includes/translation.php';

if (!isset($_SESSION['user_id'])) {
    redirect('login.php');
}

// Get the number of rows to print (default 20)
$rows = isset($_GET['rows']) ? (int)$_GET['rows'] : 20;
$rows = max(10, min(100, $rows)); // Between 10 and 100 rows
?>

<!DOCTYPE html>
<html lang="ar" dir="rtl">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>جدول متابعة المرضى - للطباعة</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        @page {
            size: A4 landscape;
            margin: 15mm;
        }
        
        body {
            font-family: 'Arial', sans-serif;
            font-size: 10px;
            direction: rtl;
            line-height: 1.2;
        }
        
        .page-container {
            width: 100%;
        }
        
        .header {
            display: flex;
            align-items: center;
            justify-content: space-between;
            margin-bottom: 15px;
            padding-bottom: 10px;
            border-bottom: 2px solid #0d6efd;
        }
        
        .logo-section {
            display: flex;
            align-items: center;
            gap: 15px;
        }
        
        .logo {
            width: 60px;
            height: 60px;
            border-radius: 50%;
        }
        
        .hospital-info h1 {
            font-size: 16px;
            color: #0d6efd;
            margin-bottom: 2px;
        }
        
        .hospital-info h2 {
            font-size: 12px;
            color: #666;
            font-weight: normal;
        }
        
        .date-info {
            text-align: left;
            font-size: 9px;
            color: #666;
        }
        
        .content {
            margin-bottom: 20px;
        }
        
        .table-title {
            text-align: center;
            margin-bottom: 10px;
            font-size: 14px;
            font-weight: bold;
            color: #0d6efd;
        }
        
        .monitoring-table {
            width: 100%;
            border-collapse: collapse;
            margin-bottom: 20px;
        }
        
        .monitoring-table th {
            background-color: #0d6efd;
            color: white;
            border: 1px solid #333;
            text-align: center;
            vertical-align: middle;
            font-weight: bold;
            padding: 6px 3px;
            font-size: 9px;
            line-height: 1.1;
        }
        
        .monitoring-table td {
            border: 1px solid #333;
            padding: 6px 3px;
            vertical-align: middle;
            text-align: center;
            height: 25px;
            font-size: 9px;
        }
        
        .footer {
            display: flex;
            justify-content: space-between;
            align-items: flex-end;
            margin-top: 20px;
            padding-top: 15px;
            border-top: 1px solid #ddd;
            font-size: 10px;
            page-break-inside: avoid;
        }
        
        .signature-section {
            display: flex;
            flex-direction: column;
            align-items: center;
            min-width: 200px;
        }
        
        .signature-line {
            border-bottom: 1px solid #333;
            width: 180px;
            height: 20px;
            margin-bottom: 5px;
        }
        
        .signature-label {
            font-weight: bold;
            color: #333;
        }
        
        @media print {
            body {
                font-size: 9px;
            }
            
            .no-print {
                display: none !important;
            }
            
            .page-container {
                page-break-inside: avoid;
            }
            
            .footer {
                page-break-inside: avoid;
                page-break-before: avoid;
            }
            
            .monitoring-table th {
                font-size: 8px;
                padding: 4px 2px;
            }
            
            .monitoring-table td {
                font-size: 8px;
                padding: 4px 2px;
                height: 20px;
            }
        }
        
        .print-controls {
            position: fixed;
            top: 10px;
            left: 10px;
            z-index: 1000;
            background: white;
            padding: 10px;
            border-radius: 5px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
            border: 1px solid #ddd;
        }
        
        .print-controls button, .print-controls select {
            margin: 0 5px;
            padding: 5px 10px;
            border: 1px solid #ddd;
            border-radius: 3px;
            cursor: pointer;
            font-size: 12px;
        }
        
        .btn-primary {
            background: #007bff;
            color: white;
            border-color: #007bff;
        }
        
        .btn-secondary {
            background: #6c757d;
            color: white;
            border-color: #6c757d;
        }
    </style>
</head>
<body>
    <!-- Print Controls -->
    <div class="print-controls no-print">
        <button class="btn-primary" onclick="window.print()">🖨️ طباعة</button>
        <button class="btn-secondary" onclick="window.location.href='key_performance_indicators.php'">↩️ العودة</button>
        <select id="rowCount" onchange="changeRows()">
            <option value="10" <?php echo $rows == 10 ? 'selected' : ''; ?>>10 صفوف</option>
            <option value="15" <?php echo $rows == 15 ? 'selected' : ''; ?>>15 صف</option>
            <option value="20" <?php echo $rows == 20 ? 'selected' : ''; ?>>20 صف</option>
            <option value="25" <?php echo $rows == 25 ? 'selected' : ''; ?>>25 صف</option>
            <option value="30" <?php echo $rows == 30 ? 'selected' : ''; ?>>30 صف</option>
            <option value="40" <?php echo $rows == 40 ? 'selected' : ''; ?>>40 صف</option>
            <option value="50" <?php echo $rows == 50 ? 'selected' : ''; ?>>50 صف</option>
            <option value="75" <?php echo $rows == 75 ? 'selected' : ''; ?>>75 صف</option>
            <option value="100" <?php echo $rows == 100 ? 'selected' : ''; ?>>100 صف</option>
        </select>
    </div>

    <div class="page-container">
        <!-- Header -->
        <div class="header">
            <div class="logo-section">
                <img src="assets/images/1222f7f3-2c1c-4177-8f88-794699164d74.png" alt="Hospital Logo" class="logo">
                <div class="hospital-info">
                    <h1>مستشفى مدينة نصر التخصصي</h1>
                    <h2>Nasr City Specialized Hospital</h2>
                </div>
            </div>
            <div class="date-info">
                <div><strong>التاريخ:</strong> <?php echo date('Y-m-d'); ?></div>
                <div><strong>الوقت:</strong> <?php 
                    date_default_timezone_set('Africa/Cairo');
                    echo date('h:i A'); 
                ?></div>
                <div><strong>رقم الصفحة:</strong> 1</div>
            </div>
        </div>

        <!-- Content -->
        <div class="content">
            <div class="table-title">
                جدول متابعة المرضى<br>
                <span style="font-size: 12px; color: #666;">Patient Monitoring Table</span>
            </div>

            <!-- Monitoring Table -->
            <table class="monitoring-table">
                <thead>
                    <tr>
                        <th style="width: 12%;">Date<br>التاريخ</th>
                        <th style="width: 20%;">Patient Name<br>اسم المريض</th>
                        <th style="width: 12%;">Medical NO<br>الرقم الطبي</th>
                        <th style="width: 18%;">Consultant name<br>اسم الاستشاري</th>
                        <th style="width: 15%;">Location<br>الموقع</th>
                        <th style="width: 23%;">Notes<br>ملاحظات</th>
                    </tr>
                </thead>
                <tbody id="tableBody">
                    <?php for ($i = 0; $i < $rows; $i++): ?>
                    <tr>
                        <td></td>
                        <td></td>
                        <td></td>
                        <td></td>
                        <td></td>
                        <td></td>
                    </tr>
                    <?php endfor; ?>
                </tbody>
            </table>
        </div>

        <!-- Footer -->
        <div class="footer">
            <div class="signature-section">
                <div class="signature-line"></div>
                <div class="signature-label">
                    <strong>Pharmacist Signature</strong><br>
                    <strong>توقيع الصيدلي</strong>
                </div>
            </div>
            
            <div style="text-align: center; flex: 1;">
                <div style="font-size: 9px; color: #666;">
                    نظام الصيدلة الإكلينيكية المتكامل (ICS) - مستشفى مدينة نصر التخصصي<br>
                    Integrated Clinical System (ICS) - Nasr City Specialized Hospital
                </div>
            </div>
            
            <div class="signature-section">
                <div class="signature-line"></div>
                <div class="signature-label">
                    <strong>Unit Manager Signature</strong><br>
                    <strong>توقيع مدير القسم</strong>
                </div>
            </div>
        </div>
    </div>

    <script>
        function changeRows() {
            const rowCount = document.getElementById('rowCount').value;
            window.location.href = `print_simple_monitoring.php?rows=${rowCount}`;
        }
        
        // Auto print when loaded from print button
        if (document.referrer.includes('key_performance_indicators.php')) {
            setTimeout(() => {
                window.print();
            }, 500);
        }
    </script>
</body>
</html>

