<div class="wrapper wrapper-content animated fadeInRight">
	<div class="row">
		<div class="col-lg-12">
			<div class="ibox float-e-margins">
				<div class="ibox-content">
					<div class="filter-content" style="">
						<form autocomplete="off" method="post" id="form_input" class="form-horizontal">
							<div class="row">
								<div class="col-sm-8">
                                    <br>
                                    <div class="form-group">
                                        <label class="col-sm-2">Nomor</label>
                                        <div class=" input-group col-sm-4">
                                            <input type="text" class="form-control" id="coal_get_number" name="t_coal_get_number" value="" placeholder="" onkeydown="if(event.keyCode==13)getDataInbound();">
                                            <span class="help-block m-b-none"><i style="color:red;">* kosongkan nomor / input nomor yang ada untuk mencetak</i></span>                                            
                                        </div>
                                    </div><br>
                                    <div class="form-group" hidden>
                                        <label class="col-sm-2">Kendaraan</label>
                                        <div class=" input-group col-sm-3">
                                            <input type="text" class="form-control" id="vehicle_id" name="t_vehicle_id" value="" placeholder="" >
                                            <!-- <span class="help-block m-b-none"><i style="color:red;">* tekan enter setelah mengisi</i></span>  -->
                                            <input type="text" class="form-control hidden" id="id_rec" name="t_id_rec" value="">
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <label class="col-sm-2">Stock Room</label>
                                        <div class=" input-group col-sm-4">
                                            <input type="text" class="form-control" id="stockroom_id" name="t_stockroom_id" value="" onkeydown="if(event.keyCode==13)getDataStokRoom();">
                                            <span class="help-block m-b-none"><i style="color:red;">* tekan enter setelah mengisi</i></span> 
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <label class="col-sm-2">Vendor Card</label>
                                        <div class=" input-group col-sm-4">
                                            <input type="text" class="form-control" id="vendor_id" name="t_vendor_id" value="">
                                        </div>
                                    </div>
                                    <div class="form-group" hidden>
                                        <label class="col-sm-2">Vendor MIS</label>
                                        <div class=" input-group col-sm-4">
                                            <input type="text" class="form-control" id="vendor_mis" name="t_vendor_mis" value="">
                                        </div>
                                    </div>
									<div class="form-group">
                                        <label class="col-sm-2"></label>
                                        <div class=" input-group col-sm-4">
                                            <input type="text" class=""style="border: none;background: none;" id="vendor_name" name="t_vendor_name" value="" readonly>
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <label class="col-sm-2">Berat (Gross)</label>
                                        <div class=" input-group col-sm-2">
                                            <input type="text" class="form-control" style="text-align:right;" id="gross_weight" name="t_gross_weight" value="">
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <label class="col-sm-2">Berat Kosong</label>
                                        <div class=" input-group col-sm-2">
                                            <input type="text" class="form-control" style="text-align:right;" id="tare_weight" name="t_tare_weight" value="" readonly>
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <label class="col-sm-2">Berat (Nett)</label>
                                        <div class=" input-group col-sm-2">
                                            <input type="text" class="form-control" style="text-align:right;" id="net_weight" name="t_net_weight" value="" readonly>
                                        </div>
                                    </div>
                                    <div class="form-group" hidden>
                                        <label class="col-sm-2">Nomor Polisi</label>
                                        <div class=" input-group col-sm-3">
                                            <input type="text" class="form-control" id="vehicle_number" name="t_vehicle_number" value="" readonly>
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <label class="col-sm-2">Tanggal</label>
										<div class="col-sm-3">
											<?php
												$tgl = new DateTime(date('Ymd'));
												$d_tgl = $tgl->format('d-m-Y');
											?>
											<div class="input-group date">
												<span class="input-group-addon"><i class="fa fa-calendar"></i></span><input id="coal_get_date" name="t_coal_get_date" data-mask="99-99-9999" type="text" class="form-control" value="<?= $d_tgl ?>">
											</div>
										</div>
                                        <!-- <div class=" input-group col-sm-2">
                                            <input type="text" class="form-control" id="coal_get_date" name="t_coal_get_date" value="" readonly>
                                        </div> -->
                                    </div>
                                    <div class="form-group">
                                        <label class="col-sm-2">Jam</label>
                                        <div class=" input-group col-sm-2">
											<?php
												$jam = new DateTime(date('H:i:s'));
												$d_jam = $jam->format('H:i:s');
											?>
                                            <input type="text" class="form-control" id="coal_get_time" name="t_coal_get_time" value="<?= $d_jam ?>">
                                        </div>
                                    </div>
								</div>
							</div>

						</form>
					</div>
					<div class="hr-line-dashed"></div>
					<div class="row">
						<div class="col-lg-6">
                            <div class="row">
                                <div class="col-sm-2"></div>
                                <div class="col-sm-2">
									<button class="btn btn-info btn-sm btn-block" type="button" onClick="printData();" id="printBtn"><i class="fa fa-print"></i> Print</button>
                                </div>
								<div class="col-sm-2">
									<button class="btn btn-primary btn-sm btn-block saveBtn pull-left" type="submit"><i class="fa fa-save"></i> Save</button>
								</div>
								<div class="col-sm-2">
									<button class="btn btn-danger btn-sm btn-block backBtn pull-right" type="submit"><i class="fa fa-arrow-circle-left"></i> Clear</button>
								</div>
							</div>
						</div>
					</div>
					
				</div>
			</div>
		</div>
	</div>
