<?php 
ini_set('display_errors', 1); error_reporting(E_ALL);
require_once '../db_connect.php'; 
include 'includes/header.php'; 
include 'includes/sidebar.php'; 

$msg = '';

// --- Delete Teacher Logic ---
if (isset($_GET['delete'])) {
    try {
        $del = $pdo->prepare("DELETE FROM teachers WHERE id = ?");
        $del->execute([$_GET['delete']]);
        $msg = "<div class='alert alert-success d-flex align-items-center mb-4 rounded-4 border-0 shadow-sm fade-up'><i class='fas fa-check-circle me-3 fa-2x text-success'></i> <div><strong class='d-block text-success'>Access Revoked!</strong> <span class='text-muted'>Teacher account removed successfully.</span></div></div>";
    } catch (PDOException $e) { }
}

// Fetch All Teachers
$teachers = [];
try {
    $stmt = $pdo->query("SELECT * FROM teachers ORDER BY id DESC");
    $teachers = $stmt->fetchAll(PDO::FETCH_ASSOC);
} catch (PDOException $e) {}
?>

<style>
    :root { --theme-green: #064e3b; --theme-dark: #022c22; --theme-gold: #d4af37; --bg-soft: #f8fafc; }
    .main-content { background-color: var(--bg-soft); min-height: 100vh; padding-bottom: 50px; font-family: 'Outfit', sans-serif; }
    
    /* Header & Mobile Menu */
    .dashboard-header { background: #fff; padding: 20px 30px; border-radius: 20px; box-shadow: 0 5px 20px rgba(0,0,0,0.03); margin-bottom: 30px; border-left: 6px solid var(--theme-green); }
    
    #sidebarToggle { display: none; width: 45px; height: 45px; border-radius: 12px; background: #f1f5f9; border: 1px solid #e2e8f0; color: var(--theme-green); cursor: pointer; transition: 0.3s; display: inline-flex; align-items: center; justify-content: center; }
    #sidebarToggle:hover { background: var(--theme-green); color: #fff; }
    
    .btn-add { background: var(--theme-green); color: #fff; border-radius: 12px; font-weight: 700; padding: 12px 24px; transition: 0.3s; border: none; text-decoration: none; display: inline-flex; align-items: center; justify-content: center; }
    .btn-add:hover { background: var(--theme-dark); color: var(--theme-gold); transform: translateY(-2px); box-shadow: 0 8px 15px rgba(6, 78, 59, 0.2); }

    /* Desktop Table */
    .desktop-table-card { background: #fff; border: 1px solid #e2e8f0; border-radius: 20px; box-shadow: 0 10px 40px rgba(0,0,0,0.02); overflow: hidden; }
    .table-hover tbody tr:hover { background-color: #f8fafc; }
    .table thead th { border: none; padding: 18px 20px; color: #64748b; font-weight: 800; text-transform: uppercase; font-size: 11px; letter-spacing: 0.5px; background-color: #f1f5f9; }
    
    /* Avatar */
    .avatar-circle { width: 45px; height: 45px; border-radius: 12px; background: rgba(6, 78, 59, 0.1); color: var(--theme-green); display: flex; align-items: center; justify-content: center; font-weight: 800; font-size: 18px; }
    
    /* Animations */
    .fade-up { animation: fadeInUp 0.5s ease-out forwards; }
    @keyframes fadeInUp { 0% { opacity: 0; transform: translateY(15px); } 100% { opacity: 1; transform: translateY(0); } }
    
    /* Actions & Badges */
    .act-btn { width: 34px; height: 34px; border-radius: 10px; display: inline-flex; align-items: center; justify-content: center; border: none; transition: 0.3s; margin: 0 3px; text-decoration: none; cursor: pointer; }
    .act-edit { background: #f1f5f9; color: var(--theme-green); } 
    .act-edit:hover { background: var(--theme-green); color: #fff; }
    .act-delete { background: #fef2f2; color: #ef4444; } 
    .act-delete:hover { background: #ef4444; color: #fff; }
    
    .status-badge { padding: 6px 14px; border-radius: 8px; font-size: 11px; font-weight: 800; text-transform: uppercase; letter-spacing: 0.5px; }

    /* 📱 Mobile Specific Styles */
    .teacher-mobile-card { background: #fff; border-radius: 16px; border: 1px solid #e2e8f0; box-shadow: 0 4px 15px rgba(0,0,0,0.03); overflow: hidden; transition: 0.3s; }
    
    @media (max-width: 991.98px) {
        .main-content { padding: 15px !important; }
        .dashboard-header { padding: 20px; flex-direction: column; align-items: flex-start !important; gap: 15px; }
        .dashboard-header .header-top { width: 100%; display: flex; justify-content: flex-start; align-items: center; gap: 15px; }
        .dashboard-header .btn-add { width: 100%; }
        .header-title { font-size: 1.2rem; }
    }
    @media (min-width: 992px) {
        #sidebarToggle { display: none !important; } /* Hide mobile menu on desktop */
    }
</style>

<div class="main-content p-3 p-md-4">
    
    <div class="dashboard-header d-flex justify-content-between align-items-center fade-up">
        <div class="header-top">
            <button id="sidebarToggle" class="shadow-sm">
                <i class="fas fa-bars fs-5"></i>
            </button>
            <div>
                <h4 class="mb-0 fw-bold text-dark header-title">System Instructors</h4>
                <p class="text-muted small fw-bold mb-0 d-none d-md-block">Manage login access for teachers</p>
            </div>
        </div>
        <a href="add_teacher.php" class="btn-add shadow-sm">
            <i class="fas fa-plus-circle me-2"></i> Add Teacher
        </a>
    </div>

    <?= $msg ?>

    <div class="desktop-table-card d-none d-lg-block fade-up" style="animation-delay: 0.1s;">
        <div class="table-responsive">
            <table class="table table-hover align-middle mb-0 border-0">
                <thead>
                    <tr>
                        <th class="ps-4">Instructor Details</th>
                        <th>Contact Info</th>
                        <th>Login Status</th>
                        <th class="text-end pe-4">Actions</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if(!empty($teachers)): foreach($teachers as $t): ?>
                    <tr>
                        <td class="py-3 ps-4">
                            <div class="d-flex align-items-center gap-3">
                                <div class="avatar-circle shadow-sm"><?= strtoupper(substr($t['name'], 0, 1)) ?></div>
                                <div>
                                    <div class="fw-bold text-dark" style="font-size: 15px;"><?= htmlspecialchars($t['name']) ?></div>
                                    <small class="text-muted fw-semibold">ID: #TCH-<?= $t['id'] ?></small>
                                </div>
                            </div>
                        </td>
                        <td>
                            <div class="fw-bold text-dark mb-1" style="font-size: 13px;"><i class="fas fa-envelope text-muted me-2"></i><?= htmlspecialchars($t['email']) ?></div>
                            <small class="text-muted fw-bold"><i class="fas fa-phone-alt me-2"></i><?= htmlspecialchars($t['phone'] ?? 'N/A') ?></small>
                        </td>
                        <td>
                            <?php if($t['status'] == 'active'): ?>
                                <span class="status-badge bg-success-subtle text-success border border-success-subtle"><i class="fas fa-check-circle me-1"></i> Active</span>
                            <?php else: ?>
                                <span class="status-badge bg-danger-subtle text-danger border border-danger-subtle"><i class="fas fa-ban me-1"></i> Inactive</span>
                            <?php endif; ?>
                        </td>
                        <td class="text-end pe-4">
                            <a href="edit_teacher.php?id=<?= $t['id'] ?>" class="act-btn act-edit" title="Edit"><i class="fas fa-pen"></i></a>
                            <a href="?delete=<?= $t['id'] ?>" class="act-btn act-delete" onclick="return confirm('Revoke login access?');" title="Delete"><i class="fas fa-trash-alt"></i></a>
                        </td>
                    </tr>
                    <?php endforeach; else: ?>
                        <tr><td colspan="4" class="text-center py-5 text-muted fw-bold">No instructors found.</td></tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>

    <div class="d-block d-lg-none fade-up" style="animation-delay: 0.1s;">
        <div class="row g-3">
            <?php if(!empty($teachers)): foreach($teachers as $t): ?>
            <div class="col-12">
                <div class="teacher-mobile-card p-3">
                    <div class="d-flex justify-content-between align-items-center mb-3">
                        <div class="d-flex align-items-center gap-3">
                            <div class="avatar-circle shadow-sm" style="width: 40px; height: 40px; font-size: 16px;"><?= strtoupper(substr($t['name'], 0, 1)) ?></div>
                            <div>
                                <h6 class="fw-bold text-dark mb-0" style="font-size: 15px;"><?= htmlspecialchars($t['name']) ?></h6>
                                <small class="text-muted fw-bold" style="font-size: 11px;">ID: #TCH-<?= $t['id'] ?></small>
                            </div>
                        </div>
                        <div>
                            <?php if($t['status'] == 'active'): ?>
                                <span class="badge bg-success-subtle text-success rounded-pill p-2"><i class="fas fa-check"></i></span>
                            <?php else: ?>
                                <span class="badge bg-danger-subtle text-danger rounded-pill p-2"><i class="fas fa-ban"></i></span>
                            <?php endif; ?>
                        </div>
                    </div>
                    
                    <div class="bg-light rounded-3 p-3 mb-3 border">
                        <div class="d-flex align-items-center mb-2">
                            <i class="fas fa-envelope text-muted me-2" style="width: 15px;"></i>
                            <span class="text-dark fw-bold" style="font-size: 13px;"><?= htmlspecialchars($t['email']) ?></span>
                        </div>
                        <div class="d-flex align-items-center">
                            <i class="fas fa-phone-alt text-muted me-2" style="width: 15px;"></i>
                            <span class="text-muted fw-bold" style="font-size: 13px;"><?= htmlspecialchars($t['phone'] ?? 'N/A') ?></span>
                        </div>
                    </div>
                    
                    <div class="d-flex gap-2">
                        <a href="edit_teacher.php?id=<?= $t['id'] ?>" class="btn btn-light border flex-grow-1 fw-bold text-success" style="font-size: 13px; padding: 8px;"><i class="fas fa-pen me-2"></i> Edit</a>
                        <a href="?delete=<?= $t['id'] ?>" class="btn btn-light border flex-grow-1 fw-bold text-danger" style="font-size: 13px; padding: 8px;" onclick="return confirm('Revoke login access?');"><i class="fas fa-trash-alt me-2"></i> Delete</a>
                    </div>
                </div>
            </div>
            <?php endforeach; else: ?>
                <div class="col-12 text-center py-5 bg-white rounded-4 border shadow-sm">
                    <i class="fas fa-user-slash fa-3x text-muted opacity-25 mb-3"></i>
                    <h6 class="fw-bold text-muted">No instructors found!</h6>
                </div>
            <?php endif; ?>
        </div>
    </div>

</div>

<script>
    // Sidebar Mobile Menu Toggle Logic
    document.addEventListener("DOMContentLoaded", function() {
        const toggleBtn = document.getElementById('sidebarToggle');
        const sidebar = document.getElementById('sidebarMenu'); // Make sure your sidebar has id="sidebarMenu"
        
        if (toggleBtn) {
            toggleBtn.addEventListener('click', function(e) {
                e.preventDefault();
                if(sidebar) {
                    sidebar.classList.toggle('active');
                } else {
                    // Fallback if class is just .sidebar
                    const fallbackSidebar = document.querySelector('.sidebar');
                    if(fallbackSidebar) fallbackSidebar.classList.toggle('active');
                }
            });
        }
    });
</script>

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