<?php require_once('../auth/auth.php');?>
<?php
  $g_itemid = (isset($_GET['g_itemid']))?$_GET['g_itemid']:0;
  $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'];
    $order= mysqli_query($conn,"SELECT i.item_name,i.item_details,i.item_code,i.item_qty,i.location,o.plus,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 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>
  <?php if($g_itemid==0) { ?>
    <td class="text-center"><?php echo ++$i;?></td>
    <td title="Avarage price Per Unit : <?php echo $orw['avg_price'];?>"><?php echo $orw['item_name']." ".$orw['item_details']." ".$orw['item_code'];?>
    </td>
  <?php } ?>
    <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" value="<?php echo $orw['qty'];?>" iddd="<?php echo $orw['eoid'];?>" c_name="qty" step="0.001" min="0.001" required="true"></td>

	<td class="text-center <?php if(input_check('u_type') !==1){echo 'd-none';}?>">
	  <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><input type="text" class="form-control text-center form-control-sm inputselect" value="<?php echo $orw['location'];?>" iddd="<?php echo $orw['eoid'];?>" c_name="details" required="true"></td>

    <td class="text-center">
    	<input type="number" class="form-control text-center form-control-sm inputselect adj_price <?php if($orw['plus']=='-'){echo "d-none";}?>" value="<?php echo $orw['amount'];?>" iddd="<?php echo $orw['eoid'];?>" c_name="amount" step="0.001" required="true">
    	<dt class="adj_price <?php if($orw['plus']=='+'){echo "d-none";}?>">N/A</dt>
    </td>
    <td class="text-center">     
      <select iddd="<?php echo $orw['eoid'];?>" c_name="plus" class="form-control select2" style="width: 100%;" required="true">
        <option value="+" <?php if($orw['plus']=='+'){echo "selected";}?>> PLUS (+) </option>  
        <option value="-" <?php if($orw['plus']=='-'){echo "selected";}?>> MINUS (-)</option> 
      </select>
    </td>
    <td class="text-center">
        <dt id="new_qty">
         <?php if ($orw['plus']=='+') {
	        	echo ($orw['item_qty']+$orw['qty']);
	        } elseif ($orw['plus']=='-'){
	        	echo ($orw['item_qty']-$orw['qty']);
	        }
         ?>
        </dt>
    </td>
  <?php if($g_itemid==0) { ?>
    <td onclick="odelete(<?php echo $orw['eoid'];?>)"><i class="fas fa-trash"></i></td>
  <?php } ?>
</tr>
<?php } ?>

<script>
 $('select').select2();
 $(document).ready(function() {
 $('#adsm_totalc').val(<?php echo $sm_qty;?>);
  $("[iddd]").on('input', function(){    
   var iddd    = $(this).attr("iddd");
   var c_name  = $(this).attr("c_name");
   var c_value = $(this).val();
   var targt   = $(this).parent().parent();

   if (c_name=='plus') {
       targt.find('.adj_price').toggleClass("d-none");
    }

   var plus      = targt.find('[c_name=plus]').val(); 
   var adj_qty   = Number(targt.find('[c_name=qty]').val()); 
   var adj_price = Number(targt.find('[c_name=amount]').val()); 
   var cur_qty   = Number(targt.find('#cur_qty').text()); 

   $.post("../itemadjust/order_list_update.php",{iddd : iddd,c_name:c_name,c_value:c_value,adj_price:adj_price,plus:plus});

   if(adj_qty>0 && plus=='+'){
  	    targt.find('#new_qty').text(Number(cur_qty+adj_qty));
  	} else if(adj_qty>0 && plus=='-'){
  		targt.find('#new_qty').text(Number(cur_qty-adj_qty));
  	} else {
      targt.find('#new_qty').text('');
    }

	var amtotal=0;
	$("[c_name=qty]").each(function(){
	      amtotal += +Number($(this).val());
	});
	$('#adsm_totalc').val(amtotal);

   });  
 });    
</script>

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