<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="hauling_number" name="hauling_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">
                                        <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="" onkeydown="if(event.keyCode==13)getDataOutbound();">
                                            <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">Current Vendor</label>
                                        <div class=" input-group col-sm-4">
                                            <input type="text" class="form-control" id="current_vendor_id" name="t_current_vendor_id" value="" readonly>
                                        </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="current_vendor_name" name="t_current_vendor_name" value="" readonly>
                                        </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="" onkeydown="if(event.keyCode==13)getDataVendor();">
											<span class="help-block m-b-none"><i style="color:red;">* tekan enter setelah mengisi</i></span> 
                                        </div>
                                    </div>
									<div class="form-group"hidden>
                                        <label class="col-sm-2">Vendor Master Card</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">Location ID</label>
                                        <div class=" input-group col-sm-3">
											<input type="text" class="form-control" id="location_id" name="t_location_id" value="" >
                                        </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="" readonly>
                                        </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">
                                        <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=" input-group col-sm-2">
                                            <input type="text" class="form-control" id="hauling_date" name="hauling_date" value="" readonly>
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <label class="col-sm-2">Jam</label>
                                        <div class=" input-group col-sm-2">
                                            <input type="text" class="form-control" id="hauling_time" name="hauling_time" value="" readonly>
                                        </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, ""))) )
		});

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

        saveBtn.click(function() {
			var current_vendor = $('#current_vendor_id').val();
			var card_vendor = $('#vendor_mis').val();

			if(current_vendor != card_vendor){
				swal.fire('Proses Simpan gagal', 'Current Vendor dan Vendor Card Berbeda !', "error"); 
			} else {
				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 + "hauling/hauling_inbound/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);
									$('#hauling_number').val(response['trxnumber']);
									$('#hauling_date').val(response['trxdate']);
									$('#hauling_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'];
									document.getElementById("printBtn").disabled = true;
								}
								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 autoFillWeight() {
			$.ajax({
				type: "POST",
				url: site_url + "hauling/hauling_inbound/getfromWeight",
				data: {},
				dataType: 'json',
				success: function (response) {
					$('#gross_weight').val(response['weight']);
					netweight( parseFloat(eval($("#gross_weight").val().replace(/,/g, ""))), parseFloat(eval($("#tare_weight").val().replace(/,/g, ""))) );
				},
				error: function (jqXhr, textStatus, errorThrown) {}
			});
		}

		// Calls the autoRefresh function every 5000 milliseconds (5 seconds)
		setInterval(autoFillWeight, 5000);

	});

    function getDataOutbound() {
		$.ajax({
			type: "POST",
			url: site_url + "hauling/hauling_outbound/getDataHaulingOutbound",
			data: {
				vehicle_id: $('#vehicle_id').val()
			},
			dataType: 'json',
			success: function (response) {
				$('#vehicle_number').val(response.haulingout.vehicle_number);
				$('#current_vendor_id').val(response.haulingout.vehicle_vendorid);
				$('#current_vendor_name').val('   '+response.vendor_name);
				$('#tare_weight').val(response.haulingout.vehicle_tare_weight);
				$('#hauling_date').val(format_date(response.tgl));
				$('#hauling_time').val(response.jam);
				$('#id_rec').val(response.haulingout.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'];
					// $("#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);
		$("#net_weight").val(number_format(nett,2));
	}

    function clearData(){
        $('#hauling_number').val('');
        $('#vehicle_id').val('');
        $('#current_vendor_id').val('');
        $('#current_vendor_name').val('');
        $('#vendor_id').val('');
        $('#vendor_name').val('');
        $('#gross_weight').val('0');
        $('#tare_weight').val('0');
        $('#net_weight').val('0');
        $('#vehicle_number').val('');
        $('#hauling_date').val('');
        $('#hauling_time').val('');
        $('#id_rec').val('');
        $('#location_id').val('');
		document.getElementById("printBtn").disabled = true;
		// $("#vendor_name").hide();
    }

    function getDataInbound() {
		$.ajax({
			type: "POST",
			url: site_url + "hauling/hauling_inbound/getDataHaulingInbound",
			data: {
				hauling_num: $('#hauling_number').val()
			},
			dataType: 'json',
			success: function (response) {
				$('#vehicle_id').val(response.haulingin.vehicle_id);
				$('#current_vendor_id').val(response.haulingin.vendor_id);
				$('#current_vendor_name').val('   '+response.haulingin.vendor_name);
				$('#vehicle_number').val(response.haulingin.vehicle_number);
				$('#gross_weight').val(response.haulingin.gross_weight);
				$('#tare_weight').val(response.haulingin.tare_weight);
				$('#net_weight').val(response.haulingin.net_weight);
				$('#hauling_date').val(format_date(response.haulingin.hauling_date));
				$('#hauling_time').val(response.haulingin.hauling_time);
				$('#id_rec').val(response.haulingin.id_rec);
				$('#location_id').val(response.haulingin.location_id);
				// $("#vendor_name").show();
			},
			error: function (jqXhr, textStatus, errorThrown) {
				var error_message = 'Data Tidak Ditemukan';
				if (jqXhr.responseJSON !== undefined) {
					error_message = jqXhr['responseJSON']['message'];
					$('#vehicle_id').val('');
					$('#current_vendor_id').val('');
					$('#current_vendor_name').val('');
					$('#vendor_id').val('');
					$('#vendor_name').val('');
					$('#gross_weight').val('0');
					$('#tare_weight').val('0');
					$('#net_weight').val('0');
					$('#vehicle_number').val('');
					$('#hauling_date').val('');
					$('#hauling_time').val('');
					$('#id_rec').val('');
					$('#location_id').val('');
					document.getElementById("printBtn").disabled = true;
					// $("#vendor_name").hide();
				}
                swal.fire('warning', error_message, "warning");
			}
		});
	}

	function printData() {
        $.ajax({
            url: site_url + "hauling/hauling_inbound/print_doc_pdf/",
            type: 'POST',
            dataType: 'html',
            data: { id : $('#hauling_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"],
                    });    
            }
        });
    }

	function getDataVendor() {
		$.ajax({
			type: "POST",
			url: site_url + "master/vendor/get_vendor",
			data: {
				vendor_id: $('#vendor_id').val()
			},
			dataType: 'json',
			success: function (response) {
				$('#vendor_name').val('   '+response.vendor_name);
				$('#vendor_mis').val(response.vendor_mis);
				netweight( parseFloat(eval($("#gross_weight").val().replace(/,/g, ""))), parseFloat(eval($("#tare_weight").val().replace(/,/g, ""))) );
				$("#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");
			}
		});
	}

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