<?php 
   
$PID = "other_cash";
$SC = $_SERVER["SCRIPT_NAME"];

require_once("lib/dbconn.php");
require_once("lib/form.php");
require_once("lib/functions.php");

$listLayanan = mysql_query( "SELECT id, layanan FROM tbl_mjurnal order by layanan ");

title("<img src='images/sales-report-icon.png' width='65' align='absmiddle' >  Other Income / Expend");

?>
<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>
<form action="actions/other_income_insert.php" method="POST">
	<input type="hidden" name="p" value="<?php echo $_GET[p]; ?>"/>
    <table border="1" class="atable" width='100%'>
        <thead>
        <tr>
            <th>DESCRIPTIONS</th>
            <th>TYPE</th>
            <th>RECEIPT NO</th>
            <th>AMOUNT</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" size="40" value="" required="required"></td>
            <td align="center">
            	<select name="dk[]" class="dk" id="dk_1">
            		<option value="D">Income</option>
            		<option value="C">Expence</option>
            	</select>
            </td>
            <td align="center"><input type="text" name="kwitansi[]" class="kwitansi" id="kwitansi_1" value="" required="required"></td>
            <td align="center"><input type="text" name="jumlah[]" class="jumlah" id="jumlah_1" value="0" style="text-align: right;" required="required"></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="3">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">&nbsp;</td>
        </tr> 
        <tr>
            <td align="right" colspan="3">DATE</td>
            <td align="center"><input type='text' name="tgl_trans" class="tanggal" readonly="readonly"></td>
            <td align="center"><input type="submit" value="Save"></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[]" size="40" class="layanan" id="layanan_'+nextrow+'" value="" required="required"></td>'+
            '<td align="center">'+
            	'<select name="dk[]" class="dk" id="dk_'+nextrow+'">'+
            		'<option value="D">Income</option>'+
            		'<option value="C">Expence</option>'+
            	'</select>'+
            '</td>'+
            '<td align="center"><input type="text" name="kwitansi[]" class="kwitansi" id="kwitansi_'+nextrow+'" value="" required="required"></td>'+
            '<td align="center"><input type="text" name="jumlah[]" class="jumlah" id="jumlah_'+nextrow+'" value="0" style="text-align: right;" required="required"></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);
	    });
	    
        $('.jumlah').keyup(function(){
            var nm_id = $(this).attr('id').split("_");
            var aud = nm_id[1];

            hitung_total(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["layanan"]);
        echo "{";
        echo "id: " . $rew["id"] . ", ";
        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('');
        $("#jumlah_"+tag).val('');
        var kecId = ui.item.id;
        var kecNama = ui.item.value;
		
        $("#layananid_"+tag).val(kecId);
        $("#layanan_"+tag).val(kecNama);
        $("#jumlah_"+tag).val(0);
        
    }
    
    });
    
}


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);
    });
    
    $('.jumlah').keyup(function(){
        var nm_id = $(this).attr('id').split("_");
        var aud = nm_id[1];

        hitung_total(aud);
   });

});
</script>