<?php
require("includes/check_auth_user.php");
require("includes/dbconnect.php");
require("includes/config.php");
require("includes/function.php");

$user     = $_SESSION["username_" . $dbname];
$pass     = $_SESSION["password_" . $dbname];
$usertype = $_SESSION["usertype_"  . $dbname];
$spltype  = $_SESSION["spltype_"   . $dbname];

$getusercode = mysqli_query($conn,
    "SELECT `code` FROM `passmain`
     WHERE `user`='" . mysqli_real_escape_string($conn, $user) . "'
       AND `password`='" . mysqli_real_escape_string($conn, $pass) . "'"
) or die(mysqli_error($conn));
$gusercode = mysqli_fetch_array($getusercode);
$usercode  = $gusercode['code'];

if (isset($_POST["submit"])) {
    $comp    = mysqli_real_escape_string($conn, $_POST["comp"]);
    $getcomp = mysqli_query($conn, "SELECT * FROM `company` WHERE `code`='$comp'") or die(mysqli_error($conn));
    $fetcomp = mysqli_fetch_array($getcomp);

    $_SESSION["comp_"    . $dbname] = $comp;
    $_SESSION["compchng"]           = $fetcomp['compchng'];
    $_SESSION["tit_"     . $dbname] = $comp;

    header("Location: year.php");
    exit;
}
?>
<!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; ?> &mdash; Select Company</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">

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

        <div class="p-10">
            <!-- Header -->
            <div class="text-center mb-8">
                <div class="inline-flex items-center justify-center w-14 h-14 bg-blue-700 rounded-full mb-4 shadow-lg">
                    <i class="fa-solid fa-building text-white text-xl"></i>
                </div>
                <h1 class="text-xl font-bold text-slate-800">Select Company</h1>
                <p class="text-slate-500 text-sm mt-1">
                    Welcome, <strong class="text-blue-700"><?php echo htmlspecialchars($user); ?></strong>
                </p>
            </div>

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

                <div class="mb-6">
                    <label class="block text-xs font-semibold text-gray-600 mb-1.5 uppercase tracking-wide">
                        Company
                    </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-building text-sm"></i>
                        </span>
                        <select
                            name="comp"
                            autofocus
                            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 bg-white appearance-none transition"
                        >
                            <?php
                            if (true) {
                                if ($spltype == "1") {
                                    $getcomp = mysqli_query($conn, "SELECT * FROM `company` WHERE `compchng`='1'") or die(mysqli_error($conn));
                                } else {
                                    $getcomp = mysqli_query($conn, "SELECT * FROM `company` WHERE `compchng`!='1'") or die(mysqli_error($conn));
                                }
                                while ($gcomp = mysqli_fetch_array($getcomp)) {
                                    echo "<option value='" . $gcomp['code'] . "'>" . htmlspecialchars($gcomp['name']) . "</option>";
                                }
                            } else {
                                $getcomp = mysqli_query($conn,
                                    "SELECT * FROM `usercomp` WHERE `username`='$usercode' GROUP BY cono"
                                ) or die(mysqli_error($conn));
                                while ($gcomp = mysqli_fetch_array($getcomp)) {
                                    echo "<option value='" . $gcomp['cono'] . "'>" . htmlspecialchars($gcomp['cname']) . "</option>";
                                }
                            }
                            ?>
                        </select>
                        <span class="absolute inset-y-0 right-0 pr-3 flex items-center text-gray-400 pointer-events-none">
                            <i class="fa fa-chevron-down text-xs"></i>
                        </span>
                    </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-arrow-right mr-2"></i> CONTINUE
                </button>

            </form>

            <div class="mt-5 text-center">
                <a href="logout.php" class="text-xs text-gray-400 hover:text-red-500 transition-colors">
                    <i class="fa fa-power-off mr-1"></i> Logout
                </a>
            </div>
        </div>

        <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; ?></p>
        </div>
    </div>

</body>
</html>
