<?php
    // session_start();
    include "../db.php";
    require '../auth.php';
    restrictAccess(['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 class="d-flex">
                                <div class="form-group">
                                    <input type="text" list="First" class="form-control form-control-sm"
                                        id="FirstFilterValue" placeholder="Sub Associate">
                                    <datalist id="First">
                                        <?php 
                                            $sql = "SELECT DISTINCT uname FROM users  WHERE utype = 'Sub Associate'  AND udisplay !='disable' AND admin_ref = '$_SESSION[uref_code]' 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">
                                <button type="button" id="filter" class="btn btn-primary mb-2 btn-sm"><i
                                        class="fa fa-search"></i>
                                </button>
                            </li>
                        </ul>
                    </div>
                    <div class="card-body">
                        <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">
                                <!-- Add summary counts if needed -->
                            </div>
                            <div class="col-md-2">
                                <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 my-3">
                                <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;">
                                    <i class="fa fa-check-circle"></i> No more data to load.
                                </div>
                                <div id="loader" style="display:none;">
                                    <i class="fa fa-spinner fa-spin"></i> Loading...
                                </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">Student 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 FirstFilterValue = '';
        let college = '';
        let course = '';
        let StatusValue = '';
        let StartDate = '';
        let EndDate = '';

        // Load unfiltered data
        function loadMore(start) {
            $.ajax({
                url: 'view-productList-Ajax.php',
                type: 'POST',
                data: {
                    start: start
                },
                beforeSend: function() {
                    $("#loader").show();
                },
                success: function(result) {
                    result = result.trim();
                    $('#full-table-data').append(result);
                    let itemCount = $(result).filter('li').length;

                    load_flag += 10;

                    if (itemCount < 10) {
                        $('#loadMoreBtn').hide();
                        $('#endMessage').removeClass('d-none');
                    } else {
                        $('#loadMoreBtn').show();
                        $('#endMessage').addClass('d-none');
                    }
                },
                complete: function() {
                    $("#loader").hide();
                }
            });
        }

        // Load filtered data
        function loadFilteredData(start) {
            $.ajax({
                url: 'view-productList-Ajax.php',
                type: 'POST',
                data: {
                    FirstFilterValue,
                    college,
                    course,
                    StatusValue,
                    StartDate,
                    EndDate,
                    start: start
                },
                beforeSend: function() {
                    $("#loader").show();
                },
                success: function(result) {
                    result = result.trim();
                    let itemCount = $(result).filter('li').length;

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

                    if (start === 0) {
                        $('#filter-table-data').html(result);
                        $('#full-table').hide();
                        $('#filter-table').show();
                        $('#summary-section').removeClass('d-none');
                    } else {
                        $('#filter-table-data').append(result);
                    }

                    load_filter_flag += 10;

                    if (itemCount < 10) {
                        $('#loadMoreBtn').hide();
                        $('#endMessage').text("End Of Result").removeClass('d-none');
                    } else {
                        $('#loadMoreBtn').show();
                        $('#endMessage').addClass('d-none');
                    }
                },
                complete: function() {
                    $("#loader").hide();
                }
            });
        }

        // Initial unfiltered load
        loadMore(load_flag);

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

            load_filter_flag = 0;

            if (FirstFilterValue || college || course || StatusValue || StartDate || EndDate) {
                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 ($('#filter-table').is(':visible')) {
                loadFilteredData(load_filter_flag);
            } else {
                loadMore(load_flag);
            }
        });
    });
    </script>


</body>

</html>