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

    $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">Global Candidate Details</h5>
                    <div class="card-body">
                        <!--  Count  -->
                        <?php  
                        $count_sql = "SELECT * FROM student_details WHERE `st_admin` ='".$uadmin."' AND ( st_name !='' && st_name IS NOT NULL ) AND ( st_by='' || st_by IS NULL ) AND `flag_status` ='A'";
                        $count_res = mysqli_query($con,$count_sql);
                        $count_total_rows=mysqli_num_rows($count_res) ;
                            echo "<div class='col-md-12 text-center'>
                                    <b><i class='fa fa-check-square' aria-hidden='true' style='color:green;'></i> Total Data : $count_total_rows &nbsp;</b><hr>
                                </div>";
                        ?>
                        <br>
                        <hr>
                        <div class="row">
                            <div class="col-md-3 mb-2">
                                <div class="form-group">
                                    <font>Name</font>
                                    <input type="text" list="Third" plaeholder="Select Name"
                                        class="form-control form-control-sm ThirdFilterValue">
                                    <datalist id="Third">
                                        <?php 
                                            $sql = "SELECT st_name FROM student_details WHERE st_by = '$_SESSION[uid]'  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-3 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-3 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-3 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>
    <div class="modal fade" id="remarkModal" tabindex="-1" role="dialog" aria-labelledby="modelTitleId"
        aria-hidden="true">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-name badge badge-primary text-light" id="modalName"></h5>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                </div>
                <div class="modal-body">

                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-warning text-dark" data-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 SecondFilterValue = '';
        var ThirdFilterValue = '';
        var aFilterValue = '';
        var bFilterValue = '';
        var cFilterValue = '';

        var a2FilterValue = '';
        var b2FilterValue = '';
        $("#filter").click(function() {
            First1FilterValue = $('#First1FilterValue').val();
            First2FilterValue = $('#First2FilterValue').val();
            SecondFilterValue = $('.SecondFilterValue').val();
            ThirdFilterValue = $('.ThirdFilterValue').val();
            aFilterValue = $('#aFilterValue').val();
            bFilterValue = $('#bFilterValue').val();
            cFilterValue = $('#cFilterValue').val();

            a2FilterValue = $('#a2FilterValue').val();
            b2FilterValue = $('#b2FilterValue').val();

            load_filter_flag = 0;
            if (First1FilterValue != '' || First2FilterValue != '' || SecondFilterValue != '' ||
                ThirdFilterValue != '' || aFilterValue != '' || bFilterValue != '' || cFilterValue !=
                '' || a2FilterValue != '' || b2FilterValue != '') {
                //alert(name);
                $.ajax({
                    url: 'global-Data-Ajax.php',
                    type: 'post',
                    data: {
                        First1FilterValue: First1FilterValue,
                        First2FilterValue: First2FilterValue,
                        SecondFilterValue: SecondFilterValue,
                        ThirdFilterValue: ThirdFilterValue,
                        aFilterValue: aFilterValue,
                        bFilterValue: bFilterValue,
                        cFilterValue: cFilterValue,

                        a2FilterValue: a2FilterValue,
                        b2FilterValue: b2FilterValue,

                        uid: SESS['uid'],
                        uadmin: SESS['uadmin'],

                        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: 'global-Data-Ajax.php',
                    type: 'post',
                    data: {
                        First1FilterValue: First1FilterValue,
                        First2FilterValue: First2FilterValue,
                        SecondFilterValue: SecondFilterValue,
                        ThirdFilterValue: ThirdFilterValue,
                        aFilterValue: aFilterValue,
                        bFilterValue: bFilterValue,
                        cFilterValue: cFilterValue,

                        a2FilterValue: a2FilterValue,
                        b2FilterValue: b2FilterValue,

                        uid: SESS['uid'],
                        uadmin: SESS['uadmin'],

                        start: start
                    },
                    success: function(result) {
                        $('#filter-table-data').html(result);
                        load_filter_flag += 25;
                    }
                });

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

        function loadMore(start) {
            //console.log('total'+start);
            $.ajax({
                url: 'global-Data-Ajax.php',
                type: 'post',
                data: {

                    uid: SESS['uid'],
                    uadmin: SESS['uadmin'],
                    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>