<?php
$db =& get_instance();
$userdetails = get_object_vars($db->user_info());
$store_setting =$db->Product_model->getSettings('store');
$products = $db->Product_model;
$notifications_count = $products->getnotificationnew_count('admin',null);
?>

<!-- Morris.js Chart Library -->
<link rel="stylesheet" href="<?= base_url('assets/plugins/morris/morris.css') ?>">
<script src="<?= base_url('assets/plugins/raphael/raphael-min.js') ?>"></script>
<script src="<?= base_url('assets/plugins/morris/morris.min.js') ?>"></script>

<div class="container-fluid">
    <div class="row">
        <div class="col-12">
            
            <!-- Smart Contextual Header -->
            <div class="d-flex justify-content-between align-items-center mb-3">
              <div class="d-flex align-items-center">
                <div class="page-title-dot me-2"></div>
                <h1 class="page-title mb-0"><?= __('admin.store_dashboard') ?></h1>
              </div>
              
              <!-- Smart Quick Actions -->
              <div class="d-flex align-items-center gap-2">
                <!-- Tour Guide Button -->
                <button type="button" class="btn btn-outline-info btn-sm rounded-pill" onclick="loadTourSystem()" title="<?= __('admin.help_tour') ?>">
                  <i class="fas fa-question-circle me-1"></i><?= __('admin.help_tour') ?>
                </button>
                
                <!-- Primary Actions (Most Used) -->
                <div class="btn-group btn-group-sm" role="group">
                  <a href="<?= base_url('admincontrol/store_dashboard') ?>" class="btn btn-primary btn-sm" title="Store Dashboard">
                    <i class="fas fa-home"></i><span class="d-none d-lg-inline ms-1">Dashboard</span>
                  </a>
                  <a href="<?= base_url('admincontrol/listproduct') ?>" class="btn btn-outline-primary btn-sm" title="<?= __('admin.store_cart_products') ?>">
                    <i class="fas fa-box"></i><span class="d-none d-xl-inline ms-1"><?= __('admin.store_cart_products') ?></span>
                  </a>
                  <a href="<?= base_url('Productsales/index') ?>" class="btn btn-outline-primary btn-sm" title="<?= __('admin.store_sales_products') ?>">
                    <i class="fas fa-chart-line"></i><span class="d-none d-xl-inline ms-1"><?= __('admin.store_sales_products') ?></span>
                  </a>
                  <a href="<?= base_url('admincontrol/listorders') ?>" class="btn btn-outline-primary btn-sm" title="Orders">
                    <i class="fas fa-shopping-cart"></i><span class="d-none d-xl-inline ms-1">Orders</span>
                  </a>
                </div>
                
                <!-- Secondary Actions (Settings) -->
                <div class="dropdown">
                  <button class="btn btn-outline-secondary btn-sm dropdown-toggle" type="button" data-bs-toggle="dropdown" title="More Options">
                    <i class="fas fa-ellipsis-h"></i><span class="d-none d-md-inline ms-1">More</span>
                  </button>
                  <ul class="dropdown-menu dropdown-menu-end">
                    <li><a class="dropdown-item" href="<?= base_url('admincontrol/listproduct/reviews') ?>">
                      <i class="fas fa-star me-2"></i><?= __('admin.store_v_requests') ?>
                    </a></li>
                    <li><a class="dropdown-item" href="<?= base_url('admincontrol/store_category') ?>">
                      <i class="fas fa-tags me-2"></i><?= __('admin.store_categories') ?>
                    </a></li>
                    <li><a class="dropdown-item" href="<?= base_url('admincontrol/listclients') ?>">
                      <i class="fas fa-users me-2"></i><?= __('admin.store_clients') ?>
                    </a></li>
                    <li><hr class="dropdown-divider"></li>
                    <li><h6 class="dropdown-header text-primary"><i class="fas fa-chart-pie me-1"></i><?= __('admin.store_analytics') ?></h6></li>
                    <li><a class="dropdown-item" href="<?= base_url('admincontrol/store_analytics') ?>">
                      <i class="fas fa-chart-pie me-2"></i><?= __('admin.store_analytics') ?>
                    </a></li>
                    <li><a class="dropdown-item" href="<?= base_url('admincontrol/store_inventory') ?>">
                      <i class="fas fa-boxes-stacked me-2"></i><?= __('admin.inventory_panel') ?>
                    </a></li>
                    <li><a class="dropdown-item" href="<?= base_url('admincontrol/store_customers') ?>">
                      <i class="fas fa-users me-2"></i><?= __('admin.customer_insights') ?>
                    </a></li>
                    <li><a class="dropdown-item" href="<?= base_url('admincontrol/vendor_payouts') ?>">
                      <i class="fas fa-money-bill-transfer me-2"></i><?= __('admin.vendor_payouts') ?>
                    </a></li>
                    <li><hr class="dropdown-divider"></li>
                    <li><h6 class="dropdown-header text-primary"><i class="fas fa-bolt me-1"></i><?= __('admin.sales_funnels') ?></h6></li>
                    <li><a class="dropdown-item" href="<?= base_url('admincontrol/sales_funnels') ?>">
                      <i class="fas fa-layer-group me-2"></i><?= __('admin.manage_sales_funnels') ?>
                    </a></li>
                    <li><a class="dropdown-item" href="<?= base_url('admincontrol/funnel_pricing') ?>">
                      <i class="fas fa-tag me-2"></i><?= __('admin.funnel_pricing') ?>
                    </a></li>
                    <li><hr class="dropdown-divider"></li>
                    <li><a class="dropdown-item" href="<?= base_url('admincontrol/store_setting') ?>">
                      <i class="fas fa-cog me-2"></i><?= __('admin.store_settings') ?>
                    </a></li>
                  </ul>
                </div>
                
                <!-- View Store Button -->
                <?php $store_url = base_url('store'); ?>
                <a href="<?= $store_url ?>" target="_blank" class="btn btn-success btn-sm">
                  <i class="fas fa-external-link-alt me-1"></i><span class="d-none d-md-inline ms-1"><?= __('admin.view_store') ?></span>
                </a>
              </div>
            </div>

            <!-- Key Metrics Row -->
            <div class="row g-4 mb-4">
                <div class="col-xl-3 col-lg-6">
                    <div class="card border-0 shadow-sm h-100">
                        <div class="card-body">
                            <div class="d-flex align-items-center">
                                <div class="flex-shrink-0">
                                    <div class="bg-primary bg-opacity-10 rounded-3 p-3">
                                        <i class="bi bi-wallet2 text-primary fs-2"></i>
                                    </div>
                                </div>
                                <div class="flex-grow-1 ms-3">
                                    <h6 class="text-muted fw-bold mb-1"><?= __('admin.total_balance') ?></h6>
                                    <h4 class="fw-bold text-primary mb-0 ajax-total_balance"><?= $totals['full_total_balance'] ?></h4>
                                    <small class="text-success">
                                        <i class="bi bi-arrow-up me-1"></i><?= __('admin.all_time') ?>
                                    </small>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                
                <div class="col-xl-3 col-lg-6">
                    <div class="card border-0 shadow-sm h-100">
                        <div class="card-body">
                            <div class="d-flex align-items-center">
                                <div class="flex-shrink-0">
                                    <div class="bg-success bg-opacity-10 rounded-3 p-3">
                                        <i class="bi bi-graph-up text-success fs-2"></i>
                                    </div>
                                </div>
                                <div class="flex-grow-1 ms-3">
                                    <h6 class="text-muted fw-bold mb-1"><?= __('admin.total_sales') ?></h6>
                                    <h4 class="fw-bold text-success mb-0 ajax-total_balance"><?= $totals['total_sale_balance'] ?></h4>
                                    <small class="text-success">
                                        <i class="bi bi-arrow-up me-1"></i><?= __('admin.revenue') ?>
                                    </small>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                
                <div class="col-xl-3 col-lg-6">
                    <div class="card border-0 shadow-sm h-100">
                        <div class="card-body">
                            <div class="d-flex align-items-center">
                                <div class="flex-shrink-0">
                                    <div class="bg-warning bg-opacity-10 rounded-3 p-3">
                                        <i class="bi bi-cursor text-warning fs-2"></i>
                                    </div>
                                </div>
                                <div class="flex-grow-1 ms-3">
                                    <h6 class="text-muted fw-bold mb-1"><?= __('admin.clicks_commission') ?></h6>
                                    <h4 class="fw-bold text-warning mb-0 ajax-all_clicks_comm"><?= $totals['full_all_clicks_comm'] ?></h4>
                                    <small class="text-muted">
                                        <i class="bi bi-info-circle me-1"></i><?= __('admin.clicks_and_earnings') ?>
                                    </small>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                
                <div class="col-xl-3 col-lg-6">
                    <div class="card border-0 shadow-sm h-100">
                        <div class="card-body">
                            <div class="d-flex align-items-center">
                                <div class="flex-shrink-0">
                                    <div class="bg-danger bg-opacity-10 rounded-3 p-3">
                                        <i class="bi bi-exclamation-triangle text-danger fs-2"></i>
                                    </div>
                                </div>
                                <div class="flex-grow-1 ms-3">
                                    <h6 class="text-muted fw-bold mb-1"><?= __('admin.pending_orders') ?></h6>
                                    <h4 class="fw-bold text-danger mb-0">
                                        <a href="<?= base_url('admincontrol/listorders') ?>" class="text-decoration-none text-danger ajax-hold_orders">
                                            <?= $totals['full_local_store_hold_orders'] ?>
                                        </a>
                                    </h4>
                                    <small class="text-danger">
                                        <i class="bi bi-clock me-1"></i><?= __('admin.needs_attention') ?>
                                    </small>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <!-- Secondary Metrics Row -->
            <div class="row g-3 mb-4">
                <div class="col-lg-2 col-md-4 col-sm-6">
                    <div class="card border-0 shadow-sm h-100">
                        <div class="card-body text-center p-3">
                            <div class="bg-info bg-opacity-10 rounded-circle p-2 d-inline-flex mb-2">
                                <i class="bi bi-truck text-info fs-4"></i>
                            </div>
                            <h6 class="text-muted fw-bold small mb-1"><?= __('admin.shipping_tax') ?></h6>
                            <p class="fw-bold text-info mb-0 small">
                                <?= c_format($local_store_shipping_cost) ?> / <?= c_format($local_store_tax_cost) ?>
                            </p>
                        </div>
                    </div>
                </div>
                
                <div class="col-lg-2 col-md-4 col-sm-6">
                    <div class="card border-0 shadow-sm h-100">
                        <div class="card-body text-center p-3">
                            <div class="bg-secondary bg-opacity-10 rounded-circle p-2 d-inline-flex mb-2">
                                <i class="bi bi-people text-secondary fs-4"></i>
                            </div>
                            <h6 class="text-muted fw-bold small mb-1">
                                <a href="<?= base_url('admincontrol/listclients') ?>" class="text-decoration-none text-muted">
                                    <?= __('admin.clients_guests') ?>
                                </a>
                            </h6>
                            <p class="fw-bold text-secondary mb-0">
                                <?= !empty($client_count) ? count($client_count) : '0'; ?> / <?= !empty($guest_count) ? count($guest_count) : '0'; ?>
                            </p>
                        </div>
                    </div>
                </div>
                
                <div class="col-lg-2 col-md-4 col-sm-6">
                    <div class="card border-0 shadow-sm h-100">
                        <div class="card-body text-center p-3">
                            <div class="bg-primary bg-opacity-10 rounded-circle p-2 d-inline-flex mb-2">
                                <i class="bi bi-box text-primary fs-4"></i>
                            </div>
                            <h6 class="text-muted fw-bold small mb-1"><?= __('admin.total_products') ?></h6>
                            <p class="fw-bold text-primary mb-0"><?= (int)$product_count ?></p>
                            <a href="<?= base_url('admincontrol/listproduct') ?>" class="btn btn-outline-primary btn-sm mt-1">
                                <?= __('admin.manage') ?>
                            </a>
                        </div>
                    </div>
                </div>
                
                <div class="col-lg-2 col-md-4 col-sm-6">
                    <div class="card border-0 shadow-sm h-100">
                        <div class="card-body text-center p-3">
                            <div class="bg-success bg-opacity-10 rounded-circle p-2 d-inline-flex mb-2">
                                <i class="bi bi-receipt text-success fs-4"></i>
                            </div>
                            <h6 class="text-muted fw-bold small mb-1"><?= __('admin.total_orders') ?></h6>
                            <p class="fw-bold text-success mb-0"><?= $ordercount ?></p>
                        </div>
                    </div>
                </div>
                
                <div class="col-lg-2 col-md-4 col-sm-6">
                    <div class="card border-0 shadow-sm h-100">
                        <div class="card-body text-center p-3">
                            <div class="bg-warning bg-opacity-10 rounded-circle p-2 d-inline-flex mb-2">
                                <i class="bi bi-percent text-warning fs-4"></i>
                            </div>
                            <h6 class="text-muted fw-bold small mb-1"><?= __('admin.product_coupons') ?></h6>
                            <p class="fw-bold text-warning mb-0"><?= $coupon_count ?></p>
                        </div>
                    </div>
                </div>
                
                <div class="col-lg-2 col-md-4 col-sm-6">
                    <div class="card border-0 shadow-sm h-100">
                        <div class="card-body text-center p-3">
                            <div class="bg-danger bg-opacity-10 rounded-circle p-2 d-inline-flex mb-2">
                                <i class="bi bi-credit-card text-danger fs-4"></i>
                            </div>
                            <h6 class="text-muted fw-bold small mb-1"><?= __('admin.payment_gateways') ?></h6>
                            <p class="fw-bold text-danger mb-0"><?= $payment_gateway_count ?></p>
                        </div>
                    </div>
                </div>
            </div>

            <!-- Sales Funnels Quick Access -->
            <div class="row g-3 mb-4">
                <div class="col-12">
                    <div class="card border-0 shadow-sm" style="background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);">
                        <div class="card-body p-4">
                            <div class="row align-items-center">
                                <div class="col-lg-8">
                                    <div class="d-flex align-items-center mb-3">
                                        <div class="bg-white bg-opacity-25 rounded-circle p-3 me-3">
                                            <i class="fas fa-bolt text-white fs-2"></i>
                                        </div>
                                        <div>
                                            <h4 class="text-white fw-bold mb-1">
                                                <i class="fas fa-layer-group me-2"></i><?= __('admin.sales_funnels') ?>
                                            </h4>
                                            <p class="text-white mb-0 opacity-75">
                                                <?= __('admin.set_exclusive_funnel_prices') ?> & <?= __('admin.manage_sales_funnels') ?>
                                            </p>
                                        </div>
                                    </div>
                                    <p class="text-white mb-0 small opacity-90">
                                        <i class="fas fa-info-circle me-1"></i>
                                        Create sequential upsell flows with exclusive pricing to maximize revenue per customer
                                    </p>
                                </div>
                                <div class="col-lg-4 text-lg-end mt-3 mt-lg-0">
                                    <div class="d-flex flex-column gap-2">
                                        <a href="<?= base_url('admincontrol/sales_funnels') ?>" class="btn btn-light btn-lg fw-bold">
                                            <i class="fas fa-layer-group me-2"></i><?= __('admin.manage_sales_funnels') ?>
                                        </a>
                                        <a href="<?= base_url('admincontrol/funnel_pricing') ?>" class="btn btn-outline-light btn-lg">
                                            <i class="fas fa-tag me-2"></i><?= __('admin.funnel_pricing') ?>
                                        </a>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <!-- Additional Metrics -->
            <div class="row g-3 mb-4">
                <div class="col-lg-4 col-md-6">
                    <div class="card border-0 shadow-sm h-100">
                        <div class="card-body text-center p-3">
                            <div class="bg-info bg-opacity-10 rounded-circle p-2 d-inline-flex mb-2">
                                <i class="bi bi-file-earmark text-info fs-4"></i>
                            </div>
                            <h6 class="text-muted fw-bold small mb-1"><?= __('admin.total_forms') ?></h6>
                            <p class="fw-bold text-info mb-0"><?= $form_count ?></p>
                        </div>
                    </div>
                </div>
                
                <div class="col-lg-4 col-md-6">
                    <div class="card border-0 shadow-sm h-100">
                        <div class="card-body text-center p-3">
                            <div class="bg-secondary bg-opacity-10 rounded-circle p-2 d-inline-flex mb-2">
                                <i class="bi bi-ticket-perforated text-secondary fs-4"></i>
                            </div>
                            <h6 class="text-muted fw-bold small mb-1"><?= __('admin.form_coupons') ?></h6>
                            <p class="fw-bold text-secondary mb-0"><?= $form_coupon_count ?></p>
                        </div>
                    </div>
                </div>
                
                <div class="col-lg-4 col-md-6">
                    <div class="card border-0 shadow-sm h-100">
                        <div class="card-body text-center p-3">
                            <div class="bg-info bg-opacity-10 rounded-circle p-2 d-inline-flex mb-2">
                                <i class="bi bi-tags text-info fs-4"></i>
                            </div>
                            <h6 class="text-muted fw-bold small mb-1"><?= __('admin.categories') ?></h6>
                            <p class="fw-bold text-info mb-0"><?= $category_count ?></p>
                        </div>
                    </div>
                </div>
            </div>

            <!-- Charts Section -->
            <div class="row g-4 mb-4">
                <div class="col-lg-8">
                    <div class="card border-0 shadow-sm h-100">
                        <div class="card-header bg-light border-0">
                            <div class="d-flex justify-content-between align-items-center">
                                <h5 class="fw-bold mb-0">
                                    <i class="bi bi-graph-up me-2 text-primary"></i><?= __('admin.sales_analytics') ?>
                                </h5>
                                <div class="d-flex gap-2">
                                    <select class="form-select form-select-sm yearSelection" style="width: auto;">
                                        <?php foreach($years as $year): ?>
                                            <option value="<?= $year ?>" <?= $year == date('Y') ? 'selected' : '' ?>>
                                                <?= $year == 'All' ? __('admin.all_years') : $year ?>
                                            </option>
                                        <?php endforeach; ?>
                                    </select>
                                    <button class="btn btn-outline-primary btn-sm" onclick="renderStackedBarChart()">
                                        <i class="bi bi-arrow-clockwise me-1"></i><?= __('admin.refresh') ?>
                                    </button>
                                </div>
                            </div>
                        </div>
                        <div class="card-body p-4">
                            <div id="chartContainer" style="height: 350px;"></div>
                        </div>
                    </div>
                </div>

                <div class="col-lg-4">
                    <div class="card border-0 shadow-sm h-100">
                        <div class="card-header bg-light border-0">
                            <h5 class="fw-bold mb-0">
                                <i class="bi bi-globe me-2 text-info"></i><?= __('admin.clients_world_map') ?>
                            </h5>
                        </div>
                        <div class="card-body p-3">
                            <div class="world-map-users" style="height: 350px;"></div>
                        </div>
                    </div>
                </div>
            </div>

            <!-- Payment Gateways Section -->
            <div class="card border-0 shadow-sm mb-4">
                <div class="card-header bg-light border-0">
                    <h5 class="fw-bold mb-0">
                        <i class="bi bi-credit-card-2-front me-2 text-success"></i><?= __('admin.supported_payment_gateways') ?>
                    </h5>
                </div>
                <div class="card-body p-4">
                    <div class="row g-3">
                        <?php 
                        $payment_gateways = [
                            ['name' => 'PayPal', 'key' => 'paypal', 'image' => 'paypal.png'],
                            ['name' => 'Stripe', 'key' => 'stripe', 'image' => 'stripe.png'],
                            ['name' => 'Razorpay', 'key' => 'razorpay', 'image' => 'razorpay.png'],
                            ['name' => 'Paystack', 'key' => 'paystack', 'image' => 'paystack.png'],
                            ['name' => 'Flutterwave', 'key' => 'flutterwave', 'image' => 'flutterwave.png'],
                            ['name' => 'Paytm', 'key' => 'paytm', 'image' => 'paytm.png'],
                            ['name' => 'Skrill', 'key' => 'skrill', 'image' => 'skrill.png'],
                            ['name' => 'YooKassa', 'key' => 'yookassa', 'image' => 'yookassa.png'],
                            ['name' => 'Xendit', 'key' => 'xendit', 'image' => 'xendit.png'],
                            ['name' => 'Opay', 'key' => 'opay', 'image' => 'opay.png'],
                            ['name' => 'Bank Transfer', 'key' => 'bank_transfer', 'image' => 'bank-transfer.png'],
                            ['name' => 'Cash on Delivery', 'key' => 'cod', 'image' => 'cod.png']
                        ];
                        
                        foreach($payment_gateways as $gateway): ?>
                            <div class="col-xl-2 col-lg-3 col-md-4 col-sm-6">
                                <div class="card bg-light border-0 h-100 payment-gateway-card">
                                    <div class="card-body text-center p-3">
                                        <div class="mb-2">
                                            <img class="img-fluid" 
                                                 src="<?= base_url('assets/payment_gateway/' . $gateway['image']) ?>" 
                                                 alt="<?= $gateway['name'] ?>" 
                                                 style="max-height: 40px; max-width: 100px;">
                                        </div>
                                        <h6 class="text-muted fw-bold small mb-0"><?= $gateway['name'] ?></h6>
                                    </div>
                                </div>
                            </div>
                        <?php endforeach; ?>
                    </div>
                    
                    <div class="text-center mt-4">
                        <div class="alert alert-info border-0 bg-info bg-opacity-10">
                            <i class="bi bi-info-circle me-2"></i>
                            <strong><?= __('admin.payment_gateway_note') ?></strong>
                            <?= __('admin.payment_gateway_desc') ?>
                        </div>
                    </div>
                </div>
            </div>

            <!-- Recent Orders Table -->
            <div class="card border-0 shadow-sm">
                <div class="card-header bg-light border-0">
                    <div class="d-flex justify-content-between align-items-center">
                        <h5 class="fw-bold mb-0">
                            <i class="bi bi-list-ul me-2 text-dark"></i><?= __('admin.recent_orders') ?>
                        </h5>
                        <div class="d-flex gap-2">
                            <button class="btn btn-outline-primary btn-sm" onclick="getPage('<?= base_url("admincontrol/store_dashboard_order_list?page=1") ?>')">
                                <i class="bi bi-arrow-clockwise me-1"></i><?= __('admin.refresh') ?>
                            </button>
                            <a href="<?= base_url('admincontrol/listorders') ?>" class="btn btn-primary btn-sm">
                                <i class="bi bi-eye me-1"></i><?= __('admin.view_all_orders') ?>
                            </a>
                        </div>
                    </div>
                </div>
                <div class="card-body p-0">
                    <div class="table-responsive">
                        <table id="store-dashboard-orders" class="table table-hover align-middle mb-0">
                            <thead class="table-light">
                                <tr>
                                    <th class="fw-bold border-0 ps-4"><?= __('admin.order_id') ?></th>
                                    <th class="fw-bold border-0 text-end"><?= __('admin.amount') ?></th>
                                    <th class="fw-bold border-0 text-center"><?= __('admin.status') ?></th>
                                    <th class="fw-bold border-0"><?= __('admin.payment_method') ?></th>
                                    <th class="fw-bold border-0"><?= __('admin.customer_ip') ?></th>
                                    <th class="fw-bold border-0"><?= __('admin.transaction_id') ?></th>
                                    <th class="fw-bold border-0 text-center"><?= __('admin.date') ?></th>
                                    <th class="fw-bold border-0 text-center pe-4"><?= __('admin.actions') ?></th>
                                </tr>
                            </thead>
                            <tbody>
                                <tr>
                                    <td colspan="8" class="text-center border-0 py-5">
                                        <div class="d-flex justify-content-center align-items-center flex-column">
                                            <div class="spinner-border text-primary mb-3" role="status">
                                                <span class="visually-hidden"><?= __('admin.loading') ?>...</span>
                                            </div>
                                            <h6 class="text-muted mb-2 fw-bold"><?= __("admin.loading_orders_data") ?></h6>
                                            <p class="text-muted small mb-0"><?= __("admin.please_wait_moment") ?></p>
                                        </div>
                                    </td>
                                </tr>
                            </tbody>
                        </table>
                    </div>
                    
                    <!-- Pagination Section -->
                    <div class="card-footer bg-light border-0">
                        <div class="d-flex justify-content-between align-items-center">
                            <div class="pagination-summary text-muted small">
                                <!-- Pagination summary will be inserted here -->
                            </div>
                            <div class="pagination-td">
                                <!-- Pagination links will be inserted here -->
                            </div>
                        </div>
                    </div>
                    
                    <!-- Empty State (hidden by default) -->
                    <div class="text-center py-5 d-none empty-div">
                        <i class="bi bi-inbox text-muted" style="font-size: 3rem;"></i>
                        <h4 class="text-muted mt-3"><?= __('admin.no_orders_found') ?></h4>
                        <p class="text-muted"><?= __('admin.no_orders_desc') ?></p>
                        <a href="<?= base_url('store') ?>" target="_blank" class="btn btn-primary">
                            <i class="bi bi-shop me-2"></i><?= __('admin.visit_store') ?>
                        </a>
                    </div>
                </div>
            </div>

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

