<?php
error_reporting(0);
include('includes/dbconnection.php');

require_once 'vendor/autoload.php';
use Dotenv\Dotenv;

$dotenv = Dotenv::createImmutable(__DIR__);
$dotenv->load();

$school_name = $_ENV['SCHOOL_NAME'];
$school_address = $_ENV['SCHOOL_ADDRESS'];
$prefix = $_ENV['Admission_prefix'];
$logo = $_ENV['WEBSITE_LOGO'];
$logo1 = $_ENV['WEBSITE_LOGO1'];

ini_set('session.gc_maxlifetime', 86400);
ini_set('session.cookie_lifetime', 86400);
session_name("student_portal");
session_start();

$tabSessionID1 = bin2hex(random_bytes(10));

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $studentInput = $_POST['studentno'];
    $passwordInput = $_POST['password'];

    $query = "SELECT id, stream, class, scholarship, password, spcase 
              FROM students WHERE studentno = ? AND status = 1";
    $stmt = $con->prepare($query);
    $stmt->bind_param("s", $studentInput);
    $stmt->execute();
    $stmt->bind_result($studentId, $stream, $cls, $sch, $storedPassword, $case);
    $stmt->fetch();
    $stmt->close();

    if (!empty($_POST["remember"])) {
        setcookie("user_login", $studentInput, time() + (10 * 365 * 24 * 60 * 60), "/");
    } else {
        setcookie("user_login", "", time() - 3600, "/");
    }

    if (password_verify($passwordInput, $storedPassword)) {
        $tab_id = bin2hex(random_bytes(10));
        $_SESSION['tabs'][$tab_id] = [
            'student_id' => $studentId,
            'class' => $cls,
            'scholarship' => $sch,
            'spcase' => $case,
            'stream' => $stream,
            'last_active' => time()
        ];

        if (strpos($studentInput, $prefix) !== false) {
            $redirectPage = ($stream == 'Day' ? "oldstdday.php" : "oldstdboard.php");
            header("Location: $redirectPage?tab_id=$tab_id");
            exit();
        } else {
            header("Location: index.php?error=4");
            exit();
        }
    } else {
        header("Location: index.php?error=1");
        exit();
    }
}
?>
<!DOCTYPE html>
<html lang="en">

