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

$formname = 'Attendance(Edit)';
$maindb = 'attend';
$printfile = "cash_print_fr.php";
$file = 'attendedit.php';
$trandbexist = 0;
$grid = '';

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

//$result = mysqli_query($conn,"SELECT * FROM `$maindb` group by vouchdt") or die(mysqli_error($conn));
//$get = mysqli_fetch_array($result);

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

    $staff = $_POST["name"];
    $count = $_POST["items"];

    $a = 1;

    $srl = 1;
//    browse(1);
    for ($i = 0; $i < $count; $i++) {
        $vouchdt = $_POST["vouchdt_$i"];
        $msrno = $_POST["srno_$i"];
        $salhrs = $_POST["salhrs_$i"];
        $hours = $_POST["hours_$i"];
        $advance = $_POST["advance_$i"];
        $remarks = $_POST["remarks_$i"];

        mysqli_query($conn,"DELETE FROM `$maindb` WHERE `vouchdt`='$vouchdt' AND staff = '$staff'") or die(mysqli_error($conn));
        if (!empty($hours)) {
            $srl = $srl + 1;

            mysqli_query($conn,"INSERT INTO $maindb(`vouchdt`, `srno`, `staff`, `hours`, `advance`,`remark`) 
                    VALUES ('$vouchdt', '$srl', '$staff', '$hours', '$advance', '$remarks')") or die(mysqli_error($conn));
        }
    }
//        browse(2);

    insmess();
    header("refresh: 1; $file?$keyfld=$mvouch&mode=edit");
}
//
//if ($addflag != 1) {
//
//    $getvouch = '';
//    if ($addflag == 0) {
//        $getvouch = $_GET[$keyfld];
//    } else {
//        $getvouch = $qvouch;
//    }
//
//    $result = mysqli_query($conn,"SELECT vouchdt FROM `$maindb` group by vouchdt ORDER BY vouchdt DESC LIMIT 0,1") or die(mysqli_error($conn));
//    $get = mysqli_fetch_array($result);
//}
//
//$senable = "";
//$cenable = "";
//
//
//if (isset($_GET["delete"])) {
//    delete($zdb, "$maindb", "$keyfld", $_GET[$keyfld], "$file?mode=last");
//}
date_default_timezone_set('Asia/Calcutta');

$date = date('Y-m-01');
$end_date = date('Y-m-t');

//while (strtotime($date) <= strtotime($end_date)) {
//    echo "$date <br>";
//    $date = date("Y-m-d", strtotime("+1 day", strtotime($date)));
//}