<script type="text/javascript">
// Modern orders loading function
function getPage(urlOrPage) {
    // Handle both URL and page number inputs
    let url;
    if (typeof urlOrPage === 'number' || (typeof urlOrPage === 'string' && /^\d+$/.test(urlOrPage))) {
        // It's a page number, construct the URL
        url = '<?= base_url("admincontrol/store_dashboard_order_list?page=") ?>' + urlOrPage;
    } else {
        // It's already a URL
        url = urlOrPage;
    }
    
    const loadingRow = `
        <tr>
            <td colspan="8" class="text-center border-0 py-5">
                <div class="d-flex justify-content-center align-items-center flex-column">
                    <div class="spinner-border text-primary mb-3" role="status">
                        <span class="visually-hidden"><?= __('admin.loading') ?>...</span>
                    </div>
                    <h6 class="text-muted mb-2 fw-bold"><?= __("admin.loading_orders_data") ?></h6>
                    <p class="text-muted small mb-0"><?= __("admin.please_wait_moment") ?></p>
                </div>
            </td>
        </tr>
    `;
    
    // Show loading state
    $("#store-dashboard-orders tbody").html(loadingRow);
    $("#store-dashboard-orders").show();
    $(".empty-div").addClass("d-none");

    $.ajax({
        url: url,
        type: 'POST',
        dataType: 'json',
        data: $("#filter-form").serialize(),
        success: function(json) {
            if (json['view']) {
                $("#store-dashboard-orders tbody").html(json['view']);
                $("#store-dashboard-orders").show();
                $(".empty-div").addClass("d-none");
                
                // Update pagination
                $(".pagination-td").html(json['pagination'] || '');
                
                // Update pagination summary
                if (json['pagination_summary']) {
                    $(".pagination-summary").html(json['pagination_summary']);
                } else if (json['total']) {
                    $(".pagination-summary").html('<?= __('admin.showing') ?> ' + json['total'] + ' <?= __('admin.orders') ?>');
                }
            } else {
                $(".empty-div").removeClass("d-none");
                $("#store-dashboard-orders").hide();
                $(".pagination-td").html('');
                $(".pagination-summary").html('');
            }
        },
        error: function() {
            const errorRow = `
                <tr>
                    <td colspan="8" class="text-center border-0 py-5">
                        <div class="text-muted">
                            <i class="bi bi-exclamation-triangle fs-1 mb-3"></i>
                            <h5><?= __('admin.error_loading_orders') ?></h5>
                            <p><?= __('admin.please_try_again') ?></p>
                            <button class="btn btn-primary" onclick="getPage('<?= base_url("admincontrol/store_dashboard_order_list?page=1") ?>')">
                                <i class="bi bi-arrow-clockwise me-2"></i><?= __('admin.retry') ?>
                            </button>
                        </div>
                    </td>
                </tr>
            `;
            $("#store-dashboard-orders tbody").html(errorRow);
        }
    });
}

