<?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'];
	$order= mysqli_query($conn,"SELECT i.imei_status,i.pro_id,i.item_name,i.item_details,i.item_code,o.net_amount,o.pro_unit,o.unit_val,o.eoid,o.qty,o.discount,o.net_amount,o.amount,o.change_slprice,o.dis_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;
	$sm_disc=0;
	$sm_total=0;
	while($orw= mysqli_fetch_assoc($order)){
        $pro_unit  = $orw['pro_unit'];
        $unit_val  = $orw['unit_val'];
		$sm_qty   += $orw['qty'];
		$pu_total = $orw['net_amount'];
		$sm_total += $pu_total;
		$imei_status = $orw['imei_status'];
		$pro_id     = $orw['pro_id'];
		
?>
<tr>
	<td class="text-center"><?php echo ++$i;?></td>
	<td><?php echo $orw['item_name']." ".$orw['item_details'].", ".$orw['item_code'];?></td>
	
	<?php if ($imei_status==1) {
 echo $count_row =mysqli_fetch_assoc(mysqli_query($conn,"SELECT  COUNT(*) as count_row FROM imei_tracker_edit 
       WHERE coid='$coid' AND bid='$bid' AND invoice='$invoice'  "))['count_row'];
?>
	<td onclick="imei_list_pu(<?php echo $pro_id; ?>)"><input type="text" class="form-control text-center form-control-sm inputselect pu_qty" required="true" readonly="true" step="0.001" min="0.001"  value="<?php echo $orw['qty'];?>" iddd="<?php echo $orw['eoid'];?>" c_name="qty"></td>
	<td >
	  <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' AND default_set='primary' LIMIT 1"))['unit_name'];
	  		?>
	  	</option>
       <?php 
         $iut= mysqli_query($conn,"SELECT puid,unit_name,unit_convart FROM pro_unit WHERE coid='$coid' AND default_set='primary' 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>

<?php }else{ ?>

	<td ><input type="text" class="form-control text-center form-control-sm inputselect pu_qty" id="pu_qty" required="true" step="0.001" min="0.001"  value="<?php echo $orw['qty'];?>" iddd="<?php echo $orw['eoid'];?>" c_name="qty">



<script>
    $(document).ready(function(){
    $('input#pu_qty,input#pu_price').blur(function(){
        var num = parseFloat($(this).val());
        var cleanNum = num.toFixed(3);
        $(this).val(cleanNum);
        if(num/cleanNum < 1){
            $('#erro').text('Please enter only 2 decimal places, we have truncated extra points');
            $("#pu_qty").bind("keypress", function(e) {
          if (e.keyCode == 13) {
             return false;
          }
       });
            }

    


        });
    });
</script>



	</td>
	<td class="<?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>
<?php } ?>	
<script>
 function imei_list_pu(productid){    
   $("#modal-xl").modal({backdrop: "static"});
    pagecall('../imei_tracking/purchase_imei_index.php?pro_id='+productid+'&invoice='+'<?php echo $invoice; ?>'+'&eoid='+'<?php echo $orw['eoid'];?>','#xlmdview');   
   } 
</script>	
	<td>
		<input type="text" step="0.001"  class="form-control text-center form-control-sm inputselect pu_price  " id="pu_price" required="true" value="<?php echo $orw['amount'];?>" iddd="<?php echo $orw['eoid'];?>" c_name="amount" >
    <?php if(input_check('s_calculator') ==1){ ?>
		<input type="text" step="0.001"  class="form-control text-center form-control-sm text-white  pucal "  iddd="<?php echo $orw['eoid'];?>" placeholder="Start Calculator" style="width:100% ;background:#8d323a;"> 
	<?php } ?>
	</td>




<script type="text/javascript">
    $(document).ready(function() {
    	$('.pucal').abacus();
       $(".pucal").bind("keypress", function(e) {
          if (e.keyCode == 13) {
             return false;
          }
       });
    });
</script>

		<td><input type="text" class="form-control text-center form-control-sm inputselect pu_disc"  required="true" value="<?php echo $orw['dis_amount'];?>" iddd="<?php echo $orw['eoid'];?>" c_name="dis_amount"> </td>



	
	<td class="text-right">
	    <input type="number" step="0.001" class="form-control text-center form-control-sm inputselect" required="true" value="<?php echo $orw['change_slprice'];?>" iddd="<?php echo $orw['eoid'];?>" c_name="change_slprice">
	</td>


	<td class="text-right">
		<dt id="pu_total" class="pu_total"><?php echo $pu_total;?></dt>
	</td>



    <?php 

  $zero_qty_row =mysqli_fetch_assoc(mysqli_query($conn,"SELECT  COUNT(*) as count_row FROM order_list_edit 
       WHERE coid='$coid' AND bid='$bid' AND brid='$brid' AND invoice='$invoice' AND qty=0 AND eoid='".$orw['eoid']."' "))['count_row'];

?>



	<?php if ($imei_status==1 && $zero_qty_row > 0) { ?>
	   <td class="text-center" onclick="odelete(<?php echo $orw['eoid'];?>)"><i class="fas fa-trash"></i></td>
	<?php }else if ($imei_status==1 && $zero_qty_row == 0){ ?>
	   <td class="text-center" title="Please frist Remove Imei Code Then Delete"><i class="fas fa-close"></i></td>
	<?php }else{  ?>
	   <td class="text-center" onclick="odelete(<?php echo $orw['eoid'];?>)"><i class="fas fa-trash"></i></td>
	<?php }  ?>
	
	</tr>
<?php } ?>

    <tr>
       <td class="text-right" colspan="2"><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 <?php if(input_check('u_type') !==1){echo 'd-none';}?>"></td>
       <td class="text-right"></td>
       <td class="text-right"></td>
       <td class="text-right"><dt id="sm_total"><?php echo $sm_total;?></dt></td>
       <td class="text-center"></td>
    </tr>
<?php 
$count_zero_qty_row =mysqli_fetch_assoc(mysqli_query($conn,"SELECT  COUNT(*) as count_row FROM order_list_edit 
       WHERE coid='$coid' AND bid='$bid' AND brid='$brid' AND invoice='$invoice' AND qty=0 "))['count_row'];
 ?>
<script>
 $('select').select2();
 $(document).ready(function() {
  $('#sm_totalc<?php echo $invoice;?>').val(<?php echo $sm_total;?>);
  $('#count_zero_qty_row<?php echo $invoice;?>').val(<?php echo $count_zero_qty_row;?>);

  blcal();
  $("[iddd]").on('input', function(e){	
   var iddd    = $(this).attr("iddd");
   var c_name  = $(this).attr("c_name");   
   var c_value = Number($(this).val());
   var targt   = $(this).parent().parent();


	    $.post("../purchase_retrun/order_list_update.php",{iddd : iddd,c_name:c_name,c_value:c_value});
        var pu_qty   = Number(targt.find('.pu_qty').val());
        var pu_price = Number(targt.find('.pu_price').val());
        var pu_disc  = Number(targt.find('.pu_disc').val());
        var convert  = Number(targt.find(':selected').attr('convert'));

		  if(!isNaN(pu_qty) && !isNaN(pu_price) && !isNaN(convert)){
		      targt.find('#pu_total').html(Number((pu_price*pu_qty*convert) - pu_disc));
		  }
		  else
		  {
              targt.find('#pu_total').html('');
		  }
		  var qty_sum=0;
		  targt.parent().find(".pu_qty").each(function(){
		    qty_sum += +Number($(this).val());
		  });
		  targt.parent().find("#sm_qty").html(qty_sum);
		      
		  var amtotal=0;
		  targt.parent().find(".pu_total").each(function(){
		      amtotal += +Number($(this).text());
		  });
		  targt.parent().find('#sm_total').html(amtotal);
		  $('#sm_totalc<?php echo $invoice;?>').val(amtotal);
		  blcal();
   });	
 });	
</script>

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