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

$zdir = $dbname;
$zcomp = 1;
$zdbroot = $zdir;
$focusfield = "user";

//browse($_SESSION["username_" . $dbname]);

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

    $syear = $_POST["cmbyear"];
    $admin = $_SESSION["admin_" . $dbname];
    $_SESSION["comp_" . $dbname] = $_POST["comp"];

    $_SESSION["tit_" . $dbname] = $_POST["comp"];
    $_SESSION["instid"] = $fetcmp["instid"];
    $_SESSION["accstkn"] = $fetcmp["accstkn"];

    header("Location: year.php");
}

include("includes/linkshead.php");
$user = $_SESSION["username_" . $dbname];
$pass = $_SESSION["password_" . $dbname];
//browse($pass);
//$spltype = $_SESSION["spltype_" . $dbname];
$getusercode = mysqli_query($conn, "SELECT * FROM `passmain` WHERE `user`='$user' and `password`='$pass'") or die(mysqli_error($conn));
$gusercode = mysqli_fetch_array($getusercode);
$usercode = $gusercode['code'];
$usertype = $gusercode['usertype'];
$spltype = $gusercode['spltype'];
?>
</head>

<body class="animated fadeIn">
    <section id="login-container">

        <div class="row">
            <div class="col-md-3" id="login-wrapper">
                <div class="panel panel-primary animated flipInY">
                    <div class="panel-heading">
                        <h3 class="panel-title">Select Company</h3>      
                    </div>
                    <div class="panel-body">
                        <p> Login to access your account.</p>
                        <form class="form-horizontal" action='' method="POST">
                            <div class="form-group">
                                <div class="col-md-12">
                                    <select id='comp' name="comp"  class="form-control select2" style="height:33px;width: 100%">
                                        <?php
                                        if ($usertype == "1") {
                                            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]'>$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]'>$gcomp[cname]</option>";
                                            }
                                        }
                                        ?>
                                    </select>
                                </div>
                            </div>
                            <div class="form-group">
                                <div class="col-md-12">
                                    <input type='submit' name='submit' value="SUBMIT" class="btn btn-primary btn-block" autofocus>
                                </div>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </section>
    <?php include("includes/linksfoot.php") ?>
</body>
</html>
