<?php 
include "../db.php";
if (isset($_POST['start'])) {
    $start= mysqli_real_escape_string($con,$_POST['start']);
    $AndWhere ='';
    if (!empty($_POST['FirstFilterValue']) && empty($_POST['SecondFilterValue'])) {
        $AndWhere = "AND uname='$_POST[FirstFilterValue]'";
    } elseif (empty($_POST['FirstFilterValue']) && !empty($_POST['SecondFilterValue'])) {
        $AndWhere = "";
    } elseif (!empty($_POST['FirstFilterValue']) && !empty($_POST['SecondFilterValue'])) {
        $AndWhere = "AND uname='$_POST[FirstFilterValue]'";
    }

    $sql = "SELECT * FROM users WHERE (utype = 'Employee' OR utype = 'Manager') AND udisplay !='disable' AND reg_approved != '0' $AndWhere ORDER BY uid DESC LIMIT $start,10";
    $res = mysqli_query($con,$sql);

        if (mysqli_num_rows($res) === 0) {
            header('Content-Type: text/plain');
            echo '__NO_MORE_DATA__';
            exit;
        } else  {
        $c = $start + 1;

        while ($rows = mysqli_fetch_assoc($res)) {

            $ap_total_rows = 0;
            $not_ap_total_rows = 0;
            if(!empty($rows["referal_code"])){
            $ap_sql = "SELECT * FROM users WHERE upload_ref_code = '$rows[referal_code]' AND reg_approved= '1'";
            $ap_res = mysqli_query($con,$ap_sql);
            $ap_total_rows = mysqli_num_rows($ap_res);

            $not_ap_sql = "SELECT * FROM users WHERE upload_ref_code = '$rows[referal_code]' AND reg_approved= '0'";
            $not_ap_res = mysqli_query($con,$not_ap_sql);   
            $not_ap_total_rows = mysqli_num_rows($not_ap_res);
            }

            $state_sql = mysqli_query($con, "SELECT * FROM state WHERE id = '$rows[state]' ");
            if ($state_row = mysqli_fetch_assoc($state_sql)) {
                $state_name = $state_row["name"];
            }

            $dist_sql = mysqli_query($con, "SELECT * FROM dist WHERE id = '$rows[dist]' ");
            if ($dist_row = mysqli_fetch_assoc($dist_sql)) {
                $dist_name = $dist_row["distName"];
            }
            
            $refer_by = '';
            if(!empty($rows["upload_ref_code"])){
                $by_sql = mysqli_query($con, "SELECT uname FROM users WHERE referal_code = '$rows[upload_ref_code]' ");
                if($by_row = mysqli_fetch_assoc($by_sql)){
                    $refer_by = $by_row["uname"];
                }
            }

            $uniqueId = uniqid();
?>
<li class='clearfix' style='float: none;' id="employee<?php echo $rows["uid"]?>">
    <div class="single-dis-admin mb-2">
        <div class='row align-items-center'>
            <div class='col-md-9 col-xs-5'>
                <div class="d-flex justify-content-between align-items-center">
                    <div class="intro-admin">
                        <a style='color:orange;font-size:17px;' class='viewDetails' data-toggle='modal'
                            data-target='#adminDetailsModal'
                            data-name='<?php echo !empty($rows["uname"]) ? $rows["uname"] : "-"; ?>'
                            data-dist='<?php echo !empty($dist_name) ? $dist_name : "-"; ?>'
                            data-pin='<?php echo !empty($rows["pin"]) ? $rows["pin"] : "-"; ?>'
                            data-userid='<?php echo !empty($rows["uphone"]) ? $rows["uphone"] : "-"; ?>'
                            data-pass='<?php echo !empty($rows["upass"]) ? $rows["upass"] : "-"; ?>'
                            data-address='<?php echo !empty($rows["saddress"]) ? $rows["saddress"] : "-"; ?>'
                            data-state='<?php echo !empty($state_name) ? $state_name : "-"; ?>'
                            data-qualification='<?php echo !empty($rows["uqualification"]) ? $rows["uqualification"] : "-"; ?>'
                            data-date='<?php echo !empty($rows["ucreated"]) ? $rows["ucreated"] : "-"; ?>'
                            data-ref='<?php echo !empty($refer_by) ? $refer_by : "-"; ?>'>
                            <i class='fa fa-user' aria-hidden='true'></i> <b><?php echo $rows["uname"];?> &nbsp;
                                <span style="font-size:12px;">( <?php echo $rows["utype"];?> )</span>
                            </b>
                        </a>
                        <br>
                        <a href='tel:<?php echo $rows["ucontact"];?>' class='small text-dark text-decoration-none'
                            title='Click here to Call'><i class='fa fa-phone fa-rotate-90'></i>
                            <?php echo  $rows["ucontact"];?>
                        </a>
                    </div>
                </div>
            </div>

            <div class='col-md-3 col-xs-4 d-flex justify-content-between'>
                <a href='uplodDocuments.php?user=<?php echo $rows["uid"];?>&show_list' class='btn btn-primary btn-sm'
                    title='Click to Upload Documents'>Documents
                </a>
                <a href='workActivity.php?user=<?php echo $rows["uid"];?>' class='btn btn-primary btn-sm'
                    title='Click to Upload Documents'>Work Activity
                </a>
                <a href='view-employee.php?edit_id=<?php echo $rows["uid"];?>' class='btn btn-primary btn-sm'
                    title='Click to Edit'><i class='fa fa-edit' aria-hidden='true'></i>
                </a>
                <br>
                <a href='view-employee.php?del_id=<?php echo $rows["uid"];?>' class='delete btn btn-danger btn-sm'
                    title='Click to Delete'><i class='fa fa-trash' aria-hidden='true'></i> </a>
            </div>
        </div>
    </div>
</li>
<?php
          $c++;
        }
        mysqli_close($con);
    }
}
?>

