<!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 "cal_banking.php";
include "cal_buy_shop.php";
include "cal_customer_pay.php";
include "cal_expense.php";
include "cal_pro_catagories.php";
include "cal_pro_remain.php";
include "cal_voucher.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">

                            <select name="product_type" id="accountInput" onchange="filterTable()" class="form-control">
                                    <?php
                                    $sql = "SELECT * FROM ".$supplier."";
                                    $result = $connection->query($sql);
                                    if ($result && $result->num_rows > 0) {
                                        while ($row = $result->fetch_assoc()){
                                            $name = $row['name']; // Database
                                            echo '
                                            <option value="' . $name . '">' . $name . '</option>
                                            ';
                                        }
                                    }
                                    ?>
                                </select>
                                <i class="fas fa-chevron-down"></i>
                            </div>
                        </div>


                        <div class="col-sm-2 mb-2">
                            <div class="select-container">
                                <select name="product_type" id="dayInput" onchange="filterTable()" class="form-control">
                                    <option value="">Select Day</option>
                                    <option value="1">1</option>
                                    <option value="2">2</option>
                                    <option value="3">3</option>
                                    <option value="4">4</option>
                                    <option value="5">5</option>
                                    <option value="6">6</option>
                                    <option value="7">7</option>
                                    <option value="8">8</option>
                                    <option value="9">9</option>
                                    <option value="10">10</option>
                                    <option value="11">11</option>
                                    <option value="12">12</option>
                                    <option value="13">13</option>
                                    <option value="14">14</option>
                                    <option value="15">15</option>
                                    <option value="16">16</option>
                                    <option value="17">17</option>
                                    <option value="18">18</option>
                                    <option value="19">19</option>
                                    <option value="20">20</option>
                                    <option value="21">21</option>
                                    <option value="22">22</option>
                                    <option value="23">23</option>
                                    <option value="24">24</option>
                                    <option value="25">25</option>
                                    <option value="26">26</option>
                                    <option value="27">27</option>
                                    <option value="28">28</option>
                                    <option value="29">29</option>
                                    <option value="30">30</option>
                                    <option value="31">31</option>
                                </select>
                                <i class="fas fa-chevron-down"></i>
                            </div>
                        </div>

                        <div class="col-sm-2 mb-2">
                            <div class="select-container">
                                <select name="product_type" id="monthInput" onchange="filterTable()" class="form-control">
                                    <option value="">Select Month</option>
                                    <option value="1">January-1</option>
                                    <option value="2">February-2</option>
                                    <option value="3">March-3</option>
                                    <option value="4">April-4</option>
                                    <option value="5">May-5</option>
                                    <option value="6">June-6</option>
                                    <option value="7">July-7</option>
                                    <option value="8">August-8</option>
                                    <option value="9">September-9</option>
                                    <option value="10">October-10</option>
                                    <option value="11">November-11</option>
                                    <option value="12">December-12</option>
                                </select>
                                <i class="fas fa-chevron-down"></i>
                            </div>
                        </div>

                        <div class="col-sm-2 mb-2">
                            <div class="select-container">
                                <select name="product_type" id="yearInput" onchange="filterTable()" class="form-control">
                                    <option value="">Select Year</option>
                                    <option value="2023">2023</option>';
                                    <option value="2024">2024</option>';
                                    <option value="2025">2025</option>';
                                    <option value="2026">2026</option>';
                                    <option value="2027">2027</option>';
                                    <option value="2028">2028</option>';
                                    <option value="2029">2029</option>';
                                    <option value="2030">2030</option>';
                                    <option value="2031">2031</option>';
                                    <option value="2032">2032</option>';
                                    <option value="2033">2033</option>';
                                    <option value="2034">2034</option>';
                                    <option value="2035">2035</option>';
                                </select>
                                <i class="fas fa-chevron-down"></i>
                            </div>
                        </div>

                        <div class="col-sm-2">
                        <div class="select-container">
                        <!-- <button type="submit" name="baht" class="btn btn-outline-success" style="margin: 10px;" onclick="refreshPage()">Show All</button> -->
                        <button type="submit" class="btn btn-outline-success" onclick="refreshPage()">Show All</button>
                    </div>

                        </form>
                    </div>

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

                    </div>

                        <div style="display:flex; align-items: center;margin-bottom:15px;margin-top:15px">
                            <h3 id="title" style="margin: 0;">Total Sell - <span id="total"></span></h3>
                        </div>
                        
                        <div class="scrollable-table" style="height:70vh;">
                            <table class="table table-bordered table-hover" style="color:white;font-size:18px;"  id="myTable">
                            <thead style="background-color:#19242e">
                            <tr>
                            <th scope="col" style='display:none;'>ဆိုင်အမည်</th>
                            <th scope="col">စဉ်</th>
                            <th scope="col">ဆိုင်အမည်</th>
                            <th scope="col">barcode</th>
                            <th scope="col">ပစ္စည်းအမည်</th>
                            <th scope="col">အမျိုးစား</th>
                            <th scope="col">ဈေးနှုန်း</th>
                            <th scope="col">အရေအတွက်</th>
                            <th scope="col">ကျသင့်ငွေ</th>
                            <th scope="col">ငွေကြေး</th>
                            <th scope="col">ငွေချေပြီး</th>
                            <th scope="col">ငွေချေဘဏ်</th>
                            <th scope="col">expire date</th>
                            <th scope="col">voucher</th>
                            <th scope="col">Day</th>
                            <th scope="col">Month</th>
                            <th scope="col">Year</th>
                            <th scope="col">Delete</th>
                        </tr>
                            </thead>
                                <tbody>
                                <?php
                                if ($_SERVER["REQUEST_METHOD"] === "POST" && isset($_POST["id"])) {
                                    $id = $connection->real_escape_string($_POST["id"]);
                                    echo "Received id: " . $id;


                                    $query = "DELETE FROM ".$sell_data." WHERE id = '$id'";
                                    if ($connection->query($query)) {
                                        http_response_code(200);
                                    } else {
                                        http_response_code(500);
                                    }
                                }

                                ?>


                                    <?php 
                                  $sql = "SELECT *, DAY(date) as date_day, MONTH(date) as date_month, YEAR(date) as date_year FROM ". $product_refill." order by id desc";
                                  $result = mysqli_query($connection, $sql);

                          
                                  if ($result) {
                                    while ($row = mysqli_fetch_assoc($result)) {
                                        $expire_date = date('Y-m-d');
                                            $row_date = $row['date'];
                                            $date1 = new DateTime($row_date);
                                            $date2 = new DateTime($expire_date);
                                            $interval = $date1->diff($date2);
                                            $days_diff = $interval->format('%a'); // Number of days difference
                                            // Determine the CSS class based on the date difference
                                            $date_class = ($days_diff <= 7) ? 'near-today' : '';

                                      echo "
                                        <tr>
                                        <td style='display:none;'>".$row['supplier']."</td>
                                        <td>".$row['id']."</td>
                                        <th scope='row'>".$row['supplier']."</td>
                                          <td>".$row['barcode']."</td>
                                          <td>".$row['product_name']."</td>
                                          <td>".$row['type']."</td>
                                           <td>".$row['qty']."</td>
                                          <td>".$row['buy']."</td>
                                          <td>".$row['total']."</td>

                                          <td>".$row['currency']."</td>
                                          <td>".$row['payment']."</td>
                                          <td>".$row['banking']."</td>

             
                                         <td class=".$date_class.">$days_diff </td> 

                                          <td>".$row['voucher']."</td>
                                          <td>".$row['date_day']."</td>
                                          <td>".$row['date_month']."</td>
                                          <td>".$row['date_year']."</td>
                                          <td><button type='submit' class='btn btn-danger' onclick='deleteRow(this)'>Delete</button></td>    
                                        </tr>";
                                    }
                                  }
                                    ?>

                                    <style>
                                        .near-today {
                                            color: red;
                                            font-weight: bold;
                                        }
                                    </style>
     
                                </tbody>

                                <script>
                                function deleteRow(button) {
                                    const row = button.parentNode.parentNode;
                                    const id = row.cells[0].textContent; // Assuming the Product ID is in the first column (index 0)
                                    // Send the tra_id to the backend PHP script using AJAX for deletion
                                    const xhr = new XMLHttpRequest();
                                    xhr.open('POST', 'product_refill_view.php', true);
                                    xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
                                    xhr.onreadystatechange = function() {
                                    if (xhr.readyState === XMLHttpRequest.DONE) {
                                        if (xhr.status === 200) {
                                        row.parentNode.removeChild(row); // Remove the row from the table on successful deletion
                                        alert('Data deleted successfully!');
                                        } else {
                                        alert('Error deleting data. Please try again.');
                                        }
                                    }
                                    };
                                    xhr.send('id=' + encodeURIComponent(id));


                                }
                                </script>

                            </table>

                            <script>
                          function filterTable() {
        var accountInput, dayInput, monthInput, yearInput;
        var filterAccount, filterDay, filterMonth, filterYear;
        var table, tr, tdAccount, tdDay, tdMonth, tdYear, tdTotal, total, i;

        accountInput = document.getElementById("accountInput");
        dayInput = document.getElementById("dayInput");
        monthInput = document.getElementById("monthInput");
        yearInput = document.getElementById("yearInput");

        filterAccount = accountInput.value.toUpperCase();
        filterDay = dayInput.value;
        filterMonth = monthInput.value;
        filterYear = yearInput.value;

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

        for (i = 1; i < tr.length; i++) {
            tdAccount = tr[i].getElementsByTagName("td")[0]; // Account/Product column
            tdDay = tr[i].getElementsByTagName("td")[13]; // Day column
            tdMonth = tr[i].getElementsByTagName("td")[14]; // Month column
            tdYear = tr[i].getElementsByTagName("td")[15]; // Year column
            tdTotal = tr[i].getElementsByTagName("td")[7]; // Total column

            if (tdAccount && tdDay && tdMonth && tdYear && tdTotal) {
                var accountMatch = filterAccount === "" || tdAccount.textContent.toUpperCase().indexOf(filterAccount) > -1;
                var dayMatch = filterDay === "" || tdDay.textContent === filterDay;
                var monthMatch = filterMonth === "" || tdMonth.textContent === filterMonth;
                var yearMatch = filterYear === "" || tdYear.textContent === filterYear;

                if (accountMatch && dayMatch && monthMatch && yearMatch) {
                    tr[i].style.display = "";
                    total += parseFloat(tdTotal.textContent || tdTotal.innerText);
                } else {
                    tr[i].style.display = "none";
                }
            }
        }
        document.getElementById("total").innerHTML = total.toFixed(2) + " MMK";
    }
                        </script>

                            <!-- Delete Data -->
                           
                        </div>
                    </div>
                     <!-- End Table Element -->
                      <div class="container-fluid"> 
                      <div class="row justify-content-center" style='margin:0px;'> 
                        <h1 style="margin:10px 0px 15px 10px;font-size:30px;text-align:center">အရောင်းစာရင်း</h1>

                        <!-- =================cont_1================================ -->
                        
                        <?php
                        include "database/database.php";
                        // Fetch all image data from the database
                        $sql = "SELECT seller,sum(total) as total_sell FROM $sell_data group by seller order by total_sell desc";
                        $result = $connection->query($sql);

                        if ($result->num_rows > 0) {
                            while ($row = $result->fetch_assoc()) {
                        
    
                                echo '
                                <div class="col-sm-4" style="border-radius:10px; width:320px; margin:10px">
                                    <div class="card mb-3">

                                        <div class="card-header bg-info text-white">
                                            <h5 class="card-title">'.$row["seller"].'</h5>
                                        </div>

                                        <div class="card-body">
                                            <div style="margin-top:10px;line-height: 1;">
                                            <p><a style="color:white;font-size:20px">'.$row["total_sell"].' MMK</a></p>
                                            </div>
                                        </div>

                                    </div>
                                </div>
                                ';
                            }
                        } else {
                            echo "No images found.";
                        }
                        ?>

                    </div>



                    <!-- End Photo Element -->

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

?>
<script src="js/script.js"></script>
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.5.4/dist/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>

</body>

</html>
