<?php
$laay = $_GET[kd];
$listLayanan = mysql_query( "SELECT id, nm_layanan, hrg_layanan FROM tbl_layanan where jns_layanan = '$laay' and status_layanan = 1 order by nm_layanan ");


?>
<style>
.atable{
	border-collapse:collapse;
	border:1px solid #276F86;
        font-family: verdana,arial,sans-serif;
        font-size: 12px;
}
.atable th{
	border:1px solid #fff;
	background:#276F86;
	font-weight:bold;
        color: #fff;
}
.atable td{
	padding:4px;
	border:1px solid #276F86;
}
.atable tfoot{
	border:1px solid #fff;
	background:#fbd850;
	font-weight:bold;
        color: #000;
}
.evenRow{
	background:#AFFBA2;
}

</style>
<div align="center"><a href="javascript:CekMenu('<?php echo $laay; ?>')"><img src="images/product-sales-report-icon.png" width="20"/>&nbsp;List Menu<img src="images/product-sales-report-icon.png" width="20"/>&nbsp;</a></div>
<form action="actions/input_layanan_insert.php" method="POST">
	<input type="hidden" name="p" value="<?php echo $_GET[p]; ?>"/>
	<input type="hidden" name="idx" value="<?php echo $_GET[t]; ?>"/>
	<input type="hidden" name="kd" value="<?php echo $_GET[kd]; ?>"/>
	<input type="hidden" name="act" value="<?php echo $_GET[act]; ?>"/>
	<input type="hidden" name="jns_trans" value="<?php echo $_GET[jns]; ?>"/>
	<input type="hidden" name="jns" value="<?php echo $_GET[jns]; ?>"/>
    <table border="1" class="atable" width='100%'>
        <thead>
        <tr>
            <th>SERVICE</th>
            <th>PRICE</th>
            <th>QTY</th>
            <th>DISCOUNT</th>
            <th>TOTAL</th>
            <th><input type="button" value="Add" id='addNewRaw'/></th>
        </tr>
        </thead>
        <tbody>
        <tr id="1">
            <input type="hidden" name="total[]" class="total" id="total_1">
            <input type="hidden" name="totharga[]" class="totharga" id="totharga_1">
            <input type="hidden" name="layananid[]" class="layananid" id="layananid_1">
            <td align="center"><input type="text" name="layanan[]" class="layanan" id="layanan_1" value="" required="required"></td>
            <td align="center"><input type="text" name="harga[]" class="harga" id="harga_1" value="0" style="text-align: right;" readonly="readonly"></td>
            <td align="center"><input type="text" name="qty[]" class="qty" id="qty_1" value="1" size="5" style="text-align: center;"></td>
            <td align="center"><input type="text" name="diskon[]" class="diskon" id="diskon_1" value="0" style="text-align: right;" ></td>
            <td align="center"><input type="text" name="jumlah[]" class="jumlah" id="jumlah_1" value="0" style="text-align: right;" readonly="readonly"></td>
            <td align="center"><img src="images/close3.png" class="delRow" id="1" width="20" style="cursor: pointer;" title="Hapus Baris"></td>
        </tr>
        </tbody>
        <tfoot>
        <tr>
            <td align="right" colspan="4">TOTAL</td>
            <td align="center"><input type='text' name="total_all" id="tot_all" value="0" style="text-align: right;" readonly="readonly"></td>
            <td align="center"><input type="submit" value="Simpan"></td>
        </tr>    
        </tfoot>
    </table>
    <input type="hidden" name="max_layanan" id="max_layanan" value="1"/>
    
</form>

