<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">Stock Room ID</label>
                                        <div class=" input-group col-sm-3">
                                            <input type="text" class="form-control" id="stockroom_id" name="t_stockroom_id" value="<?=$stockroom_data['stockroom_id']?>" placeholder="" >
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <label class="col-sm-2">Warehouse ID</label>
                                        <div class=" input-group col-sm-4">
                                            <input type="text" class="form-control" id="warehouse_id" name="t_warehouse_id" value="<?=$stockroom_data['warehouse_id']?>" >
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <label class="col-sm-2">Vendor Master Card</label>
                                        <div class=" input-group col-sm-4">
                                            <input type="text" class="form-control" id="vendor_id" name="t_vendor_id" value="<?=$stockroom_data['vendor_id']?>" 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">
                                        <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="<?=$vendorname?>" readonly>
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <label class="col-sm-2">Mine Ratio</label>
                                        <div class=" input-group col-sm-3">
                                            <input type="text" class="form-control" style="text-align:right;" id="stockroom_mineratio" name="t_stockroom_mineratio" value="<?=$stockroom_data['stockroom_mineratio']?>" >
                                        </div>
                                    </div>
                                    <div class="form-group">
										<label class="col-sm-2">Type</label>
										<div class="col-sm-3">
											<div class="i-checks"><label> <input <?=($stockroom_data['origin_type']==='STOCKROOM')?'checked=""':''?>   type="radio" value="STOCKROOM" name="t_origin_type"> <i></i> STOCKR 1</label></div>
											<div class="i-checks"><label> <input <?=($stockroom_data['origin_type']==='STOCKROOM2')?'checked=""':''?>   type="radio" value="STOCKROOM2" name="t_origin_type"> <i></i> STOCKR 2</label></div>
											<div class="i-checks"><label> <input <?=($stockroom_data['origin_type']==='PIT')?'checked=""':''?> type="radio" value="PIT" name="t_origin_type"> <i></i> PIT</label></div>
										</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/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);
                                // 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 + "stockroom" ;
                    clearData();
                }
            });            
		});

	});

    function clearData(){
        $('#stockroom_id').val('');
        $('#vendor_id').val('');
		$('#vendor_name').val('');
        $('#warehouse_id').val('');
        $('#stockroom_mineratio').val('0');
		$("#vendor_name").hide();
    }

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

</script>