<?php
    session_start();
    include "../db.php";
    date_default_timezone_set('Asia/Kolkata');
    $currentTime = date('M j, Y g:i a');

    if ( !isset($_SESSION['uid']) && !isset($_SESSION['uref']) && !isset($_SESSION['utype']) ) {
        header("Location: logout.php");
        exit();
    }

    if ( $_SESSION['utype'] != 'master_admin' && $_SESSION['uref'] != 'fssdf4543frts' ) {
        header("Location: logout.php");
        exit();
    }
?>

<!DOCTYPE html>
<html lang="en">

<head>
    <?php include "include/meta.php"; ?>
    <?php include "include/css.php"; ?>
    <?php include "include/php-functions.php"; ?>
</head>

<body>
    <!--  Header -->
    <?php include "include/header.php";?>
    <!-- #header -->

    <section id="inner-body">
        <div class="body-content">
            <div class="container-fluid">
                <div class="card mt-3 mb-3">
                    <div class="card-header">
                        <div class="row align-items-center">
                            <div class="col-md-2">
                                <h5 class="text-gray-800 m-0">Payment History</h5>
                            </div>
                            <div class="col-md-10 text-end">
                                <ul class="search-details">
                                    <li>
                                        <input type="date" class="form-control form-control-sm" id="startDate"
                                            placeholder="Start Date">
                                    </li>
                                    <li>
                                        <input type="date" class="form-control form-control-sm" id="endDate"
                                            placeholder="End Date">
                                    </li>
                                    <li>
                                        <div class="form-group">
                                            <input type="text" list="First" class="form-control form-control-sm"
                                                id="FirstFilterValue" placeholder="RA Name">
                                            <datalist id="First">
                                                <?php 
                                                    $sql = "SELECT DISTINCT uname FROM users  WHERE utype = 'Franchiee'  AND udisplay !='disable' AND admin_ref = '$_SESSION[uref]' ORDER BY uname ASC";
                                                    $res = mysqli_query($con,$sql);
                                                    while ($row = mysqli_fetch_assoc($res)) {
                                                ?>
                                                <option value="<?php echo $row['uname']; ?>">
                                                    <?php echo $row['uname']; ?></option>
                                                <?php } ?>
                                            </datalist>
                                        </div>
                                        <input type="hidden" class="form-control" value="" id="FirstFilterValue">
                                    </li>
                                    <li>
                                        <div class="form-group">
                                            <input type="text" list="ras" class="form-control form-control-sm"
                                                id="thirdValue" placeholder="RAs Name">
                                            <datalist id="ras">
                                                <?php 
                                                    $sql = "SELECT DISTINCT uname FROM users  WHERE utype = 'subFranchiee'  AND udisplay !='disable' ORDER BY uname ASC";
                                                    $res = mysqli_query($con,$sql);
                                                    while ($row = mysqli_fetch_assoc($res)) {
                                                ?>
                                                <option value="<?php echo $row['uname']; ?>">
                                                    <?php echo $row['uname']; ?></option>
                                                <?php } ?>
                                            </datalist>
                                        </div>
                                        <input type="hidden" class="form-control" value="" id="thirdValue">
                                    </li>
                                    <li>
                                        <div class="form-group">
                                            <input type="text" list="Second" class="form-control form-control-sm"
                                                id="SecondFilterValue" placeholder="Client Name">
                                            <datalist id="Second">
                                                <?php 
                                                    $esql = "SELECT DISTINCT uname FROM users  WHERE utype = 'Client'  AND udisplay !='disable' ORDER BY uname ASC";
                                                    $eres = mysqli_query($con,$esql);
                                                    while ($erow = mysqli_fetch_assoc($eres)) {
                                                ?>
                                                <option value="<?php echo $erow['uname']; ?>">
                                                    <?php echo $erow['uname']; ?></option>
                                                <?php } ?>
                                            </datalist>
                                        </div>
                                        <input type="hidden" class="form-control" value="" id="SecondFilterValue">
                                    </li>
                                    <li>
                                        <button type="button" id="filter" class="btn btn-primary mb-2 btn-sm"><i
                                                class="fa fa-search"></i>
                                        </button>
                                    </li>
                                </ul>
                            </div>
                        </div>
                    </div>
                    <div class="card-body">
                        <div class="row">
                            <!--loader -->
                            <div class="col-12" id='loader'>
                                <ul style="list-style: none; padding-left: 0;">
                                    <?php
                                    for ($count = 0; $count < 4; $count++) {
                                        echo '<li>
                                        <div class="ph-item">
                                        <div cl ass="ph-test">
                                        <div class="ph-row">
                                        <div class="ph-col-12 big"></div>
                                        <div class="ph-col-12"></div>
                                        <div class="ph-col-12"></div>
                                        <div class="ph-col-12"></div>
                                        <div class="ph-col-12"></div>
                                        <div class="ph-col-12"></div>
                                        </div>
                                        </div>
                                        </div>
                                        </li>';
                                        }
                                    ?>
                                </ul>
                            </div>

                            <!-- loader -->
                            <div class="col-12" id="full-table">
                                <ul class="timeline" id="full-table-data" style="list-style: none; padding: 0;">
                                    </>
                                </ul>
                            </div>

                            <div class="col-12" id="filter-table" style="display: none;">
                                <ul class="timeline" id="filter-table-data" style="list-style: none; padding: 0;">
                                    </>
                                </ul>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <?php include "include/footer.php"; ?>
    </section>

    <!-- JavaScript Libraries -->

    <?php include "include/js.php"; ?>

    <!-- Modal Structure -->
    <div class="modal fade" id="studentDetailsModal" data-bs-backdrop="static" tabindex="-1" role="dialog"
        aria-labelledby="modelTitleId" aria-hidden="true">
        <div class="modal-dialog modal-lg" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 style="color:orange;font-size: 20px"> <i class="fab fa-product-hunt"></i> <span
                            id="pname"></span>
                    </h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                </div>
                <div class="modal-body">
                    <div class="row">
                        <div class="col-md-12 col-12">
                            <h5 class="modal-css">Payment Amount : <span id="pamount"></span> </h5>
                            <h5 class="modal-css">Transection Id : <span id="transid"></span> </h5>
                            <h5 class="modal-css">Client Name : <span id="clientname"></span> </h5>
                            <h5 class="modal-css">RA Name : <span id="raname"></span> </h5>
                            <h5 class="modal-css">RAs Name : <span id="rasname"></span> </h5>
                            <h5 class="modal-css">Employee Name : <span id="empname"></span> </h5>
                            <h5 class="modal-css">Product Category : <span id="pcat"></span> </h5>
                            <h5 class="modal-css">Product Status : <span id="pstatus"></span> </h5>
                            <h5 class="modal-css">Date : <span id="date"></span> </h5>
                        </div>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
                </div>
            </div>
        </div>
    </div>


    <script>
    $(document).ready(function() {
        var load_filter_flag = 0;
        var FirstFilterValue = '';
        var SecondFilterValue = '';
        var thirdValue = '';
        var StatusValue = '';
        var CategoryValue = '';
        var StartDate = '';
        var EndDate = '';

        $("#filter").click(function() {
            FirstFilterValue = $('#FirstFilterValue').val();
            SecondFilterValue = $('#SecondFilterValue').val();
            thirdValue = $('#thirdValue').val();
            StatusValue = $('#pststus').val();
            CategoryValue = $('#pcategory').val();
            StartDate = $('#startDate').val(); // New filter for start date
            EndDate = $('#endDate').val(); // New filter for end date

            load_filter_flag = 0;
            if (FirstFilterValue || SecondFilterValue || thirdValue || StatusValue ||
                CategoryValue || StartDate || EndDate) {
                $.ajax({
                    url: 'view-paymentList-Ajax.php',
                    type: 'POST',
                    data: {
                        FirstFilterValue: FirstFilterValue,
                        SecondFilterValue: SecondFilterValue,
                        thirdValue: thirdValue,
                        StatusValue: StatusValue,
                        CategoryValue: CategoryValue,
                        StartDate: StartDate, // Include start date
                        EndDate: EndDate, // Include end date
                        start: load_filter_flag
                    },
                    success: function(result) {
                        $('#full-table').hide();
                        $('#filter-table').show();
                        $('#filter-table-data').html(result);
                        load_filter_flag += 10;
                    },
                    complete: function() {
                        $("#loader").hide();
                    }
                });
            } else {
                alert('Please select at least one option.');
                $('#full-table').show();
                $('#filter-table').hide();
            }
        });

        function loadMoreFilter(start) {
            if (load_filter_flag > 0) {
                $.ajax({
                    url: 'view-paymentList-Ajax.php',
                    type: 'POST',
                    data: {
                        FirstFilterValue: FirstFilterValue,
                        SecondFilterValue: SecondFilterValue,
                        thirdValue: thirdValue,
                        StatusValue: StatusValue,
                        CategoryValue: CategoryValue,
                        StartDate: StartDate, // Include start date
                        EndDate: EndDate, // Include end date
                        start: start
                    },
                    success: function(result) {
                        $('#filter-table-data').append(result);
                        load_filter_flag += 10;
                    }
                });
            }
        }

        // Scroll Loading of Full Data //
        var load_flag = 0;
        loadMore(load_flag);

        function loadMore(start) {
            //console.log('total'+start);
            $.ajax({
                url: 'view-paymentList-Ajax.php',
                type: 'POST',
                data: 'start=' + start,
                success: function(result) {
                    $('#full-table-data').append(result);
                    load_flag += 10;
                },
                complete: function(data) {
                    /* Hide loader container */
                    $("#loader").hide();
                }
            });
        }

        $('#inner-body').scroll(function() {
            if ($('#inner-body').scrollTop() >= $(document).height() - $('#inner-body').height()) {
                if (load_filter_flag > 0) {
                    loadMoreFilter(load_filter_flag);
                } else {
                    loadMore(load_flag);
                }
            }
        });
    });
    </script>


</body>

</html>