<?php
session_start();               
include 'db.php';
$title = "Dashboard";

include 'include/header.php';  
?>

<?php
/* ================= DASHBOARD COUNTS ================= */

// Total Invoices
$totalInvoices = mysqli_fetch_assoc(
    mysqli_query($con,"SELECT COUNT(*) total FROM invoices")
)['total'];

// Total Clients
$totalClients = mysqli_fetch_assoc(
    mysqli_query($con,"SELECT COUNT(DISTINCT phone) total FROM invoices")
)['total'];

// Total Products Sold
$totalProducts = mysqli_fetch_assoc(
    mysqli_query($con,"SELECT COUNT(*) total FROM invoice_items")
)['total'];

// Total Business Amount
$totalAmount = mysqli_fetch_assoc(
    mysqli_query($con,"SELECT SUM(final_amount) total FROM invoices")
)['total'] ?? 0;
?>
<div id="page-wrapper">
    <div id="page-inner">

        <div class="row">

            <!-- TOTAL CLIENTS -->
            <div class="col-lg-3 col-sm-6 col-xs-12">
                <div class="panel panel-back noti-box">
                    <span class="icon-box bg-color-green set-icon">
                        <i class="fas fa-users"></i>
                    </span>
                    <div class="text-box">
                        <p>Total Clients</p>
                        <p><?= $totalClients ?></p>
                    </div>
                </div>
            </div>

            <!-- TOTAL INVOICES -->
            <div class="col-lg-3 col-sm-6 col-xs-12">
                <div class="panel panel-back noti-box">
                    <span class="icon-box bg-color-blue set-icon">
                        <i class="fas fa-file-invoice"></i>
                    </span>
                    <div class="text-box">
                        <p>Total Invoices</p>
                        <p><?= $totalInvoices ?></p>
                    </div>
                </div>
            </div>

            <!-- TOTAL PRODUCTS SOLD -->
            <div class="col-lg-3 col-sm-6 col-xs-12">
                <div class="panel panel-back noti-box">
                    <span class="icon-box bg-color-grov set-icon">
                        <i class="fas fa-box-open"></i>
                    </span>
                    <div class="text-box">
                        <p>Products Sold</p>
                        <p><?= $totalProducts ?></p>
                    </div>
                </div>
            </div>

            <!-- TOTAL AMOUNT -->
            <div class="col-lg-3 col-sm-6 col-xs-12">
                <div class="panel panel-back noti-box">
                    <span class="icon-box bg-color-red set-icon">
                        <i class="fas fa-rupee-sign"></i>
                    </span>
                    <div class="text-box">
                        <p>Total Business</p>
                        <p>₹ <?= number_format($totalAmount,2) ?></p>
                    </div>
                </div>
            </div>

        </div>

    </div>
</div>


<?php include 'include/footer.php'; ?>