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

// Admin Security Check
if (!isset($_SESSION['admin_logged_in'])) { header("Location: login.php"); exit; }

$success = ""; $error = "";

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $name = trim($_POST['name']);
    $email = trim($_POST['email']);
    $phone = trim($_POST['phone']);
    $password = password_hash($_POST['password'], PASSWORD_DEFAULT); // Secure Password
    $status = $_POST['status'];

    if (!empty($name) && !empty($email) && !empty($_POST['password'])) {
        try {
            $stmt = $pdo->prepare("INSERT INTO teachers (name, email, phone, password, status) VALUES (?, ?, ?, ?, ?)");
            $stmt->execute([$name, $email, $phone, $password, $status]);
            $success = "<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'>Success!</strong> <span class='text-muted'>Teacher account created! They can now log in.</span></div></div>";
        } catch (PDOException $e) { 
            $error = "<div class='alert alert-danger fw-bold rounded-4 border-0 shadow-sm'><i class='fas fa-exclamation-triangle me-2'></i> Error: This Email is already in use.</div>"; 
        }
    } else { 
        $error = "<div class='alert alert-danger fw-bold rounded-4 border-0 shadow-sm'><i class='fas fa-exclamation-triangle me-2'></i> Name, Email, and Password are required!</div>"; 
    }
}

include 'includes/header.php'; 
include 'includes/sidebar.php'; 
?>

<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-back { background: #f8fafc; color: #64748b; border: 1px solid #e2e8f0; border-radius: 12px; font-weight: 700; padding: 10px 20px; transition: 0.3s; text-decoration: none; display: inline-flex; align-items: center; }
    .btn-back:hover { background: #e2e8f0; color: var(--theme-dark); transform: translateY(-2px); }

    /* Form Styles */
    .form-card { background: #fff; border: 1px solid #e2e8f0; border-radius: 20px; box-shadow: 0 10px 40px rgba(0,0,0,0.02); overflow: hidden; border-top: 5px solid var(--theme-green); }
    .form-label { color: var(--theme-dark); font-size: 13px; text-transform: uppercase; letter-spacing: 0.5px; }
    .form-control, .form-select { border-radius: 12px; border: 2px solid #f1f5f9; padding: 12px 15px; font-size: 15px; transition: 0.3s; background: #f8fafc; }
    .form-control:focus, .form-select:focus { border-color: var(--theme-green); background: #fff; box-shadow: 0 0 0 4px rgba(6, 78, 59, 0.1); }
    
    .btn-create { background: var(--theme-green); color: #fff; font-weight: 800; border-radius: 12px; transition: 0.3s; border: none; letter-spacing: 1px; }
    .btn-create:hover { background: var(--theme-dark); color: var(--theme-gold); transform: translateY(-2px); box-shadow: 0 8px 20px rgba(6, 78, 59, 0.2); }

    /* Animations */
    .fade-up { animation: fadeInUp 0.5s ease-out forwards; }
    @keyframes fadeInUp { 0% { opacity: 0; transform: translateY(15px); } 100% { opacity: 1; transform: translateY(0); } }
    
    /* 📱 Mobile Specific Styles */
    @media (max-width: 991.98px) {
        .main-content { padding: 15px !important; }
        .dashboard-header { padding: 15px 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-back { width: 100%; justify-content: center; }
        .header-title { font-size: 1.2rem; }
        .form-card { padding: 25px !important; }
    }
    @media (min-width: 992px) {
        #sidebarToggle { display: none !important; }
    }
</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">Add New Instructor</h4>
                <p class="text-muted small fw-bold mb-0 d-none d-md-block">Create a dashboard login account for a teacher</p>
            </div>
        </div>
        <a href="manage_teachers.php" class="btn-back shadow-sm">
            <i class="fas fa-arrow-left me-2"></i> Back to List
        </a>
    </div>

    <div class="form-card p-4 p-md-5 mx-auto fade-up" style="max-width: 800px; animation-delay: 0.1s;">
        <?= $success ?>
        <?= $error ?>

        <form method="POST">
            <h6 class="fw-bold mb-4 pb-2 border-bottom text-muted"><i class="fas fa-user-circle me-2"></i> Basic Information</h6>
            <div class="mb-4">
                <label class="form-label fw-bold">Full Name <span class="text-danger">*</span></label>
                <div class="input-group">
                    <span class="input-group-text border-2 border-end-0 bg-light text-muted"><i class="fas fa-user"></i></span>
                    <input type="text" name="name" class="form-control border-start-0 ps-0" placeholder="e.g. Md Nasrullah" required>
                </div>
            </div>

            <div class="row g-4 mb-5">
                <div class="col-md-6">
                    <label class="form-label fw-bold">Login Email <span class="text-danger">*</span></label>
                    <div class="input-group">
                        <span class="input-group-text border-2 border-end-0 bg-light text-muted"><i class="fas fa-envelope"></i></span>
                        <input type="email" name="email" class="form-control border-start-0 ps-0" placeholder="teacher@example.com" required>
                    </div>
                </div>
                <div class="col-md-6">
                    <label class="form-label fw-bold">Phone Number</label>
                    <div class="input-group">
                        <span class="input-group-text border-2 border-end-0 bg-light text-muted"><i class="fas fa-phone-alt"></i></span>
                        <input type="text" name="phone" class="form-control border-start-0 ps-0" placeholder="e.g. 01815...">
                    </div>
                </div>
            </div>

            <h6 class="fw-bold mb-4 pb-2 border-bottom text-muted"><i class="fas fa-shield-alt me-2"></i> Security & Access</h6>
            <div class="row g-4 mb-5">
                <div class="col-md-6">
                    <label class="form-label fw-bold">Login Password <span class="text-danger">*</span></label>
                    <div class="input-group">
                        <span class="input-group-text border-2 border-end-0 bg-light text-muted"><i class="fas fa-lock"></i></span>
                        <input type="password" name="password" class="form-control border-start-0 ps-0" placeholder="Set a strong password" required>
                    </div>
                </div>
                <div class="col-md-6">
                    <label class="form-label fw-bold">Account Status</label>
                    <select name="status" class="form-select border-2 fw-bold text-dark">
                        <option value="active">Active (Can Login)</option>
                        <option value="inactive">Inactive (Cannot Login)</option>
                    </select>
                </div>
            </div>

            <div class="text-end pt-3 border-top">
                <button type="submit" class="btn btn-create w-100 py-3 shadow-sm">
                    <i class="fas fa-user-plus me-2"></i> CREATE ACCOUNT
                </button>
            </div>
        </form>
    </div>
</div>

<script>
    // Mobile Menu Toggle Logic
    document.addEventListener("DOMContentLoaded", function() {
        const toggleBtn = document.getElementById('sidebarToggle');
        const sidebar = document.getElementById('sidebarMenu'); 
        
        if (toggleBtn) {
            toggleBtn.addEventListener('click', function(e) {
                e.preventDefault();
                if(sidebar) {
                    sidebar.classList.toggle('active');
                } else {
                    const fallbackSidebar = document.querySelector('.sidebar');
                    if(fallbackSidebar) fallbackSidebar.classList.toggle('active');
                }
            });
        }
    });
</script>

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