<?php
$act = 55;
include './login/connect.php';
?>

<!DOCTYPE html>
<!-- =========================================================
* ekseba Admin Template - Pro | v1.0.0
==============================================================

* Product Page: https://ekseba.com
* Created by: ekseba
* License: ekseba.com
* Copyright ThemeSelection (https://ekseba.com)

=========================================================
-->
<!-- beautify ignore:start -->
<html lang="en"  class="light-style layout-menu-fixed"  dir="ltr"  data-theme="theme-default"  data-assets-path="assets/"  data-template="vertical-menu-template-free">
    <head>
<script>
        // ফর্মের ড্রপডাউন আপডেট করার জন্য জাভাস্ক্রিপ্ট
        function updateDescriptionOptions() {
            const transactionType = document.getElementById("type").value;
            const descriptionDropdown = document.getElementById("description");

            // সিলেক্ট অপশন ক্লিয়ার করুন
            descriptionDropdown.innerHTML = "";

            // আয় এবং ব্যয়ের জন্য আলাদা অপশন
            const incomeOptions = ["বেতন", "ব্যবসার লাভ", "উপহার", "অন্যান্য"];
            const expenseOptions = ["বডিং খরচ","বাড়ি ভাড়া","অফিস আপ্পায়ন"," গ্যাস বিল "," বিদ্যৎ বিল","আসবাব পত্","নির্মান/মেরামত", "পরিবহন","মোবাইল খরচ", "অন্যান্য"];

            let options = transactionType === "আয়" ? incomeOptions : expenseOptions;

            options.forEach(option => {
                const opt = document.createElement("option");
                opt.value = option;
                opt.textContent = option;
                descriptionDropdown.appendChild(opt);
            });
        }
    </script>
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>

        <?php include './html/head.php'; ?>
        <script>
            function showUser(str) {
                if (str == "") {
                    document.getElementById("txtHint").innerHTML = "";
                    return;
                } else {
                    if (window.XMLHttpRequest) {
                        // code for IE7+, Firefox, Chrome, Opera, Safari
                        xmlhttp = new XMLHttpRequest();
                    } else {
                        // code for IE6, IE5
                        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
                    }
                    xmlhttp.onreadystatechange = function () {
                        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                            document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
                        }
                    }
                    // getuser.php is seprate php file. q is parameter 
                    xmlhttp.open("GET", "ajax.php?q=" + str, true);
                    xmlhttp.send();
                }
            }
        </script>
   
        <!-- serch script -->
        <script src="jeffartagame.js" type="text/javascript" charset="utf-8"></script>
        <script src="application.js" type="text/javascript" charset="utf-8"></script>
        <!-- serch script -->

        <style>
            #inputContainer {
                display: none;
                margin-top: 15px;
            }

            #customers {
                font-family: Arial, Helvetica, sans-serif;
                border-collapse: collapse;
                width: 100%; font-size: 12px;
            }

            #customers td, #customers th {
                border: 1px solid #ddd;
                padding: 5px 2px 5px 2px; font-size: 12px;
                text-align: center;
            }

            #customers tr:nth-child(even){background-color: #f2f2f2;}

            #customers tr:hover {background-color: #ddd;}

            #customers th {
                padding-top: 12px;
                padding-bottom: 12px;
                text-align: left;
                background-color: #04AA6D;
                color: white; font-size: 12px;
            }
        </style>
    </head>

    <body>
        <!-- Layout wrapper -->
        <div class="layout-wrapper layout-content-navbar">
            <div class="layout-container">
                <!-- Menu -->
                <?php
                include './menue.php';
                ?>
                <!-- / Menu -->

                <!-- Layout container -->
                <div class="layout-page">
                    <!-- Navbar -->

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

                    <!-- / Navbar -->

                    <!-- Content wrapper -->
                    <div class="content-wrapper">
                        <!-- Content -->

                        <!-- Content wrapper -->
                        <div class="content-wrapper">
                            <!-- Content -->

                            <div class="container-xxl flex-grow-1 container-p-y">
                                <h4 class="fw-bold py-3 mb-4"><span class="text-muted fw-light">Debit/Credit</span> Information</h4>

                                <!-- Basic Layout & Basic with Icons -->
                                <div class="row">
                                    <!-- Basic Layout -->
                                    <div class="col-xl">
                                        <div class="card mb-4">
                                            <div class="card-header d-flex justify-content-between align-items-center">
                                                <h5 class="mb-0">Debit/Credit Information</h5>


                                                <span class="float:right;" >
                                                    <button   type="button" class="btn btn-primary" data-bs-toggle="modal"
                                                              data-bs-target="#largeModal"> ( আয়-ব্যয় ) যোগ 
                                                    </button>
                                                    </a></span>
                                            </div>
                                            <div class="card-body">
                                                <div class="col-xxl">
                                                    <div class="card mb-4">
                                                     
                                                        <!-- Contextual Classes -->
                                                        <style>
                                                            tr:nth-child(even) {
                                                                background-color: #D6EEEE;
                                                            }
                                                        </style>
                                                        <div class="card">