</div>

<div style="display:none">
	<div id="print-content"></div>
</div>

<script>
	$(document).ready(function() {
        clearData();

        $("#gross_weight").keyup(function(){
			netweight( parseFloat(eval($("#gross_weight").val().replace(/,/g, ""))), parseFloat(eval($("#tare_weight").val().replace(/,/g, ""))) )
		});

        $("#tare_weight").keyup(function(){
			netweight( parseFloat(eval($("#gross_weight").val().replace(/,/g, ""))), parseFloat(eval($("#tare_weight").val().replace(/,/g, ""))) )
		});

		$("#coal_get_number").keyup(function(){
			let number = $('#coal_get_number').val();
			if (number != ''){
				document.getElementById("printBtn").disabled = false;
			} else {
				document.getElementById("printBtn").disabled = true;
			}
		});

        saveBtn.click(function() {
			let btn = $(this);
			startLoadingButton(btn);
			swal.fire({
				title: "Apakah data sudah benar ?",
				text: "Data akan disimpan",
				icon: "info",
				showCancelButton: true,
				confirmButtonText: 'Simpan',
                cancelButtonText:'Tidak',
			}).then(isConfirm => {
				if (isConfirm.value) {
				    var form_data = $('#form_input').serialize(); 				
					$.ajax({
						type: "POST",
						url: site_url + "coalgetting/coal_getting_inbound_stockroom/save",
						data: form_data, 
						dataType: 'json',
						success: function(response) {
							swal.fire(response['header'] || 'success', response['message'] || '', "success").then(() => {
								// window.location = site_url + "coalgetting_outbound/" + response['id'];
								stopLoadingButton(btn);
								$('#coal_get_number').val(response['trxnumber']);
								// $('#coal_get_date').val(response['trxdate']);
								// $('#coal_get_time').val(response['trxtime']);
								document.getElementById("printBtn").disabled = false;
                                // clearData();
							});
						},
						error: function(jqXhr, textStatus, errorThrown) {
							var error_message = 'internal error';
							if (jqXhr.responseJSON !== undefined) {
								error_message = jqXhr['responseJSON']['message'];
							}
							swal.fire('error', error_message, "error");
							stopLoadingButton(btn);
						}
					});
				} else {
					stopLoadingButton(btn);
					return false;
				}
			});
		});

        backBtn.click(function() {
            swal.fire({
				title: "Input data akan dibatalkan ?",
				text: "",
				icon: "warning",
				showCancelButton: true,
				confirmButtonText: 'Ya',
                cancelButtonText:'Tidak',
			}).then(isConfirm => {
                if (isConfirm.value) {
                    clearData();
                }
            });            
		});

	});

    function getDataStokRoom() {
		$.ajax({
			type: "POST",
			url: site_url + "master/stockroom/get_stockroom",
			data: {
				stockroom_id: $('#stockroom_id').val()
			},
			dataType: 'json',
			success: function (response) {
				$('#vendor_id').val(response.vendor_id);
				$('#vendor_name').val('   '+response.vendor_name);
				$("#vendor_name").show();
			},
			error: function (jqXhr, textStatus, errorThrown) {
				var error_message = 'Data Tidak Ditemukan';
				if (jqXhr.responseJSON !== undefined) {
					error_message = jqXhr['responseJSON']['message'];
					$("#vendor_name").hide();
				}
                swal.fire('warning', error_message, "warning");
			}
		});
	}

    function netweight(grossWeight, tareWeight)
	{
		var gross = grossWeight;
		var tare = tareWeight;
		var nett = grossWeight - tareWeight;

		$("#net_weight").val(nett);
	}

    function clearData(){
        $('#coal_get_number').val('');
        $('#stockroom_id').val('');
        $('#vehicle_id').val('');
        $('#vendor_id').val('');
        $('#vendor_mis').val('');
		$('#vendor_name').val('');
        $('#gross_weight').val('0');
        $('#tare_weight').val('0');
        $('#net_weight').val('0');
        $('#vehicle_number').val('');
        // $('#coal_get_date').val('');
        // $('#coal_get_time').val('');
        $('#id_rec').val('');
		document.getElementById("printBtn").disabled = true;
		$("#vendor_name").hide();
    }

    function getDataInbound() {
		$.ajax({
			type: "POST",
			url: site_url + "coalgetting/coal_getting_inbound_stockroom/getDataCoalGettingInbound",
			data: {
				coal_get_num: $('#coal_get_number').val()
			},
			dataType: 'json',
			success: function (response) {
				$('#vehicle_id').val(response.coalgetin.vehicle_id);
				$('#stockroom_id').val(response.coalgetin.stockroom_id);
				$('#vendor_id').val(response.coalgetin.vendor_id);
				$('#vendor_mis').val(response.coalgetin.vendor_mis);
				$('#vendor_name').val('   '+response.coalgetin.vendor_name);
				$('#vehicle_number').val(response.coalgetin.vehicle_number);
				$('#gross_weight').val(response.coalgetin.gross_weight);
				$('#tare_weight').val(response.coalgetin.tare_weight);
				$('#net_weight').val(response.coalgetin.net_weight);
				$('#coal_get_date').val(format_date(response.coalgetin.coal_get_date),'d-m-Y');
				$('#coal_get_time').val(response.coalgetin.coal_get_time);
				$('#id_rec').val(response.coalgetin.id_rec);
				$("#vendor_name").show();
			},
			error: function (jqXhr, textStatus, errorThrown) {
				var error_message = 'Data Tidak Ditemukan';
				if (jqXhr.responseJSON !== undefined) {
					error_message = jqXhr['responseJSON']['message'];
					$('#stockroom_id').val('');
					$('#vehicle_id').val('');
					$('#vendor_id').val('');
					$('#vendor_mis').val('');
					$('#vendor_name').val('');
					$('#gross_weight').val('0');
					$('#tare_weight').val('0');
					$('#net_weight').val('0');
					$('#vehicle_number').val('');
					$('#coal_get_date').val('');
					$('#coal_get_time').val('');
					$('#id_rec').val('');
					document.getElementById("printBtn").disabled = true;
					$("#vendor_name").hide();
				}
                swal.fire('warning', error_message, "warning");
			}
		});
	}

	function printData() {
        $.ajax({
            url: site_url + "coalgetting/coal_getting_inbound_stockroom/print_doc_pdf/",
            type: 'POST',
            dataType: 'html',
            data: { id : $('#coal_get_number').val() },
            success: function(result) {
            $('#print-content').html(result);
                    $('#print-content').printThis({
                        importCSS: false,
                        importStyle: false,
                        canvas: true,
                        copyTagClasses: false,    // copy classes from the html & body tag
                        loadCSS: ["<?=base_url()?>assets/css/cetak-packing.css"],
                    });    
            }
        });
    }

</script>
<script type="text/javascript" src="<?=base_url('assets/js/printThis.js')?>"></script>