<?php
// Filters
$search_name = $_GET['search_name'] ?? '';
$search_phone = $_GET['search_phone'] ?? '';
$search_doctor = $_GET['search_doctor'] ?? '';
$start_date = $_GET['start_date'] ?? date('Y-m-01');
$end_date = $_GET['end_date'] ?? date('Y-m-t');

// Pagination
$page = isset($_GET['page']) && is_numeric($_GET['page']) ? (int)$_GET['page'] : 1;
$limit = 10;
$offset = ($page - 1) * $limit;

// Base Query
$query = "
    SELECT 
        v.id as visit_id,
        v.sick_leave_start_date,
        v.sick_leave_days,
        p.id as patient_number,
        p.name as patient_name,
        p.phone as patient_phone,
        e.name_$lang as doctor_name
    FROM visits v
    JOIN patients p ON v.patient_id = p.id
    JOIN employees e ON v.doctor_id = e.id
    WHERE v.sick_leave_days IS NOT NULL AND v.sick_leave_days > 0
";
$params = [];

// Apply Filters
if (!empty($search_name)) {
    $query .= " AND p.name LIKE ?";
    $params[] = "%$search_name%";
}
if (!empty($search_phone)) {
    $query .= " AND p.phone LIKE ?";
    $params[] = "%$search_phone%";
}
if (!empty($search_doctor)) {
    $query .= " AND v.doctor_id = ?";
    $params[] = $search_doctor;
}
if (!empty($start_date) && !empty($end_date)) {
    $query .= " AND DATE(v.sick_leave_start_date) BETWEEN ? AND ?";
    $params[] = $start_date;
    $params[] = $end_date;
}

// Count total for pagination
$count_query = "SELECT COUNT(*) FROM (" . $query . ") as total_count";
$stmt = $db->prepare($count_query);
$stmt->execute($params);
$total_records = $stmt->fetchColumn();
$total_pages = ceil($total_records / $limit);

// Add sorting and pagination
$query .= " ORDER BY v.sick_leave_start_date DESC LIMIT $limit OFFSET $offset";
$stmt = $db->prepare($query);
$stmt->execute($params);
$sick_leaves = $stmt->fetchAll(PDO::FETCH_ASSOC);

// Fetch doctors for filter
$stmt = $db->query("SELECT e.id, e.name_$lang as name FROM employees e JOIN positions po ON e.position_id = po.id WHERE po.name_en = 'Doctor' ORDER BY e.name_$lang");
$doctors = $stmt->fetchAll(PDO::FETCH_ASSOC);

?>

<div class="d-flex justify-content-between align-items-center mb-4">
    <h3 class="fw-bold text-secondary"><?php echo __('sick_leave_report'); ?></h3>
    <button onclick="window.print()" class="btn btn-outline-dark no-print">
        <i class="bi bi-printer"></i> <?php echo __('print'); ?>
    </button>
</div>

<!-- Filters -->
<div class="card border-0 shadow-sm mb-4 no-print">
    <div class="card-body">
        <form method="GET" class="row g-3 align-items-end">
            <div class="col-md-3">
                <label class="form-label text-muted small"><?php echo __('filter_by_patient_name'); ?></label>
                <input type="text" name="search_name" class="form-control" value="<?php echo htmlspecialchars($search_name); ?>" placeholder="<?php echo __('patient'); ?>...">
            </div>
            <div class="col-md-2">
                <label class="form-label text-muted small"><?php echo __('filter_by_telephone'); ?></label>
                <input type="text" name="search_phone" class="form-control" value="<?php echo htmlspecialchars($search_phone); ?>" placeholder="<?php echo __('telephone'); ?>...">
            </div>
            <div class="col-md-2">
                <label class="form-label text-muted small"><?php echo __('filter_by_doctor'); ?></label>
                <select name="search_doctor" class="form-select">
                    <option value=""><?php echo __('all'); ?></option>
                    <?php foreach ($doctors as $doc): ?>
                        <option value="<?php echo $doc['id']; ?>" <?php echo $search_doctor == $doc['id'] ? 'selected' : ''; ?>>
                            <?php echo htmlspecialchars($doc['name']); ?>
                        </option>
                    <?php endforeach; ?>
                </select>
            </div>
            <div class="col-md-3">
                <label class="form-label text-muted small"><?php echo __('filter_by_date_range'); ?></label>
                <div class="input-group">
                    <input type="date" name="start_date" class="form-control" value="<?php echo $start_date; ?>">
                    <span class="input-group-text border-0 bg-light">-</span>
                    <input type="date" name="end_date" class="form-control" value="<?php echo $end_date; ?>">
                </div>
            </div>
            <div class="col-md-2">
                <button type="submit" class="btn btn-primary w-100">
                    <i class="bi bi-search"></i> <?php echo __('search'); ?>
                </button>
            </div>
        </form>
    </div>
</div>

<!-- Print Header -->
<div class="print-only d-none mb-4">
    <h2 class="text-center"><?php echo __('sick_leave_report'); ?></h2>
    <p class="text-center text-muted">
        <?php echo __('report_period'); ?>: <?php echo $start_date; ?> <?php echo __('to'); ?> <?php echo $end_date; ?><br>
        <?php echo __('generated_on'); ?>: <?php echo date('Y-m-d H:i'); ?>
    </p>
</div>

