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

$zdir = $dbname;
$zcomp = 1;
$zdbroot = $zdir;
$focusfield = "submit";
$usertype = $_SESSION["usertype"];
$username = $_SESSION["username"];
$getusercode = mysqli_query($conn, "SELECT code,user FROM `passmain` WHERE user = '$username' ") OR die(mysqli_error($conn));
$gusercode = mysqli_fetch_array($getusercode);
$zroot = $_SESSION["zdbroot"];
$zroot1 = substr($zroot, 0, -4);

if (isset($_POST["submit"])) {
    
    $syear = $_POST["cmbyear"];
    $admin = $_SESSION["admin_" . $dbname];
    $comp = $_SESSION["comp_" . $dbname];
    $_SESSION["zdbyear_" . $dbname] = $zroot1 . $comp . '_' . $_POST["cmbyear"];

    $_SESSION["tit_" . $dbname] = $_POST["cmbyear"];

    $year1 = mysqli_query($conn, "SELECT DATE_FORMAT(`startdate`, '%d-%m-%Y') as mfrdt, DATE_FORMAT(`lastdate`, '%d-%m-%Y') as mtodt 
                                 FROM `compyear` WHERE finyear = '$syear' ") OR die(mysqli_error($conn));
    $cyear = mysqli_fetch_array($year1);
    $zsdate = $cyear['mfrdt'];
    $zedate = $cyear['mtodt'];
    $myr = $_POST["cmbyear"];
    $mdb = $_SESSION["zdbyear_" . $dbname];
    $_SESSION["zdbyear_" . $zsdate] = $zsdate;
    $_SESSION["zdbyear_" . $zedate] = $zedate;
    $_SESSION[$myyear] = $myr;

//    browse($_SESSION["zdbyear_" . $dbname]);
    header("Location: menu.php");
}
include("includes/linkshead.php");
?>


</head>
<body>
    <!-- Page container -->
    <div class="page-container login-container">

        <!-- Page content -->
        <div class="page-content">

            <!-- Main content -->
            <div class="content-wrapper">

                <!-- Content area -->
                <div class="content">

                    <!-- Simple login form -->
                    <form action="" method="POST">
                        <div class="panel panel-body login-form">
                            <div class="text-center">
                                <!--<div class="icon-object border-slate-300 text-slate-300"><i class="icon-reading"></i></div>-->
                                <h4 class="content-group">Select Year</h4>
                            </div>

                            <div class="form-group has-feedback has-feedback-left text-center">
                                <select id='cmbyear' name="cmbyear"  class="form-control select2" style="height:33px;width: 85%">
                                    <?php
                                    if ($usertype < 2) {
                                        $year = mysqli_query($conn, "SELECT * FROM `compyear` order by `lastdate` DESC") OR die(mysqli_error($conn));
                                    } else {
                                        $year = mysqli_query($conn, "SELECT * FROM `usercomp` where `username`='$gusercode[code]' order by `lastdate` DESC") OR die(mysqli_error($conn));
                                    }

                                    while ($row = mysqli_fetch_array($year)) {
                                        echo "<option value=\"$row[finyear]\">$row[finyear]</option>";
                                    }
                                    ?>
                                </select>
                                <div class="form-control-feedback">
                                    <!--<i class="icon-user text-muted"></i>-->
                                </div>
                            </div>

                            <div class="form-group">
                                <button type="submit" id="submit" name="submit" class="btn btn-primary btn-block">Sign in <i class="icon-circle-right2 position-right"></i></button>
                            </div>
                        </div>
                    </form>
                    <!-- /simple login form -->
                </div>
                <!-- /content area -->
            </div>
            <!-- /main content -->
        </div>
        <!-- /page content -->
    </div>
    <!-- /page container -->
    <?php include("includes/linksfoot.php") ?>
    <script>
        $("#submit").focus();
    </script>
</body>
</html>
