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

// Check admin login
if (!isset($_SESSION['user_id']) || ($_SESSION['role'] ?? '') !== 'admin') {
    header('Location: ../login.php');
    exit;
}

// Fetch doctors
$doctors = [];
$dq = mysqli_query($conn, "SELECT * FROM doctors ORDER BY id DESC");
if ($dq) {
    while ($row = mysqli_fetch_assoc($dq)) {
        $doctors[] = $row;
    }
}

// Fetch doctor visits (FIXED QUERY)
$visits = [];
$vq = mysqli_query($conn, "
    SELECT 
        dv.id,
        d.name AS doctor_name,
        u.name AS executive_name,
        dv.visit_datetime AS visit_date,
        dv.remarks AS notes,
        dv.visit_status AS status
    FROM doctor_visits dv
    LEFT JOIN doctors d ON dv.doctor_id = d.id
    LEFT JOIN users u ON dv.user_id = u.id
    ORDER BY dv.id DESC
");

if ($vq) {
    while ($row = mysqli_fetch_assoc($vq)) {
        $visits[] = $row;
    }
}
?>

<!DOCTYPE html>
<html>
<head>
    <title>Doctor Reports</title>

    <link rel="stylesheet" href="../assets/style.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.0/css/all.min.css">

    <style>
     /* ===== BASE ===== */
body {
    margin: 0;
    background: #eef2f7;
    font-family: 'Segoe UI', sans-serif;
    color: #0f172a;
}

/* ===== LAYOUT ===== */
.container {
    margin-left: 250px;
    padding: 70px 15px;
}

/* ===== CARD ===== */
.card {
    background: #fff;
    border-radius: 14px;
    padding: 14px;
    margin-bottom: 15px;
    border: 1px solid rgba(148,163,184,.15);
    box-shadow: 0 6px 18px rgba(15,23,42,.05);
    transition: 0.2s;
}

.card:hover {
    transform: translateY(-2px);
}

/* ===== TITLE ===== */
h2 {
    font-size: 16px;
    font-weight: 700;
    margin-bottom: 10px;
    display: flex;
    align-items: center;
    gap: 8px;
}

/* ===== INFO ===== */
.info {
    font-size: 12px;
    color: #64748b;
    margin-bottom: 10px;
}

/* ===== TABLE ===== */
table {
    width: 100%;
    border-collapse: collapse;
    font-size: 12px;
    border-radius: 10px;
    overflow: hidden;
}

/* ===== HEADER ===== */
table th {
    background: #374155;
    color: #fff;
    padding: 8px;
    text-align: left;
    font-weight: 600;
    font-size: 11px;
}

/* ===== BODY ===== */
table td {
    padding: 8px;
    border-bottom: 1px solid #e2e8f0;
    vertical-align: middle;
}

/* ===== ROW HOVER ===== */
table tr:hover {
    background: #f4f8ff;
}

/* ===== BADGES ===== */
.badge {
    padding: 4px 8px;
    border-radius: 999px;
    font-size: 10px;
    font-weight: 600;
}

/* ===== STATUS COLORS ===== */
.success {
    background: #dcfce7;
    color: #166534;
}

.pending {
    background: #fef3c7;
    color: #92400e;
}

/* ===== HEADER ===== */
.page-header {
    margin-bottom: 15px;
}

.page-header h1 {
    font-size: 20px;
    font-weight: 800;
    display: flex;
    align-items: center;
    gap: 8px;
    margin-bottom: 3px;
    color: #374155;
}

.page-header p {
    color: #64748b;
    font-size: 12px;
    margin: 0;
}

/* ===== RESPONSIVE ===== */
@media (max-width: 768px) {
    .container {
        margin-left: 0;
        padding: 10px;
    }

    h2 {
        font-size: 14px;
    }
}
    </style>
</head>

<body>

<?php include '../header.php'; ?>
<?php include 'adminsidebar.php'; ?>

<div class="container">
<div class="page-header">
    <h1><i class="fa-solid fa-user-doctor"></i> Doctors Report</h1>
    <p>Overview of doctors and their visit activities</p>
</div>
    <!-- DOCTOR LIST -->
    <div class="card">
        <h2><i class="fa fa-user-md"></i> Doctors List</h2>
        <div class="info">Total Doctors: <?= count($doctors) ?></div>

        <table>
            <tr>
                <th>ID</th>
                <th>Name</th>
                <th>Specialization</th>
                <th>Phone</th>
                <th>City</th>
            </tr>

            <?php foreach ($doctors as $d): ?>
                <tr>
                    <td><?= $d['id'] ?></td>
                    <td><?= htmlspecialchars($d['name']) ?></td>
                    <td><?= htmlspecialchars($d['specialization']) ?></td>
                    <td><?= isset($d['mobile']) ? htmlspecialchars($d['mobile']) : '-' ?></td>
                    <td><?= htmlspecialchars($d['city']) ?></td>
                </tr>
            <?php endforeach; ?>
        </table>
    </div>

    <!-- DOCTOR VISITS -->
    <div class="card">
        <h2><i class="fa fa-stethoscope"></i> Doctor Visit Details</h2>
        <div class="info">Total Visits: <?= count($visits) ?></div>

        <table>
            <tr>
                <th>ID</th>
                <th>Doctor</th>
                <th>Executive</th>
                <th>Visit Date</th>
                <th>Notes</th>
                <th>Status</th>
            </tr>

            <?php foreach ($visits as $v): ?>
                <tr>
                    <td><?= $v['id'] ?? '-' ?></td>

                    <td><?= htmlspecialchars($v['doctor_name'] ?? '-') ?></td>

                    <td><?= htmlspecialchars($v['executive_name'] ?? '-') ?></td>

                    <td>
                        <?php
                        if (!empty($v['visit_date'])) {
                            echo date('d M Y h:i A', strtotime($v['visit_date']));
                        } else {
                            echo '-';
                        }
                        ?>
                    </td>

                    <td><?= htmlspecialchars($v['notes'] ?? '-') ?></td>

                    <td>
                        <?php
                        $status = strtolower(trim($v['status'] ?? 'pending'));

                        if ($status == 'visited') {
                            echo '<span class="badge success">Visited</span>';
                        } else {
                            echo '<span class="badge pending">Pending</span>';
                        }
                        ?>
                    </td>

                </tr>
            <?php endforeach; ?>

        </table>
    </div>

</div>

</body>
</html>