<?php

if ($session_user_id > 0) {

    $userInfo = getUserData($session_user_id);
    $session_user_fullname = $userInfo["user_firstname"] . ' ' . $userInfo["user_lastname"];

    if ($userInfo['user_status'] == "Enabled") {

        $sql = "UPDATE tb_users SET
        user_last_activity = '$currentDateTimeByZone'
        WHERE user_id = '$session_user_id';";
        executeQueryPDO($sql);

        if ($userInfo['user_image']) $userImagePath = $userInfo['user_image'];
    } else {
        echo '<script>
        window.location = "logout";
        </script>';
    }

    $userLang = $userInfo['user_language'];

    if ($userLang == "es") {
        $getLanguage = file_get_contents("lang/es.json");
    } else if ($userLang == "fr") {
        $getLanguage = file_get_contents("lang/fr.json");
    } else {
        $getLanguage = file_get_contents("lang/en.json");
    }
}

$translate = json_decode($getLanguage, true);

$currentTime  = date('H:i:s');
if ($currentTime <= date('06:59:59')) {
    $currentDayStart = date('Y-m-d', strtotime("-1 day"));
} else {
    $currentDayStart = date('Y-m-d');
}

$iconBalloon = "assets/images/balloons.png";

?>

<input type="hidden" id="user_lang" value="<?= $userLang; ?>">
<input type="hidden" id="sess_ent_id" value="<?= $session_user_ent_id; ?>">
<input type="hidden" id="sess_pos_id" value="<?= $session_user_pos_id; ?>">
<input type="hidden" id="sess_user_id" value="<?= $session_user_id; ?>">
<input type="hidden" id="sess_user_profile" value="<?= $session_user_profile; ?>">
<input type="hidden" id="sess_user_fullname" value="<?= $session_user_fullname; ?>">
<input type="hidden" id="sess_pos_currency" value="<?= $pos_currency; ?>">
<input type="hidden" id="business_category" value="<?= $business_category; ?>">
<input type="hidden" id="currency_symbol" value="<?= $currency_symbol; ?>">
<input type="hidden" id="txtTransactionId" value="">
<input type="hidden" id="stripe_customer_id" value="<?= $stripe_customer_id; ?>">
<input type="hidden" id="stripe_subscription_id" value="<?= $stripe_subscription_id; ?>">
<input type="hidden" id="csrf_token" value="<?= $_SESSION['csrf_token'] ?>">

