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

$zdb = $_SESSION["zdbyear_" . $dbname];
$formname = "Attendance Register - Monthly";
mysqli_select_db($conn, $zdb);
$file = 'attendreg.php';

$cond = "";
$ccond = "";
$ccond1 = "";

$rtit = "";


$deptcond = '';
$deptcond1 = '';
if (!empty($_POST["proces"])) {
    $proces = trim($_POST["proces"]);
    $deptcond = "AND process='$proces'";
    $deptcond1 = "AND b.process='$proces'";

    $getdept = mysqli_query($conn, "SELECT * FROM `process` WHERE code='$_POST[proces]'") or die(mysqli_error($conn));
    $dept = mysqli_fetch_array($getdept);
    $rtit = "<b>Process : " . $dept["name"] . "</b>";
}
//else {
//    browse('Process Cannot Be Empty');
//    header("refresh: 1; $file");
//}

$monno = $_POST["month"];
$year = $_POST["yearno"];
$reptype = $_POST["reptype"];


//browse($reptype);

if (empty($monno)) {
    browse('Month Cannot Be Empty');
    header("refresh: 1; $file");
}

$zyear = right($zdb, 4);
$zsdate = getzdates($zyear, 'S', $conn);
$zedate = getzdates($zyear, 'E', $conn);

if ($monno < 4) {
    $yearno = left($zedate, 4);
} else {
    $yearno = left($zsdate, 4);
}

$monthno = $monno;
if ($monthno > 9) {
    $month = $monthno;
} else {
    $month = '0' . $monthno;
}

$mfrdate = $yearno . '-' . $month . '-01';
$monthdays = date("t", strtotime($mfrdate));

