<?php
session_start();
$con = mysqli_connect("localhost", "root", "", "WSAS");
if (!$con) die("Database connection failed");

$error = "";

// Display success message from pass reset
$success_message = "";
if (!empty($_SESSION['success_message'])) {
    $success_message = $_SESSION['success_message'];
    unset($_SESSION['success_message']);
}

if (isset($_POST['submit'])) {
    $email = trim($_POST['email']);
    $pword = $_POST['pword']; 

    $stmt = mysqli_prepare($con, "SELECT * FROM adminstudent WHERE email = ?");
    mysqli_stmt_bind_param($stmt, "s", $email);
    mysqli_stmt_execute($stmt);
    $result = mysqli_stmt_get_result($stmt);
    $student = mysqli_fetch_assoc($result);

    if ($student) {
        if (password_verify($pword, $student['pword'])) {

            $_SESSION['student_id'] = $student['student_id'];
            $_SESSION['student_email'] = $student['email'];
            $_SESSION['student_name'] = $student['f_name'] . ' ' . $student['l_name'];
  
            if ($student['first_login'] == 1) {
                header("Location: ../login/change_pass.php");
                exit();
            } else {
                header("Location: ../studentpanel/Student_Dashboard.php");
                exit();
            }
            
        } else {
            $error = "Invalid password!";
        }
    } else {
        $error = "Email not found!";
    }
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title> DWSAS </title>
    <style>
        * {
            box-sizing: border-box;
            margin: 0;
            padding: 0;
        }

        body {
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: 100vh;
            background-image: url('../images/bg.jpg');
            background-size: 100% 100%;   
            background-repeat: no-repeat;
            background-position: center;   
            background-attachment: fixed;
            color: #333;
            position: relative; 
            z-index: 1;
            padding: 20px;
        }

        body::before {
            content: "";
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background-color: rgba(43, 88, 32, 0.5);
            z-index: 0;
        }

        .container {
            position: relative;
            z-index: 1;
            width: 100%;
            max-width: 320px;
        }

        .form-box {
            width: 100%;
            padding: 10px 15px; 
            background: #fff;
            border-radius: 10px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
            opacity: 0.8;
        }

        h2 {
            font-size: 16px;
            text-align: center;
            margin: 15px 12px;
            color: #2b5820;
        }

        label {
            font-weight: 400;
            color: #333;
            display: block;
            margin-bottom: 8px;
            font-size: 11px;
        }

    input[type="email"],
    input[type="password"],
    input[type="text"] {
        width: 100%;
        padding: 8px 12px;
        background: #f5f5f5;
        border: 2px solid #2b5820; 
        border-radius: 6px;       
        font-size: 11px;
        color: #333;
        margin-bottom: 10px;
        transition: border-color 0.3s ease;
    }

    input[type="email"]:focus,
    input[type="password"]:focus,
    input[type="text"]:focus {
        border-color: #5f7e62;
        outline: none;
    }

    input[type="password"]::-ms-reveal {
        display: none;
        }

        a {
            font-size: 11px;
            color: blue;     
            text-decoration: none; 
        }

        .login-btn {
            width: 30%;
            padding: 8px;
            background: #012a0d;
            border-radius: 6px;
            border: none;
            cursor: pointer;
            font-size: 11px;
            font-weight: 600;
            color: #fff;
            display: block;
            margin: 15px auto 10px;
            text-align: center;
            transition: background 0.3s ease;
        }

        .login-btn:hover {
            background: #1a4a1a;
        }

        .error-message {
            display: none; 
            color: #dc3545;
            text-align: center;
            margin-bottom: 12px;
            font-size: 11px;
            padding: 8px;
            background-color: #f8d7da;
            border-radius: 5px;
            border: 1px solid #f5c6cb;
            box-sizing: border-box;
        }
        
        .error-message:not(:empty) {
            display: block;
        }

        .password-container {
            position: relative;
            width: 100%;
        }

        .password-container input {
            width: 100%;
            padding-right: 45px; 
        }

        .toggle-icon {
            position: absolute;
            top: 35%;
            right: 15px;
            transform: translateY(-50%);
            font-size: 18px;
            cursor: pointer;
            color: #2b5820;
        }

        .watermark {
            position: fixed;
            bottom: 10px;
            right: 15px;
            opacity: 0.2;
            font-size: 12px;
            color: #fff;
            pointer-events: none;
            z-index: 9999; 
            opacity: 0.6;
        }
        
        /* Tablet */
        @media screen and (max-width: 768px) {
            body { 
                padding: 15px; 
            }

            .form-box { 
                padding: 20px 25px; 
            }

            h2 { 
                font-size: 20px; 
                margin-bottom: 25px; 
            }

            input[type="email"], 
            input[type="password"] { 
                padding: 6px; 
                font-size: 10px; 
                margin-bottom: 15px; 
            }

            .login-btn { 
                max-width: 40%; 
                padding: 6px; 
                font-size: 10px; 
                margin-top: 20px; 
            }
        }

        /* Mobile phone */
        @media screen and (max-width: 576px) {
            body { 
                padding: 10px; 
                align-items: center;      
                padding-top: 20px;
            }

            .form-box { 
                padding: 20px 25px; 
            }

            h2 { 
                font-size: 18px; 
                margin-bottom: 20px; 
            }

            label {
                color: black;
                font-size: 8px;
            }

            input[type="email"], 
            input[type="password"] { 
                padding: 3px 6px; 
                font-size: 8px; 
                margin-bottom: 6px; 
            }

            a {
                font-size: 8px;
            }

            .toggle-icon {
                position: absolute;
                top: 40%;
                right: 10px;
                transform: translateY(-50%);
                font-size: 12px;
                cursor: pointer;
                color: #2b5820;
            }

            .login-btn { 
                max-width: 30%; 
                padding: 6px; 
                font-size: 8px; 
                margin-top: 20px; 
            }
        }

        /* Small phone */
        @media screen and (max-width: 400px) {
            .form-box { 
                padding: 20px 25px; 
                width: 230px;
            }
            
            h2 { 
                font-size: 22px; 
            }

            input[type="email"], 
            input[type="password"] { 
                padding: 3px; 
                font-size: 6px; 
            }

            a {
                font-size: 6px;
            }

            .login-btn { 
                font-size: 6px; 
                padding: 3px; 
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="form-box">
            <form action="" method="post">
                <h2> Student Login </h2>

                <?php if($success_message): ?>
                    <div style="color: green; text-align:center; margin-bottom: 15px;">
                        <?php echo htmlspecialchars($success_message); ?>
                    </div>
                <?php endif; ?>

                <?php if($error): ?>
                    <div class="error-message"><?php echo $error; ?></div>
                <?php endif; ?>

                <label for="email"> Email Address </label>
                <input type="email" id="email" name="email" placeholder="Enter your email" required>

                <label for="pword"> Password </label>
                <div class="password-container">
                    <input type="password" id="pword" name="pword" placeholder="Enter your password" required>
                    
                    <ion-icon name="eye-outline" id="togglePassword" class="toggle-icon"></ion-icon>
                </div>

                <a href ="forgot_pass.php"> Forgot Password? </a>
                               
                <button type="submit" name="submit" class="login-btn"> Log In </button>
            </form>
        </div>
    </div>

    <div class="watermark">WSAS-CDLB | Developed by: Abrencillo, Delos Reyes, Landicho</div>

    <script>
        const togglePassword = document.getElementById("togglePassword");
        const password = document.getElementById("pword");

        let timeout; 

        togglePassword.addEventListener("click", function () {

            if (password.type === "password") {
                // SHOW password
                password.type = "text";
                this.setAttribute("name", "eye-off-outline");

                clearTimeout(timeout);

                // Auto hide after 2 seconds
                timeout = setTimeout(() => {
                    password.type = "password";
                    togglePassword.setAttribute("name", "eye-outline");
                }, 2000);

            } else {
                // HIDE immediately the password if the icon been clicked again
                password.type = "password";
                this.setAttribute("name", "eye-outline");

                clearTimeout(timeout); // stop timer
            }
        });
    </script>

    <script type="module" src="https://unpkg.com/ionicons@7.1.0/dist/ionicons/ionicons.esm.js"></script>
    <script nomodule src="https://unpkg.com/ionicons@7.1.0/dist/ionicons/ionicons.js"></script>
</body>
</html>