<!-- Header Main navbar -->
<!-- <div class="navbar navbar-dark navbar-expand-lg navbar-static border-bottom border-bottom-white border-opacity-10"> -->
<div class="navbar navbar-expand-lg navbar-static border-bottom border-bottom-white border-opacity-10">
    <div class="container-fluid">

        <div class="d-flex">

            <!-- Mobile -->
            <div class="d-flex d-lg-none me-2">
                <button type="button" class="navbar-toggler sidebar-mobile-main-toggle rounded-pill">
                    <i class="ph-list fs-1"></i>
                </button>
            </div>

            <!-- Desktop -->
            <button type="button" class="d-none d-lg-inline-flex navbar-toggler custom-sidebar-toggle rounded-pill me-3">
                <i class="ph-list fs-1"></i>
            </button>

            <div class="navbar-brand flex-1 flex-lg-0">

                <a href="home" class="d-inline-flex align-items-center hide-on-mobile">
                    <!-- <span class="text-warning fs-4 fw-semibold">EVOSYS POS</span> -->
                    <img src="assets/images/evosys-pos-logo-short.png" alt="evosys-pos-logo" class="d-none d-sm-inline-block" style="width: 130%;">
                    <!-- <img src="./assets/images/logo_text_light.svg" class="h-16px ms-3 d-none d-sm-inline-block" alt=""> -->
                </a>

                <!-- <button type="button" class="btn btn-icon btn-sm rounded-pill border-transparent sidebar-control sidebar-main-resize d-none d-lg-inline-flex">
                    <i class="ph-list text-dark"></i> Desktop
                </button> -->
            </div>

        </div>


        <div class="marqueediv d-flex d-none d-lg-inline-flex text-uppercase fs-6">

            <marquee scrollamount="5">

                <div class="flex">

                    <?php

                    // <!-- SHOW NOTIFICATION -->
                    if (getEnterpriseData($session_user_ent_id)["ent_show_notification"] == "Yes") { ?>
                        <span class="me-2 fw-medium" style="color: #ff6666;"><?= getEnterpriseData($session_user_ent_id)["ent_notification_text"] ?></span>&nbsp;&nbsp;
                    <?php }

                    // ph-balloon

                    // <!-- SHOW HAPPY NEW YEAR -->
                    $today = date('Y-m-d');
                    if ($today >= date('Y-01-01') && $today <= date('Y-01-10')) { ?>
                        <span class="me-2">
                            <div class="flex me-2">
                                <img src="<?= $iconBalloon; ?>" width="25px" class="fs-sm me-2">
                                <span class="me-2"><?= $translate['happy_new_year'] . ' ' . date('Y') . ' - ' . $_SESSION["user_firstname"] . ' ' . $_SESSION["user_lastname"] ?></span>
                                <img src="<?= $iconBalloon; ?>" width="25px" class="fs-sm">
                            </div>
                        </span>
                    <?php }

                    // <!-- SHOW HAPPY BIRTHDAY -->
                    $todayBirthday = date('m-d');
                    $birthdayQuery = "SELECT CONCAT(user_firstname,' ',user_lastname) AS userFullName ,DATE_FORMAT(user_birthday, '%m-%d') AS user_birthday FROM tb_users WHERE user_ent_id = {$session_user_ent_id} AND DATE_FORMAT(user_birthday, '%m-%d') = '{$todayBirthday}' AND user_status = 'Enabled' GROUP BY user_lastname;";

                    $resBirthday = executePDOFetchAll($birthdayQuery, 'success', 'error');

                    $textColor = array("text-success", "text-warning", "text-danger", "text-pink", "text-yellow", "text-lime");

                    foreach ($resBirthday as $key => $value) { ?>
                        <span class="<?= $textColor[$key] ?>">
                            <div class="flex">
                                <img src="<?= $iconBalloon; ?>" width="25px" class="fs-sm me-2">
                                <span class="me-2"><?= $translate['happy_birthday'] . ' ' . $value["userFullName"]; ?></span>
                                <img src="<?= $iconBalloon; ?>" width="25px" class="fs-sm">
                            </div>
                        </span>
                    <?php }

                    ?>

                </div>

            </marquee>
        </div>


        <ul class="nav flex-row justify-content-end">


            <li class="nav-item">
                <a href="#" class="navbar-nav-link align-items-center rounded-pill btnChangeTheme">
                    <span id="themeIcon"><i class="ph-sun fs-3"></i></span>
                </a>
            </li>

            <li class="nav-item nav-item-dropdown-lg dropdown">
                <a href="javascript:void(0)" class="navbar-nav-link align-items-center rounded-pill" data-bs-toggle="dropdown">
                    <img src="./assets/flags/<?= $translate['flag_icon']; ?>" class="" alt="flag-en" width="24" height="18"> &nbsp; <span class="d-none d-lg-inline-block"><?= $translate['flag_lang']; ?></span>
                </a>

                <div class="dropdown-menu dropdown-menu-end dropdown-menu-theme align-items-center">
                    <a href="javascript:void(0)" class="dropdown-item btnChangeLanguage" lang="en">
                        <img src="./assets/flags/flag-en.svg" alt="flag-en" width="24" height="18"> &nbsp; <?= $translate['english']; ?>
                    </a>
                    <div class="dropdown-divider"></div>
                    <a href="javascript:void(0)" class="dropdown-item btnChangeLanguage" lang="es">
                        <img src="./assets/flags/flag-es.svg" alt="flag-en" width="24" height="18"> &nbsp; <?= $translate['spanish']; ?>
                    </a>
                    <div class="dropdown-divider"></div>
                    <a href="javascript:void(0)" class="dropdown-item btnChangeLanguage" lang="fr">
                        <img src="./assets/flags/flag-fr.svg" alt="flag-en" width="24" height="18"> &nbsp; <?= $translate['french']; ?>
                    </a>
                </div>
            </li>

            <li class="nav-item me-3">
                <a href="#" class="navbar-nav-link navbar-nav-link-icon rounded-pill" data-bs-toggle="offcanvas" data-bs-target="#notifications">
                    <!-- <a href="javascript:void(0)" type="button" class="navbar-nav-link navbar-nav-link-icon rounded-pill btnOpenOrdersNotificationModal"> -->
                    <i class="ph-bell fs-3"></i>
                    <span class="badge bg-warning fw-medium position-absolute top-0 end-0 translate-middle-top zindex-1 rounded-pill mt-1 me-1 count_orders_notifications" style="display: none;">0</span>
                </a>
            </li>

            <li class="nav-item nav-item-dropdown-lg dropdown">
                <a href="javascript:void(0);" class="navbar-nav-link align-items-center rounded-pill p-1" data-bs-toggle="dropdown">
                    <div class="status-indicator-container">

                        <img src="<?= htmlspecialchars(!empty($userImagePath) ? $userImagePath : 'assets/images/anonymous.png'); ?>"
                            class="w-32px h-32px rounded-pill" alt="profile-image"
                            onerror="this.onerror=null;this.src='assets/images/no-image.png';">


                        <!-- <img src="<?= $userImagePath; ?>" class="w-32px h-32px rounded-pill" alt="profile-image"> -->
                        <span class="status-indicator bg-success"></span>
                    </div>
                    <span class="d-none d-lg-inline-block mx-lg-2"><?= $session_user_fullname; ?></span>
                </a>

                <div class="dropdown-menu dropdown-menu-end">
                    <a href="javascript:void(0);" onclick="fnShowProfile()" class="dropdown-item light-warning-on-hover">
                        <i class="ph-user me-2"></i>
                        <?= $translate['my_profile']; ?>
                    </a>

                    <?php
                    if ($_SESSION["user_profile"] == "ROOT" || $_SESSION["user_profile"] == "Owner") { ?>

                        <div class="dropdown-divider"></div>
                        <a href="billing" class="dropdown-item light-warning-on-hover">
                            <i class="ph ph-invoice me-2"></i>
                            <?= $translate['billing']; ?>
                        </a>

                    <?php } ?>

                    <?php
                    if ($_SESSION["user_profile"] == "ROOT" || $_SESSION["user_profile"] == "Owner" || $_SESSION["user_profile"] == "Manager") { ?>

                        <div class="dropdown-divider"></div>
                        <a href="javascript:void(0);" onclick="fnShowEnterprisePasswords()" class="dropdown-item light-warning-on-hover">
                            <i class="ph-lock me-2"></i>
                            <?= $translate['access_code']; ?>
                        </a>

                    <?php } ?>

                    <?php
                    if ($_SESSION["user_profile"] == "ROOT") { ?>

                        <div class="dropdown-divider"></div>
                        <a href="javascript:void(0);" onclick="modalTextMessageWhatsApp()" class="dropdown-item light-warning-on-hover">
                            <i class="ph-whatsapp-logo me-2"></i>
                            Test WS Message
                        </a>

                    <?php } ?>

                    <div class="dropdown-divider"></div>
                    <a href="javascript:void(0);" onclick="fnLockScreen()" class="dropdown-item light-warning-on-hover">
                        <i class="ph-monitor me-2"></i>
                        <?= $translate['lock_the_screen']; ?>
                    </a>

                    <div class="dropdown-divider"></div>
                    <a href="logout" class="dropdown-item text-danger danger-on-hover">
                        <i class="ph-sign-out me-2"></i>
                        <?= $translate['log_out']; ?>
                    </a>
                </div>
            </li>
        </ul>
    </div>