<form method="GET" action="">
    <label for="start_date">শুরু তারিখ:</label>
    <input type="date" id="start_date" name="start_date" value="<?php echo isset($_GET['start_date']) ? $_GET['start_date'] : ''; ?>" required>
    
    <label for="end_date">শেষ তারিখ:</label>
    <input type="date" id="end_date" name="end_date" value="<?php echo isset($_GET['end_date']) ? $_GET['end_date'] : ''; ?>" required>
    
    <button type="submit">ফিল্টার করুন</button>
</form>

<?php

// Initialize variables
$previous_balance = 0;
$total_credit = 0;
$total_debit = 0;

// Check if date filter is applied
if (isset($_GET['start_date']) && isset($_GET['end_date']) && !empty($_GET['start_date']) && !empty($_GET['end_date'])) {
    $start_date = $_GET['start_date'];
    $end_date = $_GET['end_date'];

    // Fetch previous balance before start date
    $previous_balance_sql = "SELECT SUM(credit) - SUM(debit) AS previous_balance 
                             FROM transactions 
                             WHERE date < '$start_date'";
    $previous_result = $conn->query($previous_balance_sql);
    if ($previous_result) {
        $previous_data = $previous_result->fetch_assoc();
        $previous_balance = $previous_data['previous_balance'] ?: 0;
    }

    // Fetch transactions within the selected date range
    $sql = "SELECT * FROM transactions 
            WHERE date BETWEEN '$start_date' AND '$end_date' 
            ORDER BY date ASC";
} else {
    // Default: Fetch current month's transactions
    $current_month_start = date("Y-m-01");
    $current_month_end = date("Y-m-t");

    // Fetch previous balance up to the start of the current month
    $previous_balance_sql = "SELECT SUM(credit) - SUM(debit) AS previous_balance 
                             FROM transactions 
                             WHERE date < '$current_month_start'";
    $previous_result = $conn->query($previous_balance_sql);
    if ($previous_result) {
        $previous_data = $previous_result->fetch_assoc();
        $previous_balance = $previous_data['previous_balance'] ?: 0;
    }

    // Fetch current month's transactions
    $sql = "SELECT * FROM transactions 
            WHERE date BETWEEN '$current_month_start' AND '$current_month_end' 
            ORDER BY date ASC";
}

// Execute transaction query
$result = $conn->query($sql);

?>

<table class="table">
    <thead>
        <tr>
            <th style="width: 3px;">ক্র:নং</th>
            <th>তারিখ</th>
            <th>বর্ণনা</th>
            <th>জমা</th>
            <th>ব্যয়</th>
        </tr>
    </thead>
    <tbody>
        <?php
        $serial = 1;

        // Show previous balance as the first row
        echo "<tr>";
        echo "<td colspan='4' style='text-align:right; font-weight:bold;'>পূর্ববর্তী স্থিতি:</td>";
        echo "<td style='font-weight:bold;'>" . number_format($previous_balance, 2) . "</td>";
        echo "</tr>";

        if ($result->num_rows > 0) {
            while ($row = $result->fetch_assoc()) {
                $total_credit += $row['credit']; // Sum of credits
                $total_debit += $row['debit']; // Sum of debits

                echo "<tr>";
                echo "<td>" . $serial++ . "</td>";
                echo "<td>" . $row['date'] . "</td>";
                echo "<td>" . $row['description'] . "</td>";
                echo "<td>" . number_format($row['credit'], 2) . "</td>";
                echo "<td>" . number_format($row['debit'], 2) . "</td>";
                echo "</tr>";
            }
        } else {
            echo "<tr><td colspan='5'>কোনো লেনদেন পাওয়া যায়নি</td></tr>";
        }
        ?>
    </tbody>
    <tfoot>
        <tr>
            <th colspan="3" style="text-align:right;">মোট:</th>
            <th><?php echo number_format($total_credit, 2); ?></th>
            <th><?php echo number_format($total_debit, 2); ?></th>
        </tr>
        <tr>
            <th colspan="4" style="text-align:right;">চূড়ান্ত স্থিতি:</th>
            <th><?php echo number_format($total_credit - $total_debit, 2); ?></th>
        </tr>
         <tr>
            <th colspan="4" style="text-align:right;">পূর্ববর্তী স্থিতি::</th>
            <th><?php echo number_format($previous_balance, 2); ?></th>
        </tr>
        <tr>
            <th colspan="4" style="text-align:right;">চূড়ান্ত স্থিতি:</th>
            <th><?php echo number_format($previous_balance + $total_credit - $total_debit, 2); ?></th>
        </tr>
        
    </tfoot>