<!-- Class Modal HTML -->
<div class="modal fade" id="classModal" data-bs-backdrop="static" tabindex="-1" role="dialog"
    aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="myModalLabel">Class Details</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <div class="modal-body">
                <!-- Data will be inserted here by AJAX -->
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
            </div>
        </div>
    </div>
</div>

<!-- Subject Modal HTML -->
<div class="modal fade" id="subjectModal" data-bs-backdrop="static" tabindex="-1" role="dialog"
    aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="myModalLabel">Subject Details</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <div class="modal-body">
                <!-- Data will be inserted here by AJAX -->
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
            </div>
        </div>
    </div>
</div>

<!-- Teacher Modal HTML -->
<div class="modal fade" id="teacherModal" data-bs-backdrop="static" tabindex="-1" role="dialog"
    aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="myModalLabel">Teacher Details</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <div class="modal-body">
                <!-- Data will be inserted here by AJAX -->
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
            </div>
        </div>
    </div>
</div>

<!-- Student Modal HTML -->
<div class="modal fade" id="studentModal" data-bs-backdrop="static" tabindex="-1" role="dialog"
    aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="myModalLabel">Student Details</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <div class="modal-body">
                <!-- Data will be inserted here by AJAX -->
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
            </div>
        </div>
    </div>
</div>

<script>
$(document).ready(function() {
    $('.classBtn').on('click', function() {
        var uref_code = $(this).data('uref-code');
        $.ajax({
            url: 'fetch_class_data.php', // The PHP file that fetches data
            type: 'POST',
            data: {
                uref_code: uref_code // Send the reference code to the server
            },
            success: function(response) {
                // Assuming the response is the HTML to be shown in the modal
                $('#classModal .modal-body').html(response);
                $('#classModal').modal('show');
            },
            error: function(xhr, status, error) {
                console.error(xhr);
            }
        });
    });

    $('.subjectBtn').on('click', function() {
        var uref_code = $(this).data('uref-code');
        $.ajax({
            url: 'fetch_subject_data.php', // The PHP file that fetches data
            type: 'POST',
            data: {
                uref_code: uref_code // Send the reference code to the server
            },
            success: function(response) {
                // Assuming the response is the HTML to be shown in the modal
                $('#subjectModal .modal-body').html(response);
                $('#subjectModal').modal('show');
            },
            error: function(xhr, status, error) {
                console.error(xhr);
            }
        });
    });

    $('.teacherBtn').on('click', function() {
        var uref_code = $(this).data('uref-code');
        $.ajax({
            url: 'fetch_teacher_data.php', // The PHP file that fetches data
            type: 'POST',
            data: {
                uref_code: uref_code // Send the reference code to the server
            },
            success: function(response) {
                // Assuming the response is the HTML to be shown in the modal
                $('#teacherModal .modal-body').html(response);
                $('#teacherModal').modal('show');
            },
            error: function(xhr, status, error) {
                console.error(xhr);
            }
        });
    });

    $('.studentBtn').on('click', function() {
        var uref_code = $(this).data('uref-code');
        $.ajax({
            url: 'fetch_student_data.php', // The PHP file that fetches data
            type: 'POST',
            data: {
                uref_code: uref_code // Send the reference code to the server
            },
            success: function(response) {
                // Assuming the response is the HTML to be shown in the modal
                $('#studentModal .modal-body').html(response);
                $('#studentModal').modal('show');
            },
            error: function(xhr, status, error) {
                console.error(xhr);
            }
        });
    });

    $('.viewDetails').on('click', function(e) {
        e.preventDefault(); // Prevent default anchor tag action

        var name = $(this).data('name');
        $('#modalName').text(name);

        var state = $(this).data('state');
        $('#modalState').text(state);

        var dist = $(this).data('dist');
        $('#modalDist').text(dist);

        var pin = $(this).data('pin');
        $('#modalPin').text(pin);

        var userid = $(this).data('userid');
        $('#modalId').text(userid);

        var pass = $(this).data('pass');
        $('#modalPass').text(pass);

        var qualification = $(this).data('qualification');
        $('#modalQualification').text(qualification);

        var address = $(this).data('address');
        $('#modalAddress').text(address);

        var date = $(this).data('date');
        $('#modalDate').text(date);

        var ref = $(this).data('ref');
        $('#modalRef').text(ref);

        $('#adminDetailsModal').modal('show'); // Open the modal
    });
});
</script>