<?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];
$curdate = date("Y-m-d");

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:18px;font-weight:bold;color:white'>Sun</span>";
    echo "<td style='width:10%;' class = 'td' align='center'  color=''><span style='font-size:18px;font-weight:bold;color:white'>Mon</span>";
    echo "<td style='width:10%;' class = 'td' align='center' color=''><span style='font-size:18px;font-weight:bold;color:white'>Tue</span>";
    echo "<td style='width:10%;' class = 'td' align='center' color=''><span style='font-size:18px;font-weight:bold;color:white'>Wed</span>";
    echo "<td style='width:10%;' class = 'td' align='center' color=''><span style='font-size:18px;font-weight:bold;color:white'>Thu</span>";
    echo "<td style='width:10%;' class = 'td' align='center' color=''><span style='font-size:18px;font-weight:bold;color:white'>Fri</span>";
    echo "<td style='width:10%;' class = 'td' align='center' color=''><span style='font-size:18px;font-weight:bold;color:white'>Sat</span>";
    echo "<td style='width:10%;' class = 'td' align='center' color=''><span style='font-size:18px;font-weight:bold;color:lightblue'>Week</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:7px;' 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 id='$i' style='font-size:15px;font-weight:bold;color:#0165a0'>$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));
//             $getsord = mysqli_query($conn, "SELECT * from schedtreat where `scheddate` = '$i' ") or die(mysqli_error($conn));
//             $ttot = mysqli_num_rows($getsord);
// //            $ttot = 0;
//             if ($day == "Sat") {
//                 $satctr++;
//                 if ($satctr == 2 || $satctr == 4) {
// //                    $bgcolor = '#F4C0DB';
//                 }
//             }
//             if ($curdate == $i) {
//                 $bgcolor = '#F4C0DB';
//             }


            echo "<td style='background-color:$bgcolor;text-align:center' id='$i' class='caltick'>";

            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 + $ttot;
            $montotal = $montotal + $ttot;
//            }

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

            if ($day == "Sat") {
                echo "<td style='text-align:right;width:10%'><span  style='text-align:center; color:red;font-size:17px;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%'><span  style='text-align:right; color:red;font-size:17px;font-weight:bold;'>&nbsp;</span></td>";
                }
                if ($day != "Sat") {

                    echo "<td style='text-align:right;width:10%'><span  style='color:red;font-size:17px;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>
