<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">Vendor Card</label>
                                        <div class=" input-group col-sm-4">
                                            <input type="text" class="form-control" id="vendor_id" name="t_vendor_id" value="<?=$vendor_data['vendor_id']?>" >
                                            <span class="help-block m-b-none"><i style="color:red;">Diisi dengan UID dari Vendor Card</i></span> 
                                        </div>
                                    </div>									
                                    <div class="form-group">
                                        <label class="col-sm-2">Vendor Master Card</label>
                                        <div class=" input-group col-sm-3">
                                            <input type="text" class="form-control" id="vendor_mis" name="t_vendor_mis" value="<?=$vendor_data['vendor_mis']?>" placeholder="" >
											<span class="help-block m-b-none"><i style="color:red;">Diisi sama dengan Kode Vendor di MIS</i></span> 
                                        </div>
                                    </div>
									<div class="form-group">
                                        <label class="col-sm-2">Vendor Name</label>
                                        <div class=" input-group col-sm-4">
                                            <input type="text" class="form-control" id="vendor_name" name="t_vendor_name" value="<?=$vendor_data['vendor_name']?>">
                                        </div>
                                    </div>
									<div class="form-group">
                                        <label class="col-sm-2">Ongkos dari Pit</label>
                                        <div class=" input-group col-sm-2">
                                            <input type="text" class="form-control" style="text-align:right;" id="pit_cost" name="t_pit_cost" value="<?=$vendor_data['pit_cost']?>" >
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <label class="col-sm-2">Ongkos dari Stock Room</label>
                                        <div class=" input-group col-sm-2">
                                            <input type="text" class="form-control" style="text-align:right;" id="stockr_cost" name="t_stockr_cost" value="<?=$vendor_data['stockr_cost']?>" >
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <label class="col-sm-2">Ongkos dari Stock Room 2</label>
                                        <div class=" input-group col-sm-2">
                                            <input type="text" class="form-control" style="text-align:right;" id="stockr_cost2" name="t_stockr_cost2" value="<?=$vendor_data['stockr_cost2']?>" >
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <label class="col-sm-2">Denda Selisih Berat</label>
                                        <div class=" input-group col-sm-2">
                                            <input type="text" class="form-control" style="text-align:right;" id="biaya_selisih" name="t_biaya_selisih" value="<?=$vendor_data['biaya_selisih']?>">
                                        </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-4"></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>

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

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

        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 + "master/vendor/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: "Anda yakin akan keluar ?",
				text: "",
				icon: "warning",
				showCancelButton: true,
				confirmButtonText: 'Ya',
                cancelButtonText:'Tidak',
			}).then(isConfirm => {
                if (isConfirm.value) {
                    window.location = site_url + "vendor" ;
                    clearData();
                }
            });            
		});

	});

    function clearData(){
        $('#vendor_id').val('');
		$('#vendor_mis').val('');
		$('#vendor_name').val('');
        $('#pit_cost').val('0');
        $('#stockr_cost').val('0');
        $('#stockr_cost2').val('0');
        $('#biaya_selisih').val('0');
    }

    function getDataVendor() {
		$.ajax({
			type: "POST",
			url: site_url + "master/vendor/get_vendor",
			data: {
				vendor_id: $('#vendor_id').val()
			},
			dataType: 'json',
			success: function (response) {
				$('#vendor_mis').val(response.vendor_mis);
				$('#vendor_name').val(response.vendor_name);
				$('#pit_cost').val(response.pit_cost);
				$('#stockr_cost').val(response.stockr_cost);
				$('#stockr_cost2').val(response.stockr_cost2);
				$('#biaya_selisih').val(response.biaya_selisih);
			},
			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>