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

    if (empty($_GET['st']) ) {
    	header("Location: index.php");
    }
    $st_run = mysqli_query($con, "SELECT s_name FROM status WHERE s_id = '$_GET[st]'");
    $st_row = mysqli_fetch_assoc($st_run);
    
    $adm_sql = mysqli_query($con, "SELECT * FROM users WHERE uid = '$_SESSION[uid]' ");
    if($adm_row = mysqli_fetch_assoc($adm_sql)){
        $uadmin = $adm_row["uadmin"];
        $admin_ref = $adm_row["admin_ref"];
    }
?>

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

<head>
    <?php include "include/meta.php"; ?>
    <?php include "include/css.php"; ?>
    <?php include "include/php-functions.php"; ?>
    <script>
    let SESS = [];
    SESS['uid'] = "<?php echo $_SESSION['uid']; ?>";
    SESS['uref'] = "<?php echo $_SESSION['uref']; ?>";
    SESS['uadmin'] = "<?php echo $uadmin;?>";
    // console.log(SESS)
    </script>
</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">
                    <h5 class="card-header text-dark text-center font-weight-bold"><?php echo $st_row['s_name']; ?>
                        Details</h5>
                    <div class="card-body">
                        <div class="row">
                            <div class="col-md-3 mb-2">
                                <div class="form-group">
                                    <font>Name</font>
                                    <input type="text" list="Third"
                                        class="form-control form-control-sm ThirdFilterValue">
                                    <datalist id="Third">
                                        <?php 
                                          $sql = "SELECT st_name FROM student_details WHERE st_by = '$_SESSION[uid]' AND st_status= '$_GET[st]' ORDER BY st_no DESC";
                                          $res = mysqli_query($con,$sql);
                                          while ($row = mysqli_fetch_assoc($res)) {
                                          ?>
                                        <option value="<?php echo $row['st_name']; ?>"><?php echo $row['st_name']; ?>
                                        </option>
                                        <?php } ?>
                                    </datalist>
                                </div>
                            </div>
                            <div class="col-md-2 mb-2">
                                <div class="form-group">
                                    <font>Start Dt</font>
                                    <input type="date" class="form-control form-control-sm" id="First1FilterValue">
                                </div>
                            </div>
                            <div class="col-md-2 mb-2">
                                <div class="form-group">
                                    <font>End Dt</font>
                                    <input type="date" class="form-control form-control-sm" id="First2FilterValue">
                                </div>
                            </div>
                            <div class="col-md-2 mb-2">
                                <div class="form-group">
                                    <font>FollowUp St Dt</font>
                                    <input type="date" class="form-control form-control-sm" id="Second1FilterValue">
                                </div>
                            </div>
                            <div class="col-md-2 mb-2">
                                <div class="form-group">
                                    <font>FollowUp End Dt</font>
                                    <input type="date" class="form-control form-control-sm" id="Second2FilterValue">
                                </div>
                            </div>
                            <div class="col-md-1 mb-2 d-flex align-items-end">
                                <div class="filter-btn">
                                    <button type="button" id="filter" class="btn btn-primary btn-sm"><i
                                            class="fa fa-search"></i></button>
                                </div>
                            </div>
                        </div>
                        <hr>

                        <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-left: 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-left: 0;">
                                </ul>
                            </div>

                        </div>
                        <ul class="pager">
                            <li class="previous" id="first-page" style="cursor: -webkit-grab; cursor: grab;"><a>First
                                    Page</a></li>
                            <li class="next" id="next-page" style="cursor: -webkit-grab; cursor: grab;"><a>Next Page</a>
                            </li>
                        </ul>
                    </div>
                </div>
            </div>
        </div>

        <?php include "include/footer.php"; ?>
    </section>
    <!-- Modal -->
    <div class="modal fade" id="remarkModal" tabindex="-1" aria-labelledby="modalTitle" aria-hidden="true">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title badge bg-primary text-light" id="modalTitle"></h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                </div>
                <div class="modal-body">
                    <span id="remarkShow"></span>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-warning text-dark" data-bs-dismiss="modal">Close</button>
                </div>
            </div>
        </div>
    </div>
    <!-- JavaScript Libraries -->

    <?php include "include/js.php"; ?>
    <script>
    //dropdown TWO filter Start//
    $(document).ready(function() {

        var load_filter_flag = '';
        var First1FilterValue = '';
        var First2FilterValue = '';
        var Second1FilterValue = '';
        var Second2FilterValue = '';
        var ThirdFilterValue = '';
        $("#filter").click(function() {
            First1FilterValue = $('#First1FilterValue').val();
            First2FilterValue = $('#First2FilterValue').val();
            Second1FilterValue = $('#Second1FilterValue').val();
            Second2FilterValue = $('#Second2FilterValue').val();
            ThirdFilterValue = $('.ThirdFilterValue').val();
            load_filter_flag = 0;
            if (First1FilterValue != '' || First2FilterValue != '' || Second1FilterValue != '' ||
                Second2FilterValue != '' || ThirdFilterValue != '') {
                //alert(Second1FilterValue+Second2FilterValue);
                $.ajax({
                    url: 'status-Ajax.php',
                    type: 'post',
                    data: {
                        st: st,
                        First1FilterValue: First1FilterValue,
                        First2FilterValue: First2FilterValue,
                        Second1FilterValue: Second1FilterValue,
                        Second2FilterValue: Second2FilterValue,
                        ThirdFilterValue: ThirdFilterValue,
                        start: load_filter_flag
                    },
                    success: function(result) {
                        $('#full-table').hide();
                        $('#filter-table').show();
                        $('#filter-table-data').html(result);
                        load_filter_flag += 25;
                    },
                    complete: function(data) {
                        /* Hide loader container */
                        $("#loader").hide();
                    }
                });
            } else {
                alert('Plz Select an Option');
                $('#full-table').show();
                $('#filter-table').hide();
            }
        });


        function loadMoreFilter(start) {
            console.log('Filter' + start);
            if (load_filter_flag > 0) {
                //alert(name);
                $.ajax({
                    url: 'status-Ajax.php',
                    type: 'post',
                    data: {
                        st: st,
                        First1FilterValue: First1FilterValue,
                        First2FilterValue: First2FilterValue,
                        Second1FilterValue: Second1FilterValue,
                        Second2FilterValue: Second2FilterValue,
                        ThirdFilterValue: ThirdFilterValue,
                        start: start
                    },
                    success: function(result) {
                        $('#filter-table-data').html(result);
                        load_filter_flag += 25;
                    }
                });

            }
        }
        // Scroll Loading of Full Data //
        var load_flag = 0;
        var st = "<?php echo $_GET['st'] ?>";
        loadMore(load_flag);

        function loadMore(start) {
            //console.log('total'+start);
            $.ajax({
                url: 'status-Ajax.php',
                type: 'post',
                data: {
                    st: st,
                    start: start
                },
                success: function(result) {
                    $('#full-table-data').html(result);
                    load_flag += 25;
                },
                complete: function(data) {
                    /* Hide loader container */
                    $("#loader").hide();
                }
            });
        }


        $('#next-page').click(function() {
            console.log('next-page');
            if (load_filter_flag > 0) {
                loadMoreFilter(load_filter_flag);
            } else {
                loadMore(load_flag);
            }
        });

        $('#first-page').click(function() {
            console.log('first-page');
            if (load_filter_flag > 0) {
                //console.log('first-filter-page');
                $("#filter").trigger("click");
            } else {
                // console.log('first-real-page');
                load_flag = 0;
                loadMore(load_flag);
            }
        });

    });
    </script>
</body>

</html>