<?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']);
    $role = trim($_POST['role_or_spec']);
    $type = $_POST['team_type'];
    
    $img_url = 'assets/images/default.png'; // Default image

    // Handle Image Upload
    if (isset($_FILES['member_image']) && $_FILES['member_image']['error'] == 0) {
        $allowed_ext = ['jpg', 'jpeg', 'png', 'webp'];
        $file_name = $_FILES['member_image']['name'];
        $file_ext = strtolower(pathinfo($file_name, PATHINFO_EXTENSION));
        
        if (in_array($file_ext, $allowed_ext)) {
            $upload_dir = '../uploads/team/';
            if (!file_exists($upload_dir)) { mkdir($upload_dir, 0777, true); }
            
            $new_name = 'team_' . time() . '_' . rand(100, 999) . '.' . $file_ext;
            $destination = $upload_dir . $new_name;
            
            if (move_uploaded_file($_FILES['member_image']['tmp_name'], $destination)) {
                $img_url = 'uploads/team/' . $new_name;
            }
        } else {
            $error = "<div class='alert alert-danger fw-bold rounded-4 border-0 shadow-sm'><i class='fas fa-exclamation-triangle me-2'></i> Only JPG, PNG, WEBP files are allowed.</div>";
        }
    }

    if (empty($error) && !empty($name)) {
        try {
            $stmt = $pdo->prepare("INSERT INTO team_members (name, role_or_spec, team_type, image_url) VALUES (?, ?, ?, ?)");
            $stmt->execute([$name, $role, $type, $img_url]);
            $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'>Published!</strong> <span class='text-muted'>Member added to the website successfully.</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> Database Error!</div>"; 
        }
    } elseif(empty($error)) {
        $error = "<div class='alert alert-danger fw-bold rounded-4 border-0 shadow-sm'><i class='fas fa-exclamation-triangle me-2'></i> Name is 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-publish { background: var(--theme-green); color: #fff; font-weight: 800; border-radius: 12px; transition: 0.3s; border: none; letter-spacing: 1px; }
    .btn-publish: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 Team Member</h4>
                <p class="text-muted small fw-bold mb-0 d-none d-md-block">Publish a new profile to the frontend website</p>
            </div>
        </div>
        <a href="manage_team.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" enctype="multipart/form-data">
            <h6 class="fw-bold mb-4 pb-2 border-bottom text-muted"><i class="fas fa-id-card me-2"></i> Profile 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. Mufti Ahmadullah" required>
                </div>
            </div>

            <div class="row g-4 mb-5">
                <div class="col-md-6">
                    <label class="form-label fw-bold">Designation / Role <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-briefcase"></i></span>
                        <input type="text" name="role_or_spec" class="form-control border-start-0 ps-0" placeholder="e.g. Senior Instructor" required>
                    </div>
                </div>
                <div class="col-md-6">
                    <label class="form-label fw-bold">Category Display</label>
                    <select name="team_type" class="form-select border-2 fw-bold text-dark">
                        <option value="Teacher">Teacher / Instructor</option>
                        <option value="Board Member">Board Member</option>
                        <option value="Adviser">Adviser</option>
                    </select>
                </div>
            </div>

            <h6 class="fw-bold mb-4 pb-2 border-bottom text-muted"><i class="fas fa-image me-2"></i> Profile Photo</h6>
            <div class="mb-4">
                <div class="p-4 border-2 border-dashed rounded-4 bg-light text-center" style="border-style: dashed;">
                    <i class="fas fa-cloud-upload-alt fa-3x text-muted mb-3 opacity-50"></i>
                    <input type="file" name="member_image" class="form-control mb-2 shadow-none border-0 bg-white" accept="image/*">
                    <small class="text-muted fw-bold d-block"><i class="fas fa-info-circle text-primary me-1"></i> Square image recommended (e.g. 500x500px). Leave empty for default avatar.</small>
                </div>
            </div>

            <div class="text-end pt-3 border-top">
                <button type="submit" class="btn btn-publish w-100 py-3 shadow-sm">
                    <i class="fas fa-paper-plane me-2"></i> PUBLISH TO WEBSITE
                </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'; ?>