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

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


if (isset($_POST['monthno'])) {

    echo "<tr style='height:10px;' class='dt'>";
    echo "<td style='width:10%;' class = 'td' align='center'  color=''><span style='font-size:20px;font-weight:bold;color:white'>Sunday</span>";
    echo "<td style='width:10%;' class = 'td' align='center'  color=''><span style='font-size:20px;font-weight:bold;color:white'>Monday</span>";
    echo "<td style='width:10%;' class = 'td' align='center' color=''><span style='font-size:20px;font-weight:bold;color:white'>Tueday</span>";
    echo "<td style='width:10%;' class = 'td' align='center' color=''><span style='font-size:20px;font-weight:bold;color:white'>Wednesday</span>";
    echo "<td style='width:10%;' class = 'td' align='center' color=''><span style='font-size:20px;font-weight:bold;color:white'>Thursday</span>";
    echo "<td style='width:10%;' class = 'td' align='center' color=''><span style='font-size:20px;font-weight:bold;color:white'>Friday</span>";
    echo "<td style='width:10%;' class = 'td' align='center' color=''><span style='font-size:20px;font-weight:bold;color:white'>Saturday</span>";
    echo "<td style='width:10%;' class = 'td' align='center' color=''><span style='font-size:20px;font-weight:bold;color:red'>Weekly Total</span>";
    echo "</tr>";


    $monthno = $_POST['monthno'];
    $yearno = $_POST['yearno'];
    if ($monthno > 9) {
        $month = $monthno;
    } else {
        $month = '0' . $monthno;
    }

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

    $mfrdate2 = $mfrdate;

    $timestamp2 = strtotime($mtodate);
    $lastday = date('D', $timestamp2);

    $lastdt = date("d", strtotime($mtodate));

    $ctr = 0;
    $ctr2 = 0;
    $tsat = 0;
    $montotal = 0;
    $exit = 0;
    $satctr = 0 ;
    
    $pdate = 0;
    while (strtotime($mfrdate) <= strtotime($mtodate)) {

        $timestamp1 = strtotime($mfrdate);
        $fday = date('D', $timestamp1);

        $ctr = 0;
        $ctr2 = 0;

        echo "<tr style='height:10px;' class=''>";
        if ($fday == 'Mon') {
            echo "<td>&nbsp;</td>";
            $ctr++;
        }

        if ($fday == 'Tue') {
            echo "<td>&nbsp;</td>";
            echo "<td>&nbsp;</td>";
            $ctr = $ctr + 2;
        }

        if ($fday == 'Wed') {
            echo "<td>&nbsp;</td>";
            echo "<td>&nbsp;</td>";
            echo "<td>&nbsp;</td>";
            $ctr = $ctr + 3;
        }

        if ($fday == 'Thu') {
            echo "<td>&nbsp;</td>";
            echo "<td>&nbsp;</td>";
            echo "<td>&nbsp;</td>";
            echo "<td>&nbsp;</td>";
            $ctr = $ctr + 4;
        }

        if ($fday == 'Fri') {
            echo "<td>&nbsp;</td>";
            echo "<td>&nbsp;</td>";
            echo "<td>&nbsp;</td>";
            echo "<td>&nbsp;</td>";
            echo "<td>&nbsp;</td>";
            $ctr = $ctr + 5;
        }

        if ($fday == 'Sat') {
            echo "<td>&nbsp;</td>";
            echo "<td>&nbsp;</td>";
            echo "<td>&nbsp;</td>";
            echo "<td>&nbsp;</td>";
            echo "<td>&nbsp;</td>";
            echo "<td>&nbsp;</td>";
            $ctr = $ctr + 6;
        }
        

        while ($ctr < 7) {
            $i = $mfrdate;
            $pdate = date("d", strtotime($i));
            $timestamp = strtotime($i);

            $day = date('D', $timestamp);

            echo "<td style='width:10%;' class='td' align='center' ><span style='font-size:20px;font-weight:bold;color:blue'>$pdate</span>";
            echo "</td>";

            $mfrdate = date("Y-m-d", strtotime("+1 day", strtotime($mfrdate)));
            
            if ($day == "Sat") {
            }
            

            $ctr++;
            if ($ctr == 8) {
                $ctr = 0;
            }
            
            if ($pdate >= $lastdt) {
                $exit = 1;
                break;
            }
        }
//        echo "<td>&nbsp;</td>";
        echo "</tr>";

        echo "<tr style='height:10px;'>";
        if ($fday == 'Mon') {
            echo "<td>&nbsp;</td>";
            $ctr2++;
        }

        if ($fday == 'Tue') {
            echo "<td>&nbsp;</td>";
            echo "<td>&nbsp;</td>";
            $ctr2 = $ctr2 + 2;
        }

        if ($fday == 'Wed') {
            echo "<td>&nbsp;</td>";
            echo "<td>&nbsp;</td>";
            echo "<td>&nbsp;</td>";
            $ctr2 = $ctr2 + 3;
        }

        if ($fday == 'Thu') {
            echo "<td>&nbsp;</td>";
            echo "<td>&nbsp;</td>";
            echo "<td>&nbsp;</td>";
            echo "<td>&nbsp;</td>";
            $ctr2 = $ctr2 + 4;
        }

        if ($fday == 'Fri') {
            echo "<td>&nbsp;</td>";
            echo "<td>&nbsp;</td>";
            echo "<td>&nbsp;</td>";
            echo "<td>&nbsp;</td>";
            echo "<td>&nbsp;</td>";
            $ctr2 = $ctr2 + 5;
        }

        if ($fday == 'Sat') {
            echo "<td>&nbsp;</td>";
            echo "<td>&nbsp;</td>";
            echo "<td>&nbsp;</td>";
            echo "<td>&nbsp;</td>";
            echo "<td>&nbsp;</td>";
            echo "<td>&nbsp;</td>";
            $ctr2 = $ctr2 + 6;
        }
        
        
        while ($ctr2 < 7) {
            $i = $mfrdate2;
            $bgcolor = 'white' ;
            
//            $holi = 0;
//            $holqry = mysqli_query($conn, "SELECT * from `holiday` where `date` = '$i'") or die(mysqli_error($conn));
//            if (mysqli_num_rows($holqry) > 0) {
//               $fethol =  mysqli_fetch_array($holqry);
//               $bgcolor = '#F6B366' ;
//               $holremark = $fethol['remark'] ;
//               $holi = 1;
//            }
                    
            $pdate = date("d", strtotime($i));
            $timestamp = strtotime($i);

            $day = date('D', $timestamp);

            $getsord = mysqli_query($conn, "SELECT accode,treatment,vouchno from schedtreat where `scheddate` = '$i' and compltdt = '0000-00-00'") or die(mysqli_error($conn));

            $ttot = 0;
            if ($day == "Sat") {
                $satctr++ ;
                if ($satctr == 2 || $satctr == 4) {
//                   $bgcolor = '#F4C0DB' ;
                }
            } 
            
            
            echo "<td style='background-color:$bgcolor'>";
            
            if ($holi == 1) {
//                echo "<span style='color:maroon;font-weight:bold '>$holremark</span><br><br>";
            } 
            while ($fetsord = mysqli_fetch_array($getsord)) {
                $getarticle = mysqli_query($conn, "SELECT accode, name FROM `accmast` WHERE  `accode`='$fetsord[accode]'") or die(mysqli_error($conn));
                $article = mysqli_fetch_array($getarticle);
                $gettreat = mysqli_query($conn, "SELECT code, name FROM `treatment` WHERE  `code`='$fetsord[treatment]'") or die(mysqli_error($conn));
                $ftreat = mysqli_fetch_array($gettreat);
                echo "<a href='consult_ent.php?vouchno=$fetsord[vouchno]&mode=edit' target='_blank' style='color:black'>$article[name]     -  <span style='color:green;font-weight:bold'>$ftreat[name]</span></a><br>";
                $ttot     = $ttot     + 1;
                $tsat     = $tsat     + 1;
                $montotal = $montotal + 1;
            }

            if ($ttot > 0) {
                echo "<span style='color:maroon;font-size:16px;font-weight:bold;border-top:1px solid orange'>Total : $ttot</span>";
            }
            echo "</td>";

            if ($day == "Sat") {
                echo "<td style='text-align:right;width:10%'><br><span  style='text-align:right; color:red;font-size:20px;font-weight:bold;'>$tsat</span></td>";
                $tsat = 0;
            }
            
            $mfrdate2 = date("Y-m-d", strtotime("+1 day", strtotime($mfrdate2)));
            $ctr2++;
            if ($ctr2 == 8) {
                $ctr2 = 0;
            }

            if ($pdate >= $lastdt) {
                for ($k = $ctr2; $k < 7; $k++) {
                    echo "<td style='width:10%'><br><span  style='text-align:right; color:red;font-size:20px;font-weight:bold;'>&nbsp;</span></td>";
                }
                echo "<td style='text-align:right;width:10%'><br><span  style='color:red;font-size:20px;font-weight:bold;'>$tsat</span></td>";
                $exit = 1;
                break;
            }
        }
        echo "</tr>";
        if ($exit == 1) {
            break;
        }
    }

    echo "<tr>";
    echo "<td  colspan='7' style='text-align:right;color:purple;font-size:20px;font-weight:bold;border-top:1px solid green' >Month Total </td>";
    echo "<td  style='text-align:right;color:purple;font-size:20px;font-weight:bold;border-top:1px solid green' >$montotal</td>";
    echo "</tr>";
}
?>
<style> 
    .dt{
        background-color: #454752 !important;
    }

</style>
