
$(document).ready(function() {
	
	$("#refresh-captcha-button").on("click", function(e) {
		e.preventDefault();
		
		var $this = $(this);
		$this.find(".fa").addClass("fa-spin");

		$.ajax({
			type: "POST",
			url: refreshCaptchaURL,
			dataType: "json",
			async: true,
			success: function(data) {
				if (data !== null && typeof data === "object") {
					$("#captcha-image").attr("src", data.captcha);
					$("#captcha").focus();
				}
				$this.find(".fa").removeClass("fa-spin");
			}
		});
	});
});

// Data Tables - Config
(function($) {

	'use strict';

	// we overwrite initialize of all datatables here
	// because we want to use select2, give search input a bootstrap look
	// keep in mind if you overwrite this fnInitComplete somewhere,
	// you should run the code inside this function to keep functionality.
	//
	// there's no better way to do this at this time :(
	if ( $.isFunction( $.fn[ 'dataTable' ] ) ) {

		$.extend(true, $.fn.dataTable.defaults, {
			sDom: "<'row datatables-header form-inline'<'col-sm-12 col-md-6'l><'col-sm-12 col-md-6'f>r><'table-responsive't><'row datatables-footer'<'col-sm-12 col-md-6'i><'col-sm-12 col-md-6'p>>",
			"pageLength": 25,
			language: {
				lengthMenu: '_MENU_ data per halaman',
				processing: '<i class="fa fa-spinner fa-spin"></i> Loading',
				zeroRecords: 'Tidak ada data yang sesuai dengan kriteria pencarian.',
				emptyTable: 'Tidak ada data dalam tabel.',
				info: "Menampilkan halaman _PAGE_ dari _PAGES_",
				infoEmpty: "Tidak ada data yang dapat ditampilkan.",
				infoFiltered: " - difilter dari _MAX_ data"
			},
			fnInitComplete: function( settings, json ) {
				// select 2
				if ( $.isFunction( $.fn[ 'select2' ] ) ) {
					$('.dataTables_length select', settings.nTableWrapper).select2({
						minimumResultsForSearch: -1
					});
				}

				var options = $( 'table', settings.nTableWrapper ).data( 'plugin-options' ) || {};

				// search
				var $search = $('.dataTables_filter input', settings.nTableWrapper);

				$search
					.attr({
						placeholder: typeof options.searchPlaceholder !== 'undefined' ? options.searchPlaceholder : 'Cari'
					})
					.addClass('form-control');

				if ( $.isFunction( $.fn.placeholder ) ) {
					$search.placeholder();
				}
			}
		});

	}

}).apply( this, [ jQuery ]);