<?php require 'functions.php'; 
$user = current_user();
if (!$user) header('Location: ../index.php');
$full_name = htmlspecialchars(trim($user['first_name'] . ' ' . $user['last_name']));
?>
<!DOCTYPE html>
<html lang="en" data-bs-theme="light">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>School Mail - <?= $full_name ?></title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.0/css/all.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Roboto:wght@400;500&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/4.1.1/animate.min.css"/>
    <style>
        body { 
            background: #f6f8fc; 
            font-family: 'Roboto', Arial, sans-serif; 
            margin: 0;
            padding: 0;
        }
        .sidebar { 
            width: 280px; 
            background: #fff; 
            border-right: 1px solid #dadce0; 
            height: 100vh; 
            position: fixed; 
            top: 0; 
            left: 0; 
            overflow-y: auto; 
            padding-top: 16px; 
            z-index: 10; 
        }
        .sidebar .nav-link {
            color: #5f6368 !important;
            font-weight: 500;
            border-radius: 0 24px 24px 0;
            margin: 2px 8px;
            padding: 10px 20px !important;
            transition: all 0.2s;
        }
        .sidebar .nav-link:hover {
            background-color: #f1f3f4 !important;
            color: #202124 !important;
        }
        .sidebar .nav-link.active {
            background-color: #fce8e6 !important;
            color: #d93025 !important;
            font-weight: 600;
        }
        .sidebar .badge {
            font-size: 0.75rem;
        }
        .main-content { 
            margin-left: 280px; 
            min-height: 100vh; 
            padding: 16px;
        }
        .message-row { 
            border-bottom: 1px solid #e0e0e0; 
            padding: 12px 20px; 
            cursor: pointer; 
            display: flex; 
            align-items: center; 
            gap: 12px;
            transition: background 0.2s;
            position: relative;
        }
        .message-row:hover { 
            background: #f1f3f4; 
            box-shadow: 0 1px 2px rgba(60,64,67,0.3), 0 1px 3px 1px rgba(60,64,67,0.15); 
        }
        .message-row.unread { 
            font-weight: 500; 
            background: #f8f9fa; 
        }
        .message-row.unread::before { 
            content: ''; 
            width: 5px; 
            height: 100%; 
            background: #1a73e8; 
            position: absolute; 
            left: 0; 
            border-radius: 0 3px 3px 0;
        }
        .avatar-placeholder { 
            width: 40px; 
            height: 40px; 
            background: #e8eaed; 
            border-radius: 50%; 
            display: flex; 
            align-items: center; 
            justify-content: center; 
            font-weight: 500; 
            font-size: 16px; 
            color: #5f6368;
            flex-shrink: 0;
        }
        .star-btn { 
            color: #dadce0; 
            cursor: pointer; 
            font-size: 18px;
        }
        .star-btn.active { 
            color: #fbbc04 !important; 
        }
        .compose-btn { 
            background: #d93025 !important; 
            color: white !important; 
            font-weight: 500;
        }
        .compose-btn-floating { 
            position: fixed; 
            bottom: 32px; 
            right: 32px; 
            width: 56px; 
            height: 56px; 
            border-radius: 50%; 
            background: #d93025; 
            color: white; 
            box-shadow: 0 4px 12px rgba(0,0,0,0.25); 
            z-index: 1000; 
            font-size: 24px;
            border: none;
        }
        .thread-body {
            background: #fff;
            border-top: 1px solid #dadce0;
        }
        #user-suggestions, #external-suggestions {
            top: 100%;
            left: 0;
            border-radius: 8px;
            margin-top: 4px;
            z-index: 1000;
        }
        .user-select:hover {
            background-color: #f1f3f4;
        }
        .dark-mode body { background: #202124; }
        .dark-mode .sidebar, .dark-mode .bg-white { background: #202124 !important; color: #e8eaed; border-color: #3c4043; }
        .dark-mode .message-row { border-color: #3c4043; }
        .dark-mode .message-row:hover { background: #292c30; }
        .dark-mode .avatar-placeholder { background: #3c4043; color: #9aa0a6; }
        .dark-mode .thread-body { background: #292c30; }

        /* Validation styles */
        .email-feedback {
            font-size: 0.875rem;
        }
        .is-valid {
            border-color: #28a745 !important;
            box-shadow: 0 0 0 0.2rem rgba(40,167,69,0.25);
        }
        .is-invalid {
            border-color: #dc3545 !important;
            box-shadow: 0 0 0 0.2rem rgba(220,53,69,0.25);
        }

    </style>
    <script>
    // Request notification permission
    if ('Notification' in window && Notification.permission === 'default') {
        Notification.requestPermission();
    }
    </script>
</head>
<body>

<!-- Sidebar -->
<div class="sidebar">
    <div class="px-4 py-3 mb-4">
        <button class="btn compose-btn btn-lg rounded-pill w-100 py-3 shadow compose-btn-main mb-2">
            <i class="fas fa-plus me-2"></i> Internal Compose
        </button>
        <button class="btn btn-outline-primary rounded-pill w-100 py-3" onclick="window.location.href='external_mail.php'">
            <i class="fas fa-globe me-2"></i> External Email
        </button>
    </div>

    <ul class="nav flex-column px-2">
        <li class="nav-item mb-1">
            <a class="nav-link active" href="#" data-folder="inbox">
                <i class="fas fa-inbox me-3"></i> Inbox
                <span class="badge bg-danger rounded-pill ms-auto" id="sidebar-unread">0</span>
            </a>
        </li>
        <li class="nav-item mb-1">
            <a class="nav-link" href="#" data-folder="sent">
                <i class="fas fa-paper-plane me-3"></i> Sent
            </a>
        </li>
        <li class="nav-item mb-1">
            <a class="nav-link" href="#" data-folder="starred">
                <i class="fas fa-star me-3"></i> Starred
            </a>
        </li>
        <li class="nav-item mb-1">
            <a class="nav-link" href="#" data-folder="trash">
                <i class="fas fa-trash me-3"></i> Trash
            </a>
        </li>
    </ul>
</div>

<!-- Main Content -->
<div class="main-content">
    <!-- Top Bar -->
    <nav class="navbar bg-white shadow-sm mb-3 px-4 py-2">
        <div class="container-fluid align-items-center">
            <a href="../index.php" class="btn btn-lg me-4"><i class="fas fa-home"></i></a>
            <div class="position-relative flex-grow-1 me-4">
                <input class="form-control py-3 ps-5 rounded-pill border-0 bg-light" 
                       type="search" 
                       id="search-input" 
                       placeholder="Search mail">
                <i class="fas fa-search position-absolute top-50 start-0 translate-middle-y ms-4 text-muted"></i>
            </div>
            <button class="btn btn-lg me-3 position-relative" id="notification-bell" title="Notifications">
                <i class="fas fa-bell"></i>
                <span class="badge bg-danger rounded-pill position-absolute top-0 start-100 translate-middle p-1" id="unread-count">0</span>
            </button>
            <button class="btn btn-lg me-3" id="dark-mode-toggle" title="Toggle dark mode">
                <i class="fas fa-moon"></i>
            </button>
            <span class="me-4 text-muted">
                <i class="fas fa-user me-2"></i><?= $full_name ?> (<?= htmlspecialchars($user['role']) ?>)
            </span>
            <a href="logout.php" class="btn btn-lg text-muted" title="Logout">
                <i class="fas fa-sign-out-alt"></i>
            </a>
        </div>
    </nav>

    <!-- Toolbar -->
    <div class="bg-white shadow-sm px-4 py-2 mb-3 d-flex align-items-center gap-3 rounded">
        <input type="checkbox" id="select-all" class="form-check-input">
        <button class="btn btn-outline-secondary" id="refresh-btn" title="Refresh">
            <i class="fas fa-sync-alt"></i>
        </button>
        <button class="btn btn-outline-secondary" id="delete-btn" title="Delete">
            <i class="fas fa-trash"></i>
        </button>
        <button class="btn btn-outline-secondary" id="mark-read-btn" title="Mark as read">
            <i class="fas fa-envelope-open"></i>
        </button>
    </div>

    <!-- Messages -->
    <div id="message-list" class="bg-white shadow-sm rounded" style="min-height: 70vh;"></div>
                        <!-- Footer -->
                    <footer class="mt-5">
                        <div class="container py-3 d-flex flex-column flex-md-row justify-content-between align-items-center">
                            <div>
                                &copy; <?= date('Y') ?> Bethany Christian Academy *Designed by Peter Danye. All rights reserved.
                            </div>
                            <div>
                                <a href="#" class="text-decoration-none me-3">Privacy Policy</a>
                                <a href="#" class="text-decoration-none">Terms of Service</a>
                            </div>
                        </div>
                    </footer>
</div>

<!-- Floating Compose Button (Internal) -->
<button class="btn compose-btn-floating compose-btn-main shadow-lg">
    <i class="fas fa-plus"></i>
</button>

<!-- Internal Compose Modal -->
<div class="modal fade" id="composeModal" tabindex="-1">
    <div class="modal-dialog modal-lg">
        <div class="modal-content border-0 shadow-lg">
            <form id="compose-form">
                <div class="modal-header border-0 pb-0">
                    <h5 class="modal-title">New Internal Message</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                </div>
                <div class="modal-body pt-2">
                    <div class="position-relative mb-3">
                        <input type="text" 
                               id="to-search" 
                               class="form-control form-control-lg border-0 border-bottom" 
                               placeholder="To: Type name or email" 
                               autocomplete="off">
                        <div id="user-suggestions" 
                             class="position-absolute bg-white border shadow w-100 mt-1" 
                             style="display:none; max-height:300px; overflow-y:auto; z-index:1000; border-radius:8px;">
                        </div>
                    </div>
                    <input type="hidden" name="receiver_id" id="receiver_id">
                    <input type="text" 
                           name="subject" 
                           class="form-control form-control-lg border-0 border-bottom mb-3" 
                           placeholder="Subject" 
                           required>
                    <textarea name="body" 
                              class="form-control border-0" 
                              rows="12" 
                              placeholder="Write your message..." 
                              required></textarea>
                </div>
                <div class="modal-footer border-0">
                    <button type="submit" class="btn compose-btn px-5 py-2">Send Internal</button>
                </div>
            </form>
        </div>
    </div>
</div>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="js/script.js"></script>
</body>
</html>