<?php 
ob_start();
session_start();
include "../config/config.php";

$condition='';
$purchase_items_id = $_POST['purchase_items_id'];
$count = $_POST['count'];
$tag_weight = $_POST['tag_weight'];


if($purchase_items_id!=""){
	$condition.=" and id=".$purchase_items_id;
}


$statements_items = $pdo->prepare("SELECT p.*, tbl_category.name as category_name,
            tbl_units.name as unit_name, tbl_units.short_name as unit_short_name,
            tbl_ids_purity.name as purity_name,
            tbl_products_name.quantity as products_name_quantity, 
            p.product_ids
            FROM `tbl_purchases_items` p
            LEFT JOIN tbl_category ON tbl_category.id=p.category
            LEFT JOIN tbl_ids_purity ON tbl_ids_purity.id=p.purity
            LEFT JOIN tbl_units ON tbl_units.id=p.unit
            LEFT JOIN tbl_products_name ON tbl_products_name.id=p.product_name
            WHERE p.id=?");
$statements_items->execute(array($purchase_items_id));
$items_resultss = $statements_items->fetchAll(PDO::FETCH_ASSOC);
$items_totalDatas = $statements_items->rowCount();
    
    if($items_totalDatas>0){
        
        
        $stone_weight = $items_resultss[0]['stone_weight'];
        
    
        $product_ids = $items_resultss[0]['product_ids'];
        
        
        if($items_resultss[0]['product_type']==1){
            $product_type = "Plain";
        }else if($items_resultss[0]['product_type']==2){
            $product_type = "Stone";
        }
        
        $unit_name = "(".$items_resultss[0]['unit_short_name'].")";
        
?>


<tr class="porow" style="background: gainsboro; color: black !important;">
    
    
    <td>
        &nbsp;
        <input name="product_id[]" class="product_ids" value="<?=$items_resultss[0]['id']?>" hidden>
        <input name="purchase_order_items_id[]" id="purchase_order_<?=$count?>" value="<?=$purchase_items_id?>" hidden>
        <input name="barcode_items_id[]" value="0" hidden>
        <input name="category[]"  value="<?=$items_resultss[0]['category']?>" hidden>
        <input name="unit[]"  value="<?=$items_resultss[0]['unit']?>" hidden>
        <input name="purity[]" value="<?=$items_resultss[0]['purity']?>" hidden>
        <input name="product_ids[]" value="<?=$items_resultss[0]['product_ids']?>" hidden>
        <input name="per_gram_amount[]" value="<?=$items_resultss[0]['per_gram_amount']?>" hidden>
    </td>
    
    <td>
        <select name="product_name[]" id="product_name_<?=$count?>" class="form-control select22" style="width: 100%;" onchange="productDetails(<?=$count?>)">
            <option value="">Select</option>
            <?php 
                $statement = $pdo->prepare("SELECT * FROM `tbl_products_name` WHERE status=1 and type=1 and product_ids=?");
                $statement->execute(array($product_ids));
                $resultss = $statement->fetchAll(PDO::FETCH_ASSOC); 
                foreach ($resultss as $ress) { ?>
                    <option value="<?=$ress['id']?>"><?=$ress['name']?></option>
            <?php } ?>
        </select>
    </td>
    
   
    <td>
        <select name="sub_category_name[]" class="form-control select22" style="width:100%" id="sub_category_name" style="width: 100%;" >
            <option value="">Select</option>
            <?php $statement = $pdo->prepare("SELECT * FROM `tbl_sub_category` WHERE status=1 and category_id=?");
                $statement->execute(array($items_resultss[0]['category']));
                $resultss = $statement->fetchAll(PDO::FETCH_ASSOC); 
                foreach ($resultss as $ress) { ?>
                    <option value="<?=$ress['id']?>"><?=$ress['name']?></option>
            <?php } ?>
        </select>
    </td>
    <td>
        <select name="size[]" class="form-control select22" style="width:100%" id="size_<?=$count?>" style="width: 100%;">
            <option value="">Select</option>
            <?php $statement = $pdo->prepare("SELECT s.*,tbl_products_name.name as product_name FROM `tbl_size` s LEFT JOIN tbl_products_name ON tbl_products_name.id=s.product_name_id WHERE s.status=1");
                $statement->execute();
                $resultss = $statement->fetchAll(PDO::FETCH_ASSOC); 
                foreach ($resultss as $ress) { ?>
                    <option value="<?=$ress['size']?>"><?=$ress['size']?> - <?=$ress['product_name']?></option>
            <?php } ?>
        </select>
    </td>
    
    <td>
        <b><?=$unit_name?></b>
    </td>
   
    <!--<td>
        <input type="text" name="quantity[]" class="form-control table_input" value="" id="quantity_<?=$count?>" placeholder="Quantity" onkeypress="return isNumberKey(this, event);" style="text-align: center;" required readonly>
    </td>-->
    
    
    
    
    <td>
        <input type="text" name="cross_weight[]" class="form-control table_input cross_wt_<?=$purchase_items_id?> cross_wt" id="cross_weight_<?=$count?>" oninput="calculateNetWeight(<?=$count?>)"  placeholder="Cross Wt" onkeypress="return isNumberKey(this, event);" data-itemsid="<?=$purchase_items_id?>" required>
    </td>
    
    <td>
       
        <?php if($items_resultss[0]['product_type']==2){ ?>  
            <input type="text" name="stone_weight[]" class="form-control table_input stone_wt_<?=$purchase_items_id?> stone_wt" id="stone_weight_<?=$count?>" oninput="calculateNetWeight(<?=$count?>)" placeholder="Stone Wt" onkeypress="return isNumberKey(this, event);" data-itemsid="<?=$purchase_items_id?>" required>
        <?php } else { ?>
            <b>0</b><input type="hidden" name="stone_weight[]" id="stone_weight_<?=$count?>" value="0">
        <?php } ?>
    </td>
    <td>
        <input type="text" name="net_weight[]" class="form-control table_input net_wt_<?=$purchase_items_id?> net_wt" id="net_weight_<?=$count?>" placeholder="Net Wt" onkeypress="return isNumberKey(this, event);" data-itemsid="<?=$purchase_items_id?>" required>
    </td>
    <td>
        <?php if($items_resultss[0]['product_type']==2){ ?>  
            <input type="text" name="stone_cost[]" class="form-control table_input stone_cost_<?=$purchase_items_id?> stone_cost" value="<?=$items_resultss[0]['stone_cost']?>" id="stone_cost_<?=$count?>" placeholder="Stone Cost" onkeypress="return isNumberKey(this, event);" data-itemsid="<?=$purchase_items_id?>" required>
        <?php } else { ?>
            <b>0</b><input type="hidden" name="stone_cost[]" value="0">
        <?php } ?>
    </td>
    
    <td>
        <?php if($stone_weight!=0){ ?>
        <select name="stone_name[]" class="form-control select22" style="width:100%" id="stone_name_<?=$count?>" style="width: 100%;">
            <option value="">Select</option>
            <?php $statement = $pdo->prepare("SELECT s.* FROM `tbl_stone_name` s WHERE s.status=1");
                $statement->execute();
                $resultss = $statement->fetchAll(PDO::FETCH_ASSOC); 
                foreach ($resultss as $ress) { ?>
                    <option value="<?=$ress['id']?>"><?=$ress['name']?></option>
            <?php } ?>
        </select>
        <?php } else { ?>
        <input name="stone_name[]" value="0" hidden>
        <?php } ?>
    </td>
    
    <td>
        <?php if($stone_weight!=0){ ?>
        <select name="stone_purity[]" class="form-control select22" style="width:100%" id="stone_purity_<?=$count?>" style="width: 100%;">
            <option value="">Select</option>
            <?php $statement = $pdo->prepare("SELECT s.* FROM `tbl_stone_purity` s WHERE s.status=1");
                $statement->execute();
                $resultss = $statement->fetchAll(PDO::FETCH_ASSOC); 
                foreach ($resultss as $ress) { ?>
                    <option value="<?=$ress['id']?>"><?=$ress['name']?></option>
            <?php } ?>
        </select>
        <?php } else { ?>
        <input name="stone_purity[]" value="0" hidden>
        <?php } ?>
    </td>
    
    
    <td>
        <input type="text" name="tag_weight[]" class="form-control table_input tag_wt_<?=$purchase_items_id?> tag_wt" value="<?=$tag_weight?>" id="tag_weight" placeholder="Tag Wt" onkeypress="return isNumberKey(this, event);" data-itemsid="<?=$purchase_items_id?>" required>
    </td>
    <td>
        <select name="wastage[]" class="form-control select22" style="width:100%" id="wastage" >
            <option value="0">0% Wastage</option>
            <?php $statement = $pdo->prepare("SELECT * FROM `tbl_wastage` WHERE status=1 and category_id=?");
                $statement->execute(array($items_resultss[0]['category']));
                $resultss = $statement->fetchAll(PDO::FETCH_ASSOC); 
                foreach ($resultss as $ress) { ?>
                    <option value="<?=$ress['id']?>"><?=$ress['name']?></option>
            <?php } ?>
        </select>
    </td>
    <td>
        <b><?=$items_resultss[0]['purity_name']?></b>
    </td>
    <td>
        <span class="btn btn-danger btn-sm remove_product" data-itemsid="<?=$purchase_items_id?>" style="margin-top: 10px;"><i class="bi bi-trash3-fill"></i></span>
    </td>

 
</tr>




     
<?php } ?>

