<!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>ရက်ချုပ်</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>
        .image-container {
            width: 200px; /* Adjust this width as needed */
            height: 200px; /* Adjust this height as needed */
            overflow: hidden;
            position: relative;
            border: 1px solid #ccc; /* Add a border for the frame effect */
        }
        .image-container img {
            width: 100%;
            height: 100%;
            object-fit: contain; /* Ensure the entire image is visible */
        }
    </style>



<?php

session_start();
                                
if (isset($_SESSION['browser_id'])) {
    $browserId = $_SESSION['browser_id'];
} else {
    // Generate a new browser ID
    $browserId = generateUniqueBrowserId();

    // Store the browser ID in the session for future use
    $_SESSION['browser_id'] = $browserId;
}


include "database/database.php";
include "database/data.php";
include "login_check.php";
?>


    <style>
    .select-container {
        position: relative;
        display: inline-block;
    }
    .select-container select {
        appearance: none;
        -webkit-appearance: none;
        -moz-appearance: none;
        padding-right: 30px; /* Adjust this based on your icon size */
    }
    .select-container .fa-chevron-down {
        position: absolute;
        right: 10px;
        top: 50%;
        transform: translateY(-50%);
        pointer-events: none; /* Allows clicking through the icon to the select element */
    }
</style>


<body>
    <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">ရက်ချုပ်</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">
                <div class="container-fluid">

                <div class="mb-3" style="position: relative;">
                
                    
                </div>


                    <div class="row">
                  
                    <!-- Table Element -->
                    <div class="container-fluid">


                    <div class="container-fluid" style="margin-bottom: 20px;">
                        <form>
                        <div class="row">
                        <div class="col-sm-2 mb-2">
                            <div class="select-container">
                                <input type="date" class="form-control" id="DateInput" placeholder="date" onchange="filterTable()">
                            </div>
                        </div>

                        <div class="col-sm-2">
                        <div class="select-container">
                        <button type="button" class="btn btn-outline-success" onclick="refreshPage()">Show All</button>
                    </div>

                        </form>
                    </div>

                    <script>
                        function refreshPage() {
                            location.reload();
                        }
                    </script>

                    </div>

            
                        
                        <div class="scrollable-table" style="height:70vh;">
                        <table class="table table-bordered table-hover" style="color:white;font-size:18px;"  id="dataTable">
                        <thead style="background-color:#19242e" class="text-center align-middle">
                            <tr>
                            <tr>
                            <th rowspan="2" scope="col">date</th>
                            <th colspan="3" scope="col" style="color:#FFA62F">ကျပ်ငွေ</th>
                            <th colspan="3" scope="col" style="color:#42f560">ဘတ်ငွေ</th>
                            <th colspan="3" scope="col" style="color:#c7023a">ယွမ်ငွေ</th>
                            <th colspan="3" scope="col" style="color:#ffffff">ဒေါ်လာ</th>
                        </tr>
                        <tr>
                            <th scope="col" style="color:#FFA62F">ဝင်ငွေ</th>
                            <th scope="col" style="color:#FFA62F">ထွက်ငွေ</th>
                            <th scope="col" style="color:#FFA62F">လက်ကျန်</th>

                            <th scope="col" style="color:#42f560">ဝင်ငွေ</th>
                            <th scope="col" style="color:#42f560">ထွက်ငွေ</th>
                            <th scope="col" style="color:#42f560">လက်ကျန်</th>

                            <th scope="col" style="color:#c7023a">ဝင်ငွေ</th>
                            <th scope="col" style="color:#c7023a">ထွက်ငွေ</th>
                            <th scope="col" style="color:#c7023a">လက်ကျန်</th>

                            <th scope="col" style="color:#ffffff">ဝင်ငွေ</th>
                            <th scope="col" style="color:#ffffff">ထွက်ငွေ</th>
                            <th scope="col" style="color:#ffffff">လက်ကျန်</th>
                        </tr>
                        </tr>
                            </thead>
                                <tbody>
                                <?php
                                // Retrieve all unique dates from both tables
                                $sql_dates = "
                                    SELECT date FROM $expense
                                    UNION
                                    SELECT date FROM $sell_data
                                    UNION
                                    SELECT date FROM $banking_refill
                                    UNION
                                    SELECT date FROM $product_refill
                                    UNION
                                    SELECT date FROM $pay_after
                                    ORDER BY date DESC
                                ";
                                $result_dates = mysqli_query($connection, $sql_dates);

                                $dates = [];
                                if ($result_dates) {
                                    while ($row_date = mysqli_fetch_assoc($result_dates)) {
                                        $dates[] = $row_date['date'];
                                    }
                                }

                                // Retrieve total expenses grouped by date
                                $sql_expense = "SELECT banking,date, SUM(total) AS total_expense FROM $expense GROUP BY date";
                                $result_expense = mysqli_query($connection, $sql_expense);

                                $expenses = [];
                                if ($result_expense) {
                                    while ($row_expense = mysqli_fetch_assoc($result_expense)) {
                                        $expenses[$row_expense['date']] = $row_expense['total_expense'];
                                    }
                                }

                                // Retrieve total sales grouped by date
                                $sql_sell = "SELECT date, SUM(total) AS total_sell FROM $sell_data where buy_type ='လက်ငင်း' GROUP BY date";
                                $result_sell = mysqli_query($connection, $sql_sell);

                                $sales = [];
                                if ($result_sell) {
                                    while ($row_sell = mysqli_fetch_assoc($result_sell)) {
                                        $sales[$row_sell['date']] = $row_sell['total_sell'];
                                    }
                                }

                                // Retrieve total refill grouped by date
                                $sql_refill = "SELECT date, SUM(amount) AS total_refill FROM $banking_refill GROUP BY date";
                                $result_refill = mysqli_query($connection, $sql_refill);

                                $refills = [];
                                if ($result_refill) {
                                    while ($row_refill = mysqli_fetch_assoc($result_refill)) {
                                        $refills[$row_refill['date']] = $row_refill['total_refill'];
                                    }
                                }

                                // Retrieve total pay_now refill grouped by date
                                $sql_refill_pay_now = "SELECT date, SUM(total) AS total_refill_pay_now FROM `$product_refill` WHERE payment='pay' GROUP BY date";
                                $result_refill_pay_now = mysqli_query($connection, $sql_refill_pay_now);

                                $refills_pay_now = [];
                                if ($result_refill_pay_now) {
                                    while ($row_refill = mysqli_fetch_assoc($result_refill_pay_now)) {
                                        $refills_pay_now[$row_refill['date']] = $row_refill['total_refill_pay_now'];
                                    }
                                }

                                // Fetch total pay_amount grouped by date
                                $sql_pay_after = "SELECT date, SUM(pay_amount) AS total_pay_after FROM $pay_after GROUP BY date";
                                $result_pay_after = mysqli_query($connection, $sql_pay_after);

                                $pay_afters = [];
                                if ($result_pay_after) {
                                    while ($row_pay_after = mysqli_fetch_assoc($result_pay_after)) {
                                        $pay_afters[$row_pay_after['date']] = $row_pay_after['total_pay_after'];
                                    }
                                }


                                $total_remain = 0;

                                // Display the results
                                foreach ($dates as $date) {
                                    $total_sell = isset($sales[$date]) ? $sales[$date] : 0;
                                    $total_expense = isset($expenses[$date]) ? $expenses[$date] : 0;
                                    $total_refill = isset($refills[$date]) ? $refills[$date] : 0;
                                    $total_refill_pay_now = isset($refills_pay_now[$date]) ? $refills_pay_now[$date] : 0;
                                    $total_pay_after = isset($pay_afters[$date]) ? $pay_afters[$date] : 0;
                                    $remain = ($total_refill + $total_sell) - ( $total_expense+ $total_refill_pay_now + $total_pay_after);

                                    $total_remain += $remain;

                                    echo "
                                        <tr>
                                            <td>".$date."</td>
                                            <td style='text-align:right;color:#FFA62F'>".number_format($total_sell + $total_refill)."</td>
                                            <td style='text-align:right;color:#FFA62F'>".number_format($total_expense + $total_refill_pay_now + $total_pay_after)."</td>
                                            <td style='text-align:right;color:#FFA62F'><b>".number_format($remain)."</b></td>

                                            <td style='text-align:right;color:#42f560'>".number_format($total_sell + $total_refill)."</td>
                                            <td style='text-align:right;color:#42f560'>".number_format($total_expense + $total_refill_pay_now + $total_pay_after)."</td>
                                            <td style='text-align:right;color:#42f560'><b>".number_format($remain)."</b></td>

                                            <td style='text-align:right;color:#c7023a'>".number_format($total_sell + $total_refill)."</td>
                                            <td style='text-align:right;color:#c7023a'>".number_format($total_expense + $total_refill_pay_now + $total_pay_after)."</td>
                                            <td style='text-align:right;color:#c7023a'><b>".number_format($remain)."</b></td>

                                            <td style='text-align:right;color:#ffffff'>".number_format($total_sell + $total_refill)."</td>
                                            <td style='text-align:right;color:#ffffff'>".number_format($total_expense + $total_refill_pay_now + $total_pay_after)."</td>
                                            <td style='text-align:right;color:#ffffff'><b>".number_format($remain)."</b></td>
                                        </tr>";
                                }

                                // Display the total remain
                                echo "
                                    <tr>
                                        <td colspan='3'><strong>Total Remain:</strong></td>
                                        <td style='text-align:right'><strong>".number_format($total_remain)."</strong></td>
                                    </tr>";
                                ?>


     
                                </tbody>
                            </table>

                            <script>
                          function filterTable() {
        var dateInput = document.getElementById("DateInput").value;
        var table, tr, td, i, total = 0;

        table = document.getElementById("myTable");
        tr = table.getElementsByTagName("tr");

        for (i = 1; i < tr.length; i++) {
            td = tr[i].getElementsByTagName("td")[0]; // Date column

            if (td) {
                if (dateInput === "" || td.textContent === dateInput) {
                    tr[i].style.display = "";
                } else {
                    tr[i].style.display = "none";
                }
            }
        }
    }
                        </script>

                            <!-- Delete Data -->
                    </div>


                    <h3 style="margin-top:30px">အသေးစိတ်စာရင်း</h3>
                    <div class="scrollable-table" style="height:70vh;">
                            <table class="table table-bordered rounded table-hover" style="color:white;font-size:18px;"  id="myTable">
                            <thead style="background-color:#19242e">
                            <tr>
                            <th>date</th>
                            <th>သွင်းငွေ</th>
                            <th>ဝင်ငွေ</th>
                            <th>အသုံစရိတ်</th>
                            <th>ဝယ်ပြီးငွေချေ</th>
                            <th>နောက်ပိုင်းငွေချေ</th>
                            <th>လက်ကျန်</th>
                        </tr>
                            </thead>
                                <tbody>
                                <?php