include("includes/linkshead.php");
?>
<script src="MyScript/attenedit.js" type="text/javascript"></script>
</head>
<body>
    <form action='' method='POST'>
        <!-- Default box -->
        <div class="row">
            <div class="col-lg-8">
                <div class="panel panel-default">
                    <div class="panel-heading">
                        <h3 class="panel-title" style="color:inherit"><?php Echo $formname; ?></h3>
                        <div class="actions pull-right">
                            <i class="fa fa-chevron-down"></i>
                            <i class="fa fa-times"></i>
                        </div>
                    </div>
                    <br>

                    <div class="mainhide">
                        <div class="row">
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Name</label>
                            </div>
                            <div class="col-lg-5 col-sm-8 pad">
                                <select class="form-control select2 name" name="name" required>
                                    <?php
                                    Echo "<option value=''></option>";
                                    $gstaff = mysqli_query($conn,'SELECT `code`,`name` FROM `staff` WHERE `code` != "" order by name')or die(mysqli_error($conn));
                                    while ($staff = mysqli_fetch_array($gstaff)) {
                                        Echo "<option value='$staff[code]'>$staff[name]</option>";
                                    }
                                    Echo "</select></td>";
                                    ?>
                                </select>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control">From</label>
                            </div>
                            <div class="col-lg-3 col-md-3 col-sm-5 col-xs-5 pad">
                                <input type="date" class="form-control fdate" name="fdate" value="<? echo $date; ?>" >
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control ">To</label>
                            </div>
                            <div class="col-lg-3 col-md-3 col-sm-5 col-xs-5 pad">
                                <input type="date" class="form-control tdate" name="tdate" value="<? echo $end_date; ?>" >
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-7 col-md-8 col-sm-8 col-xs-8 text-right pad">
                                <input id='attndtl' value="Attendance Details" type="button"  class="btn btn-success mySearchTr btn btn-labeled btn-success" style="background-color: red;">
                            </div>
                        </div>
                    </div>
                    <div class="panel-body tohide" >

                        <center><input type="submit" class="btn btn-success" name="submit" value="submit">
                            <button type="button" class="btn btn-danger" name="exit" onclick="window.location.href = 'default.php'">Exit</button></center>
                        <!--<div class="col-lg-12 table responsive  ssload" style="overflow-y: scroll;">-->
                        <table class="table table-striped  table-bordered" width="100%" style="font-size: 14px;padding:0px;">
                            <thead>
                                <tr class="mySearchTr" style="background-color: #77BCFF;">
                                    <!--<th style="width: 3%;"></th>-->
                                    <th style="width: 10%;">Date</th>
                                    <th style="width: 20%">Day Name</th>
                                    <th style="width: 10%">Salhrs</th>
                                    <th style="width: 10%">Hours</th>
                                    <th style="width: 15%">Advance</th>
                                    <th style="width: 35%">Remarks</th>
                                </tr>
                            </thead>
                            <tbody class="data">

                            </tbody>

                        </table>
                        <!--                        <div class="col-lg-12 pad">
                                                    <input type="button" style="height: 30px; width: 30px; padding: 0px" id="addnew" class="btn btn-success btn-lg form" accode="addnew2" value="+" />
                                                    <button type="button" class="btn btn-primary btn-square"  id="addnew">
                                                        <i class="fa fa-plus" data-toggle='tooltip' title='Add Row'></i>    
                                                    </button>
                        -->                                                    <input type="hidden" id="items" name="items" value="<?php echo ($addflag != 1) ? $ctr : 1; ?>" />  <!--
                                                                        </div>
                                                                    </div>-->
                        <!--</div>-->
                        <!--                    <div class="row">
                                                <div class="panel-footer">
                        <?php // include('includes/buttons.php');  ?>
                                                </div>
                                            </div>
                                        </div>-->
                    </div>
                </div>
                </form>
                <script type="text/javascript">
                    $(window).load(function () {
                        $(".tohide").hide();
                    });
                    $(document).ready(function () {
                        $(".tohide").hide();
                    });
                </script>
                <!--    <div class="modal fade bs-example-modal-lg" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel">
                        <div class="modal-dialog modal-lg">
                            <div class="modal-content">
                                <div class="modal-header">
                                    <button class="close" data-dismiss="modal"><span>&times;</span></button>
                                    <h4 class="modal-title" id="myLargeModalLabel">Search <? Echo $formname; ?></h4>
                                </div>
                                <div class="modal-body">
                                    <div class=" table-responsive">
                                        <table id="example1" class="table table-striped">
                                            <thead>
                                                <tr style="background-color: #1ABC9C;">
                                                    <th>Name</th>
                                                    <th>Init</th>
                                                    <th>Action</th>
                                                </tr>
                                            </thead>
                                            <tbody>
                <?php
//                                if ($addflag != 1) {
//                                    $srchresult = mysqli_query($conn,"SELECT * FROM `$maindb` WHERE `grpcode`='$mgrpcode'")or die(mysqli_error($conn));
//                                    while ($row = mysqli_fetch_array($srchresult)) {
//                                        echo "<tr class='clickable-row' data-href='$file?mode=edit&$keyfld=$row[$keyfld]'>";
//                                        echo "<td align='left'>$row[name]</td>";
//                                        echo "<td align='left'>$row[init]</td>";
//                                        echo "<td align='left'><a href=\"$file?mode=edit&$keyfld=$row[$keyfld]\"><i class='fa fa-pencil' style='font-size:1.3em;color:#00C0EF;cursor:pointer;'></i></a></td>";
//                                        echo "</tr>";
//                                    }
//                                }
                ?>
                                            </tbody>
                                        </table>
                                    </div>
                                </div>
                                <div class="modal-footer"></div>
                            </div>
                        </div>
                    </div>-->
                <?php include("includes/linksfoot.php"); ?>
                </body>
                </html>
