<div id="_hedBill">
	<?php 
		if(empty($this->data->kasir->no_order)){
			echo $this->data->meja->nm_merek; 
		}
		else{
			echo date("dmy", strtotime($this->data->kasir->tgl)).str_pad($this->data->kasir->no_order, 3, "0", STR_PAD_LEFT).' ('.date("H.i", strtotime($this->data->kasir->tgl)).')<br>'.$this->data->meja->nm_merek; 	
		}
		
	?>
	<!--<input class="_menu _to" type="button" value="CEK ORDER">-->

</div>
<div id="_detBill">
	<?php
	$grup = array();
	$subtotal = 0;
	foreach($this->data->item as $r){
    	$grup[$r->nm_app][] = $r;
    	$subtotal += $r->total;
	}
	foreach($grup as $row){
		echo "<div class='_divItem0'>";
		foreach($row as $r){
			$cLr = '_app';
			if($r->app == 9){
				$cLr = '_notApp';
			}
			else if($r->status2 == "T"){
				$cLr = '_orderFinish';
			}
	?>
	<div class="_divItem <?php echo $cLr ?>">
		<div class="_divItem1">
			<?php 
			echo $r->nm_inv;
			if(!empty($r->ket_det)){
				echo "<br>Note : <i>".$r->ket_det.'</i>';
			}
			?>
		</div>
		<div class="_divItem2">
			<?php echo $r->qty.' x Rp '.number_format($r->harga) ?>
		</div>
		<div class="_divItem3">
			<?php echo number_format($r->total) ?>
			<?php if($cLr == "_notApp"){?>
				<input type="button" no_item="<?php echo $r->no_item ?>" class="_btnClear" value="X">
			<?php }else{ ?>
				<input type="button" no_item="<?php echo $r->no_item ?>" class="_btnClearApp" value="X">
			<?php } ?>
		</div>
	</div>
	<?php 
		}
		echo "</div>";
	} 
	?>
</div>
<table cellspacing="0" cellpadding="0" class="_tabTotal">
	<?php if($this->data->meja->nm_kat == "TAKE AWAY"){?>
	<tr>
		<td>Pembeli</td>
		<td>
			<input type="text" kode="<?php echo str_ireplace(' ','_',$this->data->meja->kd_merek) ?>" id="_pembeli_<?php echo str_ireplace(' ','_',$this->data->meja->kd_merek) ?>" class="_pembeli" value="<?php echo $this->data->kasir->name ?>">
		</td>
	</tr>
	<?php }?>
	<tr>
		<td>SUB TOTAL</td>
		<td style="width: 150px;"><input type="text" readonly class="_txtTot decimal" id="_subtotal" name="_subtotal"></td>
	</tr>
    <tr>
    	<td>DISC (%)</td>
    	<td>
    		<div style="white-space: nowrap;">
        		<input type="number" class="_txtTot" id="_diskg" min="0" max="100" value="0" style="width:50px; background-color:white; display:inline-block;"> %
        		<input type="text" readonly class="_txtTot decimal" id="_diskon" name="_diskon" style="width:80px; display:inline-block;">
    		</div>
		</td>
	</tr>

    <tr>
    	<td>PB1 (10%)</td>
    	<td>
    	    <input type="checkbox" id="_ppn_check" checked style="width:50px; transform: scale(1.4); -webkit-transform: scale(1.4); background-color:white; display:inline-block;">
    		<input type="hidden" id="_ppn" name="_ppn" value="E">
    		<input type="text" readonly class="_txtTot decimal" id="_ppn_nilai" name="_ppn_nilai" style="width:90px; display:inline-block;">
    	</td>
    </tr>
    <tr>
		<td>TOTAL</td>
		<td style="width: 150px;"><input type="text" readonly class="_txtTot decimal" id="_total" name="_total"></td>
	</tr>
	<tr>
		<td colspan="2" style="width: 100%; text-align: right">
		<input class="_menu _bayar" type="button" value="BAYAR">
		</td>
	</tr>
	<!--<tr>
		<td>KEMBALI</td>
		<td><input type="text" readonly class="_txtTot decimal" id="_kembali" name="_kembali"></td>
	</tr>-->
	
</table>

<script>
$(document).ready(function(){	
    function recalcBill() {
    	subtotal = parseFloat($('#_subtotal').val().replace(/,/g, '')) || 0;
    	pct = parseFloat($('#_diskg').val().replace(/,/g, '')) || 0;
    
    	if (pct < 0) pct = 0;
    	if (pct > 100) pct = 100;
    
    	diskon = subtotal * (pct / 100);
    	dpp = subtotal - diskon;
    
    	let pajak = 0;
    	if ($('#_ppn_check').is(':checked')) {
    		pajak = dpp * 0.10;
    		$('#_ppn').val('E');
    	} else {
    		$('#_ppn').val('N');
    	}
    
    	total = dpp + pajak;
    
    	$('#_diskon').val(diskon.toFixed(0));
    	$('#_ppn_nilai').val(pajak.toFixed(0));
    	$('#_total').val(total.toFixed(0));
    	setDec();
    }
    
    $('#_ppn_check').on('change', function () {
    	recalcBill();
    });

    $('#kodemeja').val('<?php echo $this->data->meja->kd_merek ?>');
	$('._btnTambah').removeAttr('disabled');
	$('._qtyItem').val(1);
	$('#_subtotal').val('<?php echo $subtotal ?>');
	$('#_total').val('<?php echo $subtotal ?>');
	$('#_inpBayar').val(0);
	$('#_kembali').val(0);
	$('.ket_item').val('');
	$("._petugasSelect").val('<?php echo $this->data->kasir->kd_kasir; ?>');
	$('#_diskg').on('input', recalcBill);
	recalcBill();
	$('._iconMeja').removeClass('_mejaAktif');
	<?php foreach($this->data->aktif as $meja){ ?>
		$('#_iconMeja_<?php echo $meja ?>').addClass("_mejaAktif");
	<?php } ?>
	$('#pembeli').val('');
	$('._pembeli').on('change', function() {
		$('#pembeli').val($(this).val());
	});
	
	$('._btnClear').click(function(){
		_no_item = $(this).attr('no_item');
		$('#_no_item').val(_no_item);
		$('#_ket').val('');
		$('#_user').val('');
		$('#_pass').val('');
		makeDeleteItem('','',_no_item,'');
	});
	$('._btnClearApp').click(function() {
		_no_item = $(this).attr('no_item');
		$('#_no_item').val(_no_item);
		$('#_ket').val('');
		$('#_user').val('');
		$('#_pass').val('');
		$('#_dialogBatal').dialog('open');
	});
	$('._to').click(function() {
		_kode = '<?php echo $this->data->meja->kd_merek; ?>';
		_baseUrl = '<?php echo $this->baseUrl; ?>';

		newWindow = window.open(_baseUrl + 'posr/itemmenu/&kode=' + _kode, '_blank');
		newWindow.onbeforeunload = function() {
		  makeLoadMeja(_kode);
		};
	});
    $('._bayar').click(function(){
		_noref = '<?php echo $this->data->kasir->no_pos ?>';
    	_baseUrl = '<?php echo $this->baseUrl ?>';
    	_diskg = $('#_diskg').val() || 0;
    	_ppn = $('#_ppn').val() || 'E';
        
        if(_noref != ''){
            _url = _baseUrl+"posb/index/MIS/"+"&noref="+_noref+"&diskg="+_diskg+"&ppn="+_ppn;
    	    window.open(_url, "_blank");
        }
        else{
            alert("Belum ada order untuk di-bill!");
        }
	});
})
</script>