<?php
    // session_start();
    include "../db.php";
    require '../auth.php';
    restrictAccess(['Sub Associate']);
    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();
    }
?>

<!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">
                        <ul class="search-details">
                            <li>
                                <h4 class="text-gray-800 m-0">Work List</h4>
                            </li>
                            <li>
                                <div class="date-label-group">
                                    <label for="startDate">Start Date</label>
                                    <input type="date" class="form-control form-control-sm" id="startDate"
                                        placeholder="Start Date">
                                </div>
                            </li>
                            <li>
                                <div class="date-label-group">
                                    <label for="endDate">End Date</label>
                                    <input type="date" class="form-control form-control-sm" id="endDate"
                                        placeholder="End Date">
                                </div>
                            </li>
                            <li>
                                <select name="status" class="form-control form-control-sm" id="pststus">
                                    <option value="">Status</option>
                                    <option value="0">Pending</option>
                                    <option value="1">Running</option>
                                    <option value="2">Pass Out</option>
                                    <option value="3">Leave Course</option>
                                </select>
                            </li>
                            <li>
                                <select name="college" class="form-control form-control-sm" id="college">
                                    <option value="">Select College</option>
                                    <?php 
                                        $clg_sql = mysqli_query($con, "SELECT * FROM users WHERE utype = 'College' AND uStatus = '1' AND udisplay != 'disable' ORDER BY uname ASC");
                                        while($clg_row = mysqli_fetch_assoc($clg_sql)){
                                    ?>
                                    <option value="<?php echo $clg_row["uid"]; ?>"><?php echo $clg_row["uname"]; ?>
                                    </option>
                                    <?php } ?>
                                </select>
                            </li>
                            <li>
                                <div class="form-group">
                                    <select name="course" class="form-control form-control-sm" id="course">
                                        <option value="">Select College First</option>
                                        <!-- Courses will be dynamically loaded here -->
                                    </select>
                                </div>
                            </li>
                            <li>
                                <div class="d-flex">
                                    <button type="button" id="filter" class="btn btn-primary mb-2 btn-sm"><i
                                            class="fa fa-search"></i>
                                    </button>
                                </div>
                            </li>
                        </ul>
                    </div>
                    <div class="card-body">
                        <!-- Displaying Total Pricing Sum and Total Payment Sum once at the top -->
                        <div class="row justify-content-center d-none" style="font-weight: bold; margin-bottom: 10px;"
                            id="summary-section">
                            <div class="col-md-10 d-flex justify-content-between">
                            </div>
                            <div class="col-md-2">
                                <!-- Button to Download Data as Excel -->
                                <div class="text-end mb-3">
                                    <a href="export-productList.php" class="btn btn-success">Download Excel</a>
                                </div>
                            </div>
                        </div>
                        <div class="row">
                            <!--loader -->
                            <div class="col-12" id='loader'>
                                <ul id="product-list" 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 class="text-center mt-3" id="load-more-container">
                                <button id="loadMoreBtn" class="btn btn-info btn-sm text-white">Load More</button>
                                <div id="endMessage" class="text-muted mt-2" style="display: none;">End of result</div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>

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

    <!-- Modal Structure -->
    <div class="modal fade" id="adminDetailsModal" 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="fa fa-school"></i> <span id="modalName"></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-6 col-6 text-end">
                            <h5 class="modal-css">Product Name:</h5>
                            <h5 class="modal-css">Category:</h5>
                            <h5 class="modal-css">Associate Name:</h5>
                            <h5 class="modal-css">Sub Associate Name:</h5>
                            <h5 class="modal-css">Employee Name:</h5>
                            <h5 class="modal-css">Client Name:</h5>
                            <h5 class="modal-css">Status:</h5>
                            <h5 class="modal-css">Date:</h5>
                            <h5 class="modal-css">Referred By:</h5>
                        </div>
                        <div class="col-md-6 col-6">
                            <h5 class="modal-id" id="modalProductName"></h5>
                            <h5 class="modal-id" id="modalPCat"></h5>
                            <h5 class="modal-id" id="modalFranName"></h5>
                            <h5 class="modal-id" id="modalSubFranName"></h5>
                            <h5 class="modal-id" id="modalEmployeeName"></h5>
                            <h5 class="modal-id" id="modalClientName"></h5>
                            <h5 class="modal-id" id="modalStatusName"></h5>
                            <h5 class="modal-id" id="modalDate"></h5>
                            <h5 class="modal-id" id="modalRef"></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>

    <!-- Final Document Modal -->
    <div class="modal fade" id="finalDocumentModal" tabindex="-1" role="dialog"
        aria-labelledby="finalDocumentModalLabel" aria-hidden="true">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title">Upload Final Document</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                </div>
                <div class="modal-body">
                    <!-- Content will be loaded dynamically here -->
                </div>
            </div>
        </div>
    </div>

    <!-- View Product Modal -->
    <div class="modal fade" id="viewProductModal" tabindex="-1" role="dialog" aria-labelledby="viewProductModalLabel"
        aria-hidden="true">
        <div class="modal-dialog modal-lg" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title">Product Details</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                </div>
                <div class="modal-body">
                    <!-- Content will be loaded dynamically here -->
                </div>
            </div>
        </div>
    </div>

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

    <script>
    $(document).ready(function() {
        $('#college').change(function() {
            var college_id = $(this).val();

            if (college_id != '') {
                $.ajax({
                    url: 'fetch_courses_by_college.php',
                    type: 'POST',
                    data: {
                        college_id: college_id
                    },
                    success: function(response) {
                        $('#course').html(response);
                    }
                });
            } else {
                $('#course').html('<option value="">Select College First</option>');
            }
        });
    });
    </script>

    <script>
    $(document).ready(function() {
        let load_filter_flag = 0;
        let load_flag = 0;

        let StartDate = '';
        let EndDate = '';
        let StatusValue = '';
        let college = '';
        let course = '';

        // Load filtered data
        function loadFilteredData(start, scrollToNew = false) {
            $.ajax({
                url: 'view-productList-Ajax.php',
                type: 'POST',
                data: {
                    StatusValue,
                    StartDate,
                    EndDate,
                    college,
                    course,
                    start: start
                },
                beforeSend: function() {
                    $("#loader").show();
                },
                success: function(result) {
                    result = result.trim();
                    const tempContainer = $('<div>').html(result);
                    const newItems = tempContainer.find('li');

                    if (result === '__NO_MORE_DATA__' || newItems.length === 0) {
                        $('#filter-table-data').append(`
                            <li class="text-center text-muted py-3">
                                <i class="fa fa-info-circle"></i> No results match your filters.
                            </li>
                        `);
                        $('#full-table').remove();
                        $('#filter-table').show();
                        $('#loadMoreBtn').hide();
                        $('#endMessage').hide();
                        // $('#summary-section').addClass('d-none');
                        return;
                    }

                    if (start === 0) {
                        $('#filter-table-data').html(newItems);
                        $('#full-table').remove();
                        $('#filter-table').show();
                        $('#calculation').show();
                        $('#summary-section').removeClass('d-none');
                    } else {
                        $('#filter-table-data').append(newItems);
                        if (scrollToNew && newItems.length > 0) {
                            $('html, body').animate({
                                scrollTop: newItems.first().offset().top - 100
                            }, 600);
                        }
                    }

                    load_filter_flag += newItems.length;

                    if (newItems.length < 10) {
                        $('#loadMoreBtn').hide();
                        $('#endMessage').show();
                    } else {
                        $('#loadMoreBtn').show();
                        $('#endMessage').hide();
                    }
                },
                complete: function() {
                    $("#loader").hide();
                }
            });
        }

        // Load unfiltered data
        function loadMore(start, scrollToNew = false) {
            $.ajax({
                url: 'view-productList-Ajax.php',
                type: 'POST',
                data: {
                    start: start
                },
                beforeSend: function() {
                    $("#loader").show();
                },
                success: function(result) {
                    result = result.trim();
                    const tempContainer = $('<div>').html(result);
                    const newItems = tempContainer.find('li');

                    if (result === '__NO_MORE_DATA__' || newItems.length === 0) {
                        $('#loadMoreBtn').hide();
                        $('#endMessage').show();
                        return;
                    }

                    $('#full-table-data').append(newItems);

                    if (scrollToNew && newItems.length > 0) {
                        $('html, body').animate({
                            scrollTop: newItems.first().offset().top - 100
                        }, 600);
                    }

                    load_flag += newItems.length;

                    if (newItems.length < 10) {
                        $('#loadMoreBtn').hide();
                        $('#endMessage').show();
                    } else {
                        $('#loadMoreBtn').show();
                        $('#endMessage').hide();
                    }
                },
                complete: function() {
                    $("#loader").hide();
                }
            });
        }

        // Initial load
        loadMore(load_flag);

        // Filter button click
        $("#filter").click(function() {
            StartDate = $('#startDate').val();
            EndDate = $('#endDate').val();
            StatusValue = $('#pststus').val();
            college = $('#college').val();
            course = $('#course').val();

            load_filter_flag = 0;
            load_flag = 0;

            $('#loadMoreBtn').show();
            $('#endMessage').hide();
            $('#summary-section').addClass('d-none');

            if (StatusValue || StartDate || EndDate || college || course) {
                loadFilteredData(load_filter_flag);
            } else {
                alert('Please select at least one filter option.');
                $('#full-table').show();
                $('#filter-table').hide();
                $('#summary-section').addClass('d-none');
            }
        });

        // Load More button click
        $("#loadMoreBtn").click(function() {
            if (load_filter_flag > 0 || $('#filter-table').is(":visible")) {
                loadFilteredData(load_filter_flag, true);
            } else {
                loadMore(load_flag, true);
            }
        });
    });
    </script>



</body>

</html>