<?php
ob_start();
$pageTitle = 'إدارة التقارير';
include 'init.php';
use classs\ManagerCars;
use classs\FileManager;
use classs\ReservationsManager;
/* if (isset($_GET['do']) && $_GET['do'] == "Add" || isset($_GET['do']) && $_GET['do'] == "Insert") {
    $go = 1;
} */

/* if (checkadmin($_SESSION['admin'], 7, 2) == 2 || checkadmin($_SESSION['admin'], 1, 0) == 0 || checkadmin($_SESSION['admin'], 7, 1) == 1) {

} else {
    header('Location: index.php');
    exit();
} */
$Manage = isset($_GET['Manage']) ? $_GET['Manage'] : false;
$do = isset($_GET['do']) ? $_GET['do'] : 'Manage';
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    if (isset($_POST['start']) && !empty($_POST['start']) && isset($_POST['end']) && !empty($_POST['end'])) {

        $start_day = "01";
        $end_day = "31";
        $start_mon = "01";
        $end_mon = "12";

        $start_date = $_POST['start'];
        $date_start_all = explode('-', $start_date);
        $thisyear_start = $date_start_all[0];
        $thismon_start = $date_start_all[1];
        $start_day_start = $date_start_all[2];

        $end_date = $_POST['end'];
        $date_end_all = explode('-', $end_date);
        $thisyear_end = $date_end_all[0];
        $thismon_end = $date_end_all[1];
        $thisday_end = $date_end_all[2];
        $start_date = $start_day_start . "-" . $thismon_start . "-" . $thisyear_start;
        $end_date = $thisday_end . "-" . $thismon_end . "-" . $thisyear_end;
        $start_date_sql = $thisyear_start . "-" . $thismon_start . "-" . $start_day_start;
        $end_date_sql = $thisyear_end . "-" . $thismon_end . "-" . $thisday_end;
    } else {

        $start_day = "01";
        $end_day = "31";
        $start_mon = "01";
        $end_mon = "12";
        $thisyear_start = $thisyear;
        $thismon_start = $thismon;
        $start_day_start = $thisday;
        $thisyear_end = $thisyear;
        $thismon_end = $thismon;
        $thisday_end = $days;
        $start_date = $start_day . "-" . $thismon . "-" . $thisyear;
        $end_date = $days . "-" . $thismon . "-" . $thisyear;
        $start_date_sql = $thisyear . "-" . $thismon . "-" . $start_day;
        $end_date_sql = $thisyear . "-" . $thismon . "-" . $days;
    }
} else {
    $start_day = "01";
    $end_day = "31";
    $start_mon = "01";
    $end_mon = "12";
    $thisyear_start = $thisyear;
    $thismon_start = $thismon;
    $start_day_start = $thisday;
    $thisyear_end = $thisyear;
    $thismon_end = $thismon;
    $thisday_end = $days;
    $start_date = $start_day . "-" . $thismon . "-" . $thisyear;
    $end_date = $days . "-" . $thismon . "-" . $thisyear;
    $start_date_sql = $thisyear . "-" . $thismon . "-" . $start_day;
    $end_date_sql = $thisyear . "-" . $thismon . "-" . $days;
}
if ($Manage == 'files') {
        if (isset($_GET['report']) && $_GET['report'] == 'year') {
            if (checkadmin($_SESSION['admin'], 13, 2) == 1 || checkadmin($_SESSION['admin'], 12, 2) == 1) {
                echo "<h1 class='input-group-text text-center'><a href='?Manage=files&report=year'>عرض ملفات </a>: سياحة </h1>";
                ?>
                <form action="?Manage=files&report=year" method="post">
                    <div class="row">
                        <div class="form-group form-group-lg col-sm-3">
                            <div class="col-sm-12">
                                <label for="start_date" class="control-label">من تاريخ:</label>
                                <input type="date" name="start_date" class="form-control" value="<?php echo $start_date_default; ?>"
                                    required>
                            </div>
                        </div>
                        <div class="form-group form-group-lg col-sm-3">
                            <div class="col-sm-12">
                                <label for="end_date" class="control-label">الى تاريخ:</label>
                                <input type="date" name="end_date" class="form-control" value="<?php echo $end_date_default; ?>"
                                    required>
                            </div>
                        </div>
                        <div class="form-group form-group-lg col-sm-1">
                            <div class="col-sm-12">
                                <label></label>
                                <button type="submit" class="btn btn-primary form-control" name="view_reservations">بحث</button>
                            </div>
                        </div>
                        <div class="form-group form-group-lg col-sm-1">
                        </div>
                    </div>
                </form>
                <br>
                <?php
                $reservationsManager = new ReservationsManager($con);
                    $nameyear = date('Y', strtotime($start_date));
                    echo "<h4 class='text-center'>إيرادات ومصروفات سياحة عن عام {$nameyear}</>";
                    echo "<table class='main-table text-center table table-bordered' style='direction: ltr;'>";
                    echo "<tr>
                    <td scope='col'>الشهر</td>
                    <td>إجمالى الإيراد</td>
                    <td>إجمالى المصروفات</td>
                    <td>صافى الشهر</td>
                    <td>نسبة مجمل الربح</td>
                    <td>ضريبة الدخل</td>
                    <td>المساهمة التكافلية</td>
                    <td>إجمالى <hr> ضرائب + مساهمة</td>
                    <td>المصروفات العمومية</td>
                    <td>الصافى النهائى</td>
                    <td>النسبة النهائية</td>
                    </tr>";
                    $months = [
                        1 => 'يناير',
                        2 => 'فبراير',
                        3 => 'مارس',
                        4 => 'أبريل',
                        5 => 'مايو',
                        6 => 'يونيو',
                        7 => 'يوليو',
                        8 => 'أغسطس',
                        9 => 'سبتمبر',
                        10 => 'أكتوبر',
                        11 => 'نوفمبر',
                        12 => 'ديسمبر',
                    ];
                    $allcashfrom_end = 0;
                            $allcashfromto_end = 0;
                            $mora7lfrom = 0;
                            $mora7lto = 0;
                            $all_mora7lfrom = 0;
                            $all_mora7lto = 0;
                            $allcashfrom = 0;
                            $allcashfromto = 0;
                            $amasaref_end = 0;
                    foreach ($months as $number => $name): 
                        $namemon = date('m-Y', strtotime($nameyear.'-'.$number));
                        $lastDayOfMonth = date("Y-m-t", strtotime("$nameyear-$number-01"));
                        $firstDayOfMonth = date("Y-m-01", strtotime("$nameyear-$number-01"));
                        ?>
                        <tr>
                            <td scope='col'>
                                <a href='?Manage=files&report=mon&mon=<?=$number?>&year=<?=$nameyear?>'>
                                <?= $name . ' ' . $nameyear ?>
                                </a>
                            </td>
                            <td>
                            <?php
                            $sql_reservations = $con->prepare("SELECT * from reservations WHERE departure_date BETWEEN '$firstDayOfMonth' AND '$lastDayOfMonth' ");
                            $sql_reservations->execute();
                            $details_reservations = $sql_reservations->fetchAll();
                            $countreservations = $sql_reservations->rowCount();
                            $allcashfrom = 0;
                            $allcashfromto = 0;
                            if ($countreservations > 0) {
                                foreach ($details_reservations as $details_reservation) {
                                    $id_cost_center = $details_reservation['id_cost_center'];
                                    
        
                                    $sqlaccounts = $con->prepare("SELECT * FROM cost_centers WHERE ID = $id_cost_center");
                                    $sqlaccounts->execute();
                                    $accounts = $sqlaccounts->fetchAll();
                                    
                                    foreach ($accounts as $account) {
                                        $cost_center_code = $account['cost_center_code'];
                                        
                                        $sqldetails_yomya = $con->prepare("SELECT * from details_yomya WHERE cost_center_code = '$cost_center_code' AND from_or_to = 1");
                                        $sqldetails_yomya->execute();
                                        $details_yomyas = $sqldetails_yomya->fetchAll();
                                        $countfrom = $sqldetails_yomya->rowCount();
                                        $cashfrom = 0;
                                        $cashto = 0;
                                        foreach ($details_yomyas as $details_yomya) {
                                            $from_or_to = $details_yomya['from_or_to'];
                                            $cash = $details_yomya['cash'];
                                            $id_currency_codes = $details_yomya['id_currency_codes'];
                                            $amountSold = $details_yomya['amountSold'];
                                            $currencyExchangeRate = $details_yomya['currencyExchangeRate'];
                                
                                                if($id_currency_codes != 1){
                                                    $cash = $amountSold;
                                                } else {
                                                    $cash = $currencyExchangeRate * $cash;
                                                }
                                                $allcashfrom += $cash;
                                        }
                                        $sqldetails_yomya = $con->prepare("SELECT * from details_yomya WHERE cost_center_code = '$cost_center_code' AND from_or_to = 2");
                                        $sqldetails_yomya->execute();
                                        $details_yomyas = $sqldetails_yomya->fetchAll();
                                        $countfrom = $sqldetails_yomya->rowCount();
                                        $cashfrom = 0;
                                        $cashto = 0;
                                        foreach ($details_yomyas as $details_yomya) {
                                            $from_or_to = $details_yomya['from_or_to'];
                                            $cash = $details_yomya['cash'];
                                            $id_currency_codes = $details_yomya['id_currency_codes'];
                                            $amountSold = $details_yomya['amountSold'];
                                            $currencyExchangeRate = $details_yomya['currencyExchangeRate'];
                                
                                                if($id_currency_codes != 1){
                                                    $cash = $amountSold;
                                                } else {
                                                    $cash = $currencyExchangeRate * $cash;
                                                }
                                                $allcashfromto += $cash;
                                        }
                                    }
                                }
                            }
                            // $allcashfrom = getTotalCashAllCostYear($con, 1, $firstDayOfMonth, $lastDayOfMonth, 1);
                            $allcashfrom_end += $allcashfrom;
                            // $allcashfromto = getTotalCashAllCostYear($con, 1, $firstDayOfMonth, $lastDayOfMonth, 2);
                            $allcashfromto_end += $allcashfromto;
                            echo '$ '.$allcashfromto;
                            //echo $allcashfrom;
                            ?>
                            </td>
                            <td>
                                <?php
                                echo '$ '.$allcashfrom;
                                ?>
                            </td>
                            <td>
                                <?php
                                echo '$ '.$safymon = $allcashfromto - $allcashfrom ;
                                ?>
                            </td>
                            <td>
                                <?php
                                if ($allcashfromto != 0 && $safymon != 0) {
                                    echo '%' . $nesb_reb7 = ($safymon / $allcashfromto) * 100 ;
                                } else {
                                    echo $nesb_reb7 = 0 ;
                                }
                                ?>
                            </td>
                            <td>
                                <?php
                                if ($allcashfromto != 0) {
                                    echo '$ '.$d_dahl = ($allcashfromto * 1) / 100 ;
                                } else {
                                    echo $d_dahl = 0 ;
                                }
                                ?>
                            </td>
                            <td>
                                <?php
                                if ($allcashfromto != 0) {
                                    echo '$ '.$d_mosahama = ($allcashfromto * 2.5) / 1000 ;
                                } else {
                                    echo $d_mosahama = 0 ;
                                }
                                ?>
                            </td>
                            <td>
                                <?php
                                echo '$ '.$all_d = $d_dahl - $d_mosahama ;
                                ?>
                            </td>
                            <td>
                                <?php
                                $amasaref_f = 0 ;
                                $amasaref_t = 0 ;

                                $amasaref_f = calculateCashFlow($con, 'lv2', '6.2', 1, $firstDayOfMonth, $lastDayOfMonth);
                                $amasaref_t = calculateCashFlow($con, 'lv2', '6.2', 2, $firstDayOfMonth, $lastDayOfMonth);

                                echo '$ '.$amasaref = $amasaref_f - $amasaref_t ;
                                $amasaref_end += $amasaref ;
                                ?>
                            </td>
                            <td>
                                <?php
                                echo '$ '.$safymon_end = $safymon - $all_d - $amasaref  ;
                                ?>
                            </td>
                            <td>
                                <?php
                                if ($allcashfromto != 0 && $safymon_end != 0) {
                                    echo '%' . $nesb_reb7_end = ($safymon_end / $allcashfromto) * 100 ;
                                } else {
                                    echo $nesb_reb7_end = 0 ;
                                }
                                ?>
                            </td>
                        </tr>
                        <?php 
                    endforeach;
                    ?>
                    <tfoot>
                    <tr>
                            <th>
                                إجمالى
                            </th>
                            <th>
                            <?php
                            echo '$ '.$allcashfromto_end;
                            //echo $allcashfrom;
                            ?>
                            </td>
                            <th>
                                <?php
                                echo '$ '.$allcashfrom_end;
                                ?>
                            </th>
                            <th>
                                <?php
                                echo '$ '.$safymon = $allcashfromto_end - $allcashfrom_end ;
                                ?>
                            </th>
                            <th>
                                <?php
                                if ($allcashfromto_end != 0 && $safymon != 0) {
                                    echo '%' . $nesb_reb7 = ($safymon / $allcashfromto_end) * 100 ;
                                } else {
                                    echo $nesb_reb7 = 0 ;
                                }
                                ?>
                            </th>
                            <th>
                                <?php
                                if ($allcashfromto_end != 0) {
                                    echo '$ '.$d_dahl = ($allcashfromto_end * 1) / 100 ;
                                } else {
                                    echo $d_dahl = 0 ;
                                }
                                ?>
                            </th>
                            <th>
                                <?php
                                if ($allcashfromto_end != 0) {
                                    echo '$ '.$d_mosahama = ($allcashfromto_end * 2.5) / 1000 ;
                                } else {
                                    echo $d_mosahama = 0 ;
                                }
                                ?>
                            </th>
                            <th>
                                <?php
                                echo '$ '.$all_d = $d_dahl - $d_mosahama ;
                                ?>
                            </th>
                            <th>
                                <?php
                                echo '$ '.$amasaref_end ;
                                ?>
                            </th>
                            <th>
                                <?php
                                echo '$ '.$safymon_end = $safymon - $all_d - $amasaref  ;
                                ?>
                            </th>
                            <th>
                                <?php
                                if ($allcashfromto_end != 0 && $safymon_end != 0) {
                                    echo '%' . $nesb_reb7_end = ($safymon_end / $allcashfromto_end) * 100 ;
                                } else {
                                    echo $nesb_reb7_end = 0 ;
                                }
                                ?>
                            </th>
                        </tr>
                    </tfoot>
                    <?php
                    echo "</table>";
                
            } else {
                header('Location: index.php');
                exit();
            }
        } elseif (isset($_GET['report']) && $_GET['report'] == 'mon') {
            if (checkadmin($_SESSION['admin'], 13, 2) == 1 || checkadmin($_SESSION['admin'], 12, 2) == 1) {
                echo "<h1 class='input-group-text text-center'><a href='?Manage=files&report=year'>عرض ملفات </a>: سياحة</h1>";
                ?>
                <?php
                // if (!empty($hotel_bookings)) {
                    echo "<h2 class='text-center'>ايرادات ومصروفات السياحة عن شهر {$_GET['mon']} / {$_GET['year']}</>";
                    echo "<table class='main-table text-center table table-bordered'  style='direction: ltr;'>";
                    echo "<tr>
                    <td>رقم الملف</td>
                    <td>الوكيل</td>
                    <td>الوصول</td>
                    <td>المغادرة</td>
                    <td>إجمالى الايراد</td>
                    <td>إجمالى المصروفات</td>
                    <td>صافى الملف</td>
                    <td>نسبة مجمل الربح</td>
                    </tr>";
                    $number = $_GET['mon'] ;
                    $nameyear = $_GET['year'];
                    $start_date_sql = date("Y-m-01", strtotime("$nameyear-$number-01"));        
                    $end_date_sql = date("Y-m-t", strtotime("$nameyear-$number-01"));
                    $allcashfrom_end = 0;
                    $allcashfromto_end = 0;
                    $mora7lfrom = 0;
                    $mora7lto = 0;
                    $all_mora7lfrom = 0;
                    $all_mora7lto = 0;
                    $allcashfrom = 0;
                    $allcashfromto = 0;
                    $sqlaccounts = $con->prepare("SELECT * from cost_centers WHERE cost_center_up = 0 ");
                    $sqlaccounts->execute();
                    $accounts = $sqlaccounts->fetchAll();
                    // foreach ($accounts as $account) {
                        /* $ID = $account['ID'];
                        $name_account = $account['cost_center_name'];
                        $code_account = $account['cost_center_code']; */
                        // $nature_acc = $account['nature_account'];
                        $sql_reservations = $con->prepare("SELECT * from reservations WHERE departure_date BETWEEN '$start_date_sql' AND '$end_date_sql' ");
                        $sql_reservations->execute();
                        $details_reservations = $sql_reservations->fetchAll();
                        $countreservations = $sql_reservations->rowCount();
                        if ($countreservations > 0) {
                            foreach ($details_reservations as $details_reservation) {
                                $id_cost_center = $details_reservation['id_cost_center'];
                                $sqlaccounts = $con->prepare("SELECT * from cost_centers WHERE ID = $id_cost_center ");
                                $sqlaccounts->execute();
                                $accounts = $sqlaccounts->fetchAll();
                                foreach ($accounts as $account) {
                                    $IDf = $account['ID'];
                                    $name_account = $account['cost_center_name'];
                                    $cost_center_code = $account['cost_center_code'];
                                    // $nature_acc = $account['nature_account'];
                                        ?>
                                    <tr  style="height: 40px;">
                                        <th style="text-align:;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                                            <a href="reports_account.php?Manage=cost_centers&do=cost&id_cost_f=<?php echo $IDf; ?>">
                                                <?php echo $name_account; ?>
                                            </a>
                                        </th>
                                        <td>
                                            <?php
                                            $agent_id =  getId('reservations','id_cost_center',$IDf ,'agent_id');
                                            echo $agent_name =  getId('agents','ID',$agent_id ,'agent_name');
                                            ?>
                                        </td>
                                        <td>
                                            <?php echo $arrival_date =  getId('reservations','id_cost_center',$IDf ,'arrival_date'); ?>
                                        </td>
                                        <td>
                                            <?php echo $departure_date =  getId('reservations','id_cost_center',$IDf ,'departure_date'); ?>
                                        </td>
                                        <td>
                                            <?php
                                            $allcashfrom = 0;
                                            $sqldetails_yomya = $con->prepare("SELECT * from details_yomya where cost_center_code = '$cost_center_code' AND from_or_to = 1");
                                            $sqldetails_yomya->execute();
                                            $details_yomyas = $sqldetails_yomya->fetchAll();
                                            $countfrom = $sqldetails_yomya->rowCount();
                                            $cashfrom = 0;
                                            $cashto = 0;
                                            foreach ($details_yomyas as $details_yomya) {
                                                $from_or_to = $details_yomya['from_or_to'];
                                                $cash = $details_yomya['cash'];
                                                $id_currency_codes = $details_yomya['id_currency_codes'];
                                                $amountSold = $details_yomya['amountSold'];
                                                $currencyExchangeRate = $details_yomya['currencyExchangeRate'];
                                    
                                                    if($id_currency_codes != 1){
                                                        $cash = $amountSold;
                                                    } else {
                                                        $cash = $currencyExchangeRate * $cash;
                                                    }
                                                    $allcashfrom += $cash;
                                            }
                                            $allcashfromto = 0;
                                            $sqldetails_yomya = $con->prepare("SELECT * from details_yomya where cost_center_code = '$cost_center_code' AND from_or_to = 2");
                                            $sqldetails_yomya->execute();
                                            $details_yomyas = $sqldetails_yomya->fetchAll();
                                            $countfrom = $sqldetails_yomya->rowCount();
                                            $cashfrom = 0;
                                            $cashto = 0;
                                            foreach ($details_yomyas as $details_yomya) {
                                                $from_or_to = $details_yomya['from_or_to'];
                                                $cash = $details_yomya['cash'];
                                                $id_currency_codes = $details_yomya['id_currency_codes'];
                                                $amountSold = $details_yomya['amountSold'];
                                                $currencyExchangeRate = $details_yomya['currencyExchangeRate'];
                                    
                                                    if($id_currency_codes != 1){
                                                        $cash = $amountSold;
                                                    } else {
                                                        $cash = $currencyExchangeRate * $cash;
                                                    }
                                                    $allcashfromto += $cash;
                                            }
                                            /* $sqldetails_yomya = $con->prepare("SELECT SUM(cash) AS 'cashfrom' from details_yomya where dateinsert BETWEEN '$start_date_sql' AND '$end_date_sql' AND cost_center_code = '$cost_center_code' AND from_or_to = 1");
                                            $sqldetails_yomya->execute();
                                            $details_yomyas = $sqldetails_yomya->fetchAll();
                                            //$countfrom = $sqldetails_yomya->rowCount();
                                            foreach ($details_yomyas as $details_yomya) {
                                                $allcashfrom = $details_yomya['cashfrom'];
                                                // $allcashfrom_end += $allcashfrom;
                                            }
                                            $sqldetails_yomya = $con->prepare("SELECT SUM(cash) AS 'cashto' from details_yomya where dateinsert BETWEEN '$start_date_sql' AND '$end_date_sql' AND cost_center_code = '$cost_center_code' AND from_or_to = 2");
                                            $sqldetails_yomya->execute();
                                            $details_yomyas = $sqldetails_yomya->fetchAll();
                                            //$countfrom = $sqldetails_yomya->rowCount();
                                            foreach ($details_yomyas as $details_yomya) {
                                                $allcashfromto = $details_yomya['cashto'];
                                                // $allcashfromto_end += $allcashfromto;
                                            } */
                                            echo $allcashfromto;
                                            
                                            //echo $allcashfrom;
                                            ?>
                                        </td>
                                        <td>
                                            <?php
                                            echo $allcashfrom;
                                            
                                            ?>
                                        </td>
                                        <td>
                                            <?php
                                            echo $allsafy = $allcashfromto - $allcashfrom;
                                            ?>
                                        </td>
                                        <td>
                                            <?php
                                            if ($allsafy != 0 && $allcashfromto != 0) {
                                                echo $nespa = ($allsafy / $allcashfromto) * 100;
                                            } else {
                                                echo $nespa = 0;
                                            }
                                            ?>
                                        </td>
                                    </tr>
                                    <?php
                                }
                            }
                        }
                        
                                    
                    echo "</table>";
                
            } else {
                header('Location: index.php');
                exit();
            }
        }
        
    /* } else {
        $theMsg = '<div class="alert alert-danger">برجاء إتباع رابط صحيح</div>';
        redirectHome($theMsg, 'back', 4);
    } */
} elseif ($Manage == 'payment') {
    if (!isset($_GET['id_management'])) {
        $agentsQuery = "SELECT * FROM tourism_management";
        $agentsResult = $con->query($agentsQuery);
        echo '<h1 class="input-group-text text-center">عرض payment</h1>';
        ?>
        <div class="row">
            <div class='col-md-6 list-group'>
            <?php
            while ($agent = $agentsResult->fetch(PDO::FETCH_ASSOC)) {
                $id_user = $_SESSION['admin'];
                if (checkadmin($id_user, $agent['department_id'], 2) == 1) {
                    echo "
                    <a href='?Manage=payment&id_management={$agent['ID']}' class='list-group-item list-group-item-action' aria-current='true'>
                    {$agent['name_management']}
                    <!-- <span class='badge bg-primary rounded-pill'>14</span> --></a>";
                }
            }
            ?>
            </div>
        </div>
            <!-- <a class="list-group-item list-group-item-action disabled" aria-disabled="true">A disabled link item</a> -->
        <?php
    } elseif(intval($_GET['id_management']) > 0) {
        $id_management = intval($_GET['id_management']);
        $countmanagement = 0;
        $tourism_management = $con->prepare("SELECT * FROM tourism_management where ID = $id_management");
        $tourism_management->execute();
        $managements = $tourism_management->fetch(PDO::FETCH_ASSOC);
            $department_id = $managements['department_id'];
            $name_management = $managements['name_management'];
            $id_user = $_SESSION['admin'];
            if (checkadmin($id_user, $department_id, 2) == 1 ) {
                $countmanagement += 1;
            }
        if ($countmanagement > 0 ) {
            echo "<h1 class='input-group-text text-center'><a href='?Manage=payment'>عرض payments </a>: {$name_management}</h1>";
            ?>
            <form action="?Manage=payment&id_management=<?=$id_management?>" method="post">
                <div class="row">
                    <div class="form-group form-group-lg col-sm-3">
                        <div class="col-sm-12">
                            <label for="start_date" class="control-label">حدد اليوم:</label>
                            <input type="date" name="start_date" class="form-control" value="" required>
                        </div>
                    </div>
                    <div class="form-group form-group-lg col-sm-1">
                        <div class="col-sm-12">
                            <label></label>
                            <button type="submit" class="btn btn-primary form-control" name="view_reservations">بحث</button>
                        </div>
                    </div>
                    <div class="form-group form-group-lg col-sm-1">
                    </div>
                </div>
            </form>
            <br>
            <?php
            $reservationsManager = new ReservationsManager($con);

            if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['view_reservations'])) {
                $day = $_POST['start_date'];
                // You can perform validation on $start_date and $end_date if needed
                $hotel_bookings = $reservationsManager->getHotelPayment($id_management, $day, 1);
                $msgres = "No reservations found for the selected date range.";
            } else {
                $day = date('Y-m-d');
                // $day = '2024-02-25';
                $hotel_bookings = $reservationsManager->getHotelPayment($id_management, $day, 1);
                $msgres = "No reservations found.";
            }
            
            // $reservations = $reservationsManager->getReservationsForCurrentMonthWithNames($id_management);s
            $msgres = "No reservations found.";
            if (!empty($hotel_bookings)) {
                echo "<h2 class='text-center'>Payment for {$day}</>";
                echo '<form id="payment_percentage" action="?do=insert_payment_percentage" method="post">';
                echo "<table class='main-table text-center table table-bordered'>";
                echo '<tr>
                <td>رقم الملف</td>
                <td>الوكيل</td>
                <td>guest name</td>
                <td>الفندق</td>
                <td>Arr</td>
                <td>Dep</td>
                <td>نوع الغرفة</td>
                <td>عدد الغرف</td>
                <td>سعر الفرد</td>
                <td>عدد الليالى</td>
                <td> Payment <button form="payment_percentage" type="submit" class="btn btn-success" name="save_payment_percentage">حفظ</button></td>
                <td>ملاحظات</td>
                </tr>';

                foreach ($hotel_bookings as $reservation) {
                    
                    $data_reservation = $reservationsManager->getReservationById($reservation['id_reservation']);
                    $calculatedates = $reservationsManager->calculateNightAndDayCount($reservation['check_in_date'], $reservation['check_out_date']);
                    if ($data_reservation['payment_status'] == 0) {
                        $status_payment = "لم يتم الدفع";
                    } else {
                        $status_payment = "تم الدفع";
                    }
                    $price = $reservation['price'];
                    $currency_code = getId('currency', 'ID', $reservation['id_currency_codes'] , 'currency_code');
                    if (!empty($reservation['amountSold']) && $reservation['amountSold'] > 0) {
                        $price = $reservation['amountSold'];
                    }
                    if ($reservation['code_2'] == "X" || $reservation['code_1'] == "SGL") {
                        $price = $reservation['rooms_number'] * $price;
                        $price *= $calculatedates['number_of_nights'];
                        $all_Individuals = $reservation['rooms_number'];
                    } elseif ($reservation['code_2'] == "XX" || $reservation['code_1'] == "DBL") {
                        $price = $reservation['rooms_number'] * $price;
                        $price *= 2;
                        $price *= $calculatedates['number_of_nights'];
                        $all_Individuals = $reservation['rooms_number'] * 2;
                    } elseif ($reservation['code_2'] == "XXX" || $reservation['code_1'] == "TPL") {
                        $price = $reservation['rooms_number'] * $price;
                        $price *= 3;
                        $price *= $calculatedates['number_of_nights'];
                        $all_Individuals = $reservation['rooms_number'] * 3;
                    } else {
                        $price = $reservation['rooms_number'] * $price;
                        $price *= $calculatedates['number_of_nights'];
                        $all_Individuals = $reservation['rooms_number'] ;
                    }
                    $countf = count($hotel_bookings);
                    $tax3 = ($price * 3) / 100;
                    $priceafter = $price - $tax3;
                    $file_number = getId('reservations','id',$reservation['id_reservation'],'file_number');
                    $agent_id = getId('reservations','id',$reservation['id_reservation'],'agent_id');
                    $agent_name= getId('agents','ID',$agent_id,'agent_name');
                    $file_number = getId('reservations','id',$reservation['id_reservation'],'file_number');
                    echo "<tr>
                        <td>{$file_number}</td>
                        <td>{$agent_name}</td>
                        <td>{$data_reservation['client_name']}</td>
                        <td>{$reservation['hotel_name']}</td>
                        <td>{$reservation['check_in_date']}</td>
                        <td>{$reservation['check_out_date']}</td>
                        <td>{$reservation['code_1']}</td>
                        <td>{$reservation['rooms_number']}</td>
                        <td>{$reservation['amountSold']} {$currency_code}</td>
                        <td>{$calculatedates['number_of_nights']}</td>
                        <td>
                        {$currency_code}<input type='text' name='payment_cash' value='{$reservation['cash']}' class='form-control form-control-sm' placeholder='مبلغ ال payment' >
                        </td>
                        <td>{$reservation['note']}</td>
                        ";
                        /* <td>{$reservation['total_price']}</td>"; */
                    /* if (checkadmin($_SESSION['admin'], 4, 2) == 1 ) {
                        echo "<td>
                        <a href='?Manage=file&id=" . $reservation['id'] . "' class='btn btn-primary'><i class=\"fa-solid fa-gear\"></i></i></i> </a>
                    </td>";
                    } */
                    echo "</tr>";
                }
                echo "</table>";
                echo "</form>";
            } else {
                echo $msgres;
            }
        } else {
            header('Location: index.php');
            exit();
        }
    } else {
        $theMsg = '<div class="alert alert-danger">برجاء إتباع رابط صحيح</div>';
        redirectHome($theMsg, 'back', 4);
    }
} elseif ($Manage == 'arrival') {
    if (!isset($_GET['id_management'])) {
        $agentsQuery = "SELECT * FROM tourism_management";
        $agentsResult = $con->query($agentsQuery);
        echo '<h1 class="input-group-text text-center">عرض payment</h1>';
        ?>
        <div class="row">
            <div class='col-md-6 list-group'>
            <?php
            while ($agent = $agentsResult->fetch(PDO::FETCH_ASSOC)) {
                $id_user = $_SESSION['admin'];
                if (checkadmin($id_user, $agent['department_id'], 2) == 1) {
                    echo "
                    <a href='?Manage=arrival&id_management={$agent['ID']}' class='list-group-item list-group-item-action' aria-current='true'>
                    {$agent['name_management']}
                    <!-- <span class='badge bg-primary rounded-pill'>14</span> --></a>";
                }
            }
            ?>
            </div>
        </div>
            <!-- <a class="list-group-item list-group-item-action disabled" aria-disabled="true">A disabled link item</a> -->
        <?php
    } elseif(intval($_GET['id_management']) > 0) {
        $id_management = intval($_GET['id_management']);
        $countmanagement = 0;
        $tourism_management = $con->prepare("SELECT * FROM tourism_management where ID = $id_management");
        $tourism_management->execute();
        $managements = $tourism_management->fetch(PDO::FETCH_ASSOC);
            $department_id = $managements['department_id'];
            $name_management = $managements['name_management'];
            $id_user = $_SESSION['admin'];
            if (checkadmin($id_user, $department_id, 2) == 1 ) {
                $countmanagement += 1;
            }
        if ($countmanagement > 0 ) {
            echo "<h1 class='input-group-text text-center'><a href='?Manage=arrival'>عرض arrivals </a>: {$name_management}</h1>";
            ?>
            <form action="?Manage=arrival&id_management=<?=$id_management?>" method="post">
                <div class="row">
                    <div class="form-group form-group-lg col-sm-3">
                        <div class="col-sm-12">
                            <label for="start_date" class="control-label">حدد اليوم:</label>
                            <input type="date" name="start_date" class="form-control" value="" required>
                        </div>
                    </div>
                    <div class="form-group form-group-lg col-sm-1">
                        <div class="col-sm-12">
                            <label></label>
                            <button type="submit" class="btn btn-primary form-control" name="view_reservations">بحث</button>
                        </div>
                    </div>
                    <div class="form-group form-group-lg col-sm-1">
                    </div>
                </div>
            </form>
            <br>
            <?php
            $reservationsManager = new ReservationsManager($con);

            if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['view_reservations'])) {
                $day = $_POST['start_date'];
                // You can perform validation on $start_date and $end_date if needed
                $hotel_bookings = $reservationsManager->getHotelPayment($id_management, $day, 2);
                $msgres = "No reservations found for the selected date range.";
            } else {
                $day = date('Y-m-d');
                // $day = '2024-02-25';
                $hotel_bookings = $reservationsManager->getHotelPayment($id_management, $day, 2);
                $msgres = "No reservations found.";
            }
            
            // $reservations = $reservationsManager->getReservationsForCurrentMonthWithNames($id_management);s
            $msgres = "No reservations found.";
            if (!empty($hotel_bookings)) {
                echo "<h2 class='text-center'>Arrival for {$day}</>";
                echo "<table class='main-table text-center table table-bordered'>";
                echo "<tr>
                <td>رقم الملف</td>
                <td>الوكيل</td>
                <td>guest name</td>
                <td>الفندق</td>
                <td>Arr</td>
                <td>Dep</td>
                <td>نوع الغرفة</td>
                <td>نوع الاقامة</td>
                <td>عدد الليالى</td>
                <td>ملاحظات</td>
                <td>تحصيل</td>
                </tr>";

                foreach ($hotel_bookings as $reservation) {
                    
                    $data_reservation = $reservationsManager->getReservationById($reservation['id_reservation']);
                    $calculatedates = $reservationsManager->calculateNightAndDayCount($reservation['check_in_date'], $reservation['check_out_date']);
                    if ($data_reservation['payment_status'] == 0) {
                        $status_payment = "لم يتم الدفع";
                    } else {
                        $status_payment = "تم الدفع";
                    }
                    $price = $reservation['price'];
                    $currency_code = getId('currency', 'ID', $reservation['id_currency_codes'] , 'currency_code');
                    if (!empty($reservation['amountSold']) && $reservation['amountSold'] > 0) {
                        $price = $reservation['amountSold'];
                    }
                    if ($reservation['code_2'] == "X" || $reservation['code_1'] == "SGL") {
                        $price = $reservation['rooms_number'] * $price;
                        $price *= $calculatedates['number_of_nights'];
                        $all_Individuals = $reservation['rooms_number'];
                    } elseif ($reservation['code_2'] == "XX" || $reservation['code_1'] == "DBL") {
                        $price = $reservation['rooms_number'] * $price;
                        $price *= 2;
                        $price *= $calculatedates['number_of_nights'];
                        $all_Individuals = $reservation['rooms_number'] * 2;
                    } elseif ($reservation['code_2'] == "XXX" || $reservation['code_1'] == "TPL") {
                        $price = $reservation['rooms_number'] * $price;
                        $price *= 3;
                        $price *= $calculatedates['number_of_nights'];
                        $all_Individuals = $reservation['rooms_number'] * 3;
                    } else {
                        $price = $reservation['rooms_number'] * $price;
                        $price *= $calculatedates['number_of_nights'];
                        $all_Individuals = $reservation['rooms_number'] ;
                    }
                    $countf = count($hotel_bookings);
                    $tax3 = ($price * 3) / 100;
                    $priceafter = $price - $tax3;
                    $file_number = getId('reservations','id',$reservation['id_reservation'],'file_number');
                    $agent_id = getId('reservations','id',$reservation['id_reservation'],'agent_id');
                    $agent_name= getId('agents','ID',$agent_id,'agent_name');
                    $file_number = getId('reservations','id',$reservation['id_reservation'],'file_number');
                    echo "<tr>
                        <td>{$file_number}</td>
                        <td>{$agent_name}</td>
                        <td>{$data_reservation['client_name']}</td>
                        <td>{$reservation['hotel_name']}</td>
                        <td>{$reservation['check_in_date']}</td>
                        <td>{$reservation['check_out_date']}</td>
                        <td>{$reservation['code_1']}</td>
                        <td>{$reservation['short_name']}</td>
                        <td>{$calculatedates['number_of_nights']}</td>
                        <td>{$reservation['note']}</td>
                        <td>{$reservation['cash']} {$currency_code}</td>
                        ";
                        /* <td>{$reservation['total_price']}</td>"; */
                    /* if (checkadmin($_SESSION['admin'], 4, 2) == 1 ) {
                        echo "<td>
                        <a href='?Manage=file&id=" . $reservation['id'] . "' class='btn btn-primary'><i class=\"fa-solid fa-gear\"></i></i></i> </a>
                    </td>";
                    } */
                    echo "</tr>";
                }
                echo "</table>";
            } else {
                echo $msgres;
            }
        } else {
            header('Location: index.php');
            exit();
        }
    } else {
        $theMsg = '<div class="alert alert-danger">برجاء إتباع رابط صحيح</div>';
        redirectHome($theMsg, 'back', 4);
    }
} elseif ($Manage == 'file') {
            $id_file = isset($_GET['id']) && is_numeric($_GET['id']) ? intval($_GET['id']) : 0;
            $countmanagement = 0;
            $id_management = getName("reservations","id",$id_file,"id_tourism_management");
            if ($id_management > 0) {
                $tourism_management = $con->prepare("SELECT * FROM tourism_management where ID = $id_management");
                $tourism_management->execute();
                $managements = $tourism_management->fetch(PDO::FETCH_ASSOC);
                $count = $tourism_management->rowCount();
                if ($count > 0) {
                    $department_id = $managements['department_id'];
                    $name_management = $managements['name_management'];
                    $id_user = $_SESSION['admin'];
                    if (checkadmin($id_user, $department_id, 2) == 1 ) {
                        $countmanagement += 1;
                    }
                } else {
                    $countmanagement = 0;
                }
            } else {
                $countmanagement = 0;
            }
    if ($countmanagement > 0) {
        if (isset($_GET['id']) && is_numeric($_GET['id'])) {
            $reservation_id = $_GET['id'];
            $reservationsManager = new ReservationsManager($con);
            $reservationDatas = $reservationsManager->getReservationById($reservation_id);
            $file_number = explode("/", $reservationDatas['file_number']);
            echo "<h1 class='input-group-text text-center'><a href='?Manage=files'>عرض ملفات </a>: <a href='?Manage=files&id_management={$id_management}'>{$name_management}</a> / ملف رقم : {$file_number[0]} / {$file_number[1]} </h1>";
            ?>
            <br>
            <?php
            if ($reservationDatas != null) {
                echo "<table class='main-table text-center table table-bordered'>";
                echo "<tr>
                <td>رقم الملف</td>
                <td>الوكيل</td>
                <td>guest name</td>
                <td>الفندق</td>
                <td>الجنسية</td>
                <td>Arr</td>
                <td>Dep</td>
                <td>نوع الغرفة</td>
                <td>عدد الغرف</td>
                <td>سعر الفرد</td>
                <td>عدد الليالى</td>
                <td>الإجمالى قبل الضريبة</td>
                <td>tax 3%</td>
                <td>الإجمالى بعد الضريبة</td>
                <td></td>
                <td>ملاحظات</td>
                <td>حالة الحجز</td>
            </tr>";
                // foreach ($reservations as $reservation) {
                if ($reservationDatas['payment_status'] == 0) {
                    $status_payment = "لم يتم الدفع";
                } else {
                    $status_payment = "تم الدفع";
                }
                if ($reservationDatas['check_status'] == "off") {
                    $check_status = "غير مقعل";
                } elseif ($reservationDatas['check_status'] == "on") {
                    $check_status = "مفعل";
                }
                echo "<tr>
                    <td>{$reservationDatas['file_number']}</td>
                    <td>{$reservationDatas['agent_name']}</td>
                    <td>{$reservationDatas['client_name']}</td>
                    <td>{$reservationDatas['nationality_name']}</td>
                    <td>{$reservationDatas['arrival_date']}</td>
                    <td>{$reservationDatas['departure_date']}</td>
                    <td>{$status_payment}</td>
                    <td>{$reservationDatas['remarks']}</td>
                    <td>{$reservationDatas['date_insert']}</td>
                    <td>{$reservationDatas['total_price']}</td>
                    <td>{$check_status}</td>";
                echo "</tr>";
                // }
                echo "</table>";
                $NumberIndividuals = $reservationsManager->getNumberIndividualsReservationById($reservationDatas['id']);
                $hotel_bookings = $reservationsManager->getHotelBookingsReservationById($reservationDatas['id']);
                $flight_bookings = $reservationsManager->getFlightBookingsReservationById($reservationDatas['id']);
                $train_bookings = $reservationsManager->getTrainBookingsReservationById($reservationDatas['id']);

                $all_Individuals = 0;
                if (!empty($NumberIndividuals)) {
                    foreach ($NumberIndividuals as $value) {
                        $all_Individuals += $value['number'];
                    }
                } else {
                    $all_Individuals = "<strong>لا يوجد</strong>";
                }

                ?>
                <div class="row">
                    <div class="col-md-6 mb-3">
                        <h1 class="input-group-text text-center">عدد الأفراد :
                            <?php echo $all_Individuals; ?>
                        </h1>
                        <table class='main-table text-center table table-bordered'>
                            <tr>
                                <td>الأشخاص</td>
                                <td>العدد</td>
                                <td>ملاظات</td>
                            </tr>
                            <?php
                            if (!empty($NumberIndividuals)) {
                                foreach ($NumberIndividuals as $value) {
                                    echo "<tr>
                                    <td>{$value['name']}</td>
                                    <td>{$value['number']}</td>
                                    <td>{$value['age_person']}</td>
                                    </tr>";
                                }
                            } else {
                                echo "<tr>
                                    <td>لا يوجد</td>
                                    <td>لا يوجد</td>
                                    <td>لا يوجد</td>
                                    </tr>";
                            }
                            ?>
                        </table>
                    </div>
                    <div class="col-md-6 mb-3">
                        <h1 class="input-group-text text-center">إنقالات داخلية</h1>
                        <table class='main-table text-center table table-bordered'>
                            <tr>
                                <td>نوع السيارة</td>
                                <td>الشركة</td>
                                <td>من</td>
                                <td>الى</td>
                                <td>التكلفة</td>
                            </tr>
                            <?php
                            if (!empty($train_bookings)) {
                                foreach ($train_bookings as $value) {
                                    echo "<tr>
                                    <td>{$value['train_name']}</td>
                                    <td>{$value['departure_station']}</td>
                                    <td>{$value['arrival_station']}</td>
                                    <td>{$value['departure_time']}</td>
                                    <td>{$value['arrival_time']}</td>
                                    </tr>";
                                }
                            } else {
                                echo "<tr>
                                    <td>لا يوجد</td>
                                    <td>لا يوجد</td>
                                    <td>لا يوجد</td>
                                    <td>لا يوجد</td>
                                    <td>لا يوجد</td>
                                    </tr>";
                            }
                            ?>
                        </table>
                    </div>
                    <div class="col-md-12 mb-3">
                        <h1 class="input-group-text text-center">حجز الفندق </h1>
                        <table class='main-table text-center table table-bordered'>
                            <tr>
                                <td>اسم الفندق</td>
                                <td>الغرف</td>
                                <td>عدد الغرف</td>
                                <td>المميزات</td>
                                <td>من تاريخ</td>
                                <td>الى تاريخ</td>
                                <td>عدد الليالى</td>
                                <td>التكلفة</td>
                            </tr>
                            <?php
                            if (!empty($hotel_bookings)) {
                                foreach ($hotel_bookings as $value) {
                                    $calculatedates = $reservationsManager->calculateNightAndDayCount($value['check_in_date'], $value['check_out_date']);

                                    if ($value['code_2'] == "X" || $value['code_1'] == "SGL") {
                                        $price = $value['rooms_number'] * $value['price'];
                                        $price *= $calculatedates['number_of_nights'];
                                    } elseif ($value['code_2'] == "XX" || $value['code_1'] == "DBL") {
                                        $price = $value['rooms_number'] * $value['price'];
                                        $price *= 2;
                                        $price *= $calculatedates['number_of_nights'];
                                    } elseif ($value['code_2'] == "XXX" || $value['code_1'] == "TPL") {
                                        $price = $value['rooms_number'] * $value['price'];
                                        $price *= 3;
                                        $price *= $calculatedates['number_of_nights'];
                                    } else {
                                        $price = $value['rooms_number'] * $value['price'];
                                        $price *= $calculatedates['number_of_nights'];
                                    }
                                    echo "<tr>
                                    <td>{$value['hotel_name']}</td>
                                    <td>{$value['code_1']}</td>
                                    <td style=\"direction: ltr;\">{$value['rooms_number']} {$value['code_2']}</td>
                                    <td>{$value['room_disc']}</td>
                                    <td>{$value['check_in_date']}</td>
                                    <td>{$value['check_out_date']}</td>
                                    <td>{$calculatedates['number_of_nights']} ليلة</td>
                                    <td>\$$price</td>
                                    </tr>";
                                }
                            } else {
                                echo "<tr>
                                    <td>لا يوجد</td>
                                    <td>لا يوجد</td>
                                    <td>لا يوجد</td>
                                    <td>لا يوجد</td>
                                    <td>لا يوجد</td>
                                    </tr>";
                            }
                            ?>
                        </table>
                    </div>
                    <div class="col-md-12 mb-3">
                        <h1 class="input-group-text text-center">حجز الطيران </h1>
                        <table class='main-table text-center table table-bordered'>
                            <tr>
                                <td>منفذ الحجز</td>
                                <td>شركة الطيران</td>
                                <td>رقم الرحلة</td>
                                <td>عدد الأفراد</td>
                                <td>مطار المغادرة</td>
                                <td>مطار الوصول</td>
                                <td>تاريخ المغادرة</td>
                                <td>تاريخ الوصول</td>
                                <td>التكلفة</td>
                            </tr>
                            <?php
                            if (!empty($flight_bookings)) {
                                foreach ($flight_bookings as $value) {
                                    echo "<tr>
                                    <td>{$value['name_airline']}</td>
                                    <td>{$value['flight_number']}</td>
                                    <td>{$value['departure_airport']}</td>
                                    <td>{$value['arrival_airport']}</td>
                                    <td>{$value['departure_time']}</td>
                                    <td>{$value['arrival_time']}</td>
                                    </tr>";
                                }
                            } else {
                                echo "<tr>
                                    <td>لا يوجد</td>
                                    <td>لا يوجد</td>
                                    <td>لا يوجد</td>
                                    <td>لا يوجد</td>
                                    <td>لا يوجد</td>
                                    <td>لا يوجد</td>
                                    </tr>";
                            }
                            ?>
                        </table>
                    </div>
                    <div class="col-md-6 mb-3">
                        <h1 class="input-group-text text-center">حجز قطار </h1>
                        <table class='main-table text-center table table-bordered'>
                            <tr>
                                <td>رقم القطار</td>
                                <td>محطة المغادرة</td>
                                <td>محطة الوصول</td>
                                <td>موعد المغادرة</td>
                                <td>موعد الوصول</td>
                            </tr>
                            <?php
                            if (!empty($train_bookings)) {
                                foreach ($train_bookings as $value) {
                                    echo "<tr>
                                    <td>{$value['train_name']}</td>
                                    <td>{$value['departure_station']}</td>
                                    <td>{$value['arrival_station']}</td>
                                    <td>{$value['departure_time']}</td>
                                    <td>{$value['arrival_time']}</td>
                                    </tr>";
                                }
                            } else {
                                echo "<tr>
                                    <td>لا يوجد</td>
                                    <td>لا يوجد</td>
                                    <td>لا يوجد</td>
                                    <td>لا يوجد</td>
                                    <td>لا يوجد</td>
                                    </tr>";
                            }
                            ?>
                        </table>
                    </div>
                    <div class="col-md-6 mb-3">
                        <h1 class="input-group-text text-center">حجز تذاكر مزارات سياحية</h1>
                        <table class='main-table text-center table table-bordered'>
                            <tr>
                                <td>الإسم</td>
                                <td>عدد الأفراد</td>
                                <td>من</td>
                                <td>الى</td>
                                <td>التكلفة</td>
                            </tr>
                            <?php
                            if (!empty($train_bookings)) {
                                foreach ($train_bookings as $value) {
                                    echo "<tr>
                                    <td>{$value['train_name']}</td>
                                    <td>{$value['departure_station']}</td>
                                    <td>{$value['arrival_station']}</td>
                                    <td>{$value['departure_time']}</td>
                                    <td>{$value['arrival_time']}</td>
                                    </tr>";
                                }
                            } else {
                                echo "<tr>
                                    <td>لا يوجد</td>
                                    <td>لا يوجد</td>
                                    <td>لا يوجد</td>
                                    <td>لا يوجد</td>
                                    <td>لا يوجد</td>
                                    </tr>";
                            }
                            ?>
                        </table>
                    </div>
                    <div class="col-md-6 mb-3">
                        <h1 class="input-group-text text-center">حجز مطاعم</h1>
                        <table class='main-table text-center table table-bordered'>
                            <tr>
                                <td>إسم المطعم</td>
                                <td>سعر المطعم</td>
                            </tr>
                            <?php
                            if (!empty($train_bookings)) {
                                foreach ($train_bookings as $value) {
                                    echo "<tr>
                                    <td>{$value['train_name']}</td>
                                    <td>{$value['departure_station']}</td>
                                    </tr>";
                                }
                            } else {
                                echo "<tr>
                                    <td>لا يوجد</td>
                                    <td>لا يوجد</td>
                                    </tr>";
                            }
                            ?>
                        </table>
                    </div>
                    <div class="col-md-6 mb-3">
                        <h1 class="input-group-text text-center">المندوب المرافق</h1>
                        <table class='main-table text-center table table-bordered'>
                            <tr>
                                <td>الإسم</td>
                                <td>رقم الهاتف</td>
                            </tr>
                            <?php
                            if (!empty($train_bookings)) {
                                foreach ($train_bookings as $value) {
                                    echo "<tr>
                                    <td>{$value['train_name']}</td>
                                    <td>{$value['departure_station']}</td>
                                    </tr>";
                                }
                            } else {
                                echo "<tr>
                                    <td>لا يوجد</td>
                                    <td>لا يوجد</td>
                                    </tr>";
                            }
                            ?>
                        </table>
                    </div>
                    <div class="col-md-6 mb-3">
                        <h1 class="input-group-text text-center">المرشد السياحى</h1>
                        <table class='main-table text-center table table-bordered'>
                            <tr>
                                <td>الإسم</td>
                                <td>رقم الهاتف</td>
                            </tr>
                            <?php
                            if (!empty($train_bookings)) {
                                foreach ($train_bookings as $value) {
                                    echo "<tr>
                                    <td>{$value['train_name']}</td>
                                    <td>{$value['departure_station']}</td>
                                    </tr>";
                                }
                            } else {
                                echo "<tr>
                                    <td>لا يوجد</td>
                                    <td>لا يوجد</td>
                                    </tr>";
                            }
                            ?>
                        </table>
                    </div>
                </div>
                <?php
            } else {
                // رسالة خطأ أو إعادة توجيه إلى صفحة خطأ
                echo "خطأ: لا يوجد حجز بهذا الرقم.";
                exit;
            }
        } else {
            // رسالة خطأ أو إعادة توجيه إلى صفحة خطأ
            echo "خطأ: يجب تحديد رقم الحجز.";
            exit;
        }
    } else {
        header('Location: index.php');
        exit();
    }
} elseif ($Manage == 'hotels') {
    if (checkadmin($_SESSION['admin'], 2, 2) == 1) {
        ?>
        <h1 class="input-group-text text-center">تقرير الفنادق</h1>
        <br>
        <?php
        $reservationsManager = new ReservationsManager($con);
        $reservations = $reservationsManager->getAllHotels();
        $msgres = "لا توجد بيانات.";
        if (!empty($reservations)) {
            echo "<table class='main-table text-center table table-bordered'>";
            echo "<tr>
                <td>اسم الفندق</td>
                <td>العنوان</td>
                <td>عدد النجوم</td>
                <td>الوصف</td>
                <td>البريد الإلكترونى</td>
                <td>رقم الهاتف</td>
                <td>الموقع الإلكترونى</td>
                <td>Control</td>
            </tr>";

            foreach ($reservations as $reservation) {
                $maxWords = 30; // تحديد عدد الكلمات المطلوب عرضها
                // استخراج الكلمات من الوصف
                $descriptionWords = explode(' ', $reservation['description']);
                // احتفاظ بعدد معين من الكلمات
                $limitedDescription = implode(' ', array_slice($descriptionWords, 0, $maxWords));
                echo "<tr>
                    <td>{$reservation['hotel_name']}</td>
                    <td>{$reservation['location']}</td>
                    <td>{$reservation['stars']} <svg xmlns=\"http://www.w3.org/2000/svg\" width=\"16\" height=\"16\" fill=\"currentColor\" class=\"bi bi-star-fill\" viewBox=\"0 0 16 16\">
                    <path d=\"M3.612 15.443c-.386.198-.824-.149-.746-.592l.83-4.73L.173 6.765c-.329-.314-.158-.888.283-.95l4.898-.696L7.538.792c.197-.39.73-.39.927 0l2.184 4.327 4.898.696c.441.062.612.636.282.95l-3.522 3.356.83 4.73c.078.443-.36.79-.746.592L8 13.187l-4.389 2.256z\"/>
                  </svg></td>
                    <td style=\"width: 300px; max-width: 300px;\">
                        <div class='short-description' style='display: inline'>{$limitedDescription}</div>
                        <div class='full-description' style='display: none;'>{$reservation['description']}</div>
                        <button class='btn btn-outline-info' onclick='toggleDescription(this)'>أكثر</button>
                    </td>
                    <td>{$reservation['contact_email']}</td>
                    <td>{$reservation['contact_phone']}</td>
                    <td>{$reservation['website_url']}</td>";
                if (checkadmin($_SESSION['admin'], 2, 2) == 2 || checkadmin($_SESSION['admin'], 1, 0) == 0 || checkadmin($_SESSION['admin'], 2, 1) == 1) {
                    echo "<td>
                    <a href='?Manage=hotel&id=" . $reservation['ID'] . "' class='btn btn-primary'><i class=\"fa-solid fa-gear\"></i></i></i> </a>
                </td>";
                }
                echo "</tr>";
                ?>
                <script>
                    function toggleDescription(button) {
                        var row = button.closest('tr');
                        var shortDescription = row.querySelector('.short-description');
                        var fullDescription = row.querySelector('.full-description');

                        if (shortDescription.style.display === 'none') {
                            shortDescription.style.display = 'inline';
                            fullDescription.style.display = 'none';
                            button.innerText = 'أكثر';
                        } else {
                            shortDescription.style.display = 'none';
                            fullDescription.style.display = 'inline';
                            button.innerText = 'أقل';
                        }
                    }
                </script>

                <?php
            }
            echo "</table>";
        } else {
            echo $msgres;
        }
    } else {
        header('Location: index.php');
        exit();
    }
} elseif ($Manage == 'hotel') {
    if (checkadmin($_SESSION['admin'], 2, 2) == 1) {
        if (isset($_GET['id']) && is_numeric($_GET['id'])) {
            $hotel_id = $_GET['id'];
            $hotelManager = new ReservationsManager($con);
            $hotelDatas = $hotelManager->getHotelById($hotel_id);
            // $file_number = explode("/", $reservationDatas['file_number']);
            ?>
            <h1 class="input-group-text text-center">تقرير فندق  :
                <?php echo $hotelDatas['hotel_name']; ?>
            </h1>
            <br>
            <?php
            if ($hotelDatas != null) {
                echo "<table class='main-table text-center table table-bordered'>";
                echo "<tr>
                        <td>التاريخ</td>
                        <td>البيان</td>
                        <td>التكلفة</td>
                        <td>الدفعات</td>
                        <td>الرصيد</td>
                    </tr>";
                // foreach ($reservations as $reservation) {
                
               /*  echo "<tr>
                    <td>{$hotelDatas['file_number']}</td>
                    <td>{$hotelDatas['agent_name']}</td>
                    <td>{$hotelDatas['client_name']}</td>
                    <td>{$hotelDatas['nationality_name']}</td>
                    <td>{$hotelDatas['arrival_date']}</td>
                    <td>{$hotelDatas['departure_date']}</td>
                    <td>{$status_payment}</td>
                    <td>{$hotelDatas['remarks']}</td>
                    <td>{$hotelDatas['date_insert']}</td>
                    <td>{$hotelDatas['total_price']}</td>
                    <td>{$check_status}</td>"; */
                    echo "<tr>
                    <td>3/12/2023</td>
                    <td>حجز عدد 6 غرف DBL و 3 غرف TPL</td>
                    <td>2400$</td>
                    <td></td>
                    <td>-2400$</td>";
                    echo "<tr>
                    <td>4/12/2023</td>
                    <td>دفعة</td>
                    <td></td>
                    <td>2000$</td>
                    <td>-400$</td>";
                    echo "<tr>
                    <td>6/12/2023</td>
                    <td>حجز عدد 3 غرف TPL و2 غرفة DBL</td>
                    <td>1200$</td>
                    <td></td>
                    <td>-1600$</td>";
                    echo "<tr>
                    <td>7/12/2023</td>
                    <td>دفعة</td>
                    <td></td>
                    <td>400$</td>
                    <td>-1200$</td>";
                    echo "<tr>
                    <td>9/12/2023</td>
                    <td>دفعة</td>
                    <td></td>
                    <td>1200$</td>
                    <td>0</td>";
                echo "</tr>";
                // }
                echo "</table>";
            } else {
                // رسالة خطأ أو إعادة توجيه إلى صفحة خطأ
                echo "خطأ: لا يوجد حجز بهذا الرقم.";
                exit;
            }
        } else {
            // رسالة خطأ أو إعادة توجيه إلى صفحة خطأ
            echo "خطأ: يجب تحديد رقم الحجز.";
            exit;
        }
    } else {
        header('Location: index.php');
        exit();
    }
} elseif ($Manage == 'cars') {
    if (checkadmin($_SESSION['admin'], 4, 2) == 1 ) {
        ?>
        <h1 class="input-group-text text-center">تقرير السيارات</h1>
        <form action="?Manage=cars" method="post">
            <div class="row">
                <div class="form-group form-group-lg col-sm-3">
                    <div class="col-sm-12">
                        <label for="start_date" class="control-label">من تاريخ:</label>
                        <input type="date" name="start_date" class="form-control" value="<?php echo $start_date_default; ?>"
                            required>
                    </div>
                </div>
                <div class="form-group form-group-lg col-sm-3">
                    <div class="col-sm-12">
                        <label for="end_date" class="control-label">الى تاريخ:</label>
                        <input type="date" name="end_date" class="form-control" value="<?php echo $end_date_default; ?>"
                            required>
                    </div>
                </div>
                <div class="form-group form-group-lg col-sm-1">
                    <div class="col-sm-12">
                        <label></label>
                        <button type="submit" class="btn btn-primary form-control" name="view_cars">بحث</button>
                    </div>
                </div>
                <div class="form-group form-group-lg col-sm-1">
                </div>
            </div>
        </form>
        <br>
        <?php
        $ManagerCars = new ManagerCars($con);
        if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['view_cars'])) {
            $start_date = $_POST['start_date'];
            $end_date = $_POST['end_date'];
        } else {
            $start_date = $start_date_default;
            $end_date = $end_date_default;
        }
        $cars = $ManagerCars->getCarsForCurrentMonthWithNames();
        $msgres = "No cars found.";
        if (!empty($cars)) {
        // if (1 == 1) {
            echo "<table class='main-table text-center table table-bordered'>";
            echo "<tr>
                <td>رقم السيارة</td>
                <td>نوع السيارة</td>
                <td>السائق</td>
                <td>الايراد خلال الشهر</td>
                <td>المصاريف خلال الشهر</td>
                <td>الصافى</td>
                <td>Control</td>
            </tr>";
            foreach ($cars as $car) {
                $cars_exp = $ManagerCars->getSum($car['vehicle_id'], 4, $start_date, $end_date);
                $cars_rev = $ManagerCars->getSum($car['vehicle_id'], 5, $start_date, $end_date);
                echo "<tr>
                    <td>{$car['vehicle_plate_number']}</td>
                    <td>{$car['vehicle_model']}</td>
                    <td>{$car['driver_name']}</td>
                    <td>\${$cars_rev['amount']}</td>
                    <td>\${$cars_exp['amount']}</td>
                    <td>$".$cars_rev['amount'] - $cars_exp['amount']."</td>";
                    /* <td>{$reservation['total_price']}</td>"; */
                if (checkadmin($_SESSION['admin'], 4, 2) == 1) {
                    echo "<td>
                    <a href='?Manage=car&id=" . $car['vehicle_id'] . "' class='btn btn-primary'><i class=\"fa-solid fa-gear\"></i></i></i> </a>
                </td>";
                }
                echo "</tr>";
            }
            echo "</table>";
        } else {
            echo $msgres;
        }
    } else {
        header('Location: index.php');
        exit();
    }
} elseif ($Manage == 'car') {
    if (checkadmin($_SESSION['admin'], 3, 2) == 1) {
        if (isset($_GET['id']) && is_numeric($_GET['id'])) {
            $car_id = $_GET['id'];
            $ManagerCars = new ManagerCars($con);
            $carDatas = $ManagerCars->getcarById($car_id);
            
            if ($carDatas != null) {
                ?>
                <h1 class="input-group-text text-center">تقرير السيارة <?php echo $carDatas['vehicle_model']; ?> رقم  : <?php echo $carDatas['vehicle_plate_number']; ?>
                </h1>
                <form action="?Manage=car&id=<?=$car_id?>" method="post">
                    <div class="row">
                        <div class="form-group form-group-lg col-sm-3">
                            <div class="col-sm-12">
                                <label for="start_date" class="control-label">من تاريخ:</label>
                                <input type="date" name="start_date" class="form-control" value="<?php echo $start_date_default; ?>"
                                    required>
                            </div>
                        </div>
                        <div class="form-group form-group-lg col-sm-3">
                            <div class="col-sm-12">
                                <label for="end_date" class="control-label">الى تاريخ:</label>
                                <input type="date" name="end_date" class="form-control" value="<?php echo $end_date_default; ?>"
                                    required>
                            </div>
                        </div>
                        <div class="form-group form-group-lg col-sm-1">
                            <div class="col-sm-12">
                                <label></label>
                                <button type="submit" class="btn btn-primary form-control" name="view_cars">بحث</button>
                            </div>
                        </div>
                        <div class="form-group form-group-lg col-sm-1">
                        </div>
                    </div>
                </form>
                <br>
                <h1 class="input-group-text text-center">بيان تحليل ايراد السيارة
                </h1>
                <br>
                <?php
                if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['view_cars'])) {
                    $start_date = $_POST['start_date'];
                    $end_date = $_POST['end_date'];
                    $carrevenues = $ManagerCars->getRecordsByIdCar($car_id, 5, $start_date, $end_date);
                    $carexpenses = $ManagerCars->getRecordsByIdCar($car_id, 4);
                } else {
                    /* $start_date = $start_date_default;
                    $end_date = $end_date_default; */
                    $carrevenues = $ManagerCars->getRecordsByIdCar($car_id, 5);
                    $carexpenses = $ManagerCars->getRecordsByIdCar($car_id, 4);
                }
                
                echo "<table class='main-table text-center table table-bordered'>";
                echo "<tr>
                        <td>التاريخ</td>
                        <td>البيان</td>
                        <td>الشركة المستأجرة</td>
                        <td>اسم السائق</td>
                        <td>المبلغ</td>
                    </tr>";
                $allrevamountUSD = 0;
                foreach ($carrevenues as $carrevenue) {
                    $allrevamountUSD += $carrevenue['amountUSD'];
                    echo "<tr>
                        <td>{$carrevenue['date_operation']}</td>
                        <td>{$carrevenue['payan']}</td>
                        <td>{$carrevenue['id_operator']}</td>
                        <td>{$carrevenue['driver_name']}</td>
                        <td>\${$carrevenue['amountUSD']}</td>";
                    echo "</tr>";
                }
                $allrevamountUSD = (strpos($allrevamountUSD, '.') !== false) ? rtrim($allrevamountUSD, '0') : $allrevamountUSD . '.00';
                echo "<tr>
                    <td>الإجمالى</td>
                    <td></td>
                    <td></td>
                    <td></td>
                    <td>\${$allrevamountUSD}</td>";
                echo "</tr>";
                echo "</table>";
                ?>
                <h1 class="input-group-text text-center">بيان مصاريف السيارة
            </h1>
            <br>
            <?php
                echo "<table class='main-table text-center table table-bordered'>";
                echo "<tr>
                        <td>التاريخ</td>
                        <td>البيان</td>
                        <td>البند</td>
                        <td>السائق</td>
                        <td>المبلغ</td>
                    </tr>";
                $allexpamountUSD = 0;
                foreach ($carexpenses as $carexpense) {
                    $allexpamountUSD += $carexpense['amountUSD'];
                    echo "<tr>
                        <td>{$carexpense['date_operation']}</td>
                        <td>{$carexpense['payan']}</td>
                        <td>{$carexpense['id_operator']}</td>
                        <td>{$carexpense['driver_name']}</td>
                        <td>\${$carexpense['amountUSD']}</td>";
                    echo "</tr>";
                }
                $allexpamountUSD = (strpos($allexpamountUSD, '.') !== false) ? rtrim($allexpamountUSD, '0') : $allexpamountUSD . '.00';
                echo "<tr>
                        <td>الإجمالى</td>
                        <td></td>
                        <td></td>
                        <td></td>
                        <td>\${$allexpamountUSD}</td>";
                    echo "</tr>";
                echo "</table>";
            } else {
                // رسالة خطأ أو إعادة توجيه إلى صفحة خطأ
                echo "خطأ: لا يوجد سيارة بهذا الرقم.";
                exit;
            }
        } else {
            // رسالة خطأ أو إعادة توجيه إلى صفحة خطأ
            echo "خطأ: يجب تحديد رقم السيارة.";
            exit;
        }
    } else {
        $theMsg = '<div class="alert alert-danger">ليس لديك صلاحية الدخول الى هذا القسم</div>';
        redirectHome($theMsg);
    }
}

include $tpl . 'footer.php';
ob_end_flush(); // Release The Output