<script type="text/javascript">
$("document").ready(function(){
    
        $("#addNewRaw").on('click',function(){
        var jml_row = parseInt($("#max_layanan").val());
        var nextrow = jml_row + 1;
        var qty = 100;
        $("#max_layanan").val(nextrow);    
        $('.atable tbody').append('<tr id="'+nextrow+'">'+
        	'<input type="hidden" name="total[]" class="total" id="total_'+nextrow+'">'+
            '<input type="hidden" name="totharga[]" class="totharga" id="totharga_'+nextrow+'">'+
            '<input type="hidden" name="layananid[]" class="layananid" id="layananid_'+nextrow+'">'+
            '<td align="center"><input type="text" name="layanan[]" class="layanan" id="layanan_'+nextrow+'" value="" required="required"></td>'+
            '<td align="center"><input type="text" name="harga[]" class="harga" id="harga_'+nextrow+'" value="0" style="text-align: right;" readonly="readonly"></td>'+
            '<td align="center"><input type="text" name="qty[]" class="qty auto" id="qty_'+nextrow+'" value="1" size="5" style="text-align: center;"></td>'+
            '<td align="center"><input type="text" name="diskon[]" class="diskon" id="diskon_'+nextrow+'" value="0" style="text-align: right;" ></input></td>'+
            '<td align="center"><input type="text" name="jumlah[]" class="jumlah" id="jumlah_'+nextrow+'" value="0" style="text-align: right;" readonly="readonly"></td>'+
            '<td align="center"><img src="images/close3.png" class="delRow" id="'+nextrow+'" width="20" style="cursor: pointer;"  title="Hapus Baris"></td>'+
        '</tr>');
        
        $('.layanan').keyup(function(){
        	var nm_id = $(this).attr('id').split("_");
            var aud = nm_id[1];
	         cari_data(aud);
	    });
	    
        $('.diskon').keyup(function(){
            var nm_id = $(this).attr('id').split("_");
            var aud = nm_id[1];

            hitung_diskon(aud);
       });

       $('.qty').keyup(function(){
            var nm_id = $(this).attr('id').split("_");
            var aud = nm_id[1];

            hitung_qty(aud);
       });

       $('.delRow').on('click', function() { 
            id  = this.id;
            hasil = $("#max_layanan").val();
            console.log(hasil);
            
            $('.atable tbody tr#'+id).remove();
            $("#max_layanan").val(hasil - 1);
        
            hitung_total();
        });        
    });
    
});

</script>

<script type="text/javascript">

function cari_data(tag){
        
    var data = [
    <?php
    while ($rew = mysql_fetch_array($listLayanan)) {
        $layanan1 = str_replace("'", " ", $rew["nm_layanan"]);
        echo "{";
        echo "id: " . $rew["id"] . ", ";
        echo "harga: " . $rew["hrg_layanan"] . ", ";
        echo "value: '" . $layanan1 . "', ";
        echo "},";
    }
    ?>
    ];
   $("#layanan_"+tag).autocomplete({
    source: data,
    messages: {
        noResults: "",
        results: function(amount) {

        }
    },
    minLength: 2,
    select: function(event, ui) {
        $("#layananid_"+tag).val('');
        $("#layanan_"+tag).val('');
        $("#harga_"+tag).val('');
        $("#jumlah_"+tag).val('');
        var kecId = ui.item.id;
        var kecNama = ui.item.value;
        var kecHarga = ui.item.harga;
		
        $("#layananid_"+tag).val(kecId);
        $("#layanan_"+tag).val(kecNama);
        $("#harga_"+tag).val(kecHarga);
        $("#jumlah_"+tag).val(kecHarga);
        
    }
    
    });
    hitung_total();
}

function hitung_qty(tag){    
    var harga = parseInt($("#harga_"+tag).val());
    var qty = parseInt($("#qty_"+tag).val());
    var tot = 0;
    
    if(qty < 0){
		alert('Qty harus lebih dari 0 !');
		$("#qty_"+tag).val(0);
	} else {
		tot = harga * qty; 
		$("#jumlah_"+tag).val(tot);
	}    
     hitung_total();
}

function hitung_diskon(tag){    
    var harga = parseInt($("#harga_"+tag).val());
    var qty = parseInt($("#qty_"+tag).val());
    var diskon = parseInt($("#diskon_"+tag).val());
    var tot = 0;
    tot = (harga * qty) - diskon; 
    
    $("#jumlah_"+tag).val(tot);
    hitung_total();
}

function hitung_total(){
    var total_row = parseInt($("#max_layanan").val());

    tot1 = 0; 
    for (i = 1; i <= total_row; i++) {
            tot1 = tot1 + parseInt($("#jumlah_"+i).val());
        }
    $("#tot_all").val(tot1);
}

