<?php
session_start();
include "../db.php";

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

    if (!empty($_POST['FirstFilterValue'])) {
        $AndWhere .= "AND product ='" . mysqli_real_escape_string($con, $_POST['FirstFilterValue']) . "' ";
    }

    if (!empty($_POST['ThirdFilterValue'])) {
        $AndWhere .= "AND product ='" . mysqli_real_escape_string($con, $_POST['ThirdFilterValue']) . "' ";
    }

    $sql = "SELECT * FROM payment WHERE p_forProduct = '$product_id' AND p_client = '$client_id'  $AndWhere ORDER BY p_id DESC 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 * FROM users WHERE uid = '" . mysqli_real_escape_string($con, $client_id) . "'";
            $m_run = mysqli_query($con, $m_sql);
            $m_rows = mysqli_fetch_assoc($m_run);

            if($m_rows["admin_ref"] == $_SESSION["uref_code"]){
            // Fetch services for the selected category
            $product_sql = mysqli_query($con, "SELECT * FROM product WHERE pid = '$product_id' ");
            if($product_row = mysqli_fetch_assoc($product_sql)){
                $service_sql = mysqli_query($con, "SELECT * FROM data WHERE data_catagory = 'service' AND data_sl_no = '$product_row[product]' ");
    
                while ($service_row = mysqli_fetch_assoc($service_sql)) {
                    $product = $service_row["data_title"];
                }
            }
?>
<li class='clearfix' style='float: none;'>
    <div class="single-dis-admin mb-2">
        <div class='row align-items-center'>
            <div class='col-md-8 col-12'>
                <p style='color:orange;font-size:17px;cursor:pointer;margin-bottom:0;'>
                    <i class='fa fa-wrench' aria-hidden='true'></i> <b><?php echo $product;?></b> (
                    <span style="color:black;"><strong style="color:orange;"><i class="fa fa-inr"></i> Payed Amount
                            : </strong> <?php echo $rows["payment"];?></span> ) <br>
                    <span style="color:black;"><?php echo $rows["p_des"];?></span>
                </p>
            </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 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
    });

});
</script>