</div>
<!-- Header main navbar -->


<!-- Notifications -->
<div class="offcanvas offcanvas-end" tabindex="-1" id="notifications">
    <div class="offcanvas-header py-0">
        <button type="button" class="btn btn-light btn-sm btn-icon border-transparent rounded-pill" data-bs-dismiss="offcanvas">
            <i class="ph-x"></i>
        </button>
        <h5 class="offcanvas-title py-3 justify-content-center text-uppercase">
            <?= $translate['notifications']; ?>
        </h5>
    </div>

    <div class="offcanvas-body p-0">

        <ul class="nav nav-tabs nav-tabs-highlight nav-justified">

            <li class="nav-item">
                <a href="#tab-pending-orders" class="nav-link active" data-bs-toggle="tab">
                    <span class="badge-notification bg-danger me-1" id="count_pending_orders">0</span>
                    <?= $translate['pending']; ?>
                </a>
            </li>

            <?php if ($business_category == "Restaurant" || $business_category == "Fast Food" || $business_category == "Nightclub") { ?>

                <li class="nav-item">
                    <a href="#tab-undelivered" class="nav-link" data-bs-toggle="tab">
                        <span class="badge-notification bg-yellow text-black me-1" id="count_undelivered_orders">0</span>
                        <?= $translate['undelivered']; ?>
                    </a>
                </li>

                <li class="nav-item">
                    <a href="#tab-delivered" class="nav-link" data-bs-toggle="tab">
                        <span class="badge-notification bg-light text-dark me-1" id="count_delivered_orders">0</span>
                        <?= $translate['delivered']; ?>
                    </a>
                </li>

            <?php } ?>
        </ul>

        <div class="tab-content">

            <div class="tab-pane fade show active" id="tab-pending-orders">
                <table class="table table-hover tbPendingOrdersList">
                    <thead>
                        <tr>
                            <th>#</th>
                            <th><?= $translate['customer']; ?></th>
                            <th><?= $translate['total']; ?></th>
                        </tr>
                    </thead>
                    <tbody></tbody>
                </table>
            </div>

            <div class="tab-pane fade" id="tab-undelivered">

                <div class="scroll-y d-block show_undelivered_orders">

                    <!-- <div class="d-flex justify-content-center px-3 mt-2">
                        <span class="input-group-text fs-3 me-3">#2351-1</span>
                        <button class="btn btn-warning me-2"><i class="ph-eye"></i></button>
                        <button class="btn btn-primary me-2"><i class="ph-printer"></i></button>
                        <button class="btn btn-success"><i class="ph-check"></i></button>
                    </div> -->

                </div>

                <!-- <hr class="mt-2"> -->

                <div class="d-flex justify-content-center sticky-bottom mt-3 border-top bg-light divBtnCompleteAllOrders">
                    <button class="btn btn-indigo mt-2 mb-2 btnCompleteAllOrders text-uppercase"><i class="ph-checks me-2"></i> <?= $translate['deliver_all']; ?></button>
                </div>

            </div>

            <div class="tab-pane fade" id="tab-delivered">

                <div class="scroll-y d-block show_delivered_orders">

                    <!-- <div class="d-flex justify-content-center px-3 mt-2">
                        <span class="input-group-text fs-3 me-3">#2351-1</span>
                        <button class="btn btn-warning me-2"><i class="ph-eye"></i></button>
                        <button class="btn btn-primary me-2"><i class="ph-printer"></i></button>
                    </div> -->

                </div>

            </div>
        </div>

    </div>