<!-- Table -->
<div class="card border-0 shadow-sm">
    <div class="card-body p-0">
        <div class="table-responsive">
            <table class="table table-hover align-middle mb-0">
                <thead class="table-light text-secondary">
                    <tr>
                        <th class="px-4 py-3"><?php echo __('patient_number'); ?></th>
                        <th class="py-3"><?php echo __('patient'); ?></th>
                        <th class="py-3 text-center"><?php echo __('date_issued'); ?></th>
                        <th class="py-3 text-center"><?php echo __('number_of_days'); ?></th>
                        <th class="px-4 py-3 text-end"><?php echo __('issued_by'); ?></th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (empty($sick_leaves)): ?>
                        <tr>
                            <td colspan="5" class="text-center py-4 text-muted">
                                <i class="bi bi-inbox fs-2 d-block mb-2"></i>
                                <?php echo __('no_data_found'); ?>
                            </td>
                        </tr>
                    <?php else: ?>
                        <?php foreach ($sick_leaves as $leave): ?>
                            <tr>
                                <td class="px-4 fw-semibold text-primary">
                                    #<?php echo str_pad($leave['patient_number'], 6, '0', STR_PAD_LEFT); ?>
                                </td>
                                <td>
                                    <div class="fw-semibold text-dark"><?php echo htmlspecialchars($leave['patient_name']); ?></div>
                                    <small class="text-muted"><i class="bi bi-telephone"></i> <?php echo htmlspecialchars($leave['patient_phone'] ?: '-'); ?></small>
                                </td>
                                <td class="text-center">
                                    <span class="badge bg-light text-dark border">
                                        <i class="bi bi-calendar-event me-1"></i>
                                        <?php echo date('Y-m-d', strtotime($leave['sick_leave_start_date'])); ?>
                                    </span>
                                </td>
                                <td class="text-center">
                                    <span class="badge bg-info bg-opacity-10 text-info fw-bold fs-6 border border-info border-opacity-25">
                                        <?php echo htmlspecialchars($leave['sick_leave_days']); ?> <?php echo __('days'); ?>
                                    </span>
                                </td>
                                <td class="px-4 text-end">
                                    <span class="text-dark"><i class="bi bi-person-badge text-muted me-1"></i> <?php echo htmlspecialchars($leave['doctor_name']); ?></span>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

<!-- Pagination -->
<?php if ($total_pages > 1): ?>
<div class="d-flex justify-content-between align-items-center mt-4 no-print">
    <span class="text-muted small">
        <?php echo __('showing'); ?> <?php echo $offset + 1; ?> <?php echo __('to'); ?> <?php echo min($offset + $limit, $total_records); ?> <?php echo __('of'); ?> <?php echo $total_records; ?> <?php echo __('entries'); ?>
    </span>
    <nav aria-label="Page navigation">
        <ul class="pagination pagination-sm mb-0">
            <li class="page-item <?php echo $page <= 1 ? 'disabled' : ''; ?>">
                <a class="page-link" href="?page=<?php echo $page - 1; ?>&search_name=<?php echo urlencode($search_name); ?>&search_phone=<?php echo urlencode($search_phone); ?>&search_doctor=<?php echo urlencode($search_doctor); ?>&start_date=<?php echo $start_date; ?>&end_date=<?php echo $end_date; ?>">
                    <?php echo __('previous'); ?>
                </a>
            </li>
            
            <?php
            // Simple pagination logic
            $start_page = max(1, $page - 2);
            $end_page = min($total_pages, $page + 2);
            
            for ($i = $start_page; $i <= $end_page; $i++):
            ?>
                <li class="page-item <?php echo $page == $i ? 'active' : ''; ?>">
                    <a class="page-link" href="?page=<?php echo $i; ?>&search_name=<?php echo urlencode($search_name); ?>&search_phone=<?php echo urlencode($search_phone); ?>&search_doctor=<?php echo urlencode($search_doctor); ?>&start_date=<?php echo $start_date; ?>&end_date=<?php echo $end_date; ?>">
                        <?php echo $i; ?>
                    </a>
                </li>
            <?php endfor; ?>
            
            <li class="page-item <?php echo $page >= $total_pages ? 'disabled' : ''; ?>">
                <a class="page-link" href="?page=<?php echo $page + 1; ?>&search_name=<?php echo urlencode($search_name); ?>&search_phone=<?php echo urlencode($search_phone); ?>&search_doctor=<?php echo urlencode($search_doctor); ?>&start_date=<?php echo $start_date; ?>&end_date=<?php echo $end_date; ?>">
                    <?php echo __('next'); ?>
                </a>
            </li>
        </ul>
    </nav>
</div>
<?php endif; ?>

<style>
@media print {
    /* Hide Non-Printable Elements */
    #sidebar, .navbar, .btn, form, footer, .no-print, .pagination, .d-flex.justify-content-between.mt-4 {
        display: none !important;
    }
    
    /* Layout Adjustments for Print */
    .main-content {
        margin-left: 0 !important;
        padding: 0 !important;
        width: 100% !important;
    }
    
    body {
        background: white !important;
        font-size: 12pt;
    }
    
    .card {
        border: none !important;
        box-shadow: none !important;
    }
    
    .print-only {
        display: block !important;
    }

    /* Table Styling for Print */
    .table {
        width: 100% !important;
        border-collapse: collapse !important;
    }
    
    .table th, .table td {
        border: 1px solid #ddd !important;
        padding: 8px !important;
    }
}
</style>