// Retrieve all unique dates from both tables
$sql_dates = "
    SELECT date FROM $expense
    UNION
    SELECT date FROM $sell_data
    UNION
    SELECT date FROM $banking_refill
    UNION
    SELECT date FROM $product_refill
    UNION
    SELECT date FROM $pay_after
    ORDER BY date DESC
";
$result_dates = mysqli_query($connection, $sql_dates);

$dates = [];
if ($result_dates) {
    while ($row_date = mysqli_fetch_assoc($result_dates)) {
        $dates[] = $row_date['date'];
    }
}

// Retrieve total expenses grouped by date
$sql_expense = "SELECT date, SUM(total) AS total_expense FROM $expense GROUP BY date";
$result_expense = mysqli_query($connection, $sql_expense);

$expenses = [];
if ($result_expense) {
    while ($row_expense = mysqli_fetch_assoc($result_expense)) {
        $expenses[$row_expense['date']] = $row_expense['total_expense'];
    }
}

// Retrieve total sales grouped by date
$sql_sell = "SELECT date, SUM(total) AS total_sell FROM $sell_data where buy_type ='လက်ငင်း' GROUP BY date";
$result_sell = mysqli_query($connection, $sql_sell);

$sales = [];
if ($result_sell) {
    while ($row_sell = mysqli_fetch_assoc($result_sell)) {
        $sales[$row_sell['date']] = $row_sell['total_sell'];
    }
}

