<!DOCTYPE html>
<html lang="en" data-bs-theme="dark">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Dashboard</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha2/dist/css/bootstrap.min.css">
    <script src="https://kit.fontawesome.com/ae360af17e.js" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
    <link rel="stylesheet" href="css/style.css">
    <link rel="icon" href="image/logo.png" type="image/png">

    <link rel="stylesheet" href="extensions/fixed-columns/bootstrap-table-fixed-columns.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css">
    <script src="extensions/fixed-columns/bootstrap-table-fixed-columns.js"></script>
</head>
<style> 
    .card-img-start{
        height: 80px;
        width: 80px;
        margin: 20px;
        
    }


    .card:hover {
            transform: scale(1.05);
            box-shadow: 0 8px 16px rgba(0, 0, 0, 0.2);
        }
</style>
<body>
    <?php
    session_start();
    if (isset($_SESSION['browser_id'])) {
        $browserId = $_SESSION['browser_id'];
    } else {
        $browserId = generateUniqueBrowserId();
        $_SESSION['browser_id'] = $browserId;
    }

    include "database/database.php";
    include "database/data.php";
    include "login_check.php";
    include "cal_customer_pay.php";
    include "cal_buy_shop.php";
    include "cal_pro_remain.php";
    
    ?>

    <div class="wrapper">
        <?php include "sidebar.php"; ?>

        <div class="main">
            <nav class="navbar navbar-expand px-3 border-bottom">
                <button class="btn" id="sidebar-toggle" type="button">
                    <span class="navbar-toggler-icon"></span>
                </button>
                <div class="navbar-collapse navbar">
                    <ul class="navbar-nav">
                        <div class="mb-3" style="margin-right:30px">
                            <h4 style="font-size:25px">Dashboard</h4>
                        </div>
                        <li class="nav-item dropdown">
                            <a href="#" data-bs-toggle="dropdown" class="nav-icon pe-md-0">
                                <img src="image/logo.png" class="avatar img-fluid rounded" alt="">
                            </a>
                            <?php include "logo_profile.php"; ?>
                        </li>
                    </ul>
                </div>
            </nav>

            <main class="content px-3 py-2">
                <!-- Card Row -->
                <div class="container-fluid" style="margin-top:20px">
                    <div class="row">
                        <!-- Card 1 -->
                        <div class="col-md-4 mb-3">
                            <div class="card d-flex flex-row">
                                <img src="image/expense.png" class="card-img-start" alt="Image 1">
                                <div class="card-body">
                                    <h5 class="card-title">အသုံးစရိတ်</h5>
                                    <?php 
// Initialize variables
$total_kyat_expense = 0;
$total_baht_expense = 0;
$total_yuan_expense = 0;
$total_dollar_expense = 0;

// Query for Kyat expenses
$kyat_expense = "SELECT SUM(s.total) AS total_expense_kyat, s.banking, b.currency
                FROM ".$expense." s
                JOIN ".$banking." b ON s.banking = b.type
                WHERE b.currency = 'kyat'
                GROUP BY s.banking, b.currency";

$result_kyat_expense = mysqli_query($connection, $kyat_expense);

if ($result_kyat_expense) {
    $row_kyat_expense = mysqli_fetch_assoc($result_kyat_expense);
    $total_kyat_expense = $row_kyat_expense ? $row_kyat_expense['total_expense_kyat'] : 0;
} else {
    echo "Error: " . mysqli_error($connection);
}

// Query for Baht expenses
$baht_expense = "SELECT SUM(s.total) AS total_expense_baht, s.banking, b.currency
                FROM ".$expense." s
                JOIN ".$banking." b ON s.banking = b.type
                WHERE b.currency = 'baht'
                GROUP BY s.banking, b.currency";

$result_baht_expense = mysqli_query($connection, $baht_expense);

if ($result_baht_expense) {
    $row_baht_expense = mysqli_fetch_assoc($result_baht_expense);
    $total_baht_expense = $row_baht_expense ? $row_baht_expense['total_expense_baht'] : 0;
} else {
    echo "Error: " . mysqli_error($connection);
}

// Query for Yuan expenses
$yuan_expense = "SELECT SUM(s.total) AS total_expense_yuan, s.banking, b.currency
                FROM ".$expense." s
                JOIN ".$banking." b ON s.banking = b.type
                WHERE b.currency = 'yuan'
                GROUP BY s.banking, b.currency";

$result_yuan_expense = mysqli_query($connection, $yuan_expense);

if ($result_yuan_expense) {
    $row_yuan_expense = mysqli_fetch_assoc($result_yuan_expense);
    $total_yuan_expense = $row_yuan_expense ? $row_yuan_expense['total_expense_yuan'] : 0;
} else {
    echo "Error: " . mysqli_error($connection);
}

// Query for Dollar expenses
$dollar_expense = "SELECT SUM(s.total) AS total_expense_dollar, s.banking, b.currency
                FROM ".$expense." s
                JOIN ".$banking." b ON s.banking = b.type
                WHERE b.currency = 'dollar'
                GROUP BY s.banking, b.currency";

$result_dollar_expense = mysqli_query($connection, $dollar_expense);

