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

requireAdmin();

$page_title = 'Manage Doctors';

// Get all doctors with search functionality
$search = clean($_GET['search'] ?? '');
$status_filter = clean($_GET['status'] ?? '');

$doctors_query = "
    SELECT d.*, s.name as specialization_name, COUNT(a.id) as total_appointments
    FROM doctors d
    LEFT JOIN specializations s ON d.specialization_id = s.id
    LEFT JOIN appointments a ON d.id = a.doctor_id
";

$params = [];
$types = '';
$where_conditions = [];

if ($search) {
    $where_conditions[] = "(d.full_name LIKE ? OR d.email LIKE ? OR d.pmc_license_number LIKE ?)";
    $search_param = "%$search%";
    $params[] = $search_param;
    $params[] = $search_param;
    $params[] = $search_param;
    $types .= 'sss';
}

if ($status_filter) {
    $where_conditions[] = "d.verification_status = ?";
    $params[] = $status_filter;
    $types .= 's';
}

if (!empty($where_conditions)) {
    $doctors_query .= " WHERE " . implode(' AND ', $where_conditions);
}

$doctors_query .= " GROUP BY d.id ORDER BY d.created_at DESC";

$doctors_stmt = $conn->prepare($doctors_query);
if (!empty($params)) {
    $doctors_stmt->bind_param($types, ...$params);
}
$doctors_stmt->execute();
$doctors = $doctors_stmt->get_result()->fetch_all(MYSQLI_ASSOC);

// Get statistics
$stats = [
    'total_doctors' => $conn->query("SELECT COUNT(*) as count FROM doctors")->fetch_assoc()['count'],
    'approved' => $conn->query("SELECT COUNT(*) as count FROM doctors WHERE verification_status = 'approved'")->fetch_assoc()['count'],
    'pending' => $conn->query("SELECT COUNT(*) as count FROM doctors WHERE verification_status = 'pending'")->fetch_assoc()['count'],
    'rejected' => $conn->query("SELECT COUNT(*) as count FROM doctors WHERE verification_status = 'rejected'")->fetch_assoc()['count']
];

