<?php

if ($userLang == "es") {
    // Array of motivational messages
    $messages = [
        "¡Hagamos que hoy sea un día memorable! Cada momento con un cliente importa. ¡Hagamos que el día cuente!",
        "¡Démoslo todo hoy! Cada venta nos acerca a nuestros objetivos. ¡Hagamos que sea la mejor hasta ahora!",
        "¡Es hora de brillar! &#128516; Cada venta es una oportunidad de alegrarle el día a alguien. ¡Vamos!",
        "¡Aportemos algo de positividad a cada cliente! Cada interacción cuenta. ¡Continuemos con el buen trabajo!",
        "¡Hoy es un nuevo comienzo! Cada venta nos da impulso. ¡Hagamos que sea un día extraordinario!",
        "¡Comencemos con otro día productivo! Tus clientes te están esperando. ¡Terminemos con fuerza!",
        "¡Sirvamos sonrisas! &#128515; Cada cliente importa: ¡trabajemos hoy mismo para lograr un nuevo récord!",
        "¡Creemos una gran experiencia para cada cliente! Cada sonrisa &#128515; y cada venta cuentan: ¡hagamos que hoy sea inolvidable!",
        "¡Hagamos que cada momento sea significativo! Cada venta es un paso más hacia el éxito: ¡vamos más allá!"
    ];
} else if ($userLang == "fr") {
    // Array of motivational messages
    $messages = [
        "Faisons d'aujourd'hui une journée mémorable! Chaque moment passé avec un client compte. Faisons en sorte que la journée compte!",
        "Donnons-nous à fond aujourd’hui! Chaque vente nous rapproche de nos objectifs – faisons en sorte que ce soit notre meilleure vente à ce jour!",
        "Il est temps de briller! &#128516; Chaque vente est une chance de faire plaisir à quelqu’un – c’est parti!",
        "Apportons un peu de positivité à chaque client! Chaque interaction compte – Continuons notre bon travail!",
        "Aujourd'hui, c'est un nouveau départ! Chaque vente renforce notre dynamique – faisons de cette journée une journée exceptionnelle!",
        "Commençons une nouvelle journée productive! Vos clients vous attendent – ​​Terminons en force!",
        "Servons des sourires! &#128515; Chaque client compte – travaillons dès aujourd’hui pour atteindre un nouveau record!",
        "Créons une expérience formidable pour chaque client! Chaque sourire &#128515; et chaque vente comptent – ​​faisons en sorte que cette journée soit inoubliable!",
        "Faisons en sorte que chaque instant ait du sens! Chaque vente est un pas de plus vers le succès – allons au-delà de nos attentes!"
    ];
} else {
    // Array of motivational messages
    $messages = [
        "Let's make today a memorable day! Every moment with a client matters. Let's make the day count!",
        "Let’s give it our all today! Each sale brings us closer to our goals – let’s make it our best one yet!",
        "Time to shine! &#128516; Every sale is a chance to make someone’s day – let’s go for a new high score!",
        "Let’s bring some positivity to each customer! Every interaction counts – Let’s keep up the good work!",
        "Today’s a fresh start! Every sale builds our momentum – let’s make it an outstanding day!",
        "Let’s get started with another productive day! Your customers are waiting – Let’s finish strong!",
        "Let’s serve up some smiles! &#128515; Each customer matters – let's work towards a new record today!",
        "Let’s create a great experience for every customer! Each smile &#128515; and sale counts – let’s make today unforgettable!",
        "Let’s make every moment meaningful! Every sale is a step closer to success – let’s go above and beyond today!"
    ];
}

// Randomly select a motivational message
$randomMessage = $messages[array_rand($messages)];
?>

<style>
    .welcome-message {
        font-family: cursive;
    }
</style>