</div>
<!-- /notifications -->

<!-- PRINT KITCHEN ORDER MODAL -->
<div id="showOrderModal" class="modal" data-bs-keyboard="false" data-bs-backdrop="static" tabindex="-1">
    <div class="modal-dialog modal-sm">
        <div class="modal-content">

            <input type="hidden" id="order_id">
            <input type="hidden" id="order_code">

            <div id="printDivOrderHidden" style="display: none;"></div>
            <div id="printDivOrder" style="margin-left: 20px; margin-right: 20px; margin-top: 20px;"></div>

            <div class="modal-footer d-flex justify-content-center mt-3">
                <div class="col-sm-12 btn-group d-flex gap-1">
                    <button type="button" onclick="jsEditOrder()" class="btn btn-warning"><i class="ph-pencil-simple-line me-1"></i> <?= $translate["edit"]; ?></button>
                    <button type="button" class="btn btn-secondary" onclick="printTicketOrder()"><i class="ph-printer me-1"></i> <?= $translate["print"]; ?></button>
                    <button type="button" class="btn btn-success btnCompleteOrder" onclick="jsCompleteOrder()"><i class="ph-check me-1"></i> <?= $translate["deliver"]; ?></button>
                </div>
                <div class="col-sm-12 btn-group">
                    <button type="button" class="btn btn-light text-uppercase" data-bs-dismiss="modal"><i class="mi-close me-1"></i> <?= $translate["close"]; ?></button>
                </div>
            </div>
        </div>
    </div>
