<?php if($act=="add" || $act=="edit"){?>

<div class="py-3 pl-1">
	<a href="javascript:void(0)" onclick="add_detailrow()" class="btn btn-primary btn-sm">Tambah Item
	</a>
</div>

<?php } ?>

<div id="detail" style="overflow-y:scroll;height:350px;">
<table class="table table-bordered" id="jt_table">
<thead>
<tr class="thead" style="background-color:#333;color:white">
	<td style="width:30px">No</td>	
	<td class="tc" style="width:300px">Nama Item</td>
    <td class="tr" style="width:120px;">Harga Satuan</td>
    <td class="tr" style="width:100px;">Qty Kirim</td>
    <td class="tr" style="width:100px;">Qty Pabrik</td>
    <td class="tr" style="width:100px;">Qty Retur</td>
    <td class="tr" style="width:150px;">Subtotal</td>
    <td class="tr" style="width:150px;">Potongan</td>
    <td class="tc" style="width:300px;">Catatan</td>
</tr>
</thead>
<?php

$subtotal = 0;
$total_pot = 0;
$i = 0;
$total_retur = 0;
$sqldetail = "SELECT * FROM sodetail a, mst_product b 
				WHERE a.pm_autoid = b.pm_autoid AND a.so_autoid = '$autoid' ORDER BY a.sd_autoid";
$detail_con = $conn->query($sqldetail) or die($sqldetail);
while($rsd = $detail_con->fetch_assoc()){
	
	$pm_autoid 		= $rsd['pm_autoid'];
	$pmdesc 		= $rsd['pm_name'];
	
	$qty 				= $rsd['sd_qty'];
	$pm_bprice 			= $rsd['sd_price'];


	$sd_notes = $rsd['sd_notes'];

	$qtyp = $rsd['sd_qtyp'];
	$qtyr = $rsd['sd_qtyr'];
	
	$potongan = $rsd['sd_potongan'];
	$detail_subtotal 	= $qtyp * $pm_bprice;
	$retur = $qtyr * $pm_bprice;
?>
<tr>
	<td><?php echo $i+1?></td>
	<td>
    	<input type="text" id="pm<?php echo $i?>" class="input" value="<?php echo $pmdesc?>" autocomplete="off" style="width:100%" onfocus="pm_auto_complete('<?php echo $i?>')">
        <input type="hidden" id="pm_autoid<?php echo $i?>"  autocomplete="off" name="pm_autoid<?php echo $i?>" value="<?php echo $rsd['pm_autoid']?>">
    </td>
    <td>
    	<input type="text" class="input tr" onkeyup="calc_cb()" id="pm_bprice<?php echo $i?>" value="<?php echo $pm_bprice?>"
         name="pm_bprice<?php echo $i?>" style="width:100%" autocomplete="off">
    </td>
    <td>
    	<input type="text" class="input tr" onkeyup="calc_cb()" id="qty<?php echo $i?>" value="<?php echo $qty?>"
         name="qty<?php echo $i?>" style="width:100%" autocomplete="off">
    	<input type="text" class="input tr" style="width:70px;display:none"
         name="pm_disc<?php echo $i?>" id="pm_disc<?php echo $i?>" value="<?php echo $pm_disc?>"  onkeyup="calc_cb()" >
         
        <select name="pm_disctype<?php echo $i?>" id="pm_disctype<?php echo $i?>" class="input" style="width:50px;display:none"  onchange="calc_cb()" >
	        <option <?php if($pm_disctype=="P"){echo 'selected="selected"';}?> value="P">%</option>
	        <option <?php if($pm_disctype=="R"){echo 'selected="selected"';}?> value="R">Rp</option>
        </select>
        <input type="hidden" id="nett_price<?php echo $i?>" name="nett_price<?php echo $i?>" value="">
    </td>
    <td class="tr">
    <input type="text" class="input tr" onkeyup="calc_cb()" id="qty_p<?php echo $i?>" name="qty_p<?php echo $i?>" value="<?php echo $qtyp?>">
    </td>
    <td class="tr">
    <input type="text" class="input tr" onkeyup="calc_cb()" id="qty_r<?php echo $i?>" name="qty_r<?php echo $i?>" value="<?php echo $qtyr?>">
    </td>
    <td>
    	<input type="text" class="input tr" onkeyup="calc_cb()" id="subtotal<?php echo $i?>" value="<?php echo $detail_subtotal?>"
         name="subtotal<?php echo $i?>" style="width:100%" autocomplete="off" >
    </td>
    <td class="tr">
    	<input type="text" class="input tr" onkeyup="calc_cb()" id="potongan<?php echo $i?>" value="<?php echo $potongan?>"
         name="potongan<?php echo $i?>" style="width:100%" autocomplete="off" >
    </td>
    <td>
		<input type="text" class="input w-100" onkeyup="calc_cb()" id="sd_notes<?php echo $i?>" value="<?php echo $sd_notes?>"
         name="sd_notes<?php echo $i?>" autocomplete="off" >
    </td>
</tr>
<?php 
$i = $i + 1;
$subtotal = $subtotal + $detail_subtotal;
$total_pot = $total_pot + $potongan;
$total_retur = $total_retur + $retur;
}

$gtotal = $subtotal - $total_pot;
?>

</table>
</div>
<script>
</script>
<div style="border-top:1px solid black;">
<table class="table table-bordered">
<tr class="bold">
	<td style="width:80%" class="tr">TOTAL</td>
    <td class="tr">
        <input readonly class="input tr bold" name="subtotal" id="subtotal" value="<?php echo $subtotal?>">
    </td>
</tr>
<tr class="bold">
	<td style="width:80%" class="tr">TOTAL POT</td>
    <td class="tr">
        <input readonly class="input tr bold" name="total_pot" id="total_pot" value="<?php echo $total_pot?>">
    </td>
<tr class="bold">
	<td style="width:80%" class="tr">TOTAL RETUR</td>
    <td class="tr">
        <input readonly class="input tr bold" name="total_retur" id="total_retur" value="<?php echo $total_retur?>">
    </td>
</tr>
<tr class="bold">
	<td style="width:80%" class="tr">G. TOTAL</td>
    <td class="tr">
        <input readonly class="input tr bold" name="gtotal" id="gtotal" value="<?php echo $gtotal?>">
    </td>
</tr>

</table>
</div>
<div id="chk" style="display:none"></div>

							<input type="hidden" id="totalcb" name="totalcb" value="<?php echo $total?>" autocomplete="off">
							<input type="hidden" id="row" autocomplete="off" name="row" value="<?php echo $i?>" />