// Initialize on page load
$(document).ready(function() {
    getPage('<?= base_url("admincontrol/store_dashboard_order_list?page=1") ?>');
    
    // Auto-refresh every 5 minutes
    setInterval(function() {
        getPage('<?= base_url("admincontrol/store_dashboard_order_list?page=1") ?>');
    }, 300000);
});
</script>

<script>
   function renderStackedBarChart(group) {
    var group = group ? group : 'month';
    var selectedyear = $('.yearSelection').val();
    $.ajax({
        type: 'POST',
        dataType: 'json',
        data: {renderChart: group,selectedyear:selectedyear},
        success: function (json) {
            loadChartData(json);
        },
    })
}
function toArray(myObj) {
    return $.map(myObj, function(value, index) {
        return [value];
    });
}

$( document ).ready(function() {
    renderStackedBarChart();
});

function loadChartData(json) {
    var saleHigh = toArray(json['series_new']['sale']);
    var orderHigh = toArray(json['series_new']['order']);
    var commissionsHigh = toArray(json['series_new']['commissions']);

    var months = [
    '',
    '<?= substr(__('admin.january'),0,3) ?>',
    '<?= substr(__('admin.february'),0,3) ?>',
    '<?= substr(__('admin.march'),0,3) ?>',
    '<?= substr(__('admin.april'),0,3) ?>',
    '<?= substr(__('admin.may'),0,3) ?>',
    '<?= substr(__('admin.june'),0,3) ?>',
    '<?= substr(__('admin.july'),0,3) ?>',
    '<?= substr(__('admin.august'),0,3) ?>',
    '<?= substr(__('admin.september'),0,3) ?>',
    '<?= substr(__('admin.october'),0,3) ?>',
    '<?= substr(__('admin.november'),0,3) ?>',
    '<?= substr(__('admin.december'),0,3) ?>',
    ];
    
    var dataPoints=[];
    for (var j = 1; j <=12; j++) {
        dataPoints.push({y:j,a:saleHigh[j],b:orderHigh[j],c:commissionsHigh[j]})
    }

    Morris.Line({
      element: 'chartContainer',
      lineColors: ['#fc836e', '#3d5674', '#3d5674'],
      data: dataPoints,
      parseTime: false,
      xkey: 'y',
      ykeys: ['a','b','c'],
      xLabelFormat: function (x) {
        var index = parseInt(x.src.y);
        return months[index];
      },
      labels: ['Sales (<?=$CurrencySymbol?>)', 'Orders','Commission (<?=$CurrencySymbol?>)'],
});
}
</script>