$(function(){
            
    $('.layanan').keyup(function(){
    	var nm_id = $(this).attr('id').split("_");
        var aud = nm_id[1];
         cari_data(aud);
    });
    
    $('.diskon').keyup(function(){
        var nm_id = $(this).attr('id').split("_");
        var aud = nm_id[1];

        hitung_diskon(aud);
   });

   $('.qty').keyup(function(){
        var nm_id = $(this).attr('id').split("_");
        var aud = nm_id[1];
		
        hitung_qty(aud);
   });
   

});
</script>

<script language='JavaScript'>
function CekMenu(tag) {
    sWin = window.open('includes/cek_menu.php?jns='+tag, 'xWin',
 'top=0,left=0,width=750,height=550,menubar=no,scrollbars=yes');
    sWin.focus();
}
</script>

<?php
if (isset($_GET[f])){
	mysql_query("update tbl_transaksi set status_trans = 0 where id = ".$_GET[f]);
	echo "<script language='JavaScript'>document.location='index2.php?p=".$_GET[p]."&jns=".$_GET[jns]."&kd=".$_GET[kd]."&act=".$_GET[act]."&e=tambah&t=".$_GET[t]."'</script>";
}

$idx = $_GET[t];

?>
<div class="column">
	<table class="gridtable" width="100%" border="1px">
		<tr>
			<th>&nbsp;</th>
			<th>Tgl.Trans</th>
			<th>No.Kwitansi</th>
			<th>Jenis / Item</th>
			<th>Harga</th>
			<th>Qty</th>
			<th>Diskon</th>
			<th>Jumlah</th>
		</tr>
		<?php
		
		$sql4 = "select a.*, b.nm_layanan from tbl_transaksi a 
				join tbl_layanan b on b.id = a.item_id 
				where a.jns_trans = '".$_GET[jns]."' and no_booking = '$idx'
				and status_trans = 1 order by a.tgl_trans, a.wkt_entry";
		$r4 = mysql_query($sql4);
		$n4 = mysql_num_rows($r4);
		
		$noo = $no++;
		$tot = 0;
		while ($row4 = mysql_fetch_array($r4)) { ?>
			<tr>
				<?php if(strlen($row4[no_kwitansi]) > 0) { ?>
				<td>&nbsp;</td>
				<?php } else { ?>
				<td align="center">
					<a href="<?php echo "$SC?p=$PID&jns=".$_GET[jns]."&kd=".$_GET[kd]."&act=".$_GET[act]."&e=".$_GET[e]."&t=".$_GET[t]."&f=".$row4[id];?>" onClick="return confirm('Apakah yakin akan di Hapus?')"><img src="images/icon-del-left.png" title="Hapus Layanan"/></a>
				</td>
				<?php } ?>
				<td align="center"><?php echo $row4[tgl_trans]; ?></td>
				<td align="center"><?php echo $row4[no_kwitansi]; ?></td>
				<td><?php echo ucwords(strtolower($row4[nm_layanan])); ?></td>
				<td align="right">Rp. <?php echo number_format($row4[harga],2,",","."); ?></td>
				<td align="center"><?php echo $row4[qty]; ?></td>
				<td align="right">Rp. <?php echo number_format($row4[diskon],2,",","."); ?></td>
				<td align="right">Rp. <?php echo number_format($row4[jumlah],2,",","."); ?></td>				
			</tr>
			
		<?php $tot = $tot + $row4[jumlah];} ?>
		<tr>
			<td align="right" colspan="6"><a href="javascript:cetakTagihan('<?php echo $_GET[t];?>','<?php echo $_GET[jns];?>');"><img src="images/print-icon1.png" title="Print Tagihan"/></a></td>
			<td align="right" ><b>TOTAL</b></td>
			<td align="right"><b>Rp. <?php echo number_format($tot,2,",",".");?></b></td>
		</tr>
	</table>
</div>
<script language='JavaScript'>
function cetakTagihan(tag1,tag2) {
    sWin = window.open('includes/cetak_tagihan.php?idx='+tag1+'&jns='+tag2, 'xWin',
 'top=0,left=0,width=750,height=550,menubar=no,scrollbars=yes');
    sWin.focus();
}
</script>