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

$maindb = 'pax';
$trandb = 'tranpurch';
$formname = "Hotel Arrival Details";
$file = "hotelarrvdetl.php";

$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];
$zroot = $_SESSION["zdir"];

$tourcode = $_SESSION['tourcode'];
$tourcond = "AND tourno = '$tourcode'";
$myyear = $_SESSION[$myyear];
mysqli_select_db($conn, $zdb);

$mfrmdate = $_POST["frmdt"];
$mtodate = $_POST["todt"];

if (empty($_POST["frmdt"])) {
    browse("From Date Cannot Be Empty");
    echo "<script>window.close()</script>";
}

if (empty($_POST["todt"])) {
    browse("To Date Cannot Be Empty");
    echo "<script>window.close()</script>";
}

// $agent = $_POST["agent"];
$city = $_POST["city"];
$hotel = $_POST["hotel"];
$source = $_POST["source"];

$citycond = "";
$hotelcond = "";
$hotelcond1 = "";
$agentcond = "";
$agentcond1 = "";

if ($source == "F") {
    $sourceccond = "WHERE source = 'FIT'";
} elseif ($source == "H") {
    $sourceccond = "WHERE source = 'HTL'";
} elseif ($source == "U") {
    $sourceccond = "WHERE source = 'UMRAH'";
} else {
    $sourceccond = "";
}


// if (!empty($agent)) {
//     $agentcond = "AND b.`city` = '$agent'";
//     $agentcond1 = "AND a.`city` = '$agent'";
// }

if (!empty($city)) {
    $citycond = "AND b.`city` = '$city'";
    $citycond1 = "AND a.`city` = '$city'";
}

if (!empty($hotel)) {
    $hotelcond = "AND b.`hotelnm` = '$hotel'";
    $hotelcond1 = "AND `hotelnm` = '$hotel'";
}

$qrycity = mysqli_query($conn, "SELECT code, name, umrah FROM city WHERE code ='$city' ") or die(mysqli_error($conn));
$fetcity = mysqli_fetch_array($qrycity);
$umrahcity = $fetcity['umrah'];

if ($umrahcity == 1) {
    $uhotel = 'makhotel';
    $uchkin = 'makchkin';
    $uchkout = 'makchkout';
    $uroomcatg = 'mkroomcatg';
    $uroomtype = '';
    $umealtype = 'Full Board';
    if (!empty($hotel)) {
        $hotelcond1 = "AND (`makhotel` = '$hotel' OR `othhotel` = '$hotel') ";
    }

    $ur1 = 'mksingle';
    $ur2 = 'mkdouble';
    $ur3 = 'mktriple';
    $ur4 = 'mkquad';
    $ur5 = 'mkquint';
    $ur6 = 'mksuite';

    $mur1 = 'mk_singlepax';
    $mur2 = 'mk_doublepax';
    $mur3 = 'mk_triplepax';
    $mur4 = 'mk_quadpax';
    $mur5 = 'mk_quintpax';
    $mur6 = 'mk_suitepax';
}

if ($umrahcity == 2) {
    $uhotel = 'madhotel';
    $uchkin = 'madchkin';
    $uchkout = 'madchkout';
    $uroomcatg = 'mdroomcatg';
    $uroomtype = '';
    $umealtype = 'Full Board';
    if (!empty($hotel)) {
        $hotelcond1 = "AND `madhotel` = '$hotel'";
    }

    $ur1 = 'mdsingle';
    $ur2 = 'mddouble';
    $ur3 = 'mdtriple';
    $ur4 = 'mdquad';
    $ur5 = 'mdquint';
    $ur6 = 'mdsuite';

    $mur1 = 'md_singlepax';
    $mur2 = 'md_doublepax';
    $mur3 = 'md_triplepax';
    $mur4 = 'md_quadpax';
    $mur5 = 'md_quintpax';
    $mur6 = 'md_suitepax';
}

