<?php

    // session_start();

    include "../db.php";

    require '../auth.php';

    restrictAccess(['master_admin']);

    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();

    }

    // Get the status from the URL, if available

    $statusFromUrl = isset($_GET['status']) ? $_GET['status'] : '';

    

    if($statusFromUrl == 'All'){

        $statusId = '';

    }elseif($statusFromUrl == 'Pending'){

        $statusId = '0';

    }elseif($statusFromUrl == 'Ongoing'){

        $statusId = '1';

    }elseif($statusFromUrl == 'Completed'){

        $statusId = '2';

    }elseif($statusFromUrl == 'Postponed'){

        $statusId = '3';

    }elseif($statusFromUrl == 'Rejected'){

        $statusId = '4';

    }





    $statusOptions = [

        '' => 'All',

        '0' => 'Pending',

        '1' => 'Ongoing',

        '2' => 'Completed',

        '3' => 'Postponed',

        '4' => 'Rejected'

    ];



    if(isset($_GET["user"])){

        $userId = $_GET["user"];

        $user_sql = mysqli_query($con, "SELECT * FROM users WHERE uid = '$userId' ");

        if($user_row = mysqli_fetch_assoc($user_sql)){

            $userName = $user_row["uname"];

            $usertype = $user_row["utype"];

            $userUref = $user_row["uref"];

        }

    }

?>



<!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 Activity Of <?php echo $userName;?></h4>

                            </li>

                            <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>

                                <select name="status" class="form-control form-control-sm" id="pststus">

                                    <option value="">Status</option>

                                    <option value="0">pending</option>

                                    <option value="1">Ongoing</option>

                                    <option value="2">Completed</option>

                                    <option value="3">Postpond</option>

                                    <option value="4">Reject</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">

                                    <div class="form-group">

                                        <input type="text" list="Second" class="form-control form-control-sm"

                                            id="SecondFilterValue" placeholder="Search By Sub Associate">

                                        <datalist id="Second">

                                            <?php 

                                                $sql = "SELECT DISTINCT uname FROM users  WHERE utype = 'Sub Associate' AND admin_ref = '$userUref' 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="SecondFilterValue">

                                    <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">

                        <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>



    <!-- 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">Courses 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() {

        var load_filter_flag = 0;

        var FirstFilterValue = '';

        var SecondFilterValue = '';

        var StatusValue = ''; // Set from URL-based statusId

        var college = '';

        var course = '';

        var StartDate = '';

        var EndDate = '';

        var UserType = '<?php echo $usertype;?>';

        var UserId = '<?php echo $userId;?>';



        $("#filter").click(function() {

            FirstFilterValue = $('#FirstFilterValue').val();

            SecondFilterValue = $('#SecondFilterValue').val();

            college = $('#college').val();

            course = $('#course').val();

            StatusValue = $('#pststus').val();

            StartDate = $('#startDate').val();

            EndDate = $('#endDate').val();



            load_filter_flag = 0;

            if (FirstFilterValue || SecondFilterValue || StatusValue || college || course ||

                StartDate || EndDate) {

                $.ajax({

                    url: 'workActivity-Ajax.php',

                    type: 'POST',

                    data: {

                        FirstFilterValue: FirstFilterValue,

                        SecondFilterValue: SecondFilterValue,

                        StatusValue: StatusValue,

                        college: college,

                        course: course,

                        StartDate: StartDate,

                        EndDate: EndDate,

                        UserType: UserType,

                        UserId: UserId,

                        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: 'workActivity-Ajax.php',

                    type: 'POST',

                    data: {

                        FirstFilterValue: FirstFilterValue,

                        SecondFilterValue: SecondFilterValue,

                        StatusValue: StatusValue,

                        college: college,

                        course: course,

                        StartDate: StartDate,

                        EndDate: EndDate,

                        UserType: UserType,

                        UserId: UserId,

                        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: 'workActivity-Ajax.php',

                type: 'POST',

                data: {

                    UserType: UserType,

                    UserId: UserId,

                    start: start

                },

                success: function(result) {

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

                    load_flag += 10;

                },

                complete: function(data) {

                    /* Hide loader container */

                    $("#loader").hide();

                }

            });

        }



        function loadCalculationSummary() {

            $.ajax({

                url: 'calculation-summary.php',

                type: 'POST',

                data: {

                    load_filter_flag: load_filter_flag,

                    FirstFilterValue: FirstFilterValue,

                    SecondFilterValue: SecondFilterValue,

                    ThirdFilterValue: ThirdFilterValue,

                    StatusValue: StatusValue,

                    college: college,

                    course: course,

                    UserType: UserType,

                    UserId: UserId,

                    StartDate: StartDate,

                    EndDate: EndDate

                },

                success: function(result) {

                    $('#calculation').html(result);

                },

                error: function() {

                    alert('Error loading summary.');

                }

            });

        }



        // Call this function initially or when any filter values change

        loadCalculationSummary();



        $('#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>