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

$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];
$myyear = $_SESSION[$myyear];
$compcd = $_SESSION["comp_" . $dbname];

$zroot = $_SESSION["zdir"];

mysqli_select_db($conn, $zdb);

$city = $_REQUEST['city'];
$hotelnm = $_REQUEST['hotelnm'];
$roomcatg = $_REQUEST['roomcatg'];
$checkin = $_REQUEST['checkin'];
$checkout = $_REQUEST['checkout'];

if (!empty($city)) {
    $citycond = "AND `city`='$city'";
}
if (!empty($hotelnm)) {
    $hotelnmcond = "AND `hotel`='$hotelnm'";
}
if (!empty($roomcatg)) {
    $roomcatgcond = "AND `roomcatg`='$roomcatg'";
}

$gdet = mysqli_query($conn, "SELECT a.code,a.name,b.code,b.hotelnm FROM `city` a, `hotels` b 
                                 where a.code = '$city' AND b.code = '$hotelnm'") or die(mysqli_error($conn));
$fetg = mysqli_fetch_array($gdet);

?>
<style type="text/css">
    .rmctg {
        text-transform: uppercase;
    }

    .rmtp {
        background: lightyellow;
    }

    /*CSS*/
    * {
        margin: 0;
        padding: 0;
        box-sizing: border-box;
        font-family: "Poppins", sans-serif;
    }
    
    .sksahil {
        border-radius: 10px;
        background: linear-gradient(91.9deg, rgb(82, 82, 155) 6.3%, rgb(179, 179, 242) 98.9%);
        margin-left: 1%;
        line-height: 1.5;

    }

    .styled-table {
        border-collapse: collapse;
        margin: 25px 0;
        font-size: 0.9em;
        font-family: "Poppins", sans-serif;
        min-width: 400px;
        box-shadow: 0 0 20px rgba(0, 0, 0, 0.15);
        border-radius: 6px 6px 0 0;
        overflow: hidden;
    }

    .styled-table thead tr {
        background: linear-gradient(91.9deg, rgb(75, 207, 250) 6.3%, rgb(25, 159, 249) 98.9%);
        color: #ffffff;
        text-align: left;
    }

    .styled-table th,
    .styled-table td {
        padding: 12px 15px;
    }

    .styled-table tbody tr {
        border-bottom: 1px solid #dddddd;
    }

    .styled-table tbody tr:nth-of-type(even) {
        background-color: #f3f3f3;
    }

    .styled-table tbody tr:last-of-type {
        border-bottom: 2px solid #c2b47e;
    }

    .styled-table tbody tr.active-row {
        font-weight: bold;
        color: grey;
    }

    .topic-text {
        font-size: 18px;
        font-weight: 600;
        color: #fff;
        width: 30%;
        line-height: 1.5;
        border-radius: 10px;
        background: linear-gradient(91.9deg, rgb(82, 82, 155) 6.3%, rgb(179, 179, 242) 98.9%);
    }
</style>

<!-- <p> City : <b style="font-size:19px;color:green"> <?php echo $fetg['name']; ?></b>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Hotel Name : <b style="font-size:19px;color:green"> <?php echo $fetg['hotelnm']; ?> </b>

    Check In Date: <b style="font-size:19px;color:green"> <?php echo date('d-m-Y', strtotime($checkin)); ?> </b>
    &nbsp;&nbsp; &nbsp; Check Out Date : <b style="font-size:19px;color:green"> <?php echo date('d-m-Y', strtotime($checkout)); ?> </b>
</p> -->

<!-- <div class="topic-text"> &nbsp;&nbsp; HOTEL NAME &nbsp; : &nbsp; <b> <?php echo $fetg['hotelnm']; ?> </b></div> -->
<div class="row table-responsive" align="center">
    <div class="col-md-2"></div>
    <div class="col-md-4  sksahil">
        &nbsp; <b style="color:white"> <?php echo $fetg['hotelnm']; ?> </b>
    </div>
</div>


<div class="table-responsive ">

    <table style="background: white;width: 100%" class="table table-bordered table-responsive styled-table">

        <thead style="background: #64CCFC !important ;">
            <tr>
                <th colspan="2" style="width:10%" class="stick" align="center">Room Category / Type</th>
                <?php
                $start = $month = strtotime($checkin);
                $end = strtotime($checkout);
                while ($month <= $end) {
                    $day = date('D', $month);
                ?>
                    <th style="width:3%" class="stick text-center" align="center"><?php echo date('j/m', $month), PHP_EOL; ?><br><b style="color:black"><?= $day ?></b></th>
                <?php
                    $month = strtotime("+1 day", $month);
                }
                ?>
            </tr>
        </thead>
        <tbody>

            <?php
         
            $getroomcatg = mysqli_query($conn, "SELECT roomcatg FROM `roomsacn` WHERE `bookdate`>='$checkin' AND `bookdate`<='$checkout' $roomcatgcond  $citycond $hotelnmcond  
                                                       group by roomcatg having sum(qty) != 0  order by roomcatg") or die(mysqli_error($conn));

            while ($fetroomcatg = mysqli_fetch_array($getroomcatg)) {

                $mhotcatg = mysqli_query($conn, "SELECT code,name FROM roomcatg where code='$fetroomcatg[roomcatg]'") or die(mysqli_error($conn));
                $thotcatg = mysqli_fetch_array($mhotcatg);
                echo "<tr class='rmctg'>";
                echo "<th colspan='1000' style='color:#B19CD9;font-size:16px;width:100%'><b>$thotcatg[name]</b></th>";
                //                print "SELECT a.* from (SELECT roomtype,sum(qty) as tqty FROM `roomsacn` where `bookdate`>='$checkin' AND 
                //                                                   `bookdate`<='$checkout' $citycond $hotelnmcond $roomcatgcond 
                //                                                    AND roomcatg = '$fetroomcatg[roomcatg]' group by roomtype order by roomtype) a where tqty != 0";
                $getroomtype = mysqli_query($conn, "SELECT a.* from (SELECT roomtype,sum(qty) as tqty FROM `roomsacn` where `bookdate`>='$checkin' AND 
                                                   `bookdate`<='$checkout' $citycond $hotelnmcond $roomcatgcond 
                                                    AND roomcatg = '$fetroomcatg[roomcatg]' group by roomtype order by roomtype) a where tqty != 0") or die(mysqli_error($conn));
                //                print table($getroomtype);
                while ($fetroomtype = mysqli_fetch_array($getroomtype)) {

                    $mrtype = mysqli_query($conn, "SELECT code,name FROM roomtype where code='$fetroomtype[roomtype]'") or die(mysqli_error($conn));
                    $trtype = mysqli_fetch_array($mrtype);

                    echo "<tr class=''>";
                    echo "<th class='rmtp' colspan='2' style='color:#D13438;font-size:14px;width:10%'>$trtype[name]</th>";

                    $start = $month = strtotime($checkin);
                    $end = strtotime($checkout);
                    $trooms = 0;

                    $t = 0;
                    while ($month <= $end) {
                        $cdate = date('Y-m-d', $month);
                        //                            date('Y-m-d', $month), PHP_EOL 
                        $getrooms = mysqli_query($conn, "SELECT sum(qty) as qty  FROM `roomsacn` where roomcatg='$fetroomcatg[roomcatg]' 
                                                         AND roomtype='$fetroomtype[roomtype]' AND bookdate='$cdate' $citycond $hotelnmcond 
                                                         $roomcatgcond group by city,hotel,roomcatg,roomtype having sum(qty) != 0 ") or die(mysqli_error($conn));
                        $fetrooms = mysqli_fetch_array($getrooms);
                        $trooms = $trooms + $fetrooms['qty'];
                        $bgcolr = "";
                        $colr = "";
                        if ($fetrooms['qty'] > 0) {
                            $bgcolr = "#A8EEDC";
                            $colr = "#9E1F63";
                        } else if ($fetrooms['qty'] < 0) {
                            $bgcolr = "#ECEBA3";
                            $colr = "red";
                        }

                        echo "<td class='text-center' style='background-color:$bgcolr;color:$colr;text-align:center'>$fetrooms[qty]</td>";

                        $month = strtotime("+1 day", $month);
                    }
                    //                    echo "<td class='text-right' style='color:green'>$trooms</td>";

                    echo "</tr>";
                }
                echo "</tr>";
            }

            $start = $month = strtotime($checkin);
            $end = strtotime($checkout);

            echo "<tr style='background-color:#cccccc'>";
            echo "<td class='text-right' colspan='2' style='font-size:20px;font-weight:bold;color:red;text-align:center'>Total</td>";

            while ($month <= $end) {
                $cdate = date('Y-m-d', $month);
                // date('Y-m-d', $month), PHP_EOL 
                $getrooms = mysqli_query($conn, "SELECT sum(qty) as qty  FROM `roomsacn` where bookdate='$cdate' $citycond $hotelnmcond $roomcatgcond
                                                        group by bookdate having sum(qty) != 0 ") or die(mysqli_error($conn));
                //                print table($getrooms); 
                $fetrooms = mysqli_fetch_array($getrooms);
                $trooms = $trooms + $fetrooms['qty'];
                $bgcolr = "";
                $colr = "";
                if ($fetrooms['qty'] > 0) {
                    $bgcolr = "#A8EEDC";
                    $colr = "#9E1F63";
                } else if ($fetrooms['qty'] < 0) {
                    $bgcolr = "#ECEBA3";
                    $colr = "red";
                }

                echo "<td class='text-center' style='font-size:16px;font-weight:bold;color:red;text-align:center'>$fetrooms[qty]</td>";

                $month = strtotime("+1 day", $month);
            }
            echo "</tr>";
            ?>

        </tbody>
    </table>

</div>
<?php
// } else {
//     echo "<b style='color:red'>No Data as per your Requirement</b>";
// }
?>