// Retrieve total refill grouped by date
$sql_refill = "SELECT date, SUM(amount) AS total_refill FROM $banking_refill GROUP BY date";
$result_refill = mysqli_query($connection, $sql_refill);

$refills = [];
if ($result_refill) {
    while ($row_refill = mysqli_fetch_assoc($result_refill)) {
        $refills[$row_refill['date']] = $row_refill['total_refill'];
    }
}

// Retrieve total pay_now refill grouped by date
$sql_refill_pay_now = "SELECT date, SUM(total) AS total_refill_pay_now FROM `$product_refill` WHERE payment='pay' GROUP BY date";
$result_refill_pay_now = mysqli_query($connection, $sql_refill_pay_now);

$refills_pay_now = [];
if ($result_refill_pay_now) {
    while ($row_refill = mysqli_fetch_assoc($result_refill_pay_now)) {
        $refills_pay_now[$row_refill['date']] = $row_refill['total_refill_pay_now'];
    }
}

// Fetch total pay_amount grouped by date
$sql_pay_after = "SELECT date, SUM(pay_amount) AS total_pay_after FROM $pay_after GROUP BY date";
$result_pay_after = mysqli_query($connection, $sql_pay_after);

$pay_afters = [];
if ($result_pay_after) {
    while ($row_pay_after = mysqli_fetch_assoc($result_pay_after)) {
        $pay_afters[$row_pay_after['date']] = $row_pay_after['total_pay_after'];
    }
}


