<?php 

include "top.php";


if(isset($_POST['action']) && $_POST['action'] == 'search_customers') {
    $term = $_POST['term'];
    $term = mysqli_real_escape_string($con, $term);
    
    $query = "SELECT id, name, phone, father_name, safno, landmark, custid, area_name, phone_off, note, packpr 
              FROM adduser 
              WHERE (name LIKE '%$term%' OR 
                    phone LIKE '%$term%' OR 
                    father_name LIKE '%$term%' OR 
                    safno LIKE '%$term%' OR 
                    landmark LIKE '%$term%' OR 
                    custid LIKE '%$term%' OR 
                    area_name LIKE '%$term%' OR 
                    phone_off LIKE '%$term%' OR 
                    note LIKE '%$term%')
              LIMIT 20";
    
    $result = mysqli_query($con, $query);
    
    if(mysqli_num_rows($result) > 0) {
        echo '<div class="table-responsive">
                <h5>Click to Choose Customer</h5>
                <table class="table table-striped table-hover table-bordered">
                    <thead>
                        <tr>
                            <th>AC No</th>
                            <th>Name</th>
                            <th>Phone</th>
                            <th>Father</th>
                            <th>SAF No</th>
                            <th>Landmark</th>
                            
                            <th>Area</th>
                            <th>Phone (Office)</th>
                            <th>Note</th>
                            <th>Package Price</th>
                        </tr>
                    </thead>
                    <tbody>';
        
        while($row = mysqli_fetch_assoc($result)) {
            echo '<tr style="cursor:pointer;" onclick="selectCustomer(\''.$row['custid'].'\')">
                    <td>'.htmlspecialchars($row['custid']).'</td>
                    <td>'.htmlspecialchars($row['name']).'</td>
                    <td>'.htmlspecialchars($row['phone']).'</td>
                    <td>'.htmlspecialchars($row['father_name']).'</td>
                    <td>'.htmlspecialchars($row['safno']).'</td>
                    <td>'.htmlspecialchars($row['landmark']).'</td>
                    
                    <td>'.htmlspecialchars($row['area_name']).'</td>
                    <td>'.htmlspecialchars($row['phone_off']).'</td>
                    <td>'.htmlspecialchars($row['note']).'</td>
                    <td>'.htmlspecialchars($row['packpr']).'</td>
                </tr>';
        }
        
        echo '</tbody></table></div>';
    } else {
        echo '<div class="alert alert-info">No customers found</div>';
    }
    exit;
}

?>