<?php
session_start();
require_once '../config.php';
function currency($value) { return number_format($value, 0, ".", "."); }
function rdate($f,$x = '-0 days') { return date($f, strtotime($x, strtotime(date('Y-m-d H:i:s')))); }

load('middleware', 'user');
if ($_SESSION['user']['level'] !== "Admin") {
    header('location:' . base_url('id'));
    
}            

$total_data_us = mysqli_num_rows($db->query("SELECT * FROM users"));
$total_data_success = mysqli_num_rows($db->query("SELECT * FROM pembelian_guest WHERE status = 'Success'"));
$total_data_pending = mysqli_num_rows($db->query("SELECT * FROM pembelian_guest WHERE status = 'Pending'"));
$total_data_canceled = mysqli_num_rows($db->query("SELECT * FROM pembelian_guest WHERE status = 'Canceled'"));

$total_data_depo = $db->query("SELECT SUM(amount) AS total FROM deposit WHERE status='Success'")->fetch_assoc()['total'];

$total_data_pusat = $db->query("SELECT SUM(harga) AS total FROM pembelian_guest WHERE status = 'Success'")->fetch_assoc()['total'];
$qD = "SELECT device FROM agent";
$sD = $db->query($qD)->num_rows;

$qD2 = "SELECT browser FROM agent";
$sD2 = $db->query($qD)->num_rows;

$data_mobile = mysqli_num_rows($db->query("SELECT * FROM agent WHERE device = 'Mobile'"));
$data_desktop = mysqli_num_rows($db->query("SELECT * FROM agent WHERE device = 'Desktop'"));
$data_tablet = mysqli_num_rows($db->query("SELECT * FROM agent WHERE device = 'Tablet'"));
$data_unknown = mysqli_num_rows($db->query("SELECT * FROM agent WHERE device NOT IN ('Mobile','Desktop','Tablet')"));
$data_chrome = mysqli_num_rows($db->query("SELECT * FROM agent WHERE browser = 'Chrome'"));
$data_unknown_prog = mysqli_num_rows($db->query("SELECT * FROM agent WHERE browser NOT IN ('Chrome')"));