$total_remain = 0;

// Display the results
foreach ($dates as $date) {
    $total_sell = isset($sales[$date]) ? $sales[$date] : 0;
    $total_expense = isset($expenses[$date]) ? $expenses[$date] : 0;
    $total_refill = isset($refills[$date]) ? $refills[$date] : 0;
    $total_refill_pay_now = isset($refills_pay_now[$date]) ? $refills_pay_now[$date] : 0;
    $total_pay_after = isset($pay_afters[$date]) ? $pay_afters[$date] : 0;
    $remain = ($total_refill + $total_sell) - ( $total_expense+ $total_refill_pay_now + $total_pay_after);

    $total_remain += $remain;

    echo "
        <tr>
            <td>".$date."</td>
            <td style='text-align:right'>".number_format($total_refill)."</td>
            <td style='text-align:right'>".number_format($total_sell)."</td>
            <td style='text-align:right'>".number_format($total_expense)."</td>
            <td style='text-align:right'>".number_format($total_refill_pay_now)."</td>
            <td style='text-align:right'>".number_format($total_expense + $total_refill_pay_now + $total_pay_after)."</td>
            <td style='text-align:right'>".number_format($remain)."</td>
        </tr>";
}

// Display the total remain
echo "
    <tr>
        <td colspan='4'><strong>Total Remain:</strong></td>
        <td style='text-align:right'><strong>".number_format($total_remain)."</strong></td>
    </tr>";
?>


     
                                </tbody>
                            </table>
                           
                        </div>

             
                </div>
            </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;
}

?>


</body>
<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>

</html>
