<?php
session_start();
error_reporting(E_ALL);
ini_set('display_errors', 1);

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

include '../include/config.php';

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

// Get appointments for this doctor
$appointments = $conn->query("
    SELECT a.*, CONCAT(p.first_name, ' ', p.last_name) as patient_name, p.email, p.phone
    FROM appointments a
    JOIN patients p ON a.patient_id = p.patient_id
    WHERE a.doctor_id = '$doctor_id'
    ORDER BY a.appointment_date DESC
");

$page_title = 'Appointments | UHD';
include '../include/header.php';
?>

<style>
    .appointment-card {
        background: white;
        border-radius: 16px;
        padding: 20px;
        margin-bottom: 15px;
        border-left: 4px solid #d4a017;
        box-shadow: 0 2px 8px rgba(0,0,0,0.05);
    }
    .status-badge {
        padding: 4px 12px;
        border-radius: 20px;
        font-size: 0.75rem;
        font-weight: 600;
    }
    .status-scheduled { background: #fff3cd; color: #856404; }
    .status-completed { background: #d4edda; color: #155724; }
    .status-cancelled { background: #f8d7da; color: #721c24; }
</style>

<div class="container mt-4">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <h2><i class="fas fa-calendar-alt me-2" style="color: #d4a017;"></i> My Appointments</h2>
        <a href="dashboard.php" class="btn btn-secondary">Back to Dashboard</a>
    </div>

    <?php if ($appointments && $appointments->num_rows > 0): ?>
        <div class="row">
            <?php while ($apt = $appointments->fetch_assoc()): ?>
                <div class="col-md-6">
                    <div class="appointment-card">
                        <div class="d-flex justify-content-between align-items-start">
                            <div>
                                <h6 class="mb-1"><strong><?= htmlspecialchars($apt['patient_name']) ?></strong></h6>
                                <small class="text-muted">
                                    <i class="fas fa-envelope me-1"></i> <?= htmlspecialchars($apt['email'] ?? 'N/A') ?>
                                </small>
                                <br>
                                <small class="text-muted">
                                    <i class="fas fa-phone me-1"></i> <?= htmlspecialchars($apt['phone'] ?? 'N/A') ?>
                                </small>
                            </div>
                            <div>
                                <span class="status-badge status-<?= $apt['status'] ?? 'scheduled' ?>">
                                    <?= ucfirst($apt['status'] ?? 'Scheduled') ?>
                                </span>
                            </div>
                        </div>
                        <hr>
                        <div class="row">
                            <div class="col-6">
                                <i class="fas fa-calendar-day me-1 text-muted"></i>
                                <small><?= date('M d, Y', strtotime($apt['appointment_date'])) ?></small>
                            </div>
                            <div class="col-6">
                                <i class="fas fa-clock me-1 text-muted"></i>
                                <small><?= date('h:i A', strtotime($apt['appointment_time'] ?? '09:00:00')) ?></small>
                            </div>
                        </div>
                        <?php if ($apt['reason']): ?>
                            <div class="mt-2">
                                <small class="text-muted"><strong>Reason:</strong> <?= htmlspecialchars($apt['reason']) ?></small>
                            </div>
                        <?php endif; ?>
                    </div>
                </div>
            <?php endwhile; ?>
        </div>
    <?php else: ?>
        <div class="alert alert-info text-center">
            <i class="fas fa-inbox fa-2x mb-2"></i>
            <p>No appointments found.</p>
        </div>
    <?php endif; ?>
</div>

<?php include '../include/footer.php'; ?>