mysqli_select_db($conn, $zdb);
if ($monno != '') {

    $maindb = 'attend';

    $accode = "employee";

    if (!empty($_POST["acname"])) {
        $party = trim($_POST["acname"]);
        $ccond = " AND accode='$party'";
        $ccond1 = " AND b.accode='$party'";
        $rtit = "Single Employee";
    }
    $getpresent = mysqli_query($conn, "SELECT a.attdate as vouchdt, b.name, b.accode from `$maindb` a, accmast b 
                                              where month(a.attdate) = '$monno' and a.yearno ='$year'
                                              AND a.accode = b.accode $deptcond1 $ccond1 group by b.accode order by b.name") or die(mysqli_error($conn));
    
    $fpresent = mysqli_num_rows($getpresent);


//    $hrscalc = 0;
//    if ($deptcode == 'OPMN' || $deptcode == 'CLMC' || $deptcode == 'OPVH' || $deptcode == 'CLVH') {
//        
//    } else {
//        $hrscalc = 1;
//    }


    include('includes/linkshead.php');
    ?>

    <body>
        <div class="container" style="width:100%">
            <?php
            require("includes/head_rep.php");
            echo "<th style='color:red; font-size:12px'>$rtit</th>";
            ?>

            <div class="row">
                <table style="width:100%;background: white;" class="table table-condensed table-hover table-striped table-bordered">
                    <thead class="th1">
                        <tr style="">
                        <tr style="height:10px;background-color: #64CCFC;  color: #ffffff;" id="font">
                            <th style="text-align:left;width:2%"><b>Sr.</b></th>
                            <th style="text-align:left;width:25%"><b>Employee Name</b></th>
                            
                            <?php 
                            $backcolor = '#64CCFC' ; 
                            $fcolor = '#ffffff' ;                                   
                            
                            for ($j = 1; $j <= $monthdays; $j++) {
                                if ($j < 9) {
                                    $dt = $yearno . '-' . $month . '-0' . $j ;
                                } else {
                                    $dt = $yearno . '-' . $month . '-' . $j ;
                                }
                                $timestamp1 = strtotime($dt);
                                $fday = date('D', $timestamp1);
                                
//                                browse($fday);
                                
                                if ($fday == 'Sat') {
                                   $backcolor = 'grey' ; 
                                   $fcolor = 'yellow' ;                                   
                                } else {
                                  $backcolor = '#64CCFC' ; 
                                  $fcolor = '#ffffff' ;                                   
                                }
                            
                                echo "<th style='text-align:center;width:2% ; background-color: $backcolor; color:$fcolor '><b>$j</b></th>" ;
                            } 
                            ?>
                            <th style="text-align:right;width:3%" ><b>Days</b></th>
                            
                            <th style="text-align:right;width:5%" ><b>Hrs/Min</b></th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php
                        $ctr = 0;

                        while ($row = mysqli_fetch_array($getpresent)) {
                            $maccode = $row['accode'];
                            $acname  = $row['name'];
                            
                            

                            $ctr++;
                            echo "<tr>";
                            echo "<td style='color:#5D5F63; text-align:left; width:2%'>$ctr</td>";
                            echo "<td style='color:#5D5F63; text-align:left; width:15%; font-size:14px'>$acname</td>";

                            $presentstring = "";
                            $presentarray = $presentarray1 = [];
                            
                            $accpresent = mysqli_query($conn, "SELECT attdate as vouchdt,intime, outtime, noofhrs, noofmin from `$maindb`  
                                                                     where month(`attdate`) = '$monno' and yearno ='$year'
                                                                      AND accode = '$maccode' order by attdate") or die(mysqli_error($conn));
//                            }
//                            browse(1);
//                                print table($accpresent);

                            while ($fet = mysqli_fetch_array($accpresent)) {
//                                if ($hrscalc == 0) {
                                $empin = strtotime($fet['vouchdt'] . " " . $fet['intime']);
                                $empout = strtotime($fet['vouchdt'] . " " . $fet['outtime']);

//                                } else {
//                                    $empin = strtotime($fet['vouchdt'] . " " . $fet[intime] . ":00");
//                                    $empout = strtotime($fet['vouchdt'] . " " . $fet[outtime] . ":00");
//                                }

                                $diff = ($empout - $empin) / 60;
                                $pdate = date("d", strtotime($fet['vouchdt']));
                                $presentarray[] = $pdate;
                                $presentarray1[intval($pdate)] = abs($diff);
                            }

//                            print_r($presentarray1) ;

                            $tote = 0;
                            $totot = 0;
                            $tnohrs = 0;
                            $tnomin = 0;
                            $tnotot = 0;
                            for ($j = 1; $j <= $monthdays; $j++) {
                                if (in_array($j, $presentarray)) {

                                    $p2 = $presentarray1[$j];
                                    
                                    $gettime = mysqli_query($conn, "SELECT intime, outtime, noofhrs, noofmin from `$maindb` where day(`attdate`) = '$j' and month(`attdate`) = '$monno' and yearno ='$year'
                                                       AND accode = '$maccode'") or die(mysqli_error($conn));
                                    $fettime = mysqli_fetch_array($gettime);
                                    
                                    $intime = substr($fettime['intime'],0,5);
                                    $outtime = substr($fettime['outtime'],0,5);
                                    $nohrs = $fettime['noofhrs'];
                                    $nomin = $fettime['noofmin'];
                                    if ($reptype == '1') {
                                        echo "<td style='color:black; width:3%; font-size:12px'>$intime -- $outtime</td>";
//                                        echo "<td style='color:black; font-size:12px'>$outtime</td>";
                                    } else {
                                        echo "<td style='color:black; width:3%; font-size:12px'>$nohrs:$nomin</td>";
                                    }
                                    $tote++;
                                    $tnohrs = $tnohrs + $nohrs;
                                    $tnomin = $tnomin + $nomin;
                                } else {
                                    if ($j < 9) {
                                        $dt = $yearno . '-' . $month . '-0' . $j ;
                                    } else {
                                        $dt = $yearno . '-' . $month . '-' . $j ;
                                    }
                                    $timestamp1 = strtotime($dt);
                                    $fday = date('D', $timestamp1);
                                
                                    if ($fday == 'Sat') {
                                       echo "<td style='background-color: lightgrey; color:green; width:3%; font-size:12px'>SUN</td>";
                                    } else {
                                       echo "<td style='color:red; width:3%; font-size:12px'>AB</td>";
                                    }
                                }
                            }
                            echo "<td style='color:darkgreen;font-weight:800; text-align:right; width:3%; font-size:14px'>$tote</td>";
                            $tnotot = $tnohrs * 60;
                            $totot = $tnotot + $tnomin;
                            
                            $tothrs = round($totot / 60);
                            $totmins = fmod($totot, 60);
//                            if ($totot > 60) {
//                                $tothrs = round($totot / 60);
//                                $totmins = fmod($totot, 60);
//                            } else {
//                                $tothrs = 0;
//                                $totmins = fmod($totot, 60);
//                            }
                            if ($totot > 0) {
                                if ($totmins < 10) {
                                    $totmins = '0' . $totmins;
                                }
                                echo "<td style='color:darkgreen;font-weight:800; text-align:right; width:5%; font-size:14px'>$tothrs:$totmins</td>";
                            } else {
                                echo "<td></td>";
                            }
                            echo "</tr>";
                        }
                        ?>
                    </tbody>
                </table>
            </div>
    </body>
    <?php
}
?>