<?php
session_start();
require_once '../config.php';

if (!isset($_SESSION['doctor'])) {
    header('Location: login.php');
    exit;
}

$doctor_id = $_SESSION['doctor'];
$doctor_name = $_SESSION['doctor_name'];

// Get ALL appointments for this doctor
$allAppointments = $conn->query("
    SELECT a.*, p.name AS patient_name, p.mobile 
    FROM opd_appointments a 
    JOIN opd_patients p ON a.patient_id = p.id 
    WHERE a.doctor_id = $doctor_id 
    ORDER BY a.appointment_date DESC, a.serial_no ASC
");

// Count stats
$total = $allAppointments->num_rows;
$pending = $confirmed = $completed = 0;
$apps = [];
while ($a = $allAppointments->fetch_assoc()) {
    $apps[] = $a;
    if ($a['status'] == 'pending') $pending++;
    if ($a['status'] == 'confirmed') $confirmed++;
    if ($a['status'] == 'completed') $completed++;
}
?>
<!DOCTYPE html>
<html lang="bn">
<head>
    <meta charset="UTF-8">
    <title>Doctor Dashboard | স্বাস্থ্য কানন</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" rel="stylesheet">
    <style>
        :root{--p:#2d6a4f}
        body{background:#f0f2f5;font-family:'Hind Siliguri',sans-serif}
        .header{background:var(--p);color:#fff;padding:15px 0}
        .card{border:none;border-radius:15px;box-shadow:0 3px 15px rgba(0,0,0,.08)}
        .card-header{background:var(--p);color:#fff;border-radius:15px 15px 0 0;padding:12px 20px;font-weight:600}
        .btn-approve{background:#25D366;color:#fff}
        .btn-reject{background:#dc3545;color:#fff}
        .status-badge{padding:5px 12px;border-radius:20px;font-size:.8rem}
    </style>
</head>
<body>

<div class="header">
    <div class="container d-flex justify-content-between">
        <h5>👨‍⚕️ Dr. <?=$doctor_name?> | Dashboard</h5>
        <a href="logout.php" class="btn btn-outline-light btn-sm">Logout</a>
    </div>
</div>

<div class="container py-4">
    <!-- Stats Cards -->
    <div class="row mb-4">
        <div class="col-md-3"><div class="card p-3 text-center"><h3 class="text-primary"><?=$total?></h3><small>মোট</small></div></div>
        <div class="col-md-3"><div class="card p-3 text-center"><h3 class="text-warning"><?=$pending?></h3><small>পেন্ডিং</small></div></div>
        <div class="col-md-3"><div class="card p-3 text-center"><h3 class="text-success"><?=$confirmed?></h3><small>কনফার্ম</small></div></div>
        <div class="col-md-3"><div class="card p-3 text-center"><h3 class="text-info"><?=$completed?></h3><small>সম্পন্ন</small></div></div>
    </div>

    <!-- All Appointments -->
    <div class="card">
        <div class="card-header">সকল অ্যাপয়েন্টমেন্ট (<?=$total?>)</div>
        <div class="card-body">
            <?php if ($total > 0): ?>
            <div class="table-responsive">
                <table class="table table-hover">
                    <thead>
                        <tr><th>তারিখ</th><th>রোগী</th><th>মোবাইল</th><th>সিরিয়াল</th><th>স্ট্যাটাস</th><th>পেমেন্ট</th><th>একশন</th></tr>
                    </thead>
                    <tbody>
                        <?php foreach ($apps as $a): ?>
                        <tr>
                            <td><?=date('d/m/Y', strtotime($a['appointment_date']))?></td>
                            <td><strong><?=$a['patient_name']?></strong></td>
                            <td><?=$a['mobile']?></td>
                            <td><span class="badge bg-dark">#<?=$a['serial_no']?></span></td>
                            <td>
                                <?php if($a['status']=='pending'):?><span class="status-badge bg-warning">পেন্ডিং</span>
                                <?php elseif($a['status']=='confirmed'):?><span class="status-badge bg-success text-white">কনফার্ম</span>
                                <?php elseif($a['status']=='completed'):?><span class="status-badge bg-info text-white">সম্পন্ন</span>
                                <?php elseif($a['status']=='cancelled'):?><span class="status-badge bg-danger text-white">বাতিল</span>
                                <?php endif;?>
                            </td>
                            <td><?=$a['payment_status']=='paid'?'✅':'❌'?></td>
                            <td>
                                <?php if ($a['status'] == 'pending'): ?>
                                    <a href="approve.php?id=<?=$a['id']?>&action=approve" class="btn btn-approve btn-sm">Approve</a>
                                    <a href="approve.php?id=<?=$a['id']?>&action=reject" class="btn btn-reject btn-sm">Reject</a>
                                <?php elseif ($a['status'] == 'confirmed'): ?>
                                    <?php if ($a['meeting_link']): ?>
                                        <a href="<?=$a['meeting_link']?>" target="_blank" class="btn btn-primary btn-sm">Join Video</a>
                                    <?php endif; ?>
                                    <a href="prescription.php?appt_id=<?=$a['id']?>" class="btn btn-warning btn-sm">Rx</a>
                                    <a href="billing.php?appt_id=<?=$a['id']?>" class="btn btn-info btn-sm">Bill</a>
                                <?php endif; ?>
                            </td>
                        </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
            <?php else: ?>
                <p class="text-center py-4 text-muted">কোন অ্যাপয়েন্টমেন্ট নেই।</p>
            <?php endif; ?>
        </div>
    </div>
</div>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>