if ($result_dollar_expense) {
    $row_dollar_expense = mysqli_fetch_assoc($result_dollar_expense);
    $total_dollar_expense = $row_dollar_expense ? $row_dollar_expense['total_expense_dollar'] : 0;
} else {
    echo "Error: " . mysqli_error($connection);
}

// Display the results
echo '<p class="card-text">
        Kyat - <b style="color:white">'.number_format($total_kyat_expense,0).'</b> Ks <br>
        Baht - <b style="color:white">'.number_format($total_baht_expense,0).'</b> B<br>
        Yuan - <b style="color:white">'.number_format($total_yuan_expense,0).'</b> Y<br>
        Dollar - <b style="color:white">'.number_format($total_dollar_expense,2).'</b> $
      </p>';
?>


                                   
                                </div>
                            </div>
                        </div>
                        
                        <!-- Card 2 -->
                        <div class="col-md-4 mb-3">
                            <div class="card d-flex flex-row">
                                <img src="image/debt.png" class="card-img-start" alt="Image 2">
                                <div class="card-body">
                                    <h5 class="card-title">ရရန်အကြွေး</h5>
                                    <?php 

                                    $total_kyat_debt = 0;
                                    $total_baht_debt= 0;
                                    $total_yuan_debt = 0;
                                    $total_dollar_debt= 0;

                                    $kyat_debt = "SELECT SUM(remain_debt) AS total_debt_kyat,currency
                                    FROM ".$customer."
                                    WHERE currency = 'kyat'";
                                    $result_kyat_debt = mysqli_query($connection, $kyat_debt);
                                    if ($result_kyat_debt) {
                                    $row_kyat_debt = mysqli_fetch_assoc($result_kyat_debt);
                                    $total_kyat_debt = $row_kyat_debt['total_debt_kyat'];
                                    }

                                    $baht_debt = "SELECT SUM(remain_debt) AS total_debt_baht,currency
                                    FROM ".$customer."
                                    WHERE currency = 'baht'";
                                    $result_baht_debt = mysqli_query($connection, $baht_debt);
                                    if ($result_baht_debt) {
                                    $row_baht_debt = mysqli_fetch_assoc($result_baht_debt);
                                    $total_baht_debt = $row_baht_debt['total_debt_baht'];
                                    }

                                    $yuan_debt = "SELECT SUM(remain_debt) AS total_debt_yuan,currency
                                    FROM ".$customer."
                                    WHERE currency = 'yuan'";
                                    $result_yuan_debt = mysqli_query($connection, $yuan_debt);
                                    if ($result_yuan_debt) {
                                    $row_yuan_debt = mysqli_fetch_assoc($result_yuan_debt);
                                    $total_yuan_debt = $row_yuan_debt['total_debt_yuan'];
                                    }

                                    $dollar_debt = "SELECT SUM(remain_debt) AS total_debt_dollar,currency
                                    FROM ".$customer."
                                    WHERE currency = 'dollar'";
                                    $result_dollar_debt = mysqli_query($connection, $dollar_debt);
                                    if ($result_dollar_debt) {
                                    $row_dollar_debt = mysqli_fetch_assoc($result_dollar_debt);
                                    $total_dollar_debt = $row_dollar_debt['total_debt_dollar'];
                                    }

                                    echo '<p class="card-text">
                                            Kyat - <b style="color:white">'.number_format($total_kyat_debt,0).'</b> Ks <br>
                                            Baht - <b style="color:white">'.number_format($total_baht_debt,0).'</b> B<br>
                                            Yuan - <b style="color:white">'.number_format($total_yuan_debt,0).'</b> Y<br>
                                            Dollar - <b style="color:white">'.number_format($total_dollar_debt,2).'</b> $
                                        </p>';

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

                        <!-- Card 3 -->
                        <div class="col-md-4 mb-3">
                            <div class="card d-flex flex-row">
                                <img src="image/pay_debt.png" class="card-img-start" alt="Image 3">
                                <div class="card-body">
                                    <h5 class="card-title">ပေးရန်အကြွေး</h5>
                                    <?php 
                                    $total_kyat_get = 0;
                                    $total_baht_get= 0;
                                    $total_yuan_get = 0;
                                    $total_dollar_get= 0;
                                    
                                    $kyat_get = "SELECT SUM(payable) AS total_get_kyat,currency
                                    FROM ".$supplier."
                                    WHERE currency = 'kyat'";
                                    $result_kyat_get = mysqli_query($connection, $kyat_get);
                                    if ($result_kyat_get) {
                                    $row_kyat_get = mysqli_fetch_assoc($result_kyat_get);
                                    $total_kyat_get = $row_kyat_get['total_get_kyat'];
                                    }
                                    
                                    $baht_get = "SELECT SUM(payable) AS total_get_baht,currency
                                    FROM ".$supplier."
                                    WHERE currency = 'baht'";
                                    $result_baht_get = mysqli_query($connection, $baht_get);
                                    if ($result_baht_get) {
                                    $row_baht_get = mysqli_fetch_assoc($result_baht_get);
                                    $total_baht_get = $row_baht_get['total_get_baht'];
                                    }
                                    
                                    $yuan_get = "SELECT SUM(payable) AS total_get_yuan,currency
                                    FROM ".$supplier."
                                    WHERE currency = 'yuan'";
                                    $result_yuan_get = mysqli_query($connection, $yuan_get);
                                    if ($result_yuan_get) {
                                    $row_yuan_get = mysqli_fetch_assoc($result_yuan_get);
                                    $total_yuan_get = $row_yuan_get['total_get_yuan'];
                                    }
                                    
                                    $dollar_get = "SELECT SUM(payable) AS total_get_dollar,currency
                                    FROM ".$supplier."
                                    WHERE currency = 'dollar'";
                                    $result_dollar_get = mysqli_query($connection, $dollar_get);
                                    if ($result_dollar_get) {
                                    $row_dollar_get = mysqli_fetch_assoc($result_dollar_get);
                                    $total_dollar_get = $row_dollar_get['total_get_dollar'];
                                    }
                                    
                                    echo '<p class="card-text">
                                            Kyat - <b style="color:white">'.number_format($total_kyat_get,0).'</b> Ks <br>
                                            Baht - <b style="color:white">'.number_format($total_baht_get,0).'</b> B<br>
                                            Yuan - <b style="color:white">'.number_format($total_yuan_get,0).'</b> Y<br>
                                            Dollar - <b style="color:white">'.number_format($total_dollar_get,2).'</b> $
                                        </p>';
                                    
                                    ?>
                                    </div>
                            </div>
                        </div>
                    </div>
                </div>

                <!-- Circle Chart -->
                <div class="container-fluid"> 
                        <div  style="margin: 0; display: flex; flex-wrap: wrap;">
                     

                            <div class="col-sm-4" style="border-radius: 10px;">
                                <iframe src="chart_day.php" style="width: 600px; height: 500px;" ></iframe>
                            </div>

                            <div class="col-sm-4" style="border-radius: 10px;">
                                <iframe src="chart_month.php" style="width: 600px; height: 500px;" ></iframe>
                            </div>

                            <div class="col-sm-4" style="border-radius: 10px;">
                                <iframe src="chart_year.php" style="width: 600px; height: 500px;" ></iframe>
                            </div>

                        </div>
                    </div>    

                    <!-- chart_catagories Sell Chart -->
                    <div class="container-fluid"> 
                        <h3> အမျိုးအစားလိုက် အရောင်းစာရင်းပြဇယား</h3>
                        <iframe src="chart_catagories.php" style="width: 100%; height: 550px;" ></iframe>
                    </div>  

                    <!-- Horizontal Sell Chart -->
                    <div class="container-fluid"> 
                        <h3> ပစ္စည်းအလိုက် အရောင်းစာရင်းပြဇယား</h3>
                        <iframe src="chart_product.php" style="width: 100%; height: 550px;" ></iframe>
                    </div>  

                    <!-- Horizontal Sell Chart -->
                    <div class="container-fluid"> 
                        <h3> အရောင်းစာရင်းပြဇယား</h3>
                        <iframe src="iframe_hor_sell_chart.php" style="width: 100%; height: 300px;" ></iframe>
                    </div>  

                     <!-- Horizontal Expense Chart -->
                     <div class="container-fluid"> 
                        <h3>အသုံးစာရင်းပြဇယား</h3>
                        <iframe src="iframe_hor_expense_chart.php" style="width: 100%; height: 300px;" ></iframe>
                    </div> 

                <!-- Other Content -->

            </main>

            <a href="#" class="theme-toggle">
                <i class="fa-regular fa-moon"></i>
                <i class="fa-regular fa-sun"></i>
            </a>
            <footer class="footer">
                <div class="container-fluid">
                    <div class="row text-muted">
                        <div class="col-6 text-start">
                            <p class="mb-0">
                                <a href="#" class="text-muted">
                                    <?php include "version.php"; ?>
                                </a>
                            </p>
                        </div>
                        <div class="col-6 text-end">
                            <ul class="list-inline">
                                <li class="list-inline-item"><a href="#" class="text-muted">Contact</a></li>
                                <li class="list-inline-item"><a href="#" class="text-muted">About Us</a></li>
                                <li class="list-inline-item"><a href="#" class="text-muted">Terms</a></li>
                                <li class="list-inline-item"><a href="#" class="text-muted">Booking</a></li>
                            </ul>
                        </div>
                    </div>
                </div>
            </footer>
        </div>
    </div>

    <?php
    function generateUniqueBrowserId()
    {
        $randomString = bin2hex(random_bytes(16));
        $timestamp = time();
        return $randomString . $timestamp;
    }
    ?>

    <script src="js/script.js"></script>
    <link rel="stylesheet" href="extensions/sticky-header/bootstrap-table-sticky-header.css">
    <link rel="stylesheet" href="extensions/fixed-columns/bootstrap-table-fixed-columns.css">
    <script src="extensions/sticky-header/bootstrap-table-sticky-header.js"></script>
    <script src="extensions/fixed-columns/bootstrap-table-fixed-columns.js"></script>
</body>

</html>
