<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">
                                        <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">Vendor</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">
                                        <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">
                                        <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="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">
                                            <input type="text" class="form-control" id="coal_get_time" name="t_coal_get_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 printBtn" type="submit"><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> Close</button>
								</div>
							</div>
						</div>
					</div>
					
				</div>
			</div>
		</div>
	</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, ""))) )
		});

        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/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);
                                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: "Apakah input data akan dibatalkan ?",
				text: "",
				icon: "warning",
				showCancelButton: true,
				confirmButtonText: 'Ya',
                cancelButtonText:'Tidak',
			}).then(isConfirm => {
                if (isConfirm.value) {
                    clearData();
                }
            });            
		});

	});

    function getDataOutbound() {
		$.ajax({
			type: "POST",
			url: site_url + "coalgetting/coal_getting_outbound/getDataCoalGettingOutbound",
			data: {
				vehicle_id: $('#vehicle_id').val()
			},
			dataType: 'json',
			success: function (response) {
				$('#vehicle_number').val(response.coalgetout.vehicle_number);
				$('#vendor_id').val(response.coalgetout.vehicle_vendorid);
				$('#tare_weight').val(response.coalgetout.vehicle_tare_weight);
				$('#coal_get_date').val(format_date(response.tgl));
				$('#coal_get_time').val(response.jam);
				$('#id_rec').val(response.coalgetout.id_rec);
			},
			error: function (jqXhr, textStatus, errorThrown) {
				var error_message = 'Data Tidak Ditemukan';
				if (jqXhr.responseJSON !== undefined) {
					error_message = jqXhr['responseJSON']['message'];
				}
                swal.fire('warning', error_message, "warning");
			}
		});
	}

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

		$("#net_weight").val(number_format(nett,2,'.',','));
	}

    function clearData(){
        $('#coal_get_number').val('');
        $('#vehicle_id').val('');
        $('#vendor_id').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('');
    }

    function getDataInbound() {
		$.ajax({
			type: "POST",
			url: site_url + "coalgetting/coal_getting_inbound/getDataCoalGettingInbound",
			data: {
				coal_get_num: $('#coal_get_number').val()
			},
			dataType: 'json',
			success: function (response) {
				$('#vehicle_id').val(response.coalgetin.vehicle_id);
				$('#vendor_id').val(response.coalgetin.vendor_id);
				$('#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));
				$('#coal_get_time').val(response.coalgetin.coal_get_time);
				$('#id_rec').val(response.coalgetin.id_rec);
			},
			error: function (jqXhr, textStatus, errorThrown) {
				var error_message = 'Data Tidak Ditemukan';
				if (jqXhr.responseJSON !== undefined) {
					error_message = jqXhr['responseJSON']['message'];
				}
                swal.fire('warning', error_message, "warning");
			}
		});
	}

</script>