<!-- Main content -->
<div class="content-wrapper">

    <!-- Inner content -->
    <div class="content-inner">

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

            <!-- Content area -->
            <div class="d-flex justify-content-center align-items-center">

                <!-- Container -->
                <div class="flex-fill">

                    <!-- Error title -->
                    <div class="text-center mt-3 mb-2">

                        <h2 class="heading1 mt-2 mb-4"><?= $translate["hello"]; ?>! <?= $session_user_fullname; ?></h2>

                        <div class="text-center d-flex justify-content-center mb-3">
                            <p class="fs-4 text-muted col-xl-6 col-lg-8 col-md-10"><?= $randomMessage; ?></p>
                        </div>

                        <p class="fs-6 mt-2 fst-italic text-muted">-- <?= $translate["quick_links"]; ?> --</p>

                        <div class="flex justify-center animate-bounce mt-3">
                            <span class="ph-arrow-down ph-2x text-muted"></span>
                        </div>

                    </div>
                    <!-- /error title -->

                    <div class="d-flex justify-content-center">

                        <div class="col-lg-8 col-md-10">
                            <!-- Seamless vertical button group -->
                            <div class="row g-0">

                                <div class="col">
                                    <a type="button" href="sell" class="btn btn-dark w-100 h-75 flex-column rounded-0 py-2">
                                        <i class="ph-shopping-cart-simple text-pink ph-2x mb-1"></i>
                                        <?= $translate["sell"]; ?>
                                    </a>

                                    <a type="button" href="customers" class="btn btn-dark w-100 h-75 flex-column rounded-0 py-2">
                                        <i class="ph-users text-warning ph-2x mb-1"></i>
                                        <?= $translate["customers"]; ?>
                                    </a>
                                </div>

                                <div class="col">
                                    <a type="button" href="sales" class="btn btn-dark w-100 h-75 flex-column rounded-0 py-2">
                                        <i class="ph-handbag text-primary ph-2x mb-1"></i>
                                        <?= $translate["sales_list"]; ?>
                                    </a>

                                    <button type="button" onclick="fnShowProfile()" class="btn btn-dark w-100 h-75 flex-column rounded-0 py-2">
                                        <i class="ph-user text-success ph-2x mb-1"></i>
                                        <?= $translate["my_profile"]; ?>
                                    </button>
                                </div>

                                <div class="col">
                                    <a type="button" href="products" class="btn btn-dark w-100 h-75 flex-column rounded-0 py-2">
                                        <i class="ph-circles-four text-indigo ph-2x mb-1"></i>
                                        <?= $translate["products"]; ?>
                                    </a>

                                    <button type="button" onclick="fnLockScreen()" class="btn btn-dark w-100 h-75 flex-column rounded-0 py-2">
                                        <i class="ph-monitor text-danger ph-2x mb-1"></i>
                                        <?= $translate["lock_the_screen"]; ?>
                                    </button>
                                </div>

                            </div>
                            <!-- /seamless vertical button group -->
                        </div>

                    </div>

                </div>
                <!-- /container -->


            </div>
            <!-- /content area -->

        <?php } else { ?>

            <div class="w-full max-w-[95vw] xl:max-w-[1600px] mx-auto py-4 px-4">

                <!-- Billing Profile -->
                <div class="grid grid-cols-1 md:grid-cols-2 gap-2 mb-4">

                    <div class="mb-0">
                        <h2 class="page-title fw-semibold mb-0"><?= $translate["billing"]; ?></h2>
                        <p class="text-muted fw-medium"><?= $translate["manage_your_payment_methods_and_invoices_through_stripe"]; ?></p>
                    </div>

                    <?php

                    $btnSubscriptionText = $translate["activate_subscription"];
                    $btnSubscriptionClass = "btn-warning";

                    if ($entInfo['subscription_status'] == "active" || $entInfo['subscription_status'] == "trialing" || $entInfo['subscription_status'] == "past_due" || $entInfo['subscription_status'] == "paused") {

                        if ($entInfo['subscription_status'] == "active") {
                            $btnSubscriptionText = $translate["manage_subscription"];
                            $btnSubscriptionClass = "btn-success";
                        }

                        if ($entInfo['subscription_status'] == "trialing") {
                            $btnSubscriptionText = $translate["add_payment_method"];
                        }

                        if ($entInfo['subscription_status'] == "past_due") {
                            $btnSubscriptionText = $translate["update_payment_method"];
                        }

                        if ($entInfo['subscription_status'] == "paused") {
                            $btnSubscriptionText = $translate["resume_subscription"];
                        }
                    ?>

                        <div class="sm:text-left md:text-right">
                            <a href="javascript:void(0)" type="button" data-info='<?= $arrayEntInfo ?>' onclick="createCustomerPortalSession(this);" class="btn <?= $btnSubscriptionClass ?> text-uppercase"> <i class="ph ph-arrow-up-right me-1"></i>
                                <?= $btnSubscriptionText; ?>
                            </a>
                        </div>

                    <?php } else { ?>

                        <div class="sm:text-left md:text-right">
                            <a href="javascript:void(0)" type="button" data-info='<?= $arrayEntInfo ?>' onclick="createCheckoutStripeSession(this);" class="btn <?= $btnSubscriptionClass ?> text-uppercase btnActivateSubscription"><i class="ph ph-arrow-up-right me-1"></i>
                                <?= $btnSubscriptionText; ?>
                            </a>
                        </div>

                    <?php } ?>

                    <!-- subs_customer_id -->
                    <!-- subscription_status -->

                </div>
                <!-- /Billing Profile -->

                <!-- ALERT SECTION START -->
                <?php
                if (empty($entInfo['subscription_status'])) { ?>

                    <!-- Warning Alert -->
                    <div class="alert alert-warning alert-icon-start alert-dismissible fade show">
                        <span class="alert-icon bg-warning text-white">
                            <i class="ph-warning-circle"></i>
                        </span>
                        <span><?= $translate["you_dont_have_an_active_subscription_activate_your_subscription_to_skip_manual_payments_and_enable_automatic_billing"]; ?></span>
                        <!-- <button type="button" class="btn-close" data-bs-dismiss="alert"></button> -->
                    </div>

                <?php } else if ($entInfo['subscription_status'] == "trialing") { ?>

                    <!-- info Alert -->
                    <div class="alert alert-warning alert-icon-start alert-dismissible fade show">
                        <span class="alert-icon bg-warning text-white">
                            <i class="ph-warning-circle"></i>
                        </span>
                        <span><?= $translate["you_re_on_a_free_trial_add_a_payment_method_before_it_ends_to_keep_everything_running_smoothly"]; ?></span>
                        <!-- <button type="button" class="btn-close" data-bs-dismiss="alert"></button> -->
                    </div>

                <?php } else if ($entInfo['subscription_status'] == "active") { ?>

                    <!-- info Alert -->
                    <div class="alert alert-success alert-icon-start alert-dismissible fade show">
                        <span class="alert-icon bg-success text-white">
                            <i class="ph-check-circle"></i>
                        </span>
                        <span><?= $translate["your_subscription_is_active_billing_is_running_smoothly_thanks_for_being_with_us"]; ?></span>
                        <!-- <button type="button" class="btn-close" data-bs-dismiss="alert"></button> -->
                    </div>

                <?php } else if ($entInfo['subscription_status'] == "past_due") { ?>

                    <!-- info Alert -->
                    <div class="alert alert-warning alert-icon-start alert-dismissible fade show">
                        <span class="alert-icon bg-warning text-white">
                            <i class="ph-warning-circle"></i>
                        </span>
                        <span><?= $translate["your_last_payment_failed_please_update_your_payment_method_to_avoid_service_interruption"]; ?></span>
                        <!-- <button type="button" class="btn-close" data-bs-dismiss="alert"></button> -->
                    </div>

                <?php } else if ($entInfo['subscription_status'] == "paused") { ?>

                    <!-- info Alert -->
                    <div class="alert alert-warning alert-icon-start alert-dismissible fade show">
                        <span class="alert-icon bg-warning text-white">
                            <i class="ph-warning-circle"></i>
                        </span>
                        <span><?= $translate["your_subscription_is_currently_paused_resume_now_to_re_enable_automatic_billing_and_full_access"]; ?></span>
                        <!-- <button type="button" class="btn-close" data-bs-dismiss="alert"></button> -->
                    </div>

                <?php } else if ($entInfo['subscription_status'] == "incomplete") { ?>

                    <!-- info Alert -->
                    <div class="alert alert-warning alert-icon-start alert-dismissible fade show">
                        <span class="alert-icon bg-warning text-white">
                            <i class="ph-warning-circle"></i>
                        </span>
                        <span><?= $translate["your_subscription_setup_is_incomplete_complete_payment_to_activate_automatic_billing"]; ?></span>
                        <!-- <button type="button" class="btn-close" data-bs-dismiss="alert"></button> -->
                    </div>

                <?php } else if ($entInfo['subscription_status'] == "incomplete_expired") { ?>

                    <!-- Warning Alert -->
                    <div class="alert alert-danger alert-icon-start alert-dismissible fade show">
                        <span class="alert-icon bg-danger text-white">
                            <i class="ph-x-circle"></i>
                        </span>
                        <span><?= $translate["your_subscription_attempt_has_expired_subscribe_again_to_enable_automatic_billing_and_regain_access"]; ?></span>
                        <!-- <button type="button" class="btn-close" data-bs-dismiss="alert"></button> -->
                    </div>

                <?php } else if ($entInfo['subscription_status'] == "unpaid") { ?>

                    <!-- Danger Alert -->
                    <div class="alert alert-danger alert-icon-start alert-dismissible fade show">
                        <span class="alert-icon bg-danger text-white">
                            <i class="ph-x-circle"></i>
                        </span>
                        <span><?= $translate["payment_couldn_t_be_collected_for_your_subscription_subscribe_again_to_restore_service_and_enable_automatic_billing"]; ?></span>
                        <!-- <button type="button" class="btn-close" data-bs-dismiss="alert"></button> -->
                    </div>

                <?php } else if ($entInfo['subscription_status'] == "canceled") { ?>

                    <!-- Danger Alert -->
                    <div class="alert alert-danger alert-icon-start alert-dismissible fade show">
                        <span class="alert-icon bg-danger text-white">
                            <i class="ph-x-circle"></i>
                        </span>
                        <span><?= $translate["your_subscription_is_canceled_subscribe_again_to_enable_automatic_billing_and_regain_full_access"]; ?></span>
                        <!-- <button type="button" class="btn-close" data-bs-dismiss="alert"></button> -->
                    </div>

                <?php } ?>
                <!-- ALERT SECTION END -->

                <!-- <div class="alert bg-danger text-white alert-icon-start alert-dismissible fade show border-0">
                <span class="alert-icon bg-black bg-opacity-20">
                    <i class="ph-x-circle"></i>
                </span>
                <span class="fw-semibold">Action needed!</span> Your subscription has been canceled. Reactivate now to prevent suspension.
                <button type="button" class="btn-close btn-close-white" data-bs-dismiss="alert"></button>
                </div> -->

                <!-- Subscription Information -->
                <?php

                $subs_quantity = $entInfo['subs_quantity'] ?? $entInfo['pos_quantity'];
                $subs_amount_unit = $entInfo['subs_amount_unit'] ?? $entInfo['plan_price'];
                $subs_interval = $entInfo['subs_interval'] ?? $entInfo['plan_interval'];
                $subs_interval_count = $entInfo['subs_interval_count'] ?? $entInfo['plan_interval_count'];

                $subPrice = "N/A";
                $subPrice = "{$subs_quantity} x {$subs_amount_unit} -> (Each {$subs_interval_count} " . ucfirst($subs_interval) . ")";

                $subStatus = "<span class='badge bg-warning text-white'>Inactive</span>";

                if (!empty($entInfo['subscription_status'])) {
                    if ($entInfo['subscription_status'] === "active") {
                        $subStatus = "<span class='badge bg-success text-white'>" . ucfirst($entInfo['subscription_status']) . "</span>";
                    } elseif ($entInfo['subscription_status'] === "past_due" || $entInfo['subscription_status'] === "paused" || $entInfo['subscription_status'] === "trialing" || $entInfo['subscription_status'] === "incomplete") {
                        $subStatus = "<span class='badge bg-warning text-white'>" . ucfirst($entInfo['subscription_status']) . "</span>";
                    } else {
                        $subStatus = "<span class='badge bg-danger text-white'>" . ucfirst($entInfo['subscription_status']) . "</span>";
                    }
                }

                $nextBillingDate = $entInfo['subs_current_period_end'] ?? null;
                $nextBillingDate = $nextBillingDate ? date('F d, Y', strtotime($nextBillingDate)) : 'N/A';

                ?>

                <?php
                if (empty($entInfo['subs_customer_id'])) { ?>

                    <!-- NO SUBSCRIPTION INFO -->
                    <div class="simple-card mt-4 mb-4">

                        <div class="text-center">
                            <h6 class="text-uppercase font-medium"><?= $translate["billing_information"] ?></h6>
                        </div>

                        <hr style="margin-top: 0rem !important;margin-bottom: 1.9rem !important;">

                        <div class="grid grid-cols-1 md:grid-cols-4 gap-4">
                            <div>
                                <p class="text-muted fw-semibold fs-6"><?= $translate["customer"] ?></p>
                                <p class="font-medium"><?= $entInfo['subs_customer_name'] ?? $entInfo['ent_name']; ?></p>
                            </div>
                            <div>
                                <p class="text-muted fw-semibold fs-6"><?= $translate["current_plan"] ?></p>
                                <p class="font-medium"><?= $entInfo['plan_name'] ?? "(Custom)"; ?></p>
                            </div>
                            <div>
                                <p class="text-muted fw-semibold fs-6"><?= $translate["price"] ?></p>
                                <p class="font-medium"><?= $subPrice; ?></p>
                            </div>
                            <div>
                                <p class="text-muted fw-semibold fs-6"><?= $translate["status"] ?></p>
                                <p class="font-medium"><?= $subStatus; ?></p>
                            </div>
                            <!-- <div>
                            <p class="text-muted fw-semibold fs-6"><?= $translate["email"] ?></p>
                            <p class="font-medium" id="subs_customer_email"><?= $entInfo['customer_email'] ?? 'N/A'; ?></p>
                        </div>

                        <div>
                            <p class="text-muted fw-semibold fs-6"><?= $translate["next_billing"] ?></p>
                            <p class="font-medium" id="sub_next_billing"><?= $nextBillingDate; ?></p>
                        </div> -->
                        </div>
                    </div>
                    <!-- NO SUBSCRIPTION INFO -->

                <?php } else { ?>

                    <!-- SUBSCRIPTION INFORMATION LOADING -->
                    <div class="simple-card mt-4 mb-4 text-center div-subscription-loading">
                        <div class="mt-4 mb-4 text-center">
                            <h6 class="text-uppercase font-medium"><?= $translate["waiting_icon_text"] ?></h6>
                        </div>
                    </div>
                    <!-- SUBSCRIPTION INFORMATION LOADING -->

                    <!-- Subscription -->
                    <div class="simple-card mt-4 mb-4 div-subscription">

                        <div class="text-center">
                            <h6 class="text-uppercase font-medium"><?= $translate["billing_information"] ?></h6>
                        </div>

                        <hr style="margin-top: 0rem !important;margin-bottom: 1.9rem !important;">

                        <div class="grid grid-cols-1 md:grid-cols-3 gap-4">
                            <div>
                                <p class="text-muted fw-semibold fs-6"><?= $translate["customer"] ?></p>
                                <p class="font-medium" id="subs_customer_name"></p>
                            </div>
                            <div>
                                <p class="text-muted fw-semibold fs-6"><?= $translate["email"] ?></p>
                                <p class="font-medium" id="subs_customer_email"></p>
                            </div>
                            <div>
                                <p class="text-muted fw-semibold fs-6"><?= $translate["status"] ?></p>
                                <p class="font-medium" id="sub_status"></p>
                            </div>
                            <div>
                                <p class="text-muted fw-semibold fs-6"><?= $translate["current_plan"] ?></p>
                                <p class="font-medium" id="sub_plan_name"></p>
                            </div>
                            <div>
                                <p class="text-muted fw-semibold fs-6"><?= $translate["price"] ?></p>
                                <p class="font-medium" id="sub_unit_amount"></p>
                            </div>
                            <div>
                                <p class="text-muted fw-semibold fs-6"><?= $translate["next_billing"] ?></p>
                                <p class="font-medium" id="sub_next_billing"></p>
                            </div>
                        </div>
                    </div>
                    <!-- /Subscription -->


                    <!-- Recent Invoices -->
                    <div class="simple-card div-subscription">

                        <div class="text-center">
                            <h6 class="text-uppercase font-medium"><?= $translate["recent_invoices"] ?></h6>
                        </div>

                        <hr style="margin-top: 0rem !important;margin-bottom: 2.5rem !important;">

                        <table class="table table-sm table-hover stripe-customer-invoice-table">
                            <thead>
                                <tr class="table-dark text-uppercase">
                                    <!-- <th>#</th> -->
                                    <!-- <th>ID</th> -->
                                    <th><?= $translate["date"] ?></th>
                                    <th><?= $translate["amount"] ?></th>
                                    <th><?= $translate["status"] ?></th>
                                    <th><?= $translate["invoice"] ?></th>
                                </tr>
                            </thead>
                        </table>

                    </div>
                    <!-- /Recent Invoices -->

                <?php } ?>

            </div>

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

        <?php } ?>

    </div>
    <!-- /inner content -->

</div>
<!-- /main content -->