<?php
    session_start();

    // DB Connection
    $conn = new mysqli($GLOBALS['host'], $GLOBALS['user'], $GLOBALS['pass'], $GLOBALS['db']);
    if ($conn->connect_error) die("Connection failed: " . $conn->connect_error);

    // Handle form submission
    if ($_SERVER['REQUEST_METHOD'] === 'POST') {
        $userId = trim($_POST['user_id'] ?? '');
        $password = $_POST['pwd'] ?? '';

        if (empty($userId) || empty($password)) {
            echo "<div class='alert alert-danger'>Please fill in all fields.</div>";
        } else {
            // Prepare statement to prevent SQL injection
            $stmt = $conn->prepare("SELECT * FROM " . $GLOBALS['arif_users'] . " WHERE user_id = ?");
            $stmt->bind_param("s", $userId);
            $stmt->execute();
            $result = $stmt->get_result();

            if ($result->num_rows === 1) {
                $user = $result->fetch_assoc();
                
                if (password_verify($password, $user['password'])) {
                    // Login successful
                    $_SESSION['user_id'] = $user['user_id'];
                    $_SESSION['type'] = $user['type'];
                    $_SESSION['name'] = $user['user_name'];
                    $_SESSION['profile_pic'] = $user['profile_pic'] ?? '';

                    echo "<div class='alert alert-success'>Login successful. Redirecting...</div>";
                    echo "<script>setTimeout(() => { window.location.href = '/Admin/View_AC?Type=Loan'; }, 2000);</script>";
                } else {
                    echo "<div class='alert alert-danger'>Invalid password.</div>";
                }
            } else {
                echo "<div class='alert alert-danger'>No account found with this User ID.</div>";
            }

            $stmt->close();
        }
    }
?>



<!-- Gradient Background -->
<div class="bg-gradient" style=" min-height: 100vh;">
    <div class="container py-5">
        <div class="mx-auto" style="max-width: 420px; margin: auto; margin-top: 100px; background: linear-gradient(135deg, #f5f7fa, #d9dce0ff);">
            <div class="card shadow-lg border-0 rounded-4 p-4" style="padding: 20px;">
                <h4 class="text-center mb-4 fw-semibold text-primary">Login to Your Account</h4>
                <form method="post" enctype="multipart/form-data">
                    <div class="mb-3">
                        <label for="user_id" class="form-label">User ID:</label>
                        <input type="text" class="form-control form-control-lg rounded-3 shadow-sm" id="user_id" name="user_id" placeholder="Enter your user ID" required>
                    </div>
                    <div class="mb-3">
                        <label for="pwd" class="form-label">Password</label>
                        <input type="password" class="form-control form-control-lg rounded-3 shadow-sm" id="pwd" name="pwd" placeholder="Enter your password" required>
                    </div>
                    <div class="form-check mb-3">
                        <input type="checkbox" class="form-check-input" id="remember" name="remember">
                        <label class="form-check-label" for="remember">Remember me</label>
                    </div>
                    <div class="d-grid">
                        <button type="submit" class="btn btn-md btn-primary">Login</button>
                    </div>
                </form>
                <div class="text-center mt-3">
                    <a href="#" class="text-decoration-none text-secondary">Forgot password?</a>
                </div>
            </div>
        </div>
    </div>
</div>


