<?php 
include "../../db.php";
if (isset($_POST['start'])) {
    $start= mysqli_real_escape_string($con,$_POST['start']);
    $AndWhere ='';

    if (!empty($_POST['FirstFilterValue']) && !empty($_POST['SecondFilterValue']) && !empty($_POST['ThirdFilterValue'])) {
        $firstFilterValue = mysqli_real_escape_string($con, $_POST['FirstFilterValue']);
        $secondFilterValue = mysqli_real_escape_string($con, $_POST['SecondFilterValue']);
        $thirdFilterValue = mysqli_real_escape_string($con, $_POST['ThirdFilterValue']);
    
        $AndWhere = "AND uname='$firstFilterValue'";
    
        $state_sql = mysqli_query($con, "SELECT * FROM state WHERE name = '$secondFilterValue'");
        if ($state_row = mysqli_fetch_assoc($state_sql)) {
            $AndWhere .= " AND state = '$state_row[id]'";
        }
    
        $dist_sql = mysqli_query($con, "SELECT * FROM dist WHERE distName = '$thirdFilterValue'");
        if ($dist_row = mysqli_fetch_assoc($dist_sql)) {
            $AndWhere .= " AND dist = '$dist_row[id]'";
        }
    }elseif (empty($_POST['FirstFilterValue']) && !empty($_POST['SecondFilterValue']) && !empty($_POST['ThirdFilterValue'])) {
        $secondFilterValue = mysqli_real_escape_string($con, $_POST['SecondFilterValue']);
        $thirdFilterValue = mysqli_real_escape_string($con, $_POST['ThirdFilterValue']);
    
        $state_sql = mysqli_query($con, "SELECT * FROM state WHERE name = '$secondFilterValue'");
        if ($state_row = mysqli_fetch_assoc($state_sql)) {
            $AndWhere = " AND state = '$state_row[id]'";
        }
    
        $dist_sql = mysqli_query($con, "SELECT * FROM dist WHERE distName = '$thirdFilterValue'");
        if ($dist_row = mysqli_fetch_assoc($dist_sql)) {
            $AndWhere .= " AND dist = '$dist_row[id]'";
        }
    }elseif (!empty($_POST['FirstFilterValue']) && empty($_POST['SecondFilterValue']) && empty($_POST['ThirdFilterValue'])) {
        $AndWhere = "AND uname='$_POST[FirstFilterValue]'";
    }elseif (empty($_POST['FirstFilterValue']) && !empty($_POST['SecondFilterValue']) && empty($_POST['ThirdFilterValue'])) {
        $state_name = $_POST["SecondFilterValue"];
        $state_sql = mysqli_query($con, "SELECT * FROM state WHERE name = '$_POST[SecondFilterValue]'");
        if($state_row = mysqli_fetch_assoc($state_sql)){
            $AndWhere = "AND state = '$state_row[id]' ";
        }
    }elseif (empty($_POST['FirstFilterValue']) && empty($_POST['SecondFilterValue']) && !empty($_POST['ThirdFilterValue'])) {
        $dist_name = $_POST["ThirdFilterValue"];
        $dist_sql = mysqli_query($con, "SELECT * FROM dist WHERE distName = '$_POST[ThirdFilterValue]'");
        if($dist_row = mysqli_fetch_assoc($dist_sql)){
            $AndWhere = "AND dist = '$dist_row[id]' ";
        }
    }
    

    $sql = "SELECT * FROM users  WHERE utype = 'Teacher' AND udisplay ='disable' $AndWhere  ORDER BY uid DESC LIMIT $start,10";
    $res = mysqli_query($con,$sql);
    if ($total_rows=mysqli_num_rows($res)>0) {
        $s_output='';
        $c = $start+1;
        while ($rows=mysqli_fetch_assoc($res)) {
            $m_sql = "SELECT uname, uref_code FROM users WHERE uref_code = '$rows[admin_ref]' ";
            $m_run = mysqli_query($con, $m_sql);
            $m_rows = mysqli_fetch_assoc($m_run);

            $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"];
            }
           ?>
<li class='clearfix' style='float: none;'>
    <div class="single-dis-admin mb-2">
        <div class='row align-items-center'>
            <div class='col-md-10 col-xs-8'>
                <a style='color:orange;font-size:17px;cursor:pointer;' class='viewDetails' data-toggle='modal'
                    data-target='#teacherDetailsModal' data-name='<?php echo $rows["uname"];?>'
                    data-state='<?php echo $state_name;?>' data-dist='<?php echo $dist_name;?>'
                    data-pin='<?php echo $rows["pin"];?>' data-userid='<?php echo $rows["uphone"];?>'
                    data-pass='<?php echo $rows["upass"];?>' data-address='<?php echo $rows["saddress"];?>'
                    data-qualification='<?php echo $rows["uqualification"];?>'
                    data-date='<?php echo $rows["ucreated"];?>'>
                    <i class='fa fa-user-circle' aria-hidden='true'></i> <b><?php echo $rows["uname"];?></b>
                </a>

                <br>
                <a href='tel:<?php echo $rows["uphone"];?>' class='small text-dark text-decoration-none'
                    title='Click here to Call'> <i class='fa fa-phone fa-rotate-90'></i> <?php echo $rows["uphone"];?>
                </a>

                &nbsp;
                <b><i class='fa fa-user'></i> </b><?php echo $m_rows["uname"];?>
            </div>
            <div class='col-md-2 col-xs-4'>
                <div class="text-end">
                    <a href='view-teacher.php?edit_id=<?php echo $rows["uid"];?>' class='m-2 btn btn-success'
                        title='Click to Edit'><i class='fa fa-edit' aria-hidden='true'></i>
                    </a>
                    <a href='view-teacher.php?del_id=<?php echo $rows["uid"];?>' class='m-2 btn btn-success'
                        title='Click to Restore'><i class='fa fa-recycle' aria-hidden='true'></i> </a>
                </div>
            </div>
        </div>
    </div>
</li>
<?php
          $c++;
        }
        mysqli_close($con);
        // echo $s_output;
    }
}

?>


<script>
$(document).ready(function() {
    $('.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);

        $('#teacherDetailsModal').modal('show'); // Open the modal
    });

    $('.viewNotes').on('click', function() {
        // var uname = $(this).data('uname');
        var tref = $(this).data('tref');
        // $('#modalUname').text(uname);
        $.ajax({
            url: 'view-teacher-notes_ajax.php?tref=' + tref,
            data: tref,
            method: 'GET',
            success: function(data) {
                $('#feedetails').html(data);
            }
        });
    });
});
</script>