include_once '../layouts/header-admin.php'; ?>
<div class="row justify-content-center">
    <div class="col-md-6 col-12 mt-2 mb-3">
        <div class="card bg-dark shadow">
            <div class="card-body">
                <div>
                    <h2 class="font-weight-bolder mb-0"><?= number_format($total_data_canceled,0,',','.'); ?></h2>
                    <p class="card-text">Canceled</p>
                </div>
            </div>
        </div>
    </div>
    <div class="col-md-6 col-12 mt-2 mb-3">
        <div class="card bg-dark shadow">
            <div class="card-body">
                <div>
                    <h2 class="font-weight-bolder mb-0"><?= number_format($total_data_success,0,',','.'); ?></h2>
                    <p class="card-text">Success</p>
                </div>
            </div>
        </div>
    </div>
    <div class="col-md-6 col-12 mt-2 mb-3">
        <div class="card bg-dark shadow">
            <div class="card-body">
                <div>
                    <h2 class="font-weight-bolder mb-0"><?= number_format($total_data_pending,0,',','.'); ?></h2>
                    <p class="card-text">Pending</p>
                </div>
            </div>
        </div>
    </div>
    <div class="col-md-6 col-12 mb-3">
        <div class="card bg-dark shadow">
            <div class="card-body">
                <div>
                    <h2 class="font-weight-bolder mb-0"><?= number_format($total_data_pusat,0,',','.'); ?></h2>
                    <p class="card-text">Total Transaksi Pusat</p>
                </div>
            </div>
        </div>
    </div>
    <div class="col-md-6 col-12 mb-3">
        <div class="card bg-dark shadow">
            <div class="card-body">
                <div>
                    <h2 class="font-weight-bolder mb-0"><?= number_format($total_data_depo,0,',','.'); ?></h2>
                    <p class="card-text">Total Saldo Deposit</p>
                </div>
            </div>
        </div>
    </div>
    <div class="col-md-12 col-12 mb-3">
        <div class="card bg-dark shadow">
            <div class="card-header">
                <h4 class="card-title">Browser Statistics</h4>
            </div>
            <div class="card-content">
                <div class="card-body">
                                        <div class="d-flex justify-content-between mb-25">
                        <div class="browser-info">
                            <p class="mb-25">Chrome</p>
                        </div>
                        <div class="stastics-info text-right">
                            <span><?= round(($data_chrome/$sD2)*100,2); ?>%</span>
                        </div>
                    </div>
                    <div class="progress progress-bar-primary mb-2">
                        <div class="progress-bar" role="progressbar" aria-valuenow="<?= round(($data_chrome/$sD2)*100,2); ?>" aria-valuemin="<?= round(($data_chrome/$sD2)*100,2); ?>" aria-valuemax="100" style="width:<?= round(($data_chrome/$sD2)*100,2); ?>%"></div>
                    </div>
                                        <div class="d-flex justify-content-between mb-25">
                        <div class="browser-info">
                            <p class="mb-25">Unknown</p>
                        </div>
                        <div class="stastics-info text-right">
                            <span><?= round(($data_unknown_prog/$sD)*100, 2); ?>%</span>
                        </div>
                    </div>
                    <div class="progress progress-bar-primary mb-2">
                        <div class="progress-bar" role="progressbar" aria-valuenow="<?= round(($data_unknown_prog/$sD2)*100, 2); ?>" aria-valuemin="<?= round(($data_unknown_prog/$sD2)*100, 2); ?>" aria-valuemax="100" style="width:<?= round(($data_unknown_prog/$sD2)*100, 2); ?>%"></div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="col-md-12 col-12">
        <div class="card bg-dark shadow">
            <div class="card-header">
                <h4 class="card-title">Device</h4>
            </div>
            <div class="card-content">
                <div class="card-body">
                <div class="d-flex justify-content-between mb-25">
                        <div class="browser-info">
                            <p class="mb-25">Mobile</p>
                        </div>
                        <div class="stastics-info text-right">
                            <span><?= round(($data_mobile/$sD)*100, 2); ?>%</span>
                        </div>
                    </div>
                    <div class="progress progress-bar-primary mb-2">
                        <div class="progress-bar" role="progressbar" aria-valuenow="<?= round(($data_mobile/$sD)*100, 2); ?>" aria-valuemin="<?= round(($data_mobile/$sD)*100, 2); ?>" aria-valuemax="100" style="width:<?= round(($data_mobile/$sD)*100, 2); ?>%"></div>
                    </div>
                    <div class="d-flex justify-content-between mb-25">
                        <div class="browser-info">
                            <p class="mb-25">Desktop</p>
                        </div>
                        <div class="stastics-info text-right">
                            <span><?= round(($data_desktop/$sD)*100, 2); ?>%</span>
                        </div>
                    </div>
                    <div class="progress progress-bar-primary mb-2">
                        <div class="progress-bar" role="progressbar" aria-valuenow="<?= round(($data_desktop/$sD)*100, 2); ?>" aria-valuemin="<?= round(($data_desktop/$sD)*100, 2); ?>" aria-valuemax="100" style="width:<?= round(($data_desktop/$sD)*100, 2); ?>%"></div>
                    </div>
                    <div class="d-flex justify-content-between mb-25">
                        <div class="browser-info">
                            <p class="mb-25">Tablet</p>
                        </div>
                        <div class="stastics-info text-right">
                            <span><?= round(($data_tablet/$sD)*100, 2); ?>%</span>
                        </div>
                    </div>
                    <div class="progress progress-bar-primary mb-2">
                        <div class="progress-bar" role="progressbar" aria-valuenow="<?= round(($data_tablet/$sD)*100, 2); ?>" aria-valuemin="<?= round(($data_tablet/$sD)*100, 2); ?>" aria-valuemax="100" style="width:<?= round(($data_tablet/$sD)*100, 2); ?>%"></div>
                    </div>
                    <div class="d-flex justify-content-between mb-25">
                        <div class="browser-info">
                            <p class="mb-25">Unknown</p>
                        </div>
                        <div class="stastics-info text-right">
                            <span><?= round(($data_unknown/$sD)*100, 2); ?>%</span>
                        </div>
                    </div>
                    <div class="progress progress-bar-primary mb-2">
                        <div class="progress-bar" role="progressbar" aria-valuenow="<?= round(($data_unknown/$sD)*100, 2); ?>" aria-valuemin="<?= round(($data_unknown/$sD)*100, 2); ?>" aria-valuemax="100" style="width:<?= round(($data_unknown/$sD)*100, 2); ?>%"></div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<br>
<br>
<?php include_once '../layouts/footer-admin.php'; ?>
<script type="text/javascript">
$(window).on("load", function () {
    new ApexCharts(document.querySelector("#session-by-device"), {
        chart: { type: "donut", height: 315, toolbar: { show: !1 } },
        dataLabels: { enabled: !1 },
        series: [
            <?= round(($data_desktop/$sD)*100, 2); ?>,
            <?= round(($data_mobile/$sD)*100, 2); ?>,
            <?= round(($data_tablet/$sD)*100, 2); ?>,
            <?= round(($data_unknown_prog/$sD)*100, 2); ?>       
             ],
        legend: { show: !1 },
        comparedResult: [2, -3, 8],
        labels: ["Desktop", "Mobile", "Tablet", "Unknown"],
        stroke: { width: 0 },
        colors: ["#7367F0", "#FF9F43", "#EA5455", "#28313B"],
        fill: { type: "gradient", gradient: { gradientToColors: ["#9c8cfc", "#FFC085", "#f29292", "#485461"] } },
    }).render();
});
</script>