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

ini_set('display_errors', 1);
error_reporting(E_ERROR);

$maindb = "accmast";
$keycond = " AND `grpcode`='040' and `active`='1'";

$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];
mysqli_select_db($conn, $zdb);

if (isset($_REQUEST['dept'])) {
    $dept = $_REQUEST['dept'];
    $vouchdt = $_REQUEST['date'];
    $deptcond = '';
    if (!empty($dept)) {
        $deptcond = " and `deptcode` ='$dept'";
    }
    ?>
    <div class="">
        <table class="table-condensed" style="margin: 0 auto;">
            <tr>
                <td class=" buttonform tooltip-bottom" id="ssubmit" style="display: <? Echo $savedisabled ?>" data-toggle="tooltip" data-placement="bottom" title="" data-original-title="Save Record">
                    <button id='submit' type='submit' name='submit' value="Save" class="btn btn-pad btn-square " style="background-color:#008D4C; ">
                        <i class="fa fa-check" style="color:#ffffff"></i>
                    </button>
                </td>
                <td class=" buttonform tooltip-bottom" id="hexit" style="display: <? Echo $exitdisabled ?>" data-toggle="tooltip" data-placement="bottom" title="" data-original-title="Exit">
                    <button id='exit' type="button" class="btn btn-pad btn-square " style="background-color:#cccccc; "  onClick="window.location.href = '<?= 'attendance.php' ?>'">
                        <i class="fa fa-power-off" style="color:red"></i>
                    </button>
                </td>
            </tr>
        </table>
    </div>
    <div class="table-responsive" style="height:800px;overflow-y: auto">
        <table class="table-bordered" width="100%">
            <thead>
                <tr class="mySearchTr">
                    <!--<td style="width:5%" align="center">Sr</td>-->
                    <!-- <td style="width:20%" align="center">De</td> -->
                    <td style="width:5%" align="center">Code</td>
                    <td style="width:35%" align="center">Employee</td>
                    <td style="width:10%" align="center">In</td>
                    <td style="width:10%" align="center">Out</td>
                    <td style="width:10%" align="center">Hrs.</td>
                    <td style="width:10%" align="center">Mins.</td>
                </tr>
            </thead>
            <tbody>

                <?php
                $mctr = $ctr = 0;
                $qry = mysqli_query($conn, "SELECT * FROM `$maindb` WHERE `accode`!='' $keycond $deptcond  ORDER BY deptcode,`custcode`") or die(mysqli_error($conn));
                while ($row = mysqli_fetch_array($qry)) {
                    $gttime = mysqli_query($conn, "Select * from attend where accode = '$row[accode]' and `attdate`='$vouchdt'") or die(mysqli_error($conn));
                    $fttime = mysqli_fetch_array($gttime);
                    // $gproc = mysqli_query($conn, "Select * from process where code = '$row[process]'") or die(mysqli_error($conn));
                    // $fproc = mysqli_fetch_array($gproc);
                    $intime = $outtime = '';
                    $nohrs = $nomins = 0;
                    if (mysqli_num_rows($gttime) > 0) {
                        $intime = $fttime['intime'];
                        $outtime = $fttime['outtime'];
                        $nohrs = $fttime['noofhrs'];
                        $nomins = $fttime['noofmin'];
                    }
                    $mctr++;
                    echo "<tr>";
//                    echo "<td align='left' style='width:5%' valign='top'>$mctr</td>";
                    // echo "<td align='left' style='width:20%' tabindex='-1'>$fproc[name]</td>";
                    echo "<td align='left' style='width:5%' tabindex='-1'>$mctr</td>";
                    echo "<td align='left' style='width:35%' tabindex='-1'><input type='hidden' name='accode_$ctr' style='height:23px;' value='$row[accode]' >$row[name]</td>";
                    echo "<td align='left' style='width:10%'><input type='time'   name='intime_$ctr' class='intime' style='height:23px;width:100%' value='$intime'></td>";
                    echo "<td align='left' style='width:10%'><input type='time'   name='outtime_$ctr' class='outtime' style='height:23px;width:100%' value='$outtime'></td>";
                    echo "<td align='left' style='width:10%'><input type='number' name='nohrs_$ctr' class='nohrs'  style='height:23px;width:100%;text-align:right' value='$nohrs' tabindex='-1' readonly></td>";
                    echo "<td align='left' style='width:10%'><input type='number' name='nomins_$ctr' class='nomins'  style='height:23px;width:100%;text-align:right' value='$nomins' tabindex='-1' readonly></td>";
                    echo"</tr>";

                    $ctr++;
                }
                echo "<input type='hidden' name='items' value='$ctr' id='items'>";
                ?>

            </tbody>
        </table>
    </div>
    <?php
} else {
    echo "<h4 class='text-center'>No Record Found</h4>";
}
?>