</div>
<!-- PRINT KITCHEN ORDER MODAL -->

<!-- USER PROFILE MODAL -->
<div class="modal fade" id="modalUserProfile">
    <div class="modal-dialog modal-md">
        <div class="modal-content">

            <div class="modal-header content-center">
                <h5 class="modal-title"><?= $translate["my_profile"]; ?></h5>
            </div>

            <div class="modal-body">

                <div class="row">

                    <div class="col-lg-5 col-md-12 mt-1">

                        <div class="card border-start border-top">

                            <div class="text-center mt-3 mb-3">
                                <div class="card-img-actions d-inline-block mb-3">
                                    <img class="img-fluid rounded-circle w-90px h-90px profilePicturePreview" src="assets/images/no-image.png" alt="image">
                                </div>
                                <h5 class="mb-0 spanUserFullName"></h5>
                                <span class="text-muted spanUserProfile"></span>
                            </div>

                            <div class="border-top mb-1"></div>

                            <div class="px-3 py-2">
                                <i class="ph-user me-2"></i>
                                <span class="text-muted ms-auto spanUserName"></span>
                            </div>

                            <div class="px-3 py-2">
                                <i class="ph-phone me-2"></i>
                                <span class="text-muted ms-auto spanUserPhone"></span>
                            </div>

                            <div class="px-3 py-2">
                                <i class="ph-map-pin me-2"></i>
                                <span class="text-muted ms-auto spanUserAddress"></span>
                            </div>

                            <div class="px-3 py-2">
                                <div class="flex">
                                    <i class="ph-calendar"></i>
                                    <span><img src="<?= $iconBalloon; ?>" width="16px" class="fs-sm me-1"></span>
                                    <span class="text-muted spanUserBirthday"></span>
                                </div>
                            </div>

                            <div class="border-top mt-3"></div>

                            <div class="mt-3 mb-3 hover-danger text-center">
                                <a href="logout" class="text-danger">
                                    <i class="ph-sign-out me-2"></i>
                                    <?= $translate['log_out']; ?>
                                </a>
                            </div>

                        </div>
                    </div>


                    <div class="col-lg-7 col-md-12 mt-1">

                        <div class="card border-top border-end">

                            <div class="mt-3 mb-1 text-center border-bottom-0">
                                <label class="form-label text-uppercase fw-medium"><?= $translate["login_information"]; ?></label>
                            </div>

                            <hr class="mb-2">

                            <div class="card-body">
                                <div class="row">

                                    <form action="" id="frmUserProfile" autocomplete="off" method="POST" enctype="multipart/form-data">

                                        <input type="hidden" name="csrf_token" value="<?= $_SESSION['csrf_token'] ?>">

                                        <input type="hidden" name="user_id_profile" id="user_id_profile">
                                        <input type="hidden" name="current_password_profile" id="current_password_profile">

                                        <div class="col-lg-12 col-md-12 mb-3">
                                            <label class="form-label"><?= $translate["username"]; ?> <span class="text-danger">*</span></label>
                                            <div class="input-group">
                                                <span class="input-group-text"><i class="ph-user"></i></span>
                                                <input type="text" class="form-control form-control-lg" name="user_name_profile" id="user_name_profile" autocomplete="off">
                                            </div>
                                        </div>

                                        <div class="col-lg-12 col-md-12 mb-3">
                                            <label class="form-label"><?= $translate["password"]; ?> <span class="text-danger">*</span></label>
                                            <div class="input-group">
                                                <span class="input-group-text"><i class="ph-lock"></i></span>
                                                <input type="password" class="form-control form-control-lg" name="user_password_profile" id="user_password_profile" autocomplete="off">
                                            </div>
                                        </div>

                                        <div class="col-lg-12 col-md-12 mb-3">
                                            <label class="form-label"><?= $translate["confirm_password"]; ?> <span class="text-danger">*</span></label>
                                            <div class="input-group">
                                                <span class="input-group-text"><i class="ph-lock"></i></span>
                                                <input type="password" class="form-control form-control-lg" name="user_password_confirm_profile" id="user_password_confirm_profile" autocomplete="off">
                                            </div>
                                        </div>


                                        <div class="col-lg-12 col-md-12 mb-3">
                                            <label class="form-label"><?= $translate["profil_picture"]; ?></label>
                                            <input type="file" class="form-control form-control-lg new_profile_picture" name="user_image_profile" id="user_image_profile">
                                            <div class="form-text"><?= $translate["accepted_formats_png_jpg_2_mb"]; ?></div>
                                        </div>

                                    </form>

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

                    </div>

                </div>

            </div>

            <div class="modal-footer">
                <button type="button" class="btn btn-indigo text-uppercase btnSaveProfile"><i class="ph-floppy-disk-back me-1"></i> <?= $translate["save"]; ?></button>
                <button type="button" class="btn btn-light text-uppercase" data-bs-dismiss="modal"><i class="mi-close me-1"></i> <?= $translate["close"]; ?></button>
            </div>

        </div>
    </div>