<script src="<?= base_url('assets/plugins/jmap/jquery-jvectormap-2.0.3.min.js'); ?>"></script>

<script type="text/javascript" src="<?= base_url('assets/plugins/jmap/jquery-jvectormap-world-mill.js') ?>">
</script>

<link rel="stylesheet" type="text/css" href="<?= base_url('assets/plugins/jmap/css.css') ?>">

<script type="text/javascript">
    function load_userworldmap(_data) {
        $('.world-map-users').html('<div class="map"><div id="world-map-users" class="map-content"></div></div>');
        var data = {};
        $.each(_data,function(i,j){
            data[j['code']] = j['total']; 
        })

        $('.world-map-users #world-map-users').vectorMap({
            map: 'world_mill',
            zoomButtons : 1,
            zoomOnScroll: false,
            panOnDrag: 1,
            backgroundColor: 'transparent',
            markerStyle: {
                initial: {
                    fill: '#ff00ff',
                    stroke: '#ffff00',
                    "stroke-width": 1,
                    r: 5
                },
            },
            onRegionTipShow: function(e, el, code, f){
                el.html(el.html() + (data[code] ? ': <small>' + data[code]+'</small>' : ''));
            },
            series: {
                regions: [{
                    values: data,
                    scale: ['#007BFF'],
                    normalizeFunction: 'polynomial'
                }]
            },
            regionStyle: {
                initial: {
                    fill: '#2e4765'
                },
                hover: {
                  "fill-opacity": 0.8
              }
          },
          markers:false,
      });
    };

    load_userworldmap(<?= json_encode($userworldmap) ?>);
</script>

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