<?php //session_start();
include 'xyz.php';

// Check if itemName is set in POST data
if (isset($_POST['itemName'])) {
    $itemName = $_POST['itemName'];
       $my_branch_code = $_POST['myBranch'];

    // Build your query to fetch items based on itemName
    $query = "SELECT * FROM products WHERE item_name LIKE '%$itemName%' ";
    $params = [ ':itemName' => '%' . $itemName . '%' ]; 

    // Execute query and fetch results
    $results = QueryDB($query, $params);




    // Start output buffering
    ob_start();

    // Display table based on search results
    ?>
    <table class="table table-bordered" width="100%" cellspacing="0">
        <thead>
            <tr>
                <th>#</th>
                <th>Item Name</th>
                <th><i class="fa fa-camera"></i></th>
                <th>S/C</th>
                <th>B/C</th>
                <th>Cost</th>
                <th>LStk Date</th>
                <th>Lstk Suply No</th>
                <th>TOTAL STOCK</th>
                <th>T-SOLD</th>
                <th>AVAILABLE</th>
            </tr>
        </thead>
        <tbody>
        <?php
        $count = 1;
        foreach ($results as $reg_std) {
            $stk = lastStock_perBranch($reg_std['id'], '', '', $my_branch_code);
            ?>
            <tr>
                <td class="text-dark"><?php echo $count++; ?></td>
                <td class="text-dark"><b><?php echo strtoupper($reg_std['item_name']); ?></b></td>

                <td>
                    <?php
                    if (file_exists(explode(',', $reg_std['item_img'])[0])) {
                        $view_sel_img = explode(',', $reg_std['item_img'])[0];
                        echo '<img src="'.$view_sel_img.'" class="img img-thumbnail" style="width:150%;height:100px">';
                    }
                    ?>
                </td>

                <td class="text-dark"><?php echo $sized_by[$reg_std['size']] . ', ' . $colored_by[$reg_std['color']]; ?></td>
                <td class="text-dark"><?php echo $brand[$reg_std['brand']] . ', ' . $company[$reg_std['company']]; ?></td>
                <td class="text-dark"><?php echo number_format($reg_std['item_cost'], 2); ?></td>
                <td class="text-dark"><?php echo @date('Y-m-d', $stk['item_time']); ?></td>
                <td><?php echo @$stk['item_qty']; ?></td>
                <td><?php echo totalStock_branch($reg_std['id'], '', '', $my_branch_code); ?></td>
                <td><?php echo getQuantity_Branch($reg_std['id'], '', '', $my_branch_code); ?></td>
                <td><?php echo totalStock_branch($reg_std['id'],'','',$my_branch_code)-getQuantity_Branch($reg_std['id'],'','',$my_branch_code);  ?></td>
            </tr>
        <?php } ?>
        </tbody>
    </table>
    <?php

    // End output buffering and return HTML
    $html = ob_get_clean();
    echo $html;
}
?>
