<?php
session_start();
if (!isset($_SESSION['student_id'])) {
    header("Location: ../login/student_login.php");
    exit();
}

$con = mysqli_connect("localhost", "root", "", "WSAS");

if (!$con) {
    die("Database connection failed");
}

$student_id = $_SESSION['student_id'];

$check = mysqli_prepare($con, "SELECT first_login FROM adminstudent WHERE student_id = ?");
mysqli_stmt_bind_param($check, "i", $student_id); // I guess this will be string 'cuz the s_id is sometimes combined letters and numbers
mysqli_stmt_execute($check);
$result = mysqli_stmt_get_result($check);
$student = mysqli_fetch_assoc($result);

if ($student['first_login'] != 1) {
    header("Location: ../studentpanel/Student_Dashboard.php");
    exit();
}

if (isset($_POST['change_password'])) {
    $new_password = $_POST['new_password'];
    $confirm_password = $_POST['confirm_password'];
    
    if ($new_password !== $confirm_password) {
        $error = "New passwords do not match!";
    } elseif (strlen($new_password) < 8) {
        $error = "Password must be at least 8 characters!";
    } else {

        $hashed = password_hash($new_password, PASSWORD_DEFAULT);
        
        $update = mysqli_prepare($con, "UPDATE adminstudent SET pword = ?, first_login = 0 WHERE student_id = ?");
        mysqli_stmt_bind_param($update, "si", $hashed, $student_id);
        
        if (mysqli_stmt_execute($update)) {
            unset($_SESSION['first_login']);
            header("Location: ../studentpanel/Student_Dashboard.php");
            exit();
        } else {
            $error = "Something went wrong. Please try again.";
        }
    }
}
mysqli_close($con);
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Change Password - First Login</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;
        }

        p {
            text-align: center;
            color: #333;
            margin: 20px 0 25px 0;
            font-size: 11px;
        }

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

        input[type="password"] {
            width: 100%;
            padding: 8px 12px;
            background: #f5f5f5;
            border: 2px solid #2b5820; 
            border-radius: 6px;       
            font-size: 11px;
            color: #333;
            margin-bottom: 20px;
        }

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

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

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

        .submit-btn {
            width: 100%;
            max-width: 200px;
            padding: 8px;
            background: #012a0d;
            border-radius: 6px;
            border: none;
            cursor: pointer;
            font-size: 11px;
            color: #fff;
            font-weight: 600;
            display: block;
            margin: 15px auto 5px;
            text-align: center;
        }

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

        .error-message {
            color: #dc3545;
            text-align: center;
            margin-bottom: 12px;
            font-size: 11px;
            padding: 10px;
            background-color: #f8d7da;
            border-radius: 5px;
            border: 1px solid #f5c6cb;
        }

        /* Tablet */
        @media screen and (max-width: 768px) {
            body { 
                padding: 15px; 
            }

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

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

            p {
                text-align: center;
                color: #333;
                font-size: 20px; 
            }

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

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

            .submit-btn { 
                max-width: 70%; 
                padding: 12px; 
                font-size: 22px; 
                margin-top: 20px; 
            }
        }

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

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

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

            p {
                text-align: center;
                color: #333;
                font-size: 14px; 
            }

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

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

            .submit-btn { 
                max-width: 50%; 
                padding: 12px; 
                font-size: 16px; 
                margin-top: 20px; 
            }
        }

        /* Small phone */
        @media screen and (max-width: 400px) {
            .form-box { 
                padding: 20px 15px; 
            }
            
            h2 { 
                font-size: 18px; 
                margin-bottom: 20px; 
            }

            p {
                text-align: center;
                color: #333;
                font-size: 12px; 
            }

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

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

            .submit-btn { 
                max-width: 30%; 
                padding: 11px; 
                font-size: 12px; 
                margin-top: 20px; 
            }
        }
    </style>
</head>

<body>
    <div class="container">
        <div class="form-box">
            <form action="" method="post">
                <h2> Change Password </h2>
                <p> You must change your password before accessing the system. </p>

                <?php if(!empty($error)) { ?>
                    <p class="error-message"><?php echo $error; ?></p>
                <?php } ?>
                
                <label for="new_password"> New Password </label>
                <input type="password" id="new_password" name="new_password" placeholder="Enter new password" required>
                
                <label for="confirm_password"> Confirm New Password </label>
                <input type="password" id="confirm_password" name="confirm_password" placeholder="Confirm new password" required>

                <button type="submit" name="change_password" class="submit-btn"> Change Password </button>
            </form>
        </div>
    </div>
</body>
</html>