<?php  include "header.php"; ?>



<div class="content-wrapper">
        <section class="content-header">
            <h1>Create Barcode</h1>
            <ol class="breadcrumb">
                <li><a href="index.php"><i class="fa fa-dashboard"></i> Home</a></li>
                <li class="active">Create Barcode</li>            
            </ol>
        </section>


        <div class="col-lg-12 alerts">
            <div id="custom-alerts" style="display:none;">
                <div class="alert alert-dismissable">
                    <div class="custom-msg"></div>
                </div>
            </div>
            
            <?php if ($error)  { ?>
            <div class="alert alert-danger alert-dismissable">
                <button aria-hidden="true" data-dismiss="alert" class="close" type="button">×</button>
                <h4><i class="icon fa fa-ban"></i> Error</h4>
                <?= $error; ?>
            </div>
            <?php $error=0;} if ($warning) { ?>
            <div class="alert alert-warning alert-dismissable">
                <button aria-hidden="true" data-dismiss="alert" class="close" type="button">×</button>
                <h4><i class="icon fa fa-warning"></i> Warning</h4>
                <?= $warning; ?>
            </div>
            <?php  $warning=0;} if ($message) { ?>
            <div class="alert alert-success alert-dismissable">
                <button aria-hidden="true" data-dismiss="alert" class="close" type="button">×</button>
                <h4>    <i class="icon fa fa-check"></i> Success</h4>
                <?= $message; ?>
            </div>
            <?php $message=0; } ?>
        </div>
        <div class="clearfix"></div>







