<?php
// Fetch Stats
$total_patients = $db->query("SELECT COUNT(*) FROM patients")->fetchColumn();
$today_appointments = $db->query("SELECT COUNT(*) FROM appointments WHERE DATE(start_time) = CURDATE()")->fetchColumn();
$total_visits = $db->query("SELECT COUNT(*) FROM visits")->fetchColumn();
$total_revenue = $db->query("SELECT SUM(total_amount) FROM bills WHERE status = 'Paid'")->fetchColumn() ?: 0;
$pending_revenue = $db->query("SELECT SUM(total_amount) FROM bills WHERE status = 'Pending'")->fetchColumn() ?: 0;
$total_xrays = $db->query("SELECT COUNT(*) FROM xray_inquiries")->fetchColumn();
$total_labs = $db->query("SELECT COUNT(*) FROM laboratory_inquiries")->fetchColumn();

$patients_sql = "
    SELECT p.*, ic.name_$lang as insurance_name 
    FROM patients p 
    LEFT JOIN insurance_companies ic ON p.insurance_company_id = ic.id 
    ORDER BY p.id DESC LIMIT 5";
$patients = $db->query($patients_sql)->fetchAll();

$appointments_sql = "
    SELECT a.*, p.name as patient_name, d.name_$lang as doctor_name 
    FROM appointments a 
    JOIN patients p ON a.patient_id = p.id 
    JOIN doctors d ON a.doctor_id = d.id 
    ORDER BY a.start_time DESC 
    LIMIT 5";
$appointments = $db->query($appointments_sql)->fetchAll();
?>

<!-- Dashboard Stats -->
<div class="row mb-4">
    <div class="col-md-3 mb-3">
        <div class="card stat-card h-100">
            <i class="bi bi-people"></i>
            <h3><?php echo $total_patients; ?></h3>
            <p class="text-muted mb-0"><?php echo __('total_patients'); ?></p>
        </div>
    </div>
    <div class="col-md-3 mb-3">
        <div class="card stat-card h-100">
            <i class="bi bi-calendar-check"></i>
            <h3><?php echo $today_appointments; ?></h3>
            <p class="text-muted mb-0"><?php echo __('today_appointments'); ?></p>
        </div>
    </div>
    <div class="col-md-3 mb-3">
        <div class="card stat-card h-100">
            <i class="bi bi-flask text-info"></i>
            <h3><?php echo $total_labs; ?></h3>
            <p class="text-muted mb-0"><?php echo __('laboratory'); ?> <?php echo __('inquiries'); ?></p>
        </div>
    </div>
    <div class="col-md-3 mb-3">
        <div class="card stat-card h-100">
            <i class="bi bi-x-diamond text-primary"></i>
            <h3><?php echo $total_xrays; ?></h3>
            <p class="text-muted mb-0"><?php echo __('xray'); ?> <?php echo __('inquiries'); ?></p>
        </div>
    </div>
</div>

<div class="row mb-4">
    <div class="col-md-6 mb-3">
        <div class="card stat-card h-100">
            <i class="bi bi-currency-dollar text-success"></i>
            <h3>$<?php echo number_format($total_revenue, 2); ?></h3>
            <p class="text-muted mb-0"><?php echo __('revenue'); ?></p>
        </div>
    </div>
    <div class="col-md-6 mb-3">
        <div class="card stat-card h-100">
            <i class="bi bi-hourglass-split text-warning"></i>
            <h3>$<?php echo number_format($pending_revenue, 2); ?></h3>
            <p class="text-muted mb-0"><?php echo __('pending'); ?></p>
        </div>
    </div>
</div>

<!-- Quick Actions -->
<div class="row mb-4">
    <div class="col-12">
        <div class="card p-3 d-flex flex-row justify-content-between align-items-center">
            <h5 class="mb-0 fw-bold"><?php echo __('dashboard'); ?></h5>
            <div class="d-flex flex-wrap gap-2">
                <button class="btn btn-primary btn-sm" data-bs-toggle="modal" data-bs-target="#addPatientModal">
                    <i class="bi bi-plus-lg"></i> <?php echo __('add_patient'); ?>
                </button>
                <button class="btn btn-success btn-sm" data-bs-toggle="modal" data-bs-target="#bookAppointmentModal">
                    <i class="bi bi-calendar-plus"></i> <?php echo __('book_appointment'); ?>
                </button>
                <button class="btn btn-info btn-sm text-white" data-bs-toggle="modal" data-bs-target="#recordVisitModal">
                    <i class="bi bi-clipboard-plus"></i> <?php echo __('add_visit'); ?>
                </button>
                <button class="btn btn-warning btn-sm text-white" data-bs-toggle="modal" data-bs-target="#addXrayInquiryModal">
                    <i class="bi bi-x-diamond"></i> <?php echo __('add_xray_inquiry'); ?>
                </button>
            </div>
        </div>
    </div>
</div>

<!-- Tables Section -->
<div class="row">
    <div class="col-lg-6 mb-4">
        <div class="card shadow-sm h-100">
            <div class="card-header py-3">
                <h6 class="mb-0 fw-bold text-white"><i class="bi bi-people-fill me-2"></i> <?php echo __('patients'); ?></h6>
            </div>
            <div class="card-body p-0">
                <div class="table-responsive">
                    <table class="table table-hover mb-0">
                        <thead>
                            <tr>
                                <th><?php echo __('name'); ?></th>
                                <th><?php echo __('age'); ?></th>
                                <th><?php echo __('phone'); ?></th>
                                <th><?php echo __('insurance'); ?></th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($patients as $p): ?>
                            <tr>
                                <td><?php echo htmlspecialchars($p['name']); ?></td>
                                <td><?php echo calculate_age($p['dob']); ?></td>
                                <td><?php echo htmlspecialchars($p['phone']); ?></td>
                                <td><span class="badge <?php echo $p['insurance_name'] ? 'bg-primary' : 'bg-secondary'; ?>"><?php echo $p['insurance_name'] ?: __('not_insured'); ?></span></td>
                            </tr>
                            <?php endforeach; if (empty($patients)): ?>
                            <tr><td colspan="4" class="text-center py-4 text-muted">No patients found.</td></tr>
                            <?php endif; ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
    <div class="col-lg-6 mb-4">
        <div class="card shadow-sm h-100">
            <div class="card-header py-3">
                <h6 class="mb-0 fw-bold text-white"><i class="bi bi-calendar-event-fill me-2"></i> <?php echo __('appointments'); ?></h6>
            </div>
            <div class="card-body p-0">
                <div class="table-responsive">
                    <table class="table table-hover mb-0">
                        <thead>
                            <tr>
                                <th><?php echo __('patient'); ?></th>
                                <th><?php echo __('doctor'); ?></th>
                                <th><?php echo __('date'); ?></th>
                                <th><?php echo __('status'); ?></th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($appointments as $a): ?>
                            <tr>
                                <td><?php echo htmlspecialchars($a['patient_name']); ?></td>
                                <td><?php echo htmlspecialchars($a['doctor_name']); ?></td>
                                <td><?php echo date('M d, H:i', strtotime($a['start_time'])); ?></td>
                                <td><span class="badge <?php echo $a['status'] === 'Completed' ? 'bg-success' : 'bg-secondary'; ?>"><?php echo __($a['status']); ?></span></td>
                            </tr>
                            <?php endforeach; if (empty($appointments)): ?>
                            <tr><td colspan="4" class="text-center py-4 text-muted">No appointments found.</td></tr>
                            <?php endif; ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>