</table>

<?php
$conn->close();
?>                                                        
<!--/3333333333333333333333333333333333333333333333333333333333333333333333333333333333333333 Contextual Classes -->
                                                        </div>
                                                        <!--/ Contextual Classes -->
                                                    </div>
                                                </div>
                                                <!-- Basic with Icons -->

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

                            <!-- / Content -->
                        </div>
                        <!-- Footer -->
                        <?php require './footer.php'; ?>
                        <!-- Large Modal -->
                        <form method="post" action="devit-cradit.php" style="font-size: 25px; padding: 15px;">
                            <div class="modal fade" id="largeModal" tabindex="-1" aria-hidden="true">
                                <div class="modal-dialog modal-lg" role="document">
                                    <div class="modal-content">
                                        <div class="modal-header">
                                            <h5 class="modal-title" id="exampleModalLabel3">  আয়/ব্যয় যুক্ত করুন  </h5> <!-- cost add floting  ber  -->
                                            <button type="button" class="btn-close" data-bs-dismiss="modal"  aria-label="Close" ></button>
                                        </div>
                                        <div class="modal-body">
                                            <style>.w{width: 250px;}</style>
                                            <label for="type">ধরন (আয়/ব্যয়)<span>&nbsp;&nbsp;&nbsp;</span>:</label>
                                            <select class="w" id="type" name="type" onchange="updateDescriptionOptions()" required>
                                                    <option value=""> নির্বাচন করুন --</option>
                                                    <option value="ব্যয়">ব্যয়</option>
                                                    <option value="আয়">আয়</option>
                                                    
                                                </select><br><br>

                                                <label for="description">  বর্ণনা <span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>:</label>
                                                <select class="w" id="description" name="description" required>
                                                    <option value=""> নির্বাচন করুন --</option>
                                                </select><br><br>

                                                <label for="date"></span>তারিখ <span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>:</label>
                                                <input class="w" type="date" id="date" name="date" required><br><br>

                                                <label for="amount">পরিমাণ <span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>:</label>
                                                <input class="w" type="number" id="amount" name="amount" step="0.01" required><br><br>

                                                <span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><button type="submit" name="submit">যোগ করুন</button>
                                       
                                        </div>
                                        </div>
                                        <div class="modal-footer">
                                            <button type="button" class="btn btn-outline-secondary" data-bs-dismiss="modal">
                                                Close
                                            </button>
                                            <button type="submit" name="fees"  class="btn btn-primary">Save</button>
                                       </div>
                                        <script>
                                            function calculateSum() {
                                                var input1 = document.getElementById("input1").value;
                                                var input2 = document.getElementById("input2").value;
                                                var sum = parseFloat(input1) + parseFloat(input2);

                                                if (!isNaN(sum)) {
                                                    document.getElementById("sum").value = sum;
                                                } else {
                                                    document.getElementById("sum").value = "Invalid input";
                                                }
                                            }
                                        </script>
                                    </div>
                                </div>
                            
                        </form>
                    </div>
<script>
$(document).ready(()=>{
  $('.js-revealer').on('change', function(){
    var $select = $(this);
    var $selected = $select.find('option:selected');
    var hideSelector = $selected.data('r-hide-target');
    var showSelector = $selected.data('r-show-target');
    
    $(hideSelector).addClass('is-hidden');
    $(showSelector).removeClass('is-hidden');
  });
});

</script>

                        <!-- Extra Large Modal -->
                        <script>
                            function toggleInputField() {
                                var inputContainer = document.getElementById("inputContainer");
                                var checkbox = document.getElementById("showInputCheckbox");

                                if (checkbox.checked) {
                                    inputContainer.style.display = "block";
                                } else {
                                    inputContainer.style.display = "none";
                                }
                            }
                        </script>
                        