<section class="content">
    <div class="row">
        <div class="col-xs-12">
            <div class="box box-primary" style="box-shadow: rgba(0, 0, 0, 0.16) 0px 3px 6px, rgba(0, 0, 0, 0.23) 0px 3px 6px;">
                <div class="box-body">
                    <div class="col-lg-12">

                        <form id="" action="" method="get">
                                                                 
                        <div class="row">
                            
                            <div class="col-md-4">
                                <div class="form-group">
                                    <label class="required" for="purchase">Purchase Details</label>                                   
                                    <select name="purchase" class="form-control select2" style="width:100%" id="purchase" required="required">
                                        
                                        <?php 
                                            if(isset($_GET['purchase']) && $_GET['purchase']!="0"){
                                                $cond=" and id=".$_GET['purchase'];
                                            }else{
                                                $cond = "";
                                                echo '<option value="">Select Purchase</option>';
                                            }
                                            $statement = $pdo->prepare("SELECT * FROM `tbl_purchases` where status=1 $cond");
                                            $statement->execute();
                                            $location_result = $statement->fetchAll(PDO::FETCH_ASSOC); 
                                            foreach ($location_result as $result) { ?>
                                                <option value="<?=$result['id']?>"><?=$result['invoice_no']?></option>
                                        <?php  }  ?>
                                    </select>
                                </div>
                            </div>
                            
                        <div class="col-md-4">
                                <div class="form-group">
                                    <label class="" for="submit">Actions</label>     <br>                               
                                    <input type="submit"  id="" value="Submit" class="btn btn-success" />
                                </div>
                        </div>
                            
               
                        
                        </div>
                        
                        </form>
                        
                        
        
        <form id="form_details">
            
            
                        
        <?php if(isset($_GET['purchase']) && $_GET['purchase']!="0"){ 
            
        $statements = $pdo->prepare("SELECT tbl_purchases.*,tbl_suppliers.name as supplier_name, 
        (SELECT COUNT(id) FROM `tbl_purchases_items` WHERE tbl_purchases_items.purchase_id=tbl_purchases.id) as products_count
        FROM `tbl_purchases` 
        LEFT JOIN tbl_suppliers ON tbl_suppliers.id=tbl_purchases.supplier_id 
        where tbl_purchases.id=?");
        $statements->execute(array($_GET['purchase']));
        $resultss = $statements->fetchAll(PDO::FETCH_ASSOC);
        $totalDatas = $statements->rowCount();
             
        if($totalDatas>0) {        
            
            if($resultss[0]['total_weight']==""){
                $total_weight = "0.00";
            }else{
                $total_weight = $resultss[0]['total_weight'];
            }
        ?>       
        
        <input type="hidden" name="purchase_id" value="<?=$_GET['purchase']?>">
                    <div class="row">
                        <div class="col-md-12">
                            
                            
                            <div class="col-md-3">
                                <b>Supplier Name: </b></br>
                                <b>Purchase Invoice No: </b></br>
                                <b>Purchase Date: </b></br>
                                <b>Purchase Items Counts: </b></br>
                                <b>Total Weight: </b></br>
                            </div>
                            
                            <div class="col-md-3">
                                <b><?=$resultss[0]['supplier_name']?></b></br>
                                <b><?=$resultss[0]['invoice_no']?></b></br>
                                <b><?=date('d-m-Y', strtotime($resultss[0]['date']))?></b></br>
                                <b><?=$resultss[0]['products_count']?></b></br>
                                <b><?=$total_weight?></b></br>
                            </div>
                            
                            
                        </div>
                    </div>
                    
                    
                    
                    <div class="row" style="padding-top: 10px;">
                        <div class="col-md-12">
                             <div class="col-md-3">
                                <b>Tag Weight: </b></br>
                            </div>
                            <div class="col-md-3">
                                <b><input type="text" id="tag_weight" class="form-control" style="width: 50%;"></b></br>
                            </div>
                        </div>
                    </div>
                    
                    
                    
                    <!--table table-striped-->
                    <table style="width: 100%; margin-top: 10px;" class="">
                    
                            <tr class="tr_tbody">
                                <th style="width: 3%">Barcode</th>
                                <th style="width: 6%">Products</th>
                                <th style="width: 10%">Product Name</th>
                                <th style="width: 7%">Category</th>
                                <th style="width: 8%">Type</th>
                                <th style="width: 8%">Unit</th>
                                <th style="width: 7%">Qty</th>
                                <th style="width: 8%">Cross Wt</th>
                                <th style="width: 8%">Stone Wt</th>
                                <th style="width: 8%">Net Wt</th>
                                <th style="width: 8%">Stone Cost</th>
                                <th style="width: 7%">Tag Wt</th>
                                <th style="width: 7%">Wastage</th>
                                <th style="width: 15%">Purity</th>
                                <th style="width: 3%">Actions</th>
                            </tr>
                 
                        
                <?php
                
                $statements_items = $pdo->prepare("SELECT p.*,tbl_category.name as category_name, tbl_ids_products.name as product,
                tbl_units.name as unit_name, tbl_units.short_name as unit_short_name,
                tbl_ids_purity.name as purity_name,
                tbl_products_name.name as products_name
                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_ids_products ON tbl_ids_products.id=p.product_ids
                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.purchase_id=?");
                $statements_items->execute(array($_GET['purchase']));
                $items_resultss = $statements_items->fetchAll(PDO::FETCH_ASSOC);
                $items_totalDatas = $statements_items->rowCount();
                
                if($items_totalDatas>0){
                    $i=1;
                    foreach($items_resultss as $items){
                        
                    $purchase_items_id = $items['id'];
                    if($items['product_type']==1){
                        $product_type = "Plain";
                    }else if($items['product_type']==2){
                        $product_type = "Stone";
                    }
                ?>
                
                            <tr style="background: aliceblue;">
                                <td></td>
                                <td><?=$items['product']?></td>
                                <td><?=$items['products_name']?></td>
                                <td><?=$items['category_name']?></td>
                                <td><?=$product_type?></td>
                                <td>(<?=$items['unit_short_name']?>)</td>
                                <td></td>
                                <td><?=$items['cross_weight']?></td>
                                <td><?=$items['stone_weight']?></td>
                                <td><?=$items['net_weight']?></td>
                                <td><?=$items['stone_cost']?></td>
                                <td></td>
                                <td></td>
                                <td><?=$items['purity_name']?></td>
                                <td><span class="btn btn-success btn-md" onclick="ProductsItemsAppend(<?=$items['id']?>)" id="add_row_span_<?=$items['id']?>"><i class='fa fa-plus'></i></span></td>
                            </tr>
                            
                            <tr>
                                <td colspan="13">
                                    <div class="row">
                                        <div class="col-lg-12">
                                            <div class="col-lg-3">
                                                <b>Cross Weight: <span id="items_cross_wt_<?=$purchase_items_id?>" style=""><?=$items['cross_weight']?></span>&nbsp;</b>
                                            </div>
                                            <div class="col-lg-3">
                                                <b>Stone Weight: <span id="items_stone_wt_<?=$purchase_items_id?>" style=""><?=$items['stone_weight']?></span>&nbsp;</b>
                                            </div>
                                            <div class="col-lg-3">
                                                <b>Net Weight: <span id="items_net_wt_<?=$purchase_items_id?>" style=""><?=$items['net_weight']?></span>&nbsp;</b>
                                            </div>
                                            <div class="col-lg-3">
                                                <b>Stone Cost: <span id="items_stone_cost_<?=$purchase_items_id?>" style=""><?=$items['stone_cost']?></span>&nbsp;</b>
                                            </div>
                                        </div>
                                        <div class="col-lg-2"></div>
                                        <div class="col-lg-8" style="padding: 5px; background: blue; border: 1px solid gainsboro; border-radius: 20px;" id="alert_section_<?=$purchase_items_id?>">
                                            <div class="col-lg-3">
                                                <b style="color: white;">Enter Cross Weight: <span id="enter_items_cross_wt_<?=$purchase_items_id?>" style="color: white;">0</span>&nbsp;</b>
                                            </div>
                                            <div class="col-lg-3">
                                                <b style="color: white;">Enter Stone Weight: <span id="enter_items_stone_wt_<?=$purchase_items_id?>" style="color: white;">0</span>&nbsp;</b>
                                            </div>
                                            <div class="col-lg-3">
                                                <b style="color: white;">Enter Net Weight: <span id="enter_items_net_wt_<?=$purchase_items_id?>" style="color: white;">0</span>&nbsp;</b>
                                            </div>
                                            <div class="col-lg-3">
                                                <b style="color: white;">Enter Stone Cost: <span id="enter_items_stone_cost_<?=$purchase_items_id?>" style="color: white;">0</span>&nbsp;</b>
                                            </div>
                                        </div>
                                        <div class="col-lg-2"></div>
                                    </div>
                                    <div id="product_items_<?=$items['id']?>"></div>
                                </td>
                            </tr>
                            <tr>
                                <th ></th>
                                <th >Products</th>
                                <th >Product Name</th>
                                <th >Category</th>
                                <th >Type</th>
                                <th >Unit</th>
                                <th >Qty</th>
                                <th >Cross Wt</th>
                                <th >Stone Wt</th>
                                <th >Net Wt</th>
                                <th >Stone Cost</th>
                                <th >Tag Wt</th>
                                <th >Wastage</th>
                                <th >Purity</th>
                                <th >Actions</th>
                            </tr>
                    </thead>  
                    <tbody id="product_tbody_<?=$items['id']?>">
                        
                <?php
                    $statements_items_barcode = $pdo->prepare("SELECT p.*,tbl_category.name as category_name, tbl_ids_products.name as product,
                tbl_units.name as unit_name, tbl_units.short_name as unit_short_name,
                tbl_ids_purity.name as purity_name,
                tbl_products_name.name as products_name,
                tbl_products_name.quantity as products_name_quantity,
                tbl_product_variants.id as main_product_id
                FROM `tbl_barcode_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_ids_products ON tbl_ids_products.id=p.pro_ids
                LEFT JOIN tbl_units ON tbl_units.id=p.unit
                LEFT JOIN tbl_products_name ON tbl_products_name.id=p.product_name
                LEFT JOIN tbl_product_variants ON tbl_product_variants.barcode_items_id=p.id
                WHERE p.purchase_items_id=?");
                $statements_items_barcode->execute(array($items['id']));
                $items_resultss_barcode = $statements_items_barcode->fetchAll(PDO::FETCH_ASSOC);
                $items_totalDatas_barcode = $statements_items_barcode->rowCount();
                
                if($items_totalDatas_barcode>0){
                    $i=1;
                    foreach($items_resultss_barcode as $barcode){ 
                        
                        $unit_name = "(".$barcode['unit_short_name'].")";
                        
                        $barcode_items_id = $barcode['id'];
                        $purchase_items_id = $barcode['purchase_items_id'];
                        $barcode_category = $barcode['category'];
                        $barcode_sub_category = $barcode['sub_category'];
                        $barcode_product_type = $barcode['type'];
                        $barcode_unit = $barcode['unit'];
                        $barcode_purity = $barcode['purity'];
                        $barcode_wastage = $barcode['wastage'];
                        $b_cross_weight = $barcode['cross_weight'];
                        $b_stone_weight = $barcode['stone_weight'];
                        $b_net_weight = $barcode['net_weight'];
                        $b_stone_cost = $barcode['stone_cost'];
                        $b_others_weight = $barcode['others_weight'];
                        
                        $b_product_name = $barcode['product_name'];
                        $b_product_ids = $barcode['pro_ids'];
                        
                        $b_quantity = $barcode['quantity'];
                        
                        $main_product_id = $barcode['main_product_id'];
                    ?>
                    
                <tr class="porow" style="background: burlywood; color: black !important;">
    
    
    <td><span class="btn btn-info" onclick="form_barcodeSubmit(<?=$i?>, <?=$main_product_id?>)"><i class="bi bi-upc-scan"></i></span></td>
    <td>
        <input name="product_id[]" class="product_ids" value="<?=$barcode['id']?>" hidden>
        <input name="purchase_order_items_id[]" id="purchase_order_<?=$i?>" value="<?=$purchase_items_id?>" hidden>
        <input name="barcode_items_id[]" value="<?=$barcode_items_id?>" hidden>
        <input name="category[]"  value="<?=$barcode_category?>" hidden>
        <input name="unit[]"  value="<?=$barcode_unit?>" hidden>
        <input name="purity[]" value="<?=$barcode_purity?>" hidden>
        
        
        <select name="product_ids[]" id="product_ids" class="form-control select22 select_normal" style="width: 100%;">
            <?php $statement = $pdo->prepare("SELECT * FROM `tbl_ids_products` WHERE status=1 and id=?");
                $statement->execute(array($b_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="product_name[]" id="product_name" class="form-control select22 select_normal" style="width: 100%;">
            <?php $statement = $pdo->prepare("SELECT * FROM `tbl_products_name` WHERE status=1 and id=?");
                $statement->execute(array($b_product_name));
                $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 select2" 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($barcode_category));
                $resultss = $statement->fetchAll(PDO::FETCH_ASSOC); 
                foreach ($resultss as $ress) { ?>
                    <option value="<?=$ress['id']?>"><?=$ress['name']?></option>
            <?php } ?>
        </select>
    </td>
    <td>
        <select name="product_type[]" class="form-control select22 select_normal" style="width:100%" id="product_type" style="width: 100%;">
            <option value="1" <?= ($barcode_product_type==1) ? "selected":""?>>Plain</option>
            <option value="2" <?= ($barcode_product_type==2) ? "selected":""?>>Stone</option>
        </select>
    </td>
    
    <td>
        <b><?=$unit_name?></b>
    </td>
   
    <td>
        <input type="text" name="quantity[]" class="form-control table_input" value="<?=$b_quantity?>" id="quantity_<?=$i?>" placeholder="Quantity" onkeypress="return isNumberKey(this, event);" required>
    </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="plain_NetWeight(<?=$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[]" 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" 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>
        <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 select2" style="width:100%;" id="wastage" required="required">
            <?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" onclick="barcodeItems(<?=$barcode_items_id?>)" data-itemsid="<?=$purchase_items_id?>" style="margin-top: 10px;"><i class="bi bi-trash3-fill"></i></span>
    </td>

 
</tr>
                    
                    
                        
                <?php  $i++; } } ?>
                        
                        
                        
                        
                        
                        
                        
                        
                        
                    </tbody>
                    
                    
                    
                            
                <?php  }} ?>
                        
                    </table>
                    
                    
                    <div id="product_tbody">
                        
                    </div>
                    
                    
        <?php }else{ ?>
                <center><b>No Records Found</b></center>
        <?php } } ?>
                        
             
                            
                            <!--<div class="col-md-12" style="margin-top: 20px;">
                                <div class="form-group search-box">
                                    <label class="" for="search_products">Search Products</label>   &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span id="add_new_products" class="btn btn-primary btn-xs"><i class="fa fa-plus"></i> Add New Products</span>                                
                                    <input type="text" name="search_products" placeholder="Enter the Products" value=""  class="form-control" id="search_products"  />
                                    <div class="result"></div>
                                </div>
                            </div>
                            
                            
                            <div class="col-lg-12" id="product_tbody"></div>-->
                            
                            
                         
                        
                        
                        
                    <?php if(isset($_GET['purchase']) && $_GET['purchase']!=""){ ?>
                        <div class="form-group" style="text-align:center; margin: 20px 0px;">
                            <input type="submit"  id="submit" value="Add Stocks" class="btn btn-success" />
                        </div>
                    <?php } ?>
                        
                </form>

                    </div>
                        <div class="form-group" style="text-align:center;">
                    <?php if(isset($_GET['purchase']) && $_GET['purchase']!=""){ ?>
                            <a href="barcode.php?purchase=<?=$_GET['purchase']?>"><button class="btn btn-primary" ><i class="fa fa-refresh"></i> Reset Form</button></a>
                    <?php } ?>
                            <a href="barcode.php"><button class="btn btn-warning" ><i class="fa fa-chevron-left"></i> Back to List</button></a>
                         </div>
                    <div class="clearfix"></div>
                </div>
            </div>
        </div>
    </div>
</section>

<script>
// On Key Press - Weight Calculation Starts Here
function calculateNetWeight(count) {
    var purchase_itemsID = $("#purchase_order_"+count).val();
    var crossWeight = parseFloat($("#cross_weight_"+count).val()) || 0;
    var stoneWeight = parseFloat($("#stone_weight_"+count).val()) || 0;

    var netWeight = crossWeight - stoneWeight;

    if (!isNaN(netWeight)) {
        $("#net_weight_"+count).val(netWeight);
    } else {
        $("#net_weight_"+count).val("");
    }
    
    
    
    CrossWeight(purchase_itemsID);
    
    NetWeight(purchase_itemsID);
    
    StoneWeight(purchase_itemsID);
    
    FinalFunction(purchase_itemsID);
}

// On Key Press - Weight Calculation Endsss Here



let count = 0;
function ProductsItemsAppend(purchase_items_id){
    var tag_weight = $("#tag_weight").val();
count++;
$("#add_row_span_"+purchase_items_id).hide();
$.ajax({
        url: 'ui-api/barcode-purchase-items-table-append.php',
        type: 'post',
        dataType: 'text',
        data:{purchase_items_id:purchase_items_id, count:count, tag_weight:tag_weight},
        success: function (response) {
            console.log(response);
            $('#product_tbody_'+purchase_items_id).append(response);
            
            $("#add_row_span_"+purchase_items_id).show();
            $(".select22").select2();
        }
});

}




</script>
<?php include "footer.php"; ?>

<script>
/*$("body").on("keyup",".po_qty",function(){
  var po_qty=Number($(this).val());
  var po_price=Number($(this).closest("tr").find(".po_price").val());
  $(this).closest("tr").find(".total").val(po_price*po_qty);
  grand_total();
});*/


     $(document).on('click', '.remove_product', function() {
        var purchaseItemsID = $(this).data("itemsid");
        $(this).closest('.porow').remove();
        CrossWeight(purchaseItemsID);
        StoneWeight(purchaseItemsID);
        NetWeight(purchaseItemsID);
        FinalFunction(purchaseItemsID);
    });
    
    
    
    $("body").on("keyup",".cross_wt",function(){
          var Weight = Number($(this).val());
          var purchaseItemsID = $(this).data("itemsid");
          CrossWeight(purchaseItemsID);
    });
        
    $("body").on("keyup",".stone_wt",function(){
          var Weight = Number($(this).val());
          var purchaseItemsID = $(this).data("itemsid");
          StoneWeight(purchaseItemsID);
    });
    
    $("body").on("keyup",".net_wt",function(){
          var Weight = Number($(this).val());
          var purchaseItemsID = $(this).data("itemsid");
          NetWeight(purchaseItemsID);
    });
        
    function CrossWeight(purchaseItemsID){
          var tot=0;
          $(".cross_wt_"+purchaseItemsID).each(function(){
            tot+=Number($(this).val());
          });
          var TotalCrossWeight = parseFloat($("#items_cross_wt_"+purchaseItemsID).text());
          $("#enter_items_cross_wt_"+purchaseItemsID).text(tot);
          var EnterCrossWeight = parseFloat($("#enter_items_cross_wt_"+purchaseItemsID).text());
          


                if (TotalCrossWeight == EnterCrossWeight) {
                    $("#alert_section_" + purchaseItemsID).css({"background": "green", "color": "white" });
                    $("#enter_items_cross_wt_" + purchaseItemsID).css("color", "white");
                    $("#submit").prop('disabled', false);
                } else {
                    $("#alert_section_" + purchaseItemsID).css({"background": "red", "color": "white" });
                    $("#enter_items_cross_wt_" + purchaseItemsID).css("color", "white");
                    $("#submit").prop('disabled', true);
                }
            
        FinalFunction(purchaseItemsID);

    }
    
    function StoneWeight(purchaseItemsID){
          var tot=0;
          $(".stone_wt_"+purchaseItemsID).each(function(){
            tot+=Number($(this).val());
          });
          var TotalStoneWeight = parseFloat($("#items_stone_wt_"+purchaseItemsID).text());
          $("#enter_items_stone_wt_"+purchaseItemsID).text(tot);
          var EnterStoneWeight = parseFloat($("#enter_items_stone_wt_"+purchaseItemsID).text());
          
                if (TotalStoneWeight == EnterStoneWeight) {
                    $("#alert_section_" + purchaseItemsID).css({"background": "green", "color": "white" });
                    $("#enter_items_stone_wt_" + purchaseItemsID).css("color", "white");
                    $("#submit").prop('disabled', false);
                } else {
                    $("#alert_section_" + purchaseItemsID).css({"background": "red", "color": "white" });
                    $("#enter_items_stone_wt_" + purchaseItemsID).css("color", "white");
                    $("#submit").prop('disabled', true);
                }
                
        FinalFunction(purchaseItemsID);

    }
    
    function NetWeight(purchaseItemsID){
        console.log("Net Weight Working: "+purchaseItemsID)
          var tot=0;
          $(".net_wt_"+purchaseItemsID).each(function(){
            tot+=Number($(this).val());
          });
          var TotalNetWeight = parseFloat($("#items_net_wt_"+purchaseItemsID).text());
          $("#enter_items_net_wt_"+purchaseItemsID).text(tot);
          var EnterNetWeight = parseFloat($("#enter_items_net_wt_"+purchaseItemsID).text());
          


                if (TotalNetWeight == EnterNetWeight) {
                    $("#alert_section_" + purchaseItemsID).css({"background": "green", "color": "white" });
                    $("#enter_items_net_wt_" + purchaseItemsID).css("color", "white");
                    $("#submit").prop('disabled', false);
                } else {
                    $("#alert_section_" + purchaseItemsID).css({"background": "red", "color": "white" });
                    $("#enter_items_net_wt_" + purchaseItemsID).css("color", "white");
                    $("#submit").prop('disabled', true);
                }
        
        FinalFunction(purchaseItemsID);

    }
    
    function FinalFunction(purchaseItemsID){
        // Net Weight - Function
        var TotalNetWeight = parseFloat($("#items_net_wt_"+purchaseItemsID).text());
        var EnterNetWeight = parseFloat($("#enter_items_net_wt_"+purchaseItemsID).text());
        // Stone Weight - Function
        var TotalStoneWeight = parseFloat($("#items_stone_wt_"+purchaseItemsID).text());
        var EnterStoneWeight = parseFloat($("#enter_items_stone_wt_"+purchaseItemsID).text());
        // Cross Weight - Function
        var TotalCrossWeight = parseFloat($("#items_cross_wt_"+purchaseItemsID).text());
        var EnterCrossWeight = parseFloat($("#enter_items_cross_wt_"+purchaseItemsID).text());
        
        
        
        if (TotalCrossWeight==EnterCrossWeight && TotalStoneWeight==EnterStoneWeight && TotalNetWeight == EnterNetWeight) {
            $("#alert_section_" + purchaseItemsID).css({"background": "green", "color": "white" });
            $("#submit").prop('disabled', false);
        } else {
            $("#alert_section_" + purchaseItemsID).css({"background": "red", "color": "white" });
            $("#submit").prop('disabled', true);
        }
    }
    
    
function plain_NetWeight(count){
    console.log("Plain "+count);
    var purchase_itemsID = $("#purchase_order_"+count).val();
    var crossWeight = parseFloat($("#cross_weight_"+count).val()) || 0;
    console.log(crossWeight);
    
    $("#net_weight_"+count).val(crossWeight);
    
    CrossWeight(purchase_itemsID);
    NetWeight(purchase_itemsID);
    FinalFunction(purchase_itemsID);
}
</script>



<script>
function barcodeItems(id){
            var result = confirm("Confirm Want to Delete This Barcode Items Id?");
            if (result) {
                    $.ajax({
                    type: 'POST',
                    url: 'api/delete.php',
                    data: {type:'barcode_items_delete',delete_id:id, table: 'tbl_barcode_items'},                  
                    success: function(response) {
                        console.log(response);
                    }
                });
            }
    }
$(document).ready(function() {
   $('#form_details').on('submit', function(e){
       console.log("Form Submitted");
                e.preventDefault();
                $('#loading-div').show();
                $('#content-div').hide();
                $("#submit").prop('disabled', true);
                var formData = new FormData(this);
                formData.append('type', 'create_barcode');
                    $.ajax({
                            url: 'apis/barcode.php',
                            type: 'post',
                            dataType: 'json',
                            data: formData,
                            cache: false,
                            contentType: false,
                            processData: false,
                            enctype: 'multipart/form-data',
                            success: function (response) {
                                console.log(response);
                                if(response['status']==200){
                                     alert(response['message']);
                                     /*$('#form_details').trigger("reset");*/
                                     $("#submit").prop('disabled', false);
                                     $('#content-div').show();
                                     $('#loading-div').hide();
                                }else{
                                    alert(response['message']);
                                    $("#submit").prop('disabled', false);
                                    $('#loading-div').hide();
                                    $('#content-div').show();
                                }
                            }
                        });
   });
});
</script>


<style>
        /*table {
          border-collapse: collapse;
          width: 100%;
        }
        
        th, td {
          padding: 8px;
          text-align: left;
          border-bottom: 1px solid #ddd;
        }
        */
        /*tr:hover {background-color: coral;}*/
        
    /* Formatting search box */
    .search-box{
        width: auto;
        display: inline-block;
        font-size: 14px;
    }
    .search-box {
        padding: 5px 10px;
        border: 1px solid #CCCCCC;
        font-size: 14px;
    }
    .result{
        /*position: absolute; */       
        z-index: 999;
        top: 100%;
        left: 0;
    }
    .search-box, .result{
        width: 100%;
        box-sizing: border-box;
    }
    /* Formatting result items */
    .result div{
        margin: 0;
        background: white;
        padding: 7px 10px;
        border: 1px solid #CCCCCC;
        border-top: none;
        cursor: pointer;
    }
    .result div:hover{
        background: blue;
        color: white;
    }
</style>
<script>
function product_selection(id, quantity){ 
       console.log("Product Selection ID: "+id);
    var product_present = 0;
    $('.product_ids').each(function(){
        if(id==$(this).val()){
            product_present = 1;
        }
    });
    
    if(product_present==0){
    $.ajax({
        url: 'api/product-search-append-api.php',
        type: 'post',
        dataType: 'text',
        data:{pro_id:id, quantity:quantity },
        success: function (response) {
            console.log('Id   :'+id);
            $(".select22").select2();
            console.log('Response   :'+response);
            $('#product_tbody').append(response);
            $('.search-box input[type="text"]').val('');
            $(".result").empty();
            $('.search-box input[type="text"]').focus();
        }
    });
    }else{
        alert("Product Already Added");
    }
   }
$(document).ready(function(){
    var n='';
    n=parseInt(n) || 0;

    
    $('.search-box input[type="text"]').on("keyup input", function(){
        console.log('bn Value='+n);
        var search_text = $(this).val();
        var resultDropdown = $(this).siblings(".result");
        if(search_text.length>1){
            $.get("api/product-search-api.php", {search_text: search_text}).done(function(data){
                resultDropdown.html(data);
            });
        } else{
            resultDropdown.empty();
        }
    });
});

function form_barcodeSubmit(count, product_variant_id){
    
    var quantity = $("#quantity_"+count).val();
    var url = "show-barcode-label-front.php?product_variant_id=" + product_variant_id + "&quantity="+quantity;
    
    window.open(url, '_blank');
    
    
}


</script>
<style>
    td, th{
        text-align: center;
        padding: 5px;
    }
    thead{
        background: turquoise;
    }
    .tr_tbody{
        background: sandybrown !important;
        color: white !important;
    }
table.table tbody tr:hover td {
    background: none!important;
}
.select2-selection__arrow{
    display: none;
}
.table_input{
    padding: 0px 0px 0px 5px;
}
.select2-selection{
    font-size: 13px;
}
.select_normal{
    -webkit-appearance: none;
}
</style>
