<?php 
    $data = array();
    $data['title'] = $config->get('site_title');
    $data['links'] = [];
    $data['styles'] = ['/admin/public/assets/css/jstable.css'];
    $data['header']['scripts'] = [];
    $data['version'] = $config->get('version');
    $data['csrf_token'] = $csrf_generator->create();
    $data['sidebar_menu'] = $menu->get(array('position'=>'sidebar'));
    $data['sidebar_active_menu'] = $menu->getActiveMenuItemId(trim($_SERVER['REQUEST_URI']));
    $data['footer']['scripts'] = ['/admin/public/assets/js/jstable.min.js', $config->get('site_url').'admin/public/assets/js/moment.min.js', $config->get('site_url').'admin/public/assets/js/daterangepicker.js', '/admin/public/assets/js/echarts.min.js', '/admin/public/assets/js/pages/home.js'];
    $menu_items = $menu->get(array('position'=>'footer'));

    $data['cards']["1"] = $product->getTotalProducts();
    $data['cards']["2"] = $user->getTotalUsers();
    $data['cards']["3"] = $session->getOnlineVisitorsCount();

    $data['results']["1"] = 0;
    $data['results']["2"] = 0;
    $data['results']["3"] = 0;
    $data['results']["4"] = 0;

    foreach($menu_items as $item){
        if($item['col'] == "1"){
            $data['menu']['footer']["1"][] = $item;
        }
        if($item['col'] == "2"){
            $data['menu']['footer']["2"][] = $item;
        }
        if($item['col'] == "3"){
            $data['menu']['footer']["3"][] = $item;
        }
        if($item['col'] == "4"){
            $data['menu']['footer']["4"][] = $item;
        }
    }
    $data['breadcrumbs'] = array();
    $data['breadcrumbs'][] = array(
        'text' => "Pano",
        'href' => "/admin",
        'index' => 1
    );