</div>
<!-- USER PROFILE MODAL -->

<!-- ENTERPRISE PASSWORD MODAL -->
<div class="modal fade" id="modalEnterprisePasswords">
    <div class="modal-dialog modal-sm">
        <div class="modal-content">

            <div class="modal-header">
                <h5 class="modal-title"><?= $translate["access_code"]; ?></h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>

            <div class="modal-body">

                <div class="col-lg-12 col-sm-12 mt-1">
                    <div class="mb-3">
                        <label class="form-label text-uppercase"><?= $translate["cancellation_code"]; ?></label>
                        <div class="input-group">
                            <span class="input-group-text text-white"><i class="ph-lock"></i></span>
                            <input type="number" class="form-control form-control-lg fs-5" name="ent_cancel_password" id="txtCancelPassword" required autocomplete="off" value="" placeholder="****">
                        </div>
                    </div>
                </div>

                <div class="col-lg-12 col-sm-12 mt-4">
                    <div class="mb-3">
                        <label class="form-label text-uppercase"><?= $translate["discount_code"]; ?></label>
                        <div class="input-group">
                            <span class="input-group-text text-white"><i class="ph-lock"></i></span>
                            <input type="number" class="form-control form-control-lg fs-5" name="ent_discount_password" id="txtDiscountPassword" required autocomplete="off" value="" placeholder="****">
                        </div>
                    </div>
                </div>

                <div class="col-lg-12 col-sm-12 mt-4 hide">
                    <div class="mb-3">
                        <label class="form-label text-uppercase"><?= $translate["statistics_code"]; ?></label>
                        <div class="input-group">
                            <span class="input-group-text bg-info text-white"><i class="ph-lock"></i></span>
                            <input type="number" class="form-control form-control-lg fs-5" name="ent_statistic_password" id="txtStatisticPassword" required autocomplete="off" value="" placeholder="****">
                        </div>
                    </div>
                </div>

            </div>

            <div class="modal-footer w-full btn-group">
                <button type="button" class="btn btn-warning btnSetNewPasswords"><i class="ph-arrows-clockwise me-1"></i> <?= $translate["regenerate"]; ?></button>
                <button type="button" class="btn btn-indigo btnRefreshPasswords"><i class="ph-arrow-clockwise me-1"></i> <?= $translate["refresh"]; ?></button>
                <button type="button" class="btn btn-light" data-bs-dismiss="modal"><i class="mi-close me-1"></i> <?= $translate["close"]; ?></button>
            </div>

        </div>
    </div>
</div>
<!-- ENTERPRISE PASSWORD MODAL -->

<script src="scripts/header.js?v=<?= md5(time()) ?>"></script>