include 'header.php';
?>

                <div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom">
                    <h1 class="h2">
                        <i class="fas fa-user-md text-primary"></i> All Doctors (<?= count($doctors) ?>)
                    </h1>
                </div>


                <!-- Stats Cards -->
                <div class="row mb-4">
                    <div class="col-md-6 col-xl-3">
                        <div class="stat-card card bg-primary text-white">
                            <div class="card-body">
                                <div class="d-flex justify-content-between">
                                    <div>
                                        <h4 class="card-title"><?= number_format($stats['total_doctors']) ?></h4>
                                        <p class="card-text">Total Doctors</p>
                                    </div>
                                    <div class="align-self-center">
                                        <i class="fas fa-user-md fa-2x opacity-75"></i>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="col-md-6 col-xl-3">
                        <div class="stat-card card bg-success text-white">
                            <div class="card-body">
                                <div class="d-flex justify-content-between">
                                    <div>
                                        <h4 class="card-title"><?= number_format($stats['approved']) ?></h4>
                                        <p class="card-text">Approved</p>
                                    </div>
                                    <div class="align-self-center">
                                        <i class="fas fa-check fa-2x opacity-75"></i>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="col-md-6 col-xl-3">
                        <div class="stat-card card bg-warning text-white">
                            <div class="card-body">
                                <div class="d-flex justify-content-between">
                                    <div>
                                        <h4 class="card-title"><?= number_format($stats['pending']) ?></h4>
                                        <p class="card-text">Pending</p>
                                    </div>
                                    <div class="align-self-center">
                                        <i class="fas fa-clock fa-2x opacity-75"></i>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="col-md-6 col-xl-3">
                        <div class="stat-card card bg-danger text-white">
                            <div class="card-body">
                                <div class="d-flex justify-content-between">
                                    <div>
                                        <h4 class="card-title"><?= number_format($stats['rejected']) ?></h4>
                                        <p class="card-text">Rejected</p>
                                    </div>
                                    <div class="align-self-center">
                                        <i class="fas fa-times fa-2x opacity-75"></i>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>

                <!-- Doctors Management -->
                <div class="card">
                    <div class="card-header bg-light">
                        <div class="row align-items-center">
                            <div class="col">
                                <h5 class="card-title mb-0">
                                    <i class="fas fa-user-md"></i> Doctor List
                                </h5>
                            </div>
                            <div class="col-auto">
                                <form method="GET" class="d-flex gap-2">
                                    <input type="text" name="search" class="form-control form-control-sm" placeholder="Search doctors..." value="<?= htmlspecialchars($search) ?>">
                                    <select name="status" class="form-select form-select-sm">
                                        <option value="">All Status</option>
                                        <option value="approved" <?= $status_filter === 'approved' ? 'selected' : '' ?>>Approved</option>
                                        <option value="pending" <?= $status_filter === 'pending' ? 'selected' : '' ?>>Pending</option>
                                        <option value="rejected" <?= $status_filter === 'rejected' ? 'selected' : '' ?>>Rejected</option>
                                    </select>
                                    <button type="submit" class="btn btn-primary btn-sm">Filter</button>
                                </form>
                            </div>
                        </div>
                    </div>
                    <div class="table-responsive">
                        <table class="table table-hover mb-0">
                            <thead class="table-light">
                                <tr>
                                    <th>Doctor Info</th>
                                    <th>Specialization</th>
                                    <th>PMC License</th>
                                    <th>Experience</th>
                                    <th>Fee</th>
                                    <th>Status</th>
                                    <th>Appointments</th>
                                    <th>Registered</th>
                                    <th>Actions</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php if (empty($doctors)): ?>
                                    <tr>
                                        <td colspan="9" class="text-center text-muted py-4">
                                            <i class="fas fa-user-md fa-2x mb-2"></i><br>
                                            <?= $search || $status_filter ? 'No matching doctors found' : 'No doctors available' ?>
                                        </td>
                                    </tr>
                                <?php else: ?>
                                    <?php foreach ($doctors as $doctor): ?>
                                        <tr>
                                            <td>
                                                <div class="d-flex align-items-center">
                                                    <?php if ($doctor['profile_photo']): ?>
                                                        <img src="<?= SITE_URL ?>/<?= $doctor['profile_photo'] ?>" alt="Dr. <?= htmlspecialchars($doctor['full_name']) ?>" class="rounded-circle me-2" width="35" height="35">
                                                    <?php else: ?>
                                                        <div class="bg-success text-white rounded-circle d-flex align-items-center justify-content-center me-2" style="width: 35px; height: 35px; font-size: 16px;">
                                                            <i class="fas fa-user-md"></i>
                                                        </div>
                                                    <?php endif; ?>
                                                    <div>
                                                        <strong>Dr. <?= htmlspecialchars($doctor['full_name']) ?></strong><br>
                                                        <small class="text-muted"><?= htmlspecialchars($doctor['email']) ?></small>
                                                    </div>
                                                </div>
                                            </td>
                                            <td>
                                                <span class="badge bg-light text-dark"><?= htmlspecialchars($doctor['specialization_name'] ?? 'N/A') ?></span><br>
                                                <small class="text-muted"><?= htmlspecialchars($doctor['qualification'] ?? '') ?></small>
                                            </td>
                                            <td>
                                                <code><?= htmlspecialchars($doctor['pmc_license_number']) ?></code>
                                            </td>
                                            <td>
                                                <?= $doctor['experience_years'] ?> years
                                            </td>
                                            <td>
                                                Rs. <?= number_format($doctor['consultation_fee']) ?>
                                            </td>
                                            <td>
                                                <span class="badge bg-<?= $doctor['verification_status'] === 'approved' ? 'success' : ($doctor['verification_status'] === 'pending' ? 'warning' : 'danger') ?>">
                                                    <?= ucfirst($doctor['verification_status']) ?>
                                                </span>
                                            </td>
                                            <td>
                                                <span class="badge bg-info"><?= $doctor['total_appointments'] ?? 0 ?></span>
                                            </td>
                                            <td>
                                                <small><?= date('M d, Y', strtotime($doctor['created_at'])) ?></small>
                                            </td>
                                            <td>
                                                <div class="btn-group btn-group-sm">
                                                    <a href="doctor-verification.php?id=<?= $doctor['id'] ?>" class="btn btn-outline-primary" title="View Details">
                                                        <i class="fas fa-eye"></i>
                                                    </a>
                                                    <button class="btn btn-outline-danger" onclick="return confirm('Delete this doctor?')" title="Delete">
                                                        <i class="fas fa-trash"></i>
                                                    </button>
                                                </div>
                                            </td>
                                        </tr>
                                    <?php endforeach; ?>
                                <?php endif; ?>
                            </tbody>
                        </table>
                    </div>
                </div>

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