<head>
    <?php @include("includes/head.php"); ?>
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&display=swap" rel="stylesheet">
    <style>
        *,
        *::before,
        *::after {
            box-sizing: border-box;
            margin: 0;
            padding: 0;
        }

        body.login-page {
            font-family: 'Inter', sans-serif;
            background: #dbeafe;
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
            padding: 20px;
        }

        .login-wrapper {
            display: flex;
            width: 860px;
            max-width: 96vw;
            height: 520px;
            max-height: 96vh;
            border-radius: 24px;
            overflow: hidden;
            box-shadow: 0 25px 60px rgba(37, 99, 235, 0.18);
        }

        .panel-left {
            flex: 1;
            position: relative;
            background: linear-gradient(160deg, #1d4ed8 0%, #2563eb 55%, #0891b2 100%);
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            padding: 2rem;
            overflow: hidden;
        }

        .panel-left::before {
            content: '';
            position: absolute;
            width: 280px;
            height: 280px;
            border-radius: 50%;
            border: 2px solid rgba(255, 255, 255, 0.15);
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
        }

        .panel-left::after {
            content: '';
            position: absolute;
            width: 180px;
            height: 180px;
            border-radius: 50%;
            border: 2px solid rgba(255, 255, 255, 0.1);
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
        }

        .panel-left .school-logo {
            width: 72px;
            height: 72px;
            object-fit: contain;
            border-radius: 50%;
            background: rgba(255, 255, 255, 0.15);
            padding: 8px;
            margin-bottom: 1.25rem;
            position: relative;
            z-index: 2;
        }

        .panel-left .portal-label {
            color: rgba(255, 255, 255, 0.7);
            font-size: 0.7rem;
            font-weight: 600;
            letter-spacing: 0.15em;
            text-transform: uppercase;
            margin-bottom: 0.4rem;
            position: relative;
            z-index: 2;
        }

        .panel-left .portal-title {
            color: #fff;
            font-size: 1.5rem;
            font-weight: 700;
            text-align: center;
            line-height: 1.2;
            position: relative;
            z-index: 2;
        }

        .panel-left .portal-title span {
            display: block;
            font-size: 0.85rem;
            font-weight: 400;
            color: rgba(255, 255, 255, 0.65);
            margin-top: 0.4rem;
        }

        .blob {
            position: absolute;
            border-radius: 50%;
            opacity: 0.12;
        }

        .blob-1 {
            width: 120px;
            height: 120px;
            background: #93c5fd;
            top: -30px;
            left: -30px;
        }

        .blob-2 {
            width: 80px;
            height: 80px;
            background: #bae6fd;
            bottom: -20px;
            right: -20px;
        }

        .panel-right {
            width: 380px;
            background: #fff;
            display: flex;
            flex-direction: column;
            justify-content: center;
            padding: 2rem 2rem;
        }

        .form-title {
            font-size: 1.25rem;
            font-weight: 700;
            color: #1e3a8a;
            margin-bottom: 0.3rem;
        }

        .form-subtitle {
            font-size: 0.8rem;
            color: #64748b;
            margin-bottom: 1.25rem;
        }

        .field-label {
            display: block;
            font-size: 0.78rem;
            font-weight: 600;
            color: #374151;
            margin-bottom: 0.35rem;
        }

        .field-wrap {
            position: relative;
            margin-bottom: 0.9rem;
        }

        .field-wrap input {
            width: 100%;
            height: 42px;
            border: 1.5px solid #e2e8f0;
            border-radius: 10px;
            padding: 0 40px 0 14px;
            font-size: 0.875rem;
            color: #1e293b;
            background: #f8fafc;
            transition: border-color 0.2s, box-shadow 0.2s;
            outline: none;
        }

        .field-wrap input:focus {
            border-color: #2563eb;
            background: #fff;
            box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.1);
        }

        .field-wrap .fi {
            position: absolute;
            right: 13px;
            top: 50%;
            transform: translateY(-50%);
            color: #94a3b8;
            font-size: 0.8rem;
            cursor: pointer;
        }

        .remember-row {
            display: flex;
            align-items: center;
            gap: 7px;
            margin-bottom: 1rem;
            font-size: 0.78rem;
            color: #64748b;
        }

        .btn-login {
            width: 100%;
            height: 42px;
            background: linear-gradient(135deg, #1d4ed8 0%, #2563eb 100%);
            color: #fff;
            border: none;
            border-radius: 10px;
            font-size: 0.875rem;
            font-weight: 600;
            cursor: pointer;
            transition: opacity 0.2s, transform 0.2s;
            margin-bottom: 1rem;
        }

        .btn-login:hover {
            opacity: 0.92;
            transform: translateY(-1px);
        }

        .divider {
            border: none;
            border-top: 1px solid #f1f5f9;
            margin-bottom: 0.75rem;
        }

        .alt-link {
            display: flex;
            align-items: center;
            gap: 8px;
            padding: 9px 12px;
            border: 1.5px solid #e2e8f0;
            border-radius: 10px;
            font-size: 0.8rem;
            font-weight: 500;
            color: #475569;
            text-decoration: none;
            transition: background 0.2s, border-color 0.2s;
            margin-bottom: 0.5rem;
        }

        .alt-link:hover {
            background: #f8fafc;
            border-color: #cbd5e1;
            color: #1e293b;
        }

        .alt-link.entrance {
            background: #ecfeff;
            border-color: #cffafe;
            color: #0891b2;
        }

        .alt-link.entrance:hover {
            background: #cffafe;
        }

        .alert-err {
            background: #fff1f2;
            color: #e11d48;
            border: 1px solid #ffe4e6;
            border-radius: 8px;
            padding: 9px 12px;
            font-size: 0.78rem;
            margin-bottom: 1rem;
        }

        /* ── RESPONSIVE ── */
        @media (max-width: 680px) {
            body.login-page {
                padding: 16px;
                align-items: flex-start;
            }

            .login-wrapper {
                flex-direction: column;
                width: 100%;
                height: auto;
                border-radius: 20px;
            }

            .panel-left {
                flex: none;
                width: 100%;
                padding: 1.5rem 1.25rem;
                border-radius: 0;
            }

            .panel-left::before {
                width: 180px;
                height: 180px;
            }

            .panel-left::after {
                width: 110px;
                height: 110px;
            }

            .panel-left .school-logo {
                width: 52px;
                height: 52px;
                margin-bottom: 0.75rem;
            }

            .panel-left .portal-label {
                font-size: 0.65rem;
            }

            .panel-left .portal-title {
                font-size: 1.15rem;
            }

            .panel-left .portal-title span {
                font-size: 0.78rem;
            }

            .panel-right {
                width: 100%;
                padding: 1.75rem 1.25rem;
            }
        }
    </style>
</head>

<body class="hold-transition login-page">

    <div class="login-wrapper">
        <!-- LEFT -->
        <div class="panel-left">
            <div class="blob blob-1"></div>
            <div class="blob blob-2"></div>
            <img src="<?php echo $logo; ?>" alt="Logo" class="school-logo">
            <p class="portal-label"><?php echo $school_name; ?></p>
            <h1 class="portal-title">
                Student Portal<span>Learn. Grow. Succeed.</span>
            </h1>
        </div>

        <!-- RIGHT -->
        <div class="panel-right">
            <h2 class="form-title">Sign In</h2>
            <p class="form-subtitle">Enter your admission number and password to continue.</p>

            <?php if (isset($_GET['error'])) { ?>
                <div class="alert-err">
                    <i class="fas fa-info-circle mr-1"></i>
                    <?php
                    switch ($_GET['error']) {
                        case 1:
                            echo "Incorrect Login Details.";
                            break;
                        case 2:
                            echo "Invalid Session. Please login again.";
                            break;
                        case 3:
                            echo "Session Expired. Please login again.";
                            break;
                        case 4:
                            echo "Account not found in this portal.";
                            break;
                    }
                    ?>
                </div>
            <?php } ?>

            <form action="" method="post">
                <div class="field-wrap">
                    <label class="field-label">Admission No.</label>
                    <input type="text" name="studentno" placeholder="enter your admission number" required
                        oninput="this.value = this.value.toUpperCase()"
                        value="<?php echo isset($_COOKIE["user_login"]) ? $_COOKIE["user_login"] : ''; ?>">
                    <i class="fas fa-id-card fi"></i>
                </div>

                <div class="field-wrap">
                    <label class="field-label">Password</label>
                    <input type="password" name="password" id="passwordField" placeholder="enter your password"
                        required>
                    <i class="fas fa-eye-slash fi" id="togglePassword"></i>
                </div>

                <div class="remember-row">
                    <input type="checkbox" id="remember" name="remember" <?php echo isset($_COOKIE["user_login"]) ? 'checked' : ''; ?>>
                    <label for="remember">Remember Me</label>
                </div>

                <button type="submit" name="login" class="btn-login">Sign In to Portal</button>
            </form>

            <hr class="divider">

            <a href="new_student.php" class="alt-link">
                <i class="fas fa-user-plus"></i> Student with Registration No.
            </a>
            <a href="entrancedashboard.php?tab_id=<?php echo $tabSessionID1; ?>" class="alt-link entrance">
                <i class="fas fa-door-open"></i> Guest &amp; Entrance Portal
            </a>
        </div>
    </div>

    <script>
        document.getElementById('togglePassword').addEventListener('click', function () {
            const f = document.getElementById('passwordField');
            const show = f.type === 'password';
            f.type = show ? 'text' : 'password';
            this.classList.toggle('fa-eye', show);
            this.classList.toggle('fa-eye-slash', !show);
        });
    </script>
    <?php @include("includes/foot.php"); ?>
</body>

</html>