<?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 = 'Admin' 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)) {
            $c_sql = mysqli_query($con, "SELECT sid FROM class WHERE uploadedby = '$rows[uref_code]'");
            $class_count = mysqli_num_rows($c_sql);

            $s_sql = mysqli_query($con, "SELECT sc_id FROM subject WHERE uploadedby = '$rows[uref_code]'");
            $subject_count = mysqli_num_rows($s_sql);

            $t_sql = mysqli_query($con, "SELECT uid FROM users WHERE utype = 'Teacher' AND admin_ref = '$rows[uref_code]'");

            $teacher_count = mysqli_num_rows($t_sql);

            $st_sql = mysqli_query($con, "SELECT uid FROM users WHERE utype = 'Student' AND admin_ref = '$rows[uref_code]'");

            $student_count = mysqli_num_rows($st_sql);

            //referance count start//
            $ap_total_rows= 0;
            $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_total_rows= 0;
            $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"];
            }

            //referance count end//
            ?>
<li class='clearfix' style='float: none;'>
    <div class="single-dis-admin mb-2">
        <div class='row align-items-center'>
            <div class='col-md-5 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 $rows["uname"];?>'
                            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-state='<?php echo $state_name;?>'
                            data-qualification='<?php echo $rows["uqualification"];?>'
                            data-date='<?php echo $rows["ucreated"];?>'>
                            <i class='fa fa-school' aria-hidden='true'></i> <b><?php echo $rows["uname"];?></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>
                        <a href='add-admin-payment.php?uref_id=<?php echo $rows["uref_code"];?>'
                            class='btn btn-primary'>Pay</a>
                        <a href='view-under-ref-user.php?uref_id=<?php echo $rows["uref_code"];?>'
                            class='btn btn-primary'>Ref</a>
                    </div>
                </div>
            </div>

            <div class='col-md-6 col-xs-4 d-flex justify-content-between'>
                <p class='btn btn-success mb-0'>
                    <span class='font-weight-bold'>Class : <?php echo $class_count;?></span>
                </p>
                <p class='btn btn-success mb-0'>
                    <span class='font-weight-bold'>Subject : <?php echo $subject_count;?></span>
                </p>
                <p class='btn btn-success mb-0'>
                    <span class='font-weight-bold'>Teacher : <?php echo $teacher_count;?></span>
                </p>
                <p class='btn btn-success mb-0'>
                    <span class='font-weight-bold'>Student : <?php echo $student_count;?></span>
                </p>
                <p class='btn btn-success mb-0'>
                    <i class='fa fa-line-chart'></i> &nbsp; <?php echo  $ap_total_rows;?>
                </p>
                <p class='btn btn-success mb-0'>
                    <i class='fa fa-ban'></i> &nbsp; <?php echo $not_ap_total_rows;?>
                </p>
            </div>
            <div class='col-md-1 col-xs-3 d-flex justify-content-between'>
                <a href='view-admin.php?edit_id=<?php echo $rows["uid"];?>' class='btn btn-primary'
                    title='Click to Edit'><i class='fa fa-edit' aria-hidden='true'></i>
                </a>
                <br>
                <a href='view-admin.php?del_id=<?php echo $rows["uid"];?>' class='btn btn-success'
                    title='Click to Recycle'><i class='fa fa-recycle' aria-hidden='true'></i> </a>
            </div>
        </div>
    </div>
</li>
<?php
          $c++;
        }
        mysqli_close($con);
    }
}
?>


<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);

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