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

    if ( $_SESSION['utype'] != 'master_admin' && $_SESSION['uref'] != 'fssdf4543frts' ) {
        header("Location: logout.php");
        exit();
    }
    $admin_sql = mysqli_query($con, "SELECT * FROM users WHERE uid = '' ");
?>

<!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'] = "";
    </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">
                    <div class="card-header">
                        <div class="row">
                            <div class="col-8">
                                <h5 class="text-dark text-center font-weight-bold">All Candidate Details</h5>
                            </div>
                            <div class="col-4 text-end">
                                <a href="new_entry.php" class="btn btn-primary btn-sm">Add New</a>
                            </div>
                        </div>
                    </div>
                    <div class="card-body">
                        <!--  Count  -->
                        <?php  
                        $count_sql = "SELECT * FROM student_details
                        WHERE ( st_name !='' && st_name IS NOT NULL )
                        AND  `flag_status` ='A' ";
                                        
                        $count_res = mysqli_query($con,$count_sql);
                        $count_total_rows=mysqli_num_rows($count_res) ;
                        if ($count_total_rows > 0) {
                            echo 
                            "<div class='col-md-12 text-center'>
                            <b><i class='fa fa-check-square' aria-hidden='true' style='color:green;'></i> Total Candidate : $count_total_rows &nbsp; </b><hr>
                            </div>";
                        }
                        ?>
                        <br>
                        <div class="row filter">
                            <ul class="search-details">
                                <li>
                                    <div class="form-group">
                                        <font>Candidate</font>
                                        <input type="text" list="Third" class="form-control form-control-sm"
                                            placeholder="Search By Candidate Name" id="ThirdFilterValue">
                                        <datalist id="Third">
                                            <?php 
                                            $sql = "SELECT st_name FROM student_details ORDER BY st_name ASC";
                                            $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>
                                </li>
                                <li>
                                    <div class="form-group">
                                        <font>Start Dt</font>
                                        <input type="date" class="form-control form-control-sm" id="First1FilterValue">
                                    </div>
                                </li>
                                <li>
                                    <div class="form-group">
                                        <font>End Dt</font>
                                        <input type="date" class="form-control form-control-sm" id="First2FilterValue">
                                    </div>
                                </li>
                                <li>
                                    <div class="form-group">
                                        <font>Lead Source</font>
                                        <input type="text" list="b" class="form-control form-control-sm"
                                            id="aFilterValue">
                                        <datalist id="b">
                                            <?php 
                                            $sql = "SELECT DISTINCT lead_source_name FROM student_details WHERE lead_source_name != '' ORDER BY lead_source_name DESC";
                                            $res = mysqli_query($con,$sql);
                                            while ($row = mysqli_fetch_assoc($res)) {
                                            ?>
                                            <option value="<?php echo $row['lead_source_name']; ?>">
                                                <?php echo $row['lead_source_name']; ?></option>
                                            <?php } ?>
                                        </datalist>
                                    </div>
                                </li>
                                <li class="d-flex align-items-end">
                                    <div class="form-group">
                                        <font>Source Form</font>
                                        <select class="form-control form-control-sm" id="bFilterValue">
                                            <option value="">select</option>
                                            <?php 
                                            $sql = "SELECT id, lead_source_form_name FROM lead_source WHERE upload_by = '$_SESSION[uref]' ORDER BY lead_source_form_name ASC";
                                            $res = mysqli_query($con,$sql);
                                            while ($row = mysqli_fetch_assoc($res)) {
                                            ?>
                                            <option value="<?php echo $row['id']; ?>">
                                                <?php echo $row['lead_source_form_name']; ?></option>
                                            <?php } ?>
                                        </select>
                                    </div>
                                    <div class="filter-btn">
                                        <button type="button" id="filter" class="btn btn-primary btn-sm"><i
                                                class="fa fa-search"></i></button>
                                    </div>
                                </li>
                            </ul>
                        </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>

        <!--=============Modals Start================= -->
        <div class="modal fade" id="remarkModal" tabindex="-1" aria-labelledby="modalName" aria-hidden="true">
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header">
                        <h5 class="modal-name badge bg-primary text-light" id="modalName"></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>

        <!--==============Modals End================ -->
        <?php include "include/footer.php"; ?>
    </section>

    <!-- 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 = '';

        $("#filter").click(function() {
            First1FilterValue = $('#First1FilterValue').val();
            First2FilterValue = $('#First2FilterValue').val();
            SecondFilterValue = $('.SecondFilterValue').val();
            ThirdFilterValue = $('#ThirdFilterValue').val();

            aFilterValue = $('#aFilterValue').val();
            bFilterValue = $('#bFilterValue').val();

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

                        aFilterValue: aFilterValue,
                        bFilterValue: bFilterValue,

                        uid: SESS['uid'],
                        uref: SESS['uref'],
                        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: 'status-Ajax.php',
                    type: 'post',
                    data: {
                        First1FilterValue: First1FilterValue,
                        First2FilterValue: First2FilterValue,
                        SecondFilterValue: SecondFilterValue,
                        ThirdFilterValue: ThirdFilterValue,

                        aFilterValue: aFilterValue,
                        bFilterValue: bFilterValue,

                        uid: SESS['uid'],
                        uref: SESS['uref'],
                        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: 'status-Ajax.php',
                type: 'post',
                data: {
                    uid: SESS['uid'],
                    uref: SESS['uref'],
                    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>