<?php
session_start();
// echo 1;
require("includes/dbconnect.php");
// echo 2;
require("includes/config.php");
// echo 3;
require("includes/function.php");



$error = false;

if (isset($_POST["submit"])) {

    $login = mysqli_query($conn,
        "SELECT * FROM `passmain`
         WHERE `user`     = '" . mysqli_real_escape_string($conn, $_POST['user']) . "'
           AND `password` = '" . mysqli_real_escape_string($conn, $_POST['pass']) . "'"
    ) or die(mysqli_error($conn));

    $_SESSION["zdbroot"] = $dbname;   // sim_root
    $_SESSION["zdir"]    = $dbname;
    $_SESSION["zcompany"] = '';

    if (mysqli_num_rows($login) == 1) {
        $getuser = mysqli_fetch_object($login);

        $_SESSION['username_' . $dbname]   = $_POST["user"];
        $_SESSION['logged-in']             = true;
        $_SESSION["password_" . $dbname]   = $getuser->password;
        $_SESSION["usertype_" . $dbname]   = $getuser->usertype;
        $_SESSION["spltype_"  . $dbname]   = $getuser->spltype;
        $_SESSION["origin_"   . $dbname]   = isset($getuser->origin) ? $getuser->origin : '';
        $_SESSION["agentcode_". $dbname]   = isset($getuser->agentcode) ? (int)$getuser->agentcode : 0;

        header("Location: comp.php");
        exit;
    } else {
        $error = true;
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?php echo $project_title; ?></title>
    <script src="https://cdn.tailwindcss.com"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>
<body class="min-h-screen bg-gradient-to-br from-slate-900 via-blue-950 to-slate-800 flex items-center justify-center p-4">

    <div class="bg-white rounded-2xl shadow-2xl w-full max-w-md overflow-hidden">

        <!-- Top accent bar -->
        <div class="h-1.5 bg-gradient-to-r from-blue-500 via-cyan-400 to-blue-700"></div>

        <div class="p-10">
            <!-- Logo / Brand -->
            <div class="text-center mb-8">
                <div class="inline-flex items-center justify-center w-16 h-16  rounded-full mb-4 shadow-lg">
                    <img src="images/tclublogo.jpeg">
                </div>
                <h1 class="text-2xl font-bold text-slate-800 tracking-wide">
                    <?php echo strtoupper($project_name); ?>
                </h1>
                <p class="text-slate-500 text-sm mt-1">BAG TRADING INVENTORY</p>
            </div>

            <!-- Error alert -->
            <?php if ($error): ?>
            <div class="flex items-center gap-2 bg-red-50 border border-red-200 text-red-700 px-4 py-3 rounded-lg mb-5 text-sm">
                <i class="fa fa-circle-exclamation"></i>
                Invalid username or password. Please try again.
            </div>
            <?php endif; ?>

            <!-- Login Form -->
            <form method="POST" action="">

                <div class="mb-4">
                    <label class="block text-xs font-semibold text-gray-600 mb-1.5 uppercase tracking-wide">
                        Username
                    </label>
                    <div class="relative">
                        <span class="absolute inset-y-0 left-0 pl-3 flex items-center text-gray-400 pointer-events-none">
                            <i class="fa fa-user text-sm"></i>
                        </span>
                        <input
                            type="text"
                            name="user"
                            placeholder="Enter username"
                            autocomplete="off"
                            autofocus
                            value="<?php echo isset($_POST['user']) ? htmlspecialchars($_POST['user']) : ''; ?>"
                            class="w-full pl-9 pr-4 py-2.5 border border-gray-300 rounded-lg text-sm
                                   focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-transparent
                                   text-gray-800 transition"
                        >
                    </div>
                </div>

                <div class="mb-7">
                    <label class="block text-xs font-semibold text-gray-600 mb-1.5 uppercase tracking-wide">
                        Password
                    </label>
                    <div class="relative">
                        <span class="absolute inset-y-0 left-0 pl-3 flex items-center text-gray-400 pointer-events-none">
                            <i class="fa fa-lock text-sm"></i>
                        </span>
                        <input
                            type="password"
                            name="pass"
                            placeholder="Enter password"
                            class="w-full pl-9 pr-4 py-2.5 border border-gray-300 rounded-lg text-sm
                                   focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-transparent
                                   text-gray-800 transition"
                        >
                    </div>
                </div>

                <button
                    type="submit"
                    name="submit"
                    class="w-full bg-blue-700 hover:bg-blue-800 active:bg-blue-900
                           text-white font-semibold py-2.5 rounded-lg
                           transition-colors duration-200 text-sm tracking-widest"
                >
                    <i class="fa fa-sign-in-alt mr-2"></i> LOGIN
                </button>

            </form>
        </div>

        <!-- Footer -->
        <div class="bg-gray-50 border-t border-gray-100 px-10 py-3 text-center">
            <p class="text-gray-400 text-xs">&copy; <?php echo date('Y') . " " . $project_title; ?> &mdash; All rights reserved</p>
        </div>
    </div>

</body>
</html>
