function PreviewImage() {
	var oFReader = new FileReader();
	oFReader.readAsDataURL(document.getElementById("image").files[0]);

	oFReader.onload = function (oFREvent) {
		document.getElementById("uploadPreview").src = oFREvent.target.result;
	};
	$('#editPicForm').submit();
};


function loadData() {
	var oTable = $('#warehouse').DataTable( {
	"serverSide": true,
	  "ajax": {
		"url": "/company/warehouse/list",
		"type": "POST",
	  },
	  "fnDrawCallback": function() {
		$(".make-switch").bootstrapSwitch();
		$('.make-switch').on('switchChange.bootstrapSwitch', function (event, state) {
			var id = $(this).data('id');
			var dataStringg = 'id='+id+'&state='+state;
			$.ajax({
                type: "POST",
                url: "/company/warehouse/status-edit",				
                cache: false,
                data: dataStringg
			});
					
		});
		},
	"language": {
                "aria": {
                    "sortAscending": ": activate to sort column ascending",
                    "sortDescending": ": activate to sort column descending"
                },
                "emptyTable": "No data available in table",
                "info": "Showing _START_ to _END_ of _TOTAL_ entries",
                "infoEmpty": "No entries found",
                "infoFiltered": "(filtered1 from _MAX_ total entries)",
                "lengthMenu": "_MENU_ entries",
                "search": "Search:",
                "zeroRecords": "No matching records found"
            },
	buttons: [
		{ extend: 'print', className: 'btn dark btn-outline' },
		{ extend: 'excel', className: 'btn yellow btn-outline ' },
		{ extend: 'colvis', className: 'btn dark btn-outline', text: 'Columns'}
	],
	"lengthMenu": [ [10, 25, 50,100,99999999], [10, 25, 50,100, "All"] ],
	"order": [
		[ 0, 'asc' ]
	],
	
	});
	$('.search-input-text').on( 'keyup click change', function () {   // for text boxes
		var i =$(this).attr('data-column');  // getting column index
		var v =$(this).val();  // getting search input value
		oTable.columns(i).search(v).draw();
	} );
	$('.search-input-select').on( 'change', function () {   // for select box
		var i =$(this).attr('data-column');  
		var v =$(this).val();  
		oTable.columns(i).search(v).draw();
	} );
	$('.filter-cancel').on( 'click', function () {   // for select box
		$('.search-input-text').val('');
		$('.search-input-select').val('');
		$('.search-input-text').trigger('change');
		$('.search-input-select').trigger('change');
		
		oTable.draw();
	} );
	$("#warehouse_filter").css("display","none"); 
	$('#sample_3_tools > li > a.tool-action').on('click', function() {
		var action = $(this).attr('data-action');
		oTable.DataTable().button(action).trigger();
	});
	

}
$(document).ready(function() {
    loadData(1);
	$(document).on("click",".edit",function() {
			var id = $(this).data('id');
			var dataStringg = 'id='+id;
			$.ajax({
				type: "POST",
				url: "/company/warehouse/get-data",				
				cache: false,
				data: dataStringg,
				success: function(data) {
					$("#getCode").html(data);
					jQuery("#form_edit").modal('show');
					
					$(function(){
						$("#branch_edit").not("[style*='display:none']").select2({
							allowClear: true,
							placeholder: "Select your warehouse branch",
							width: null
						});
					});
					
				}
			});
		});
	$(document).on("click",".delete",function() {
			var id = $(this).data('id');
			var dataStringg = 'id='+id;
			$.ajax({
				type: "POST",
				url: "/company/warehouse/delete",				
				cache: false,
				data: dataStringg,
				success: function(data) {
					location.reload();
				}
			});
		});

});
var Script = function () {
	$().ready(function() {
        
		 $.fn.select2.defaults.set("theme", "bootstrap");


        $("#branch").not("[style*='display:none']").select2({
            allowClear: true,
            placeholder: "Select your warehouse branch",
            width: null
        });

        $("button[data-select2-open]").click(function() {
            $("#" + $(this).data("select2-open")).select2("open");
        });

        $(":checkbox").on("click", function() {
            $(this).parent().nextAll("select").prop("disabled", !this.checked);
        });

        // copy Bootstrap validation states to Select2 dropdown
        //
        // add .has-waring, .has-error, .has-succes to the Select2 dropdown
        // (was #select2-drop in Select2 v3.x, in Select2 v4 can be selected via
        // body > .select2-container) if _any_ of the opened Select2's parents
        // has one of these forementioned classes (YUCK! ;-))
        $(".select2, .select2-multiple, .select2-allow-clear, .js-data-example-ajax").on("select2:open", function() {
            if ($(this).parents("[class*='has-']").length) {
                var classNames = $(this).parents("[class*='has-']")[0].className.split(/\s+/);

                for (var i = 0; i < classNames.length; ++i) {
                    if (classNames[i].match("has-")) {
                        $("body > .select2-container").addClass(classNames[i]);
                    }
                }
            }
        });

        $(".js-btn-set-scaling-classes").on("click", function() {
            $("#select2-multiple-input-sm, #select2-single-input-sm").next(".select2-container--bootstrap").addClass("input-sm");
            $("#select2-multiple-input-lg, #select2-single-input-lg").next(".select2-container--bootstrap").addClass("input-lg");
            $(this).removeClass("btn-primary btn-outline").prop("disabled", true);
        });
		
		$('#form_edit').on('hidden.bs.modal', function (e) {
			$("#getCode").html("");
		})
    });
    $().ready(function() {
		
        // validate the comment form when it is submitted
          $('#addwarehouse').validate({
			  submitHandler: function() { 
				var name                = $("#name").val();
				var address             = $("#address").val();
				var city                = $("#city").val();
				var zip                 = $("#zip").val();
				var state               = $("#state").val();
				var contact_person      = $("#contact_person").val();
				var contact_person_no   = $("#contact_person_no").val();
				var country             = $("#country").val();
				var branch              = $("#branch").val();
				var dataString          = 'name='+name+
								          '&address='+address+
								          '&city='+city+
								          '&zip='+zip+
								          '&state='+state+
								          '&contact_person='+contact_person+
								          '&contact_person_no='+contact_person_no+
								          '&branch='+branch+
								          '&country='+country;
			   
				 $.ajax({
					type: "POST",
					url: "/company/warehouse/add",
					data: dataString,
					cache: false,
					beforeSend: function(){ 
						$("#submitpin").hide();
						$("#waitingpin").show();
						$("#suksesupdate").hide();
						$("#gagalupdate").hide();
					},
					complete: function(e, xhr, settings){
						if(e.status === 200){
							$("#submitpin").show();
							$("#waitingpin").hide();
							$("#suksesupdate").show();
							location.reload();
							//$("#gagalupdate").hide();
						}else{
							$("#submitpin").show();
							$("#waitingpin").hide();
							//$("#suksesupdate").show();
							$("#gagalupdate").show();
						}
					}
					
				 });
			  }
			
			});
			
			$('#editwarehouse').validate({
			  submitHandler: function() { 
				var id_edit				   = $("#id_edit").val();
				var name_edit			   = $("#name_edit").val();
				var address_edit     	   = $("#address_edit").val();
				var city_edit              = $("#city_edit").val();
				var zip_edit               = $("#zip_edit").val();
				var state_edit			   = $("#state_edit").val();
				var country_edit  		   = $("#country_edit").val();
				var contact_person_edit    = $("#contact_person_edit").val();
				var contact_person_no_edit = $("#contact_person_no_edit").val();
				var notice_edit  		   = $("#notice_edit").val();
				var branch_edit            = $("#branch_edit").val();
				var dataString             = 'id='+id_edit+
								             '&name='+name_edit+
								             '&address='+address_edit+
								             '&city='+city_edit+
								             '&zip='+zip_edit+
								             '&state='+state_edit+
								             '&notice='+notice_edit+
								             '&contact_person='+contact_person_edit+
								             '&contact_person_no='+contact_person_no_edit+
								             '&branch='+branch_edit+
								             '&country='+country_edit;
			   
				 $.ajax({
					type: "POST",
					url: "/company/warehouse/edit",
					data: dataString,
					cache: false,
					beforeSend: function(){ 
						$("#submitpin").hide();
						$("#waitingpin").show();
						$("#suksesupdate").hide();
						$("#gagalupdate").hide();
					},
					complete: function(e, xhr, settings){
						if(e.status === 200){
							$("#submitpin").show();
							$("#waitingpin").hide();
							$("#suksesupdate").show();
							location.reload();
							//$("#gagalupdate").hide();
						}else{
							$("#submitpin").show();
							$("#waitingpin").hide();
							//$("#suksesupdate").show();
							$("#gagalupdate").show();
						}
					}
					
				  });
				}
			
			});
			
			
    });


	$().ready(function() {
        // validate the comment form when it is submitted
        $("#editForm").validate();
		if(window.location.search == '?success'){
			$("#suksesupdate").show();
		}else if(window.location.search == '?failed'){
			$("#gagalupdate").show();
		}
		setTimeout(
		  function() 
		  {
			$("#suksesupdate").hide();
			$("#gagalupdate").hide();
		  }, 2000);
    });
}();