?>
<?php include DIR_VIEWS."parts/header.php"; ?>
        <main class="content">
            <div class="container-fluid">
                <div class="row">
                    <div class="col-12 col-sm-6 col-xxl-3 d-flex">
                        <div class="card card-shadow illustration flex-fill">
                            <div class="card-body p-0 d-flex flex-fill">
                                <div class="row g-0 w-100">
                                    <div class="col-6">
                                        <div class="illustration-text p-3 m-1">
                                            <h4 class="illustration-text">Hoşgeldin, <?php echo $session->data["user_name"]; ?>!</h4>
                                            <p class="mb-0"><?php echo $config->get('site_title'); ?></p>
                                        </div>
                                    </div>
                                    <div class="col-6 align-self-end text-end">
                                        <img src="<?php echo $config->get('site_url'); ?>admin/public/assets/images/customer-support.png" alt="Customer Support" class="img-fluid illustration-img">
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="col-12 col-sm-6 col-xxl-3 d-flex">
                        <div class="card card-shadow flex-fill">
                            <div class="card-body py-4">
                                <div class="d-flex align-items-start">
                                    <div class="flex-grow-1">
                                        <h3 class="mb-2"><?php echo $data['cards']["1"]; ?></h3>
                                        <p class="mb-2">Toplam Ürün</p>
                                    </div>
                                    <div class="d-inline-block ms-3">
                                        <div class="stat">
                                            <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" fill="currentColor"  viewBox="0 0 16 16">
                                                <path fill-rule="evenodd" d="M10.854 7.146a.5.5 0 0 1 0 .708l-3 3a.5.5 0 0 1-.708 0l-1.5-1.5a.5.5 0 1 1 .708-.708L7.5 9.793l2.646-2.647a.5.5 0 0 1 .708 0z"></path>
                                                <path d="M4 1.5H3a2 2 0 0 0-2 2V14a2 2 0 0 0 2 2h10a2 2 0 0 0 2-2V3.5a2 2 0 0 0-2-2h-1v1h1a1 1 0 0 1 1 1V14a1 1 0 0 1-1 1H3a1 1 0 0 1-1-1V3.5a1 1 0 0 1 1-1h1v-1z"></path>
                                                <path d="M9.5 1a.5.5 0 0 1 .5.5v1a.5.5 0 0 1-.5.5h-3a.5.5 0 0 1-.5-.5v-1a.5.5 0 0 1 .5-.5h3zm-3-1A1.5 1.5 0 0 0 5 1.5v1A1.5 1.5 0 0 0 6.5 4h3A1.5 1.5 0 0 0 11 2.5v-1A1.5 1.5 0 0 0 9.5 0h-3z"></path>
                                            </svg>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="col-12 col-sm-6 col-xxl-3 d-flex">
                        <div class="card card-shadow flex-fill">
                            <div class="card-body py-4">
                                <div class="d-flex align-items-start">
                                    <div class="flex-grow-1">
                                        <h3 class="mb-2"><?php echo $data['cards']["2"]; ?></h3>
                                        <p class="mb-2">Toplam Kullanıcı</p>
                                    </div>
                                    <div class="d-inline-block ms-3">
                                        <div class="stat">
                                            <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" fill="currentColor" viewBox="0 0 16 16">
                                                <path d="M15 14s1 0 1-1-1-4-5-4-5 3-5 4 1 1 1 1h8zm-7.978-1A.261.261 0 0 1 7 12.996c.001-.264.167-1.03.76-1.72C8.312 10.629 9.282 10 11 10c1.717 0 2.687.63 3.24 1.276.593.69.758 1.457.76 1.72l-.008.002a.274.274 0 0 1-.014.002H7.022zM11 7a2 2 0 1 0 0-4 2 2 0 0 0 0 4zm3-2a3 3 0 1 1-6 0 3 3 0 0 1 6 0zM6.936 9.28a5.88 5.88 0 0 0-1.23-.247A7.35 7.35 0 0 0 5 9c-4 0-5 3-5 4 0 .667.333 1 1 1h4.216A2.238 2.238 0 0 1 5 13c0-1.01.377-2.042 1.09-2.904.243-.294.526-.569.846-.816zM4.92 10A5.493 5.493 0 0 0 4 13H1c0-.26.164-1.03.76-1.724.545-.636 1.492-1.256 3.16-1.275zM1.5 5.5a3 3 0 1 1 6 0 3 3 0 0 1-6 0zm3-2a2 2 0 1 0 0 4 2 2 0 0 0 0-4z"/>
                                            </svg>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="col-12 col-sm-6 col-xxl-3 d-flex">
                        <div class="card card-shadow flex-fill">
                            <div class="card-body py-4">
                                <div class="d-flex align-items-start">
                                    <div class="flex-grow-1">
                                        <h3 class="mb-2"><?php echo $data['cards']["3"]; ?></h3>
                                        <p class="mb-2">Online Ziyaretçi</p>
                                    </div>
                                    <div class="d-inline-block ms-3">
                                        <div class="stat">
                                            <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-person-check" viewBox="0 0 16 16">
                                                <path d="M6 8a3 3 0 1 0 0-6 3 3 0 0 0 0 6zm2-3a2 2 0 1 1-4 0 2 2 0 0 1 4 0zm4 8c0 1-1 1-1 1H1s-1 0-1-1 1-4 6-4 6 3 6 4zm-1-.004c-.001-.246-.154-.986-.832-1.664C9.516 10.68 8.289 10 6 10c-2.29 0-3.516.68-4.168 1.332-.678.678-.83 1.418-.832 1.664h10z"/>
                                                <path fill-rule="evenodd" d="M15.854 5.146a.5.5 0 0 1 0 .708l-3 3a.5.5 0 0 1-.708 0l-1.5-1.5a.5.5 0 0 1 .708-.708L12.5 7.793l2.646-2.647a.5.5 0 0 1 .708 0z"/>
                                            </svg>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="row">
                    <div class="col-lg-6 col-md-6">
                        <div class="card card-shadow">
                            <div class="card-header">
                                <h4 class="card-title">Satış Toplamları</h4>
                                <button class="btn btn-ghost-secondary btn-sm dropdown-toggle" id="total_sales_date">
                                    <i class="bi bi-calendar-week"></i>
                                    <span class="js-daterangepicker-predefined-preview ms-1"><?php echo date("m-d-Y", strtotime("-1 month"))." - ".date("m-d-Y"); ?></span>
                                </button>
                                <input type="hidden" id="total_sales_date_start" value="<?php echo date("Y-m-d", strtotime("-1 month")); ?>">
                                <input type="hidden" id="total_sales_date_end" value="<?php echo date("Y-m-d"); ?>">
                            </div>
                            <div class="card-body">
                                <div class="row">
                                    <div class="col-12">
                                        <div id="sales-chart" style="width: 100%; min-height: 450px"></div >
                                    </div>
                                    <div class="col-12 text-center mt-1">
                                        <span id="total-sale-price" class="fw-bold"></span>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="col-lg-6 col-md-6">
                        <div class="card card-shadow">
                            <div class="card-header">
                                <h4 class="card-title">En Son Siparişler</h4>
                            </div>
                            <div class="card-body p-0">
                                <div class="row">
                                    <div class="col-12">
                                        <div class="table-responsive" style="width: 100%; max-height:500px;">
                                            <table class="table table-striped-table-hover table-condensed">
                                                <thead>
                                                    <tr>
                                                        <th style="width:120px;">Sipariş No</th>
                                                        <th>Durum</th>
                                                        <th>Alıcı Adı</th>
                                                        <th>İşlem Tarihi</th>
                                                        <th>Tutar</th>
                                                        <th style="width:110px;"></th>
                                                    </tr>
                                                </thead>
                                                <tbody>
                                                    <?php
                                                        $orders = $order->getOrders(array('sort'=>'id', 'order'=>'DESC','offset'=>0, 'limit'=>10));
                                                        if(count($orders)>0) {
                                                            foreach($orders as $row) {
                                                                echo "<tr class=\"order-status-".$row['status']."\">
                                                                        <td class=\"text-center\">".$row['order_id']."</td>
                                                                        <td>".$row['status_text']."</td>
                                                                        <td>".$row['name']."</td>
                                                                        <td>".$row['date_added']."</td>
                                                                        <td class=\"text-end\">".$utils->moneyFormatter($row['order_total'])." TL</td>
                                                                        <td style=\"width: 110px;vertical-align: middle;\" class=\"text-center\">
                                                                            <a class=\"btn btn-sm btn-primary me-1\" href=\"/admin/orders/edit?id=".$row['id']."\"><i class=\"bi bi-pencil-square\"></i></a>
                                                                        </td>
                                                                    </tr>";
                                                            }
                                                        }else{
                                                            echo "<tr class=\"text-center\"><td colspan=\"6\">Hiçbir kayıt  bulunamadı!</td></tr>";
                                                        }
                                                    ?>
                                                </tbody>
                                            </table>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="row">
                    <div class="col-lg-6 col-md-6">
                        <div class="card card-shadow">
                            <div class="card-header">
                                <h4 class="card-title">Günlük Siparişler</h4>
                            </div>
                            <div class="card-body p-0">
                                <div class="row">
                                    <div class="col-12">
                                        <div id="daily-order-counts" style="width: 100%; min-height: 450px"></div >
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="col-lg-6 col-md-6">
                        <div class="card card-shadow">
                            <div class="card-header">
                                <h4 class="card-title">Aylık Sipariş Toplamları</h4>
                            </div>
                            <div class="card-body">
                                <div class="row">
                                    <div class="col-12">
                                        <div id="monthy-orders-chart" style="width: 100%; min-height: 450px"></div >
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="row">
                    <div class="col-xl-6">
                        <div class="card card-shadow">
                            <div class="card-header">
                                <h4 class="card-title">Kritik Stoktaki Ürünler</h4>
                            </div>
                            <div class="card-body p-0">
                                <div class="row">
                                    <div class="col-12">
                                        <div class="table-responsive" style="width: 100%; max-height:400px;">
                                            <table class="table table-striped-table-hover table-condensed" id="stocks-table">
                                                <thead>
                                                    <tr>
                                                        <th>SKU</th>
                                                        <th>Adı</th>
                                                        <th>Stok</th>
                                                        <th>Kritik Seviye</th>
                                                        <th style="width:90px;"></th>
                                                    </tr>
                                                </thead>
                                                <tbody>
                                                    <?php
                                                        $critical_stocks = $product->getCriticalStocs();
                                                        if(count($critical_stocks)>0) {
                                                            foreach($critical_stocks as $row) {
                                                                echo "<tr>
                                                                        <td class=\"text-center\" style=\"white-space: normal;\">".$row['sku']."</td>
                                                                        <td style=\"white-space: normal;\">".$row['title']."</td>
                                                                        <td>".$row['stock_level']."</td>
                                                                        <td>".$row['stock_critical_level']."</td>
                                                                        <td style=\"width: 90px;vertical-align: middle;\" class=\"text-center\">
                                                                            <a class=\"btn btn-sm btn-primary me-1\" href=\"/admin/product/edit?id=".$row['id']."\"><i class=\"bi bi-pencil-square\"></i></a>
                                                                        </td>
                                                                    </tr>";
                                                            }
                                                        }else{
                                                            echo "<tr class=\"text-center\"><td colspan=\"6\">Hiçbir kayıt  bulunamadı!</td></tr>";
                                                        }
                                                    ?>
                                                </tbody>
                                            </table>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="col-xl-6">
                        <div class="card card-shadow">
                            <div class="card-header">
                                <h4 class="card-title">Ürün Görüntülemeleri</h4>
                                <div class="btn-group nav nav-tabs" role="group" aria-label="Basic example">
                                    <button type="button" class="btn btn-primary btn-sm active" data-bs-toggle="tab" data-bs-target="#hit-today" role="tab" aria-selected="true">Bugün</button>
                                    <button type="button" class="btn btn-primary btn-sm" data-bs-toggle="tab" data-bs-target="#hit-anytime" role="tab" aria-selected="false">Tüm Zamanlar</button>
                                </div>
                            </div>
                            <div class="card-body p-0" style="height: 448px;overflow: scroll;">
                                <div class="tab-content p-0" id="myTabContent">
                                    <div class="tab-pane" id="hit-anytime" role="tabpanel">
                                        <div class="table-responsive">
                                            <table class="table table-bordered table-hover table-condensed table-striped">
                                                <thead>
                                                    <tr>
                                                        <th>Adı</th>
                                                        <th style="width:50px; text-align:center;">Hit</th>
                                                    </tr>
                                                </thead>
                                                <tbody class="text-left products">
                                                    <?php
                                                        $hit_anytime = $product->getHitCounts("", 10);
                                                        if(count($hit_anytime)>0) {
                                                            foreach($hit_anytime as $row) {
                                                                echo "<tr>
                                                                        <td><a href=\"/admin/product/edit?id=".$row['id']."\" target=\"_blank\">".$row['title']."</a></td>
                                                                        <td>".$row['visit']."</td>
                                                                    </tr>";
                                                            }
                                                        }else{
                                                            echo "<tr class=\"text-center\"><td colspan=\"3\">Hiçbir kayıt  bulunamadı!</td></tr>";
                                                        }
                                                    ?>
                                                </tbody>
                                            </table>
                                        </div>
                                    </div>
                                    <div class="tab-pane active show" id="hit-today" role="tabpanel">
                                        <div class="table-responsive">
                                            <table class="table table-bordered table-hover table-condensed table-striped table-cf">
                                                <thead>
                                                    <tr>
                                                        <th>Adı</th>
                                                        <th style="width:50px; text-align:center;">Hit</th>
                                                    </tr>
                                                </thead>
                                                <tbody class="text-left products">
                                                    <?php
                                                        $hit_today = $product->getHitCounts(date('Y-m-d'), 10);
                                                        if(count($hit_today)>0) {
                                                            foreach($hit_today as $row) {
                                                                echo "<tr>
                                                                        <td><a href=\"/admin/product/edit?id=".$row['id']."\" target=\"_blank\">".$row['title']."</a></td>
                                                                        <td>".$row['visit']."</td>
                                                                    </tr>";
                                                            }
                                                        }else{
                                                            echo "<tr class=\"text-center\"><td colspan=\"3\">Hiçbir kayıt  bulunamadı!</td></tr>";
                                                        }
                                                    ?>
                                                </tbody>
                                            </table>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </main>
<?php include DIR_VIEWS."parts/footer.php"; ?>
<script>
    var table = new JSTable("#stocks-table", {
        serverSide : false,
        perPage : 8,
        labels: {
            placeholder: "Ara...",
            perPage: "sayfa başına kayıt",
            noRows: "Hiçbir kayıt bulunamadı!",
            info: "{rows} kayıttan {start} - {end} arası görüntüleniyor",
            loading: "Yükleniyor...",
            infoFiltered: "{rows} kayıttan {start} - {end} arası görüntüleniyor ({rowsTotal} kayıt arasından filtrelendi)"
        },
        layout: {
            top: "{search}",
            bottom: "{info}{pager}"
        }
    });
</script>