<?php 
include "../../db.php";

if (isset($_POST['start'])) {
    $start= mysqli_real_escape_string($con,$_POST['start']);
    $AndWhere ='';

    if ( !empty($_POST['FirstFilterValue']) ) {
        $AndWhere .= "AND u.uname='$_POST[FirstFilterValue]' ";
    }

    if ( !empty($_POST['SecondFilterValue']) ) {
        $sf_sql = "SELECT uref_code FROM users WHERE utype='Admin' AND uname='$_POST[SecondFilterValue]'";
        $sf_run = mysqli_query($con, $sf_sql);
        $sf_rows = mysqli_fetch_assoc($sf_run);
        $AndWhere .= "AND u.admin_ref='$sf_rows[uref_code]' ";
    }

    if (!empty($_POST['ThirdFilterValue'])) {
        $AndWhere .="AND u.uphone='$_POST[ThirdFilterValue]' ";
    }

    if (!empty($_POST['FourthFilterValue'])) {
        $state_name = $_POST["FourthFilterValue"];
        $state_sql = mysqli_query($con, "SELECT * FROM state WHERE name = '$_POST[FourthFilterValue]'");
        if($state_row = mysqli_fetch_assoc($state_sql)){
            $AndWhere = "AND u.state = '$state_row[id]' ";
        }
    }
    if (!empty($_POST['FifthFilterValue'])) {
        $dist_name = $_POST["FifthFilterValue"];
        $dist_sql = mysqli_query($con, "SELECT * FROM dist WHERE distName = '$_POST[FifthFilterValue]'");
        if($dist_row = mysqli_fetch_assoc($dist_sql)){
            $AndWhere = "AND u.dist = '$dist_row[id]' ";
        }
    }

    $sql = "SELECT  u.*, f.sid, f.fmonth, f.amount, f.fdate FROM users AS u LEFT JOIN fees AS f ON u.uid = f.sid WHERE u.utype = 'student' AND u.udisplay ='disable' $AndWhere GROUP BY u.uname LIMIT $start,10";

    $res = mysqli_query($con,$sql);
    if ($total_rows=mysqli_num_rows($res)>0) {
        $c = $start+1;

        while ($rows=mysqli_fetch_assoc($res)) {
            $m_sql = "SELECT uname, uref FROM users WHERE uref_code = '$rows[admin_ref]' ";
            $m_run = mysqli_query($con, $m_sql);
            $m_rows = mysqli_fetch_assoc($m_run);

            $class_sql = mysqli_query($con, "SELECT sclass FROM class WHERE sid = '$rows[sclass]' ");
            if($class_row = mysqli_fetch_assoc($class_sql)){
                $class_name = $class_row["sclass"];
            }
            $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-9'>
                <a style='color:orange;font-size:17px;cursor:pointer;' class='viewDetails' data-toggle='modal'
                    data-target='#studentDetailsModal' 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-classs='<?php echo $class_name;?>' data-date='<?php echo $rows["ucreated"];?>'>
                    <i class='fa fa-user' 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-school'></i> </b><?php echo $m_rows["uname"];?>
            </div>

            <div class='col-md-2 col-xs-3'>
                <div class="text-end">
                    <a href='view-student.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-student.php?del_id=<?php echo $rows["uid"];?>' class='m-2 btn btn-success'
                        title='Click to Recycle'><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 classs = $(this).data('classs');
        $('#modalClasss').text(classs);

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

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

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

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

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