<?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 wp_name='$_POST[FirstFilterValue]'";
    } elseif (empty($_POST['FirstFilterValue']) && !empty($_POST['SecondFilterValue'])) {
        $AndWhere = "";
    } elseif (!empty($_POST['FirstFilterValue']) && !empty($_POST['SecondFilterValue'])) {
        $AndWhere = "AND wp_name='$_POST[FirstFilterValue]'";
    }

    $sql = "SELECT * FROM work_place WHERE wp_recycle !='0' $AndWhere ORDER BY wp_id 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)) {

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

            $dist_sql = mysqli_query($con, "SELECT * FROM dist WHERE id = '$rows[wp_dist_id]' ");
            if ($dist_row = mysqli_fetch_assoc($dist_sql)) {
                $dist_name = $dist_row["distName"];
            }

            $uniqueId = uniqid();
?>
<li class='clearfix' style='float: none;' id="employee<?php echo $rows["wp_id"]?>">
    <div class="single-dis-admin mb-2">
        <div class='row align-items-center'>
            <div class='col-md-10 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["wp_name"]) ? $rows["wp_name"] : "-"; ?>'
                            data-dist='<?php echo !empty($dist_name) ? $dist_name : "-"; ?>'
                            data-pin='<?php echo !empty($rows["wp_pin"]) ? $rows["wp_pin"] : "-"; ?>'
                            data-userid='<?php echo !empty($rows["wp_contact"]) ? $rows["wp_contact"] : "-"; ?>'
                            data-pass='<?php echo !empty($rows["wp_password"]) ? $rows["wp_password"] : "-"; ?>'
                            data-address='<?php echo !empty($rows["wp_address"]) ? $rows["wp_address"] : "-"; ?>'
                            data-state='<?php echo !empty($state_name) ? $state_name : "-"; ?>'
                            data-date='<?php echo !empty($rows["wp_cDate"]) ? $rows["wp_cDate"] : "-"; ?>'>
                            <i class='fa fa-hotel' aria-hidden='true'></i> <b><?php echo $rows["wp_name"];?>
                            </b>
                        </a>
                        <br>
                        <a href='tel:<?php echo $rows["wp_contact"];?>' class='small text-dark text-decoration-none'
                            title='Click here to Call'><i class='fa fa-phone fa-rotate-90'></i>
                            <?php echo  $rows["wp_contact"];?>
                        </a>
                    </div>
                </div>
            </div>

            <div class='col-md-2 col-xs-4 d-flex justify-content-between'>
                <a href='view-work.php?vendor_id=<?php echo $rows["wp_id"];?>' class='btn btn-primary btn-sm'
                    title='Click to view work list'>Work List
                </a>
                <a href='view-vendor.php?edit_id=<?php echo $rows["wp_id"];?>' class='btn btn-primary btn-sm'
                    title='Click to Edit'><i class='fa fa-edit' aria-hidden='true'></i>
                </a>
                <br>
                <a href='view-vendor.php?del_id=<?php echo $rows["wp_id"];?>'
                    class='delete btn btn-warning btn-sm text-white' 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 address = $(this).data('address');
        $('#modalAddress').text(address);

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

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