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

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

    // Get products in batch
    $query = mysqli_query($con, "SELECT DISTINCT pClient FROM product WHERE pBatch='$batch_id'");
    
    if(mysqli_num_rows($query) > 0) {
        echo '<div class="row">';

        while($row = mysqli_fetch_assoc($query)) {
            $client_id = $row['pClient'];
            
            // Get client details
            $client_q = mysqli_query($con, "SELECT uid, uname, uemail, ucontact 
                                            FROM users 
                                            WHERE uid='$client_id'");
            $client = mysqli_fetch_assoc($client_q);

            if($client) {
                // ✅ Send both student id and batch id
                echo "
                <div class='col-md-3 mb-3'>
                    <div class='card p-2 shadow-sm'>
                        <div class='form-check'>
                            <input class='form-check-input' type='checkbox' 
                                   name='clients[]' value='{$client['uid']}|{$batch_id}' 
                                   id='client_{$client['uid']}'>
                            <label class='form-check-label fw-bold' for='client_{$client['uid']}'>
                                {$client['uname']}
                            </label>
                        </div>
                        <p class='mb-0'><strong>Email:</strong> {$client['uemail']}</p>
                        <p class='mb-0'><strong>Mobile:</strong> {$client['ucontact']}</p>
                    </div>
                </div>";
            }
        }

        echo '</div>'; // End row
    } else {
        echo "<p>No products found for this batch.</p>";
    }
}
?>