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

$zdir = $dbname;
$zcomp = 1;
$zdbroot = $zdir;
$focusfield = "user";
$zroot = $_SESSION["zdbroot"];
$zroot1 = substr($zroot, 0, -4);
$password = $_SESSION["password_" . $dbname];

$user = $_SESSION["username_" . $dbname];
$comp = $_SESSION["comp_" . $dbname];
$menutype = $_SESSION["menutype"];
$getusercode = mysqli_query($conn, "SELECT * FROM `passmain` WHERE `user`='$user' and `password`='$password'")or die(mysqli_error($conn));
$gusercode = mysqli_fetch_array($getusercode);
$usercode = $gusercode['code'];
// $usertype = $gusercode['usertype'];
$spltype = $_SESSION["spltype_" . $dbname];
$usertype = $_SESSION["usertype_" . $dbname];

//browse($comp);

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

    $syear = $_POST["cmbyear"];
    $admin = $_SESSION["admin_" . $dbname];
    $comp = $_SESSION["comp_" . $dbname];
    $_SESSION["zdbyear_" . $dbname] = $zroot1 . $comp . '_' . $_POST["cmbyear"];
    $_SESSION["zdbyear_" . $yearname] = $_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;

    header("Location: $menutype");
    // if ($menutype == "1") {
    //     if ($comp == "1") {
    //         header("Location: menumsg.php");
    //     } else {
    //     }
    // } elseif ($menutype == "2") {
    //     if ($comp == "2") {
    //         header("Location: mainmenuip.php");
    //     } else {
    //         header("Location: menuip.php");
    //     }
    // } elseif ($menutype == "3") {
    //     if ($comp == "3") {
    //         header("Location: mainmenunoor.php");
    //     } else {
    //         header("Location: menunoor.php");
    //     }
    // }
}
//                                            print "SELECT * FROM `compyear` WHERE `cono`='$comp' order by `lastdate` DESC";
// browse($comp);
include("includes/linkshead.php");
?>
<script>
    $(document).ready(function() {
        var year = $('#cmbyear').val();

        function showyear() {
            $.ajax({
                type: "GET",
                url: "ajaxyear.php",
                data: {
                    qyear: year
                },
                success: function(result) {
                    $("#date").html(result);
                }
            });
            $('#date').css("height", "60px");
        }
        showyear();
    });
</script>
</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 Year
                        </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='cmbyear' name="cmbyear" class="form-control select2" onchange="return showyear();" style="height:33px;width: 100%">
                                        <?php
                                        if ($usertype == '1') {
                                            $year = mysqli_query($conn, "SELECT * FROM `compyear` WHERE `cono`='$comp' order by `lastdate` DESC") or die(mysqli_error($conn));
                                            while ($row = mysqli_fetch_array($year)) {
                                                echo "<option value=\"$row[finyear]\">$row[yearname]</option>";
                                            }
                                        } else {
                                            $year = mysqli_query($conn, "SELECT `finyear`,`yearname` FROM `usercomp` WHERE `cono`='$comp' and `username`='$usercode' group by finyear order by `lastdate` DESC ") or die(mysqli_error($conn));
                                            //                                            print table($year);
                                            while ($row = mysqli_fetch_array($year)) {
                                                echo "<option value=\"$row[finyear]\">$row[yearname]</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>