mysqli_query($conn, "drop temporary table if exists hotdbf") or die(mysqli_error($conn));
mysqli_query($conn, "create temporary table hotdbf 
                            SELECT a.vouchno, a.prname as custname, a.customer as agent, b.name as agentname
                                   FROM hotelslmain a, accmast b
                                   WHERE a.customer = b.accode  and a.vouchno in
                                   (select vouchno from hotelsltran where `checkin` >= '$mfrmdate' AND `checkin` <= '$mtodate' ) ") or die(mysqli_error($conn));

if ($umrahcity == 0) {
    $getpmain = mysqli_query($conn, "select a.* from 
                                        (SELECT 'FIT' as source, a.vouchno, concat(a.surname, a.name, a.midname) as custname, a.agent as agcode, '' as agentname, 
                                        b.hotelnm, b.checkin, b.checkout, b.roomcatg, b.roomtype, b.nonights, b.mealtype, '' as mealname, 
                                        b.qty, b.hotelpax FROM $maindb a, $trandb b 
                                        WHERE  a.vouchno = b.vouchno AND b.`checkin` >= '$mfrmdate' AND b.`checkin` <= '$mtodate' and a.hotelaprov = '1'
                                        $citycond $hotelcond 
                                        union all
                                        SELECT 'HTL' as source, a.vouchno, a.custname, a.agent, b.hotelnm, b.checkin, b.checkout, 
                                        b.roomcatg, b.roomtype, b.nonights, b.mealtype, '' as mealname, b.qty, b.hotelpax FROM hotdbf a, hotelsltran b 
                                        WHERE  a.vouchno = b.vouchno AND b.`checkin` >= '$mfrmdate' AND b.`checkin` <= '$mtodate' and a.approve = '1'
                                        $citycond $hotelcond) a
                                        $sourceccond order by hotelnm, checkin, custname") or die(mysqli_error($conn));
} else {
    
    $getpmain = mysqli_query($conn, "
                                    SELECT a.* from 
                                    (select 'FIT' as source, a.vouchno, concat(a.surname, a.name, a.midname) as custname, a.agent as agcode, '' as agentname, 
                                        b.hotelnm, b.checkin, b.checkout, b.roomcatg, b.roomtype, b.nonights, b.mealtype, '' as mealname, 
                                        b.qty, 0 as $ur1, 0 as $ur2, 0 as $ur3, 0 as $ur4, 0 as $ur5, 0 as $ur6 ,
                                        0 as $mur1, 0 as $mur2, 0 as $mur3, 0 as $mur4, 0 as $mur5, 0 as $mur6, b.hotelpax as pax     
                                        FROM $maindb a, $trandb b WHERE  a.vouchno = b.vouchno and a.hotelaprov = '1' AND 
                                        b.`checkin` >= '$mfrmdate' AND b.`checkin` <= '$mtodate' $citycond $hotelcond 
                                    UNION ALL        
                                    select 'HTL' as source, a.vouchno, a.custname, a.agent as agcode, a.agentname,
                                        b.hotelnm, b.checkin, b.checkout, b.roomcatg, b.roomtype, b.nonights, b.mealtype, '' as mealname, 
                                        b.qty, 0 as $ur1, 0 as $ur2, 0 as $ur3, 0 as $ur4, 0 as $ur5, 0 as $ur6 ,
                                        0 as $mur1, 0 as $mur2, 0 as $mur3, 0 as $mur4, 0 as $mur5, 0 as $mur6, b.hotelpax as pax     
                                        FROM hotdbf a, hotelsltran b WHERE  a.vouchno = b.vouchno AND 
                                        b.`checkin` >= '$mfrmdate' AND b.`checkin` <= '$mtodate' $citycond $hotelcond 
                                    UNION ALL
                                    SELECT 'UMRAH' as source, vouchno, concat(surname, haji) as custname, '' as agcode, agent as agentname,
                                        $uhotel as hotelnm, $uchkin as checkin, $uchkout as checkout, 
                                        $uroomcatg as roomcatg, 1 as roomtype, datediff($uchkout, $uchkin) as nonights,
                                        '1' as mealtype, 'Full Board' as mealname, 0 as qty, $ur1, $ur2, $ur3, $ur4, $ur5, $ur6,
                                        $mur1, $mur2, $mur3, $mur4, $mur5, $mur6, ($mur1 + $mur2 + $mur3 + $mur4 + $mur5 + $mur6) as pax FROM bookform                                          
                                        WHERE $uchkin >= '$mfrmdate' AND $uchkin <= '$mtodate' and cancdate = '0000-00-00' $hotelcond1 $tourcond
                                    UNION ALL
                                    SELECT 'UMRAH' as source, vouchno, concat(surname, haji) as custname, '' as agcode, agent as agentname,
                                        othhotel as hotelnm, othchkin as checkin, othchkout as checkout, 
                                        $uroomcatg as roomcatg, 1 as roomtype, datediff(othchkout, othchkin) as nonights,
                                        '1' as mealtype, 'Full Board' as mealname, 0 as qty, $ur1, $ur2, $ur3, $ur4, $ur5, $ur6,
                                        $mur1, $mur2, $mur3, $mur4, $mur5, $mur6, ($mur1 + $mur2 + $mur3 + $mur4 + $mur5 + $mur6) as pax FROM bookform                                          
                                        WHERE othchkin >= '$mfrmdate' AND othchkin <= '$mtodate'  and cancdate = '0000-00-00' $hotelcond1 $tourcond ) a 
                                        $sourceccond order by hotelnm, checkin, custname ") or die(mysqli_error($conn));
}

// print table($getpmain);
// die();

if (isset($_POST["view"])) {
    include("includes/linkshead.php");
    ?>
    <style type="text/css">
        #tabital {
            font-style: italic;
        }

        #font1 {
            font-style: italic;
        }

        #boldfont {
            font-weight: bold;
        }

        @media screen {


            .overlay-scroll {
                height: 550px;
                /* Set your desired height */
                overflow-y: auto;
                /* scrollbar-width: thin;         Firefox */
                scrollbar-color: rgba(0, 0, 0, 0.5) transparent;
                /* Firefox */
            }

            .overlay-scroll::-webkit-scrollbar {
                width: 20px;
            }

            .overlay-scroll::-webkit-scrollbar-track {
                background: transparent;
            }

            .overlay-scroll::-webkit-scrollbar-thumb {
                background-color: rgba(0, 0, 0, 0.4);
                border-radius: 4px;
            }
        }

        .table thead th {
            position: sticky;
            top: 0;
            background-color: #D0F4DE;
            color: black !important;
            font-weight: bold;
            /* same as your header background */
            z-index: 2;
        }
    </style>

    </head>

    <body>

        <div class="container">
            <div class="row">
                <div class="col-lg-12 ">
                    <?php include("includes/head_rep.php"); ?>

                    <div class="overlay-scroll">
                        <table class="table table-condensed table-bordered" style="width:100%;">

                            <?php
                            $ctr = 0;
                            $ttotamt = 0;
                            $totsaleamt = 0;
                            $totpnl = 0;
                            $thotel = '';
                            $aa = 0;
                            while ($pmain = mysqli_fetch_array($getpmain)) {
                                $mhotel = $pmain['hotelnm'];
                                $agentname = $pmain['agentname'];
                                $fullname = $pmain['custname'];
                                $source = $pmain['source'];
                                $roomqty = $pmain['qty'];

                                if ($thotel != $mhotel) {
                                    if ($aa == 1 && $thotel != '') {
                                        echo "<tr style='height: 30px;'>";
                                        echo "<td colspan='12' style='font-size:15px;'>&nbsp;</td>";
                                        echo "</tr>";
                                    }
                                    $qryhotel = mysqli_query($conn, "SELECT code, hotelnm FROM hotels WHERE code ='$mhotel'") or die(mysqli_error($conn));
                                    $fethotel = mysqli_fetch_array($qryhotel);

                                    echo "<tr style='height: 30px;'>";
                                    echo "<td colspan='12' style='font-size:18px; background-color:#BDE0FE;text-align:center'>$fethotel[hotelnm]</td>";
                                    echo "</tr>";
                                    ?>
                                    <thead>
                                        <tr class="" style="height: 30px;background-color:#D0F4DE">
                                            <th style="font-size:16px;width:3%">Sr. </th>
                                            <th style="font-size:16px;width:20%">Lead Pax</th>
                                            <th style="font-size:16px;width:20%;">Agent</th>
                                            <th style="font-size:16px;width:8%;">Arrival</th>
                                            <th style="font-size:16px;width:8%;">Departure</th>
                                            <th style="font-size:16px;width:5%;text-align:center">Nights</th>
                                            <th style="font-size:16px;width:5%;text-align:center">Pax</th>
                                            <th style="font-size:16px;width:14%;">Room Type</th>
                                            <!--                                        <th style="font-size:16px;width:6%;">Rooms</th>-->
                                            <th style="font-size:16px;width:8%;">Category</th>
                                            <th style="font-size:16px;width:8%;">Meals</th>
                                            <th style="font-size:16px;width:6%;">Source</th>
                                        </tr>
                                    </thead>
                                    <?php
                                    $ctr = 0;
                                    $thotel = $mhotel;
                                    if ($aa == 0) {
                                        $aa = 1;
                                    }
                                }

                                if ($source == 'FIT') {
                                    $url = "fit_ent.php?mode=edit&paxid=$pmain[vouchno]";

                                    $getagent = mysqli_query($conn, "SELECT accode,name FROM `accmast` WHERE accode = '$pmain[agcode]' ") or die(mysqli_error($conn));
                                    $fetagent = mysqli_fetch_array($getagent);
                                    $agentname = $fetagent["name"];

                                    $mmeal = mysqli_query($conn, "SELECT code,name FROM $zroot.mealtype WHERE code ='$pmain[mealtype]'") or die(mysqli_error($conn));
                                    $tmeal = mysqli_fetch_array($mmeal);
                                    $mealname = $tmeal['name'];

                                    $mrtype = mysqli_query($conn, "SELECT code, name FROM roomtype WHERE code ='$pmain[roomtype]' ") or die(mysqli_error($conn));
                                    $trtype = mysqli_fetch_array($mrtype);
                                    
                                    $rmty = strtoupper($trtype['name']) ;
                                            
                                    if ($rmty == 'QUAD') {
                                       if ($pmain['pax'] < 4) {
                                          $roomtype = $rmty . " SHARING - " . $roomqty ;
                                       } else {    
                                          $roomtype = $rmty . " - " . $roomqty;
                                       }
                                    } else {
                                       $roomtype = $rmty . " - " . $roomqty;
                                    }
                                               
                                    
                                } else if ($source == 'HTL') {
                                    $url = "hotelsl.php?mode=edit&vouchno=$pmain[vouchno]";
                                    $mmeal = mysqli_query($conn, "SELECT code,name FROM $zroot.mealtype WHERE code ='$pmain[mealtype]'") or die(mysqli_error($conn));
                                    $tmeal = mysqli_fetch_array($mmeal);
                                    $mealname = $tmeal['name'];

                                    $mrtype = mysqli_query($conn, "SELECT code, name FROM roomtype WHERE code ='$pmain[roomtype]' ") or die(mysqli_error($conn));
                                    $trtype = mysqli_fetch_array($mrtype);
                                    
                                    $rmty = strtoupper($trtype['name']) ;
                                            
                                    if ($rmty == 'QUAD') {
                                       if ($pmain['pax'] < 4) {
                                          $roomtype = $rmty . " SHARING - " . $roomqty ;
                                       } else {    
                                          $roomtype = $rmty . " - " . $roomqty;
                                       }
                                    } else {
                                       $roomtype = $rmty . " - " . $roomqty;
                                    }
                                    
                                } else {
                                    $url = "bookform.php?mode=edit&vouchno=$pmain[vouchno]";
                                    $mealname = $pmain['mealname'];
                                    $u1 = $pmain[$ur1] * $pmain[$mur1];
                                    $u2 = $pmain[$ur2] * $pmain[$mur2];
                                    $u3 = $pmain[$ur3] * $pmain[$mur3];
                                    $u4 = $pmain[$ur4] * $pmain[$mur4];
                                    $u5 = $pmain[$ur5] * $pmain[$mur5];
                                    $u6 = $pmain[$ur6] * $pmain[$mur6];
                                    $roomtot = $u1 + $u2 + $u3 + $u4 + $u5 + $u6;

//                                    if ($umrahcity == 1) {
//                                        $mkrm = '';
//                                        $mkr = 0;
//                                        if ($pmain['mksingle'] != 0) {
//                                            $mkrm = 'SGL-' . $pmain['mksingle'];
//                                            $mkr = $pmain[$ur1] * 1;
//                                        }
//
//                                        if ($pmain['mkdouble'] > 0) {
//                                            if ($mkrm != '') {
//                                                $mkrm = $mkrm . ' / DBL-' . $pmain['mkdouble'];
//                                            } else {
//                                                $mkrm = 'DBL-' . $pmain['mkdouble'];
//                                            }
//                                            $mkr = $pmain[$ur2] * 2;
//                                        }
//
//                                        if ($pmain['mktriple'] != 0) {
//                                            if ($mkrm != '') {
//                                                $mkrm = $mkrm . ' / TRP-' . $pmain['mktriple'];
//                                            } else {
//                                                $mkrm = 'TRP-' . $pmain['mktriple'];
//                                            }
//                                            $mkr = $pmain[$ur3] * 3;
//                                        }
//
//                                        if ($pmain['mkquad'] != 0) {
//                                            if ($mkrm != '') {
//                                                $mkrm = $mkrm . ' / QUAD-' . $pmain['mkquad'];
//                                            } else {
//                                                $mkrm = 'QUAD-' . $pmain['mkquad'];
//                                            }
//                                            $mkr = $pmain[$ur4] * 4;
//                                        }
//
//                                        if ($pmain['mkquint'] != 0) {
//                                            if ($mkrm != '') {
//                                                $mkrm = $mkrm . ' / QUINT-' . $pmain['mkqunit'];
//                                            } else {
//                                                $mkrm = 'QUINT-' . $pmain['mkquint'];
//                                            }
//                                            $mkr = $pmain[$ur5] * 5;
//                                        }
//
//                                        if ($pmain['mksuite'] != 0) {
//                                            if ($mkrm != '') {
//                                                $mkrm = $mkrm . ' / SUITE-' . $pmain['mksuite'];
//                                            } else {
//                                                $mkrm = 'SUITE-' . $pmain['mksuite'];
//                                            }
//                                            $mkr = $pmain[$ur6] * 6;
//                                        }
//
//                                        $roomtype = $mkrm;
//                                        $roomqty = round($roomtot / $mkr, 2);
//                                    }
                                    /////////////// madina
//                                    if ($umrahcity == 2) {
//                                        $mdrm = '';
//                                        $mdr = 0;
//                                        if ($pmain['mdsingle'] != 0) {
//                                            $mdrm = 'SGL-' . $pmain['mdsingle'];
//                                            $m1++;
//                                            $mdr = $pmain[$ur1] * 1;
//                                        }
//
//                                        if ($pmain['mddouble'] != 0) {
//                                            if ($mdrm != '') {
//                                                $mdrm = $mdrm . ' / DBL-' . $pmain['mddouble'];
//                                            } else {
//                                                $mdrm = 'DBL-' . $pmain['mddouble'];
//                                            }
//                                            $m2++;
//                                            $mdr = $pmain[$ur2] * 2;
//                                        }
//
//                                        if ($pmain['mdtriple'] != 0) {
//                                            if ($mdrm != '') {
//                                                $mdrm = $mdrm . ' / TRP-' . $pmain['mdtriple'];
//                                            } else {
//                                                $mdrm = 'TRP-' . $pmain['mdtriple'];
//                                            }
//                                            $m3++;
//                                            $mdr = $pmain[$ur3] * 3;
//                                        }
//
//                                        if ($pmain['mdquad'] != 0) {
//                                            if ($mdrm != '') {
//                                                $mdrm = $mdrm . ' /  QUAD-' . $pmain['mdquad'];
//                                            } else {
//                                                $mdrm = 'QUAD-' . $pmain['mdquad'];
//                                            }
//                                            $m4++;
//                                            $mdr = $pmain[$ur4] * 4;
//                                        }
//
//                                        if ($pmain['mdquint'] != 0) {
//                                            if ($mdrm != '') {
//                                                $mdrm = $mdrm . ' / QUINT-' . $pmain['mdquint'];
//                                            } else {
//                                                $mdrm = 'QUINT-' . $pmain['mdqunit'];
//                                            }
//                                            $m5++;
//                                            $mdr = $pmain[$ur5] * 5;
//                                        }
//
//                                        if ($pmain['mdsuite'] != 0) {
//                                            if ($mdrm != '') {
//                                                $mdrm = $mdrm . ' / SUITE-' . $pmain['mdsuite'];
//                                            } else {
//                                                $mdrm = 'SUITE-' . $pmain['mdsuite'];
//                                            }
//                                            $m6++;
//                                            $mdr = $pmain[$ur6] * 6;
//
//                                        }
//
//                                        $roomtype = $mdrm;
//                                      
//                                    }


                                    $roomConfig = [
                                        1 => [// MAKKAH
                                            ['label' => 'SGL', 'qty' => 'mksingle', 'rate' => $ur1, 'cap' => 1],
                                            ['label' => 'DBL', 'qty' => 'mkdouble', 'rate' => $ur2, 'cap' => 2],
                                            ['label' => 'TRP', 'qty' => 'mktriple', 'rate' => $ur3, 'cap' => 3],
                                            ['label' => 'QUAD', 'qty' => 'mkquad', 'rate' => $ur4, 'cap' => 4],
                                            ['label' => 'QUINT', 'qty' => 'mkquint', 'rate' => $ur5, 'cap' => 5],
                                            ['label' => 'SUITE', 'qty' => 'mksuite', 'rate' => $ur6, 'cap' => 6],
                                        ],
                                        2 => [// MADINA
                                            ['label' => 'SGL', 'qty' => 'mdsingle', 'rate' => $ur1, 'cap' => 1],
                                            ['label' => 'DBL', 'qty' => 'mddouble', 'rate' => $ur2, 'cap' => 2],
                                            ['label' => 'TRP', 'qty' => 'mdtriple', 'rate' => $ur3, 'cap' => 3],
                                            ['label' => 'QUAD', 'qty' => 'mdquad', 'rate' => $ur4, 'cap' => 4],
                                            ['label' => 'QUINT', 'qty' => 'mdquint', 'rate' => $ur5, 'cap' => 5],
                                            ['label' => 'SUITE', 'qty' => 'mdsuite', 'rate' => $ur6, 'cap' => 6],
                                        ]
                                    ];

                                    $roomLabels = [];
                                    $totalCapacity = 0;

                                    foreach ($roomConfig[$umrahcity] as $room) {
                                        if (!empty($pmain[$room['qty']]) && $pmain[$room['qty']] > 0) {

                                            $label = $room['label'];

                                            // ✅ QUAD SHARING logic for pax < 4
                                            if ($label === 'QUAD' && $pmain['pax'] < 4) {
                                                $label = 'QUAD SHARING';
                                            }

                                            $roomLabels[] = $label . '-' . $pmain[$room['qty']];
                                            $totalCapacity += $pmain[$room['rate']] * $room['cap'];
                                        }
                                    }

                                    $roomtype = implode(' / ', $roomLabels);
                                    
                                    if ($totalCapacity > 0) {
                                        $roomqty = round($roomtot / $totalCapacity, 2);
                                    } else {
                                        $roomqty = 0;
                                    }
                                    
                                }

                                $mhotcatg = mysqli_query($conn, "SELECT code,name FROM roomcatg WHERE code='$pmain[roomcatg]'") or die(mysqli_error($conn));
                                $thotcatg = mysqli_fetch_array($mhotcatg);

                                $ctr++;
                                echo "<tr style='height: 30px;' class='clickable-row2' data-href='$url' data-target='_blank' >";
                                echo "<td style='font-size:12px;'>$ctr</td>";
                                echo "<td style='font-size:13px;'>$fullname</td>";
                                echo "<td style='font-size:13px;'>$agentname</td>";
                                echo "<td style='font-size:13px;'>" . format_date($pmain['checkin']) . "</td>";
                                echo "<td style='font-size:13px;'>" . format_date($pmain['checkout']) . "</td>";
                                echo "<td style='font-size:14px;text-align:center'>$pmain[nonights]</td>";
                                echo "<td style='font-size:14px;text-align:center'>$pmain[pax]</td>";
                                echo "<td style='font-size:12px;'>$roomtype</td>";
                                //                                echo "<td style='font-size:11px;text-align:center'>$roomqty</td>";
                                echo "<td style='font-size:12px;'>$thotcatg[name]</td>";
                                echo "<td style='font-size:12px;'>$mealname</td>";
                                echo "<td style='font-size:12px;'>$source</td>";
                                echo "</tr>";
                            }
                            ?>

                        </table>
                    </div>
                </div>
            </div>
        </div>

        <?php include("includes/linksfoot.php");
        ?>
    </body>

    </html>
    <?php
}



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


    $ctr = 0;
    $ttotamt = 0;
    $totsaleamt = 0;
    $totpnl = 0;
    $thotel = '';
    $aa = 0;
    while ($pmain = mysqli_fetch_array($getpmain)) {
        $mhotel = $pmain['hotelnm'];
        $agentname = $pmain['agentname'];
        $fullname = $pmain['custname'];
        $source = $pmain['source'];
        $roomqty = $pmain['qty'];

        if ($thotel != $mhotel) {
            if ($aa == 1) {
                $contents .= "\n";
            }
            $qryhotel = mysqli_query($conn, "SELECT code, hotelnm FROM hotels WHERE code ='$pmain[hotelnm]'") or die(mysqli_error($conn));
            $fethotel = mysqli_fetch_array($qryhotel);

            $contents .= "$fethotel[hotelnm] \n";

            $contents .= "Sr." . ",";
            $contents .= "Lead Pax" . ",";
            $contents .= "Agent" . ",";
            $contents .= "Arrival" . ",";
            $contents .= "Departure" . ",";
            $contents .= "Nights" . ",";
            $contents .= "Pax" . ",";
            $contents .= "Room Type" . ",";
            $contents .= "Rooms" . ",";
            $contents .= "Category" . ",";
            $contents .= "Meals" . ",";
            $contents .= "Source" . "\n";

            $ctr = 0;
            $thotel = $mhotel;
            if ($aa == 0) {
                $aa = 1;
            }
        }

        if ($source == 'FIT') {
            $url = "fit_ent.php?mode=edit&paxid=$pmain[vouchno]";

            $getagent = mysqli_query($conn, "SELECT accode,name FROM `accmast` WHERE accode = '$pmain[agcode]' ") or die(mysqli_error($conn));
            $fetagent = mysqli_fetch_array($getagent);
            $agentname = $fetagent["name"];

            $mmeal = mysqli_query($conn, "SELECT code,name FROM $zroot.mealtype WHERE code ='$pmain[mealtype]'") or die(mysqli_error($conn));
            $tmeal = mysqli_fetch_array($mmeal);
            $mealname = $tmeal['name'];

            $mrtype = mysqli_query($conn, "SELECT code, name FROM roomtype WHERE code ='$pmain[roomtype]' ") or die(mysqli_error($conn));
            $trtype = mysqli_fetch_array($mrtype);
            $roomtype = $trtype['name'];
        } else if ($source == 'HTL') {
            $url = "hotelsl.php?mode=edit&vouchno=$pmain[vouchno]";
            $mmeal = mysqli_query($conn, "SELECT code,name FROM $zroot.mealtype WHERE code ='$pmain[mealtype]'") or die(mysqli_error($conn));
            $tmeal = mysqli_fetch_array($mmeal);
            $mealname = $tmeal['name'];

            $mrtype = mysqli_query($conn, "SELECT code, name FROM roomtype WHERE code ='$pmain[roomtype]' ") or die(mysqli_error($conn));
            $trtype = mysqli_fetch_array($mrtype);
            $roomtype = $trtype['name'];
        } else {
            $url = "bookform.php?mode=edit&vouchno=$pmain[vouchno]";
            $mealname = $pmain['mealname'];
            $u1 = $pmain[$ur1] * $pmain[$mur1];
            $u2 = $pmain[$ur2] * $pmain[$mur2];
            $u3 = $pmain[$ur3] * $pmain[$mur3];
            $u4 = $pmain[$ur4] * $pmain[$mur4];
            $u5 = $pmain[$ur5] * $pmain[$mur5];
            $u6 = $pmain[$ur6] * $pmain[$mur6];
            $roomtot = $u1 + $u2 + $u3 + $u4 + $u5 + $u6;

//            if ($umrahcity == 1) {
//                $mkrm = '';
//                $mkr = 0;
//                if ($pmain['mksingle'] != 0) {
//                    $mkrm = 'SGL';
//                    $mkr = $pmain[$ur1] * 1;
//                }
//                if ($pmain['mkdouble'] > 0) {
//                    if ($mkrm != '') {
//                        $mkrm = $mkrm . ' / DBL';
//                    } else {
//                        $mkrm = 'DBL';
//                    }
//                    $mkr = $pmain[$ur2] * 2;
//                }
//
//                if ($pmain['mktriple'] != 0) {
//                    if ($mkrm != '') {
//                        $mkrm = $mkrm . ' / TRP';
//                    } else {
//                        $mkrm = 'TRP';
//                    }
//                    $mkr = $pmain[$ur3] * 3;
//                }
//
//                if ($pmain['mkquad'] != 0) {
//                    if ($mkrm != '') {
//                        $mkrm = $mkrm . ' / QUAD';
//                    } else {
//                        $mkrm = 'QUAD';
//                    }
//                    $mkr = $pmain[$ur4] * 4;
//                }
//
//                if ($pmain['mkquint'] != 0) {
//                    if ($mkrm != '') {
//                        $mkrm = $mkrm . ' / QUINT';
//                    } else {
//                        $mkrm = 'QUINT';
//                    }
//                    $mkr = $pmain[$ur5] * 5;
//                }
//
//                if ($pmain['mksuite'] != 0) {
//                    if ($mkrm != '') {
//                        $mkrm = $mkrm . ' / SUITE';
//                    } else {
//                        $mkrm = 'SUITE';
//                    }
//                    $mkr = $pmain[$ur6] * 6;
//                }
//
//                $roomtype = $mkrm;
//                $roomqty = round($roomtot / $mkr, 2);
//            }
//
//            /////////////// madina
//
//            if ($umrahcity == 2) {
//                $mdrm = '';
//                $mdr = 0;
//                if ($pmain['mdsingle'] != 0) {
//                    $mdrm = 'SGL';
//                    $m1++;
//                    $mdr = $pmain[$ur1] * 1;
//                }
//
//                if ($pmain['mddouble'] != 0) {
//                    if ($mdrm != '') {
//                        $mdrm = $mdrm . ' / DBL';
//                    } else {
//                        $mdrm = 'DBL';
//                    }
//                    $m2++;
//                    $mdr = $pmain[$ur2] * 2;
//                }
//
//                if ($pmain['mdtriple'] != 0) {
//                    if ($mdrm != '') {
//                        $mdrm = $mdrm . ' / TRP';
//                    } else {
//                        $mdrm = 'TRP';
//                    }
//                    $m3++;
//                    $mdr = $pmain[$ur3] * 3;
//                }
//
//                if ($pmain['mdquad'] != 0) {
//                    if ($mdrm != '') {
//                        $mdrm = $mdrm . ' /  QUAD';
//                    } else {
//                        $mdrm = 'QUAD';
//                    }
//                    $m4++;
//                    $mdr = $pmain[$ur4] * 4;
//                }
//
//                if ($pmain['mdquint'] != 0) {
//                    if ($mdrm != '') {
//                        $mdrm = $mdrm . ' / QUINT';
//                    } else {
//                        $mdrm = 'QUINT';
//                    }
//                    $m5++;
//                    $mdr = $pmain[$ur5] * 5;
//                }
//
//                if ($pmain['mdsuite'] != 0) {
//                    if ($mdrm != '') {
//                        $mdrm = $mdrm . ' / SUITE';
//                    } else {
//                        $mdrm = 'SUITE';
//                    }
//                    $m6++;
//                    $mdr = $pmain[$ur6] * 6;
//                }
//
//                $roomtype = $mdrm;
//                $roomqty = round($roomtot / $mdr, 2);
//            }

            $roomConfig = [
                1 => [// MAKKAH
                    ['label' => 'SGL', 'qty' => 'mksingle', 'rate' => $ur1, 'cap' => 1],
                    ['label' => 'DBL', 'qty' => 'mkdouble', 'rate' => $ur2, 'cap' => 2],
                    ['label' => 'TRP', 'qty' => 'mktriple', 'rate' => $ur3, 'cap' => 3],
                    ['label' => 'QUAD', 'qty' => 'mkquad', 'rate' => $ur4, 'cap' => 4],
                    ['label' => 'QUINT', 'qty' => 'mkquint', 'rate' => $ur5, 'cap' => 5],
                    ['label' => 'SUITE', 'qty' => 'mksuite', 'rate' => $ur6, 'cap' => 6],
                ],
                2 => [// MADINA
                    ['label' => 'SGL', 'qty' => 'mdsingle', 'rate' => $ur1, 'cap' => 1],
                    ['label' => 'DBL', 'qty' => 'mddouble', 'rate' => $ur2, 'cap' => 2],
                    ['label' => 'TRP', 'qty' => 'mdtriple', 'rate' => $ur3, 'cap' => 3],
                    ['label' => 'QUAD', 'qty' => 'mdquad', 'rate' => $ur4, 'cap' => 4],
                    ['label' => 'QUINT', 'qty' => 'mdquint', 'rate' => $ur5, 'cap' => 5],
                    ['label' => 'SUITE', 'qty' => 'mdsuite', 'rate' => $ur6, 'cap' => 6],
                ]
            ];

            $roomLabels = [];
            $totalCapacity = 0;

            foreach ($roomConfig[$umrahcity] as $room) {
                if (!empty($pmain[$room['qty']]) && $pmain[$room['qty']] > 0) {
                    $roomLabels[] = $room['label'] . '-' . $pmain[$room['qty']];
                    $totalCapacity += $pmain[$room['rate']] * $room['cap'];
                }
            }

            $roomtype = implode(' / ', $roomLabels);

            if ($totalCapacity > 0) {
                $roomqty = round($roomtot / $totalCapacity, 2);
            } else {
                $roomqty = 0;
            }
        }

        $mhotcatg = mysqli_query($conn, "SELECT code,name FROM roomcatg WHERE code='$pmain[roomcatg]'") or die(mysqli_error($conn));
        $thotcatg = mysqli_fetch_array($mhotcatg);

        $ctr++;

        $contents .= "$ctr" . ",";
        $contents .= "$fullname" . ",";
        $contents .= "$agentname" . ",";
        $contents .= format_date($pmain['checkin']) . ",";
        $contents .= format_date($pmain['checkout']) . ",";
        $contents .= "$pmain[nonights]" . ",";
        $contents .= "$pmain[pax]" . ",";
        $contents .= "$roomtype" . ",";
        $contents .= "$roomqty" . ",";
        $contents .= "$thotcatg[name]" . ",";
        $contents .= "$mealname" . ",";
        $contents .= "$source" . "\n";
    }


    $contents = strip_tags($contents);

    if ($_POST["dtype"] == 1) {
        header("Content-Disposition: attachment; filename=$formname" . "_Summary" . date('d-m-Y') . ".csv");
        print $contents;
    } else {
        header("Content-Disposition: attachment; filename=$formname" . date('d-m-Y') . ".csv");
        print $contents;
    }
}
?>