<?php require_once('../auth/auth.php');?>
<?php
  $custom_field= mysqli_query($conn,"SELECT purchase FROM custom_field WHERE coid='$coid' LIMIT 1 ") or die(mysqli_error($conn));
  $rowcf= mysqli_fetch_assoc ($custom_field);
  $p=$rowcf['purchase'];
  function input_check($ap)
  {
   if (strpos($GLOBALS['p'], $ap) !== false) { return 1;} 
  }
    $invoice = $_GET['invoice'];
    $get_whid = $_GET['wh_id'];

    $order= mysqli_query($conn,"SELECT i.item_name,i.item_details,i.item_code,(select SUM(COALESCE(CASE WHEN plus = '+' THEN qty END,0)) - SUM(COALESCE(CASE WHEN plus = '-' THEN qty END,0)) from order_list where pro_id= i.pro_id AND wh_id='$get_whid' GROUP BY pro_id) item_qty,o.plus,o.wh_id,o.pro_unit,o.unit_val,o.eoid,o.qty,o.avg_price,o.amount FROM order_list_edit o LEFT JOIN item i ON(o.pro_id=i.pro_id) WHERE o.coid='$coid' AND o.bid='$bid' AND o.brid='$brid' AND o.invoice='$invoice' AND wh_id='$get_whid' AND o.active=0 ORDER BY eoid ASC") or die(mysqli_error($conn));
    $i=0;
    $sm_qty=0;
    $avg_total = 0;
    while($orw= mysqli_fetch_assoc($order)){
        $pro_unit  = $orw['pro_unit'];
        $unit_val  = $orw['unit_val'];
        $sm_qty    += $orw['qty'];
    ?>

<tr>
    <td class="text-center"><?php echo ++$i;?></td>
    <td title="Wharehouse Code : <?php echo  mysqli_fetch_assoc(mysqli_query($conn,"SELECT warehouse_code FROM business_w_house WHERE coid='$coid' AND bwid ='$get_whid' LIMIt 1"))['warehouse_code'];?>">

        <?php echo $orw['item_name']." ".$orw['item_details']." ".$orw['item_code'];
?>  </td>
    <td class="text-center"> <dt id="cur_qty"><?php echo $orw['item_qty'];?></dt></td>
    <td><input type="number" class="form-control text-center form-control-sm inputselect  loadqty" value="<?php echo $orw['qty'];?>" iddd="<?php echo $orw['eoid'];?>" c_name="qty" step="0.001" min="0.001" required="true" max="<?php echo $orw['item_qty'];?>"></td>

    <td class="">
      <select iddd="<?php echo $orw['eoid'];?>" c_name="pro_unit" class="form-control select2" style="width: 100%;" required="true">
        <option value='<?php echo $pro_unit;?>' convert='<?php echo $unit_val;?>'>
            <?php echo mysqli_fetch_assoc(mysqli_query($conn,"SELECT unit_name FROM pro_unit WHERE coid='$coid' AND puid='$pro_unit' LIMIt 1"))['unit_name'];
            ?>
        </option>
       <?php 
         $iut= mysqli_query($conn,"SELECT puid,unit_name,unit_convart FROM pro_unit WHERE coid='$coid' ORDER BY puid DESC") or die(mysqli_error($conn));
            while($it= mysqli_fetch_assoc($iut)){
            echo '<option value='.$it["puid"].' convert='.$it["unit_convart"].'>'.$it["unit_name"].'</option>';
          }?>   
      </select>
    </td>



    <td class="text-center"><dt id="new_qty"><?php echo ($orw['item_qty']-$orw['qty']) ?> </dt></td>
    <td class="text-center" onclick="odelete(<?php echo $orw['eoid'];?>)"><i class="fas fa-trash"></i></td>
</tr>
<?php } ?>



    <tr>
       <td class="text-right" colspan="3"><dt><?php echo language(140);?> = </dt></td>
       <td class="text-center"><dt id="sm_qty"><?php echo $sm_qty;?></dt></td>
       <td class="text-right "></td>
       <td class="text-right"></td>
       <td class="text-right"></td>
    </tr>




<script>
 $('select').select2();
   $("[name=minimumqty]").val(<?php echo $sm_qty;?>);

 $(document).ready(function() {
  $("[iddd]").on('input change', function(){    
   var iddd    = $(this).attr("iddd");
   var c_name  = $(this).attr("c_name");
   var c_value = $(this).val();
   var targt   = $(this).parent().parent();
   var tranfer_qty   = Number(targt.find('[c_name=qty]').val()); 
   var cur_qty   = Number(targt.find('#cur_qty').text()); 
   $.post("../item_transfer/order_list_update.php",{iddd : iddd,c_name:c_name,c_value:c_value});
     var convert  = Number(targt.find(':selected').attr('convert'));
     
     targt.find('[c_name=qty]').attr("max", (cur_qty/convert));

  	targt.find('#new_qty').text(Number(( (cur_qty/convert)-tranfer_qty)*convert));
      var qty_sum=0;
          $(".loadqty").each(function(){
            qty_sum += +Number($(this).val());
          });
          $("#sm_qty").html(qty_sum);
          $("[name=minimumqty]").val(qty_sum);

   });  
 });    
</script>

<script>
    function odelete(itemid) {
        $.post("../item_transfer/order_list_delete.php",
            {item_id : itemid,oinvoice:'<?php echo $invoice;?>'},
         function(data){
          pagecall('../item_transfer/order_list.php?invoice=<?php echo $invoice?>'+'&wh_id=<?php echo $get_whid;?>','#itra_row_show<?php echo $invoice;?>',0);
        });     
    }
</script>