var nadgodziny = (function($, undefined) {
	"use strict"

	var view = $('#NadgodzinyPracownikow'),
		archive = $('.archive'),
		realiz = $('.realiz'),
		rodzaj = $('.rodzaj-zapisu'),
		body = null; //body formularz


	var tabsInfo = {
		moje: ['moje', 'tab1'],
		pracownikow: ['pracownikow', 'tab2'],
		tab1: ['moje', 'tab1'],
		tab2: ['pracownikow', 'tab2'],
	}

	//=========================================
	//	START INIT LISTENER LISTA
	//=========================================

	//view.ready(function() {
		ajaxList(nadgodziny_tab_active.name, nadgodziny_tab_active.id, nadgodziny_tab_active.archive, nadgodziny_tab_active.nrewid);
		$('.tab-pane').removeClass('active');
		$('#' + nadgodziny_tab_active.id).addClass('active');

		var checkbox = archiveByTab(nadgodziny_tab_active.id);
		if (checkbox.length > 0 && nadgodziny_tab_active.archive == 1) {
			checkbox.prop('checked', true);
		}
	//});

	view.on('click', 'li', function() {
		var item = $(this),
			a = item.children('a').eq(0),
			filter = a.data('value');

		if (filter !== undefined) {

			nadgodziny_tab_active.name = tabsInfo[filter][0];
			nadgodziny_tab_active.id = tabsInfo[filter][1];

			var checkbox = archiveByTab(nadgodziny_tab_active.id);

			if (checkbox.is(':checked')) {
				nadgodziny_tab_active.archive = 1;
				ajaxList(nadgodziny_tab_active.name, nadgodziny_tab_active.id, 1, nadgodziny_tab_active.nrewid);
			} else {
				nadgodziny_tab_active.archive = 0;
				ajaxList(nadgodziny_tab_active.name, nadgodziny_tab_active.id, 0, nadgodziny_tab_active.nrewid);
			}

		}
	});

	archive.on('click', function(){
		var input = $(this).find('input');

		if (input.length > 0 && input.is(':checked')) {
			ajaxList(nadgodziny_tab_active.name, nadgodziny_tab_active.id, 1, nadgodziny_tab_active.nrewid);
		} else {
			ajaxList(nadgodziny_tab_active.name, nadgodziny_tab_active.id,0 , nadgodziny_tab_active.nrewid);
		}
	});

	realiz.on('click', function(){
		var input = archiveByTab(nadgodziny_tab_active.id)

		if (input.length > 0 && input.is(':checked')) {
			ajaxList(nadgodziny_tab_active.name, nadgodziny_tab_active.id, 1, nadgodziny_tab_active.nrewid);
		} else {
			ajaxList(nadgodziny_tab_active.name, nadgodziny_tab_active.id,0 , nadgodziny_tab_active.nrewid);
		}
	});

	rodzaj.on('click', function(){
		var input = archive.find('input');

		if (input.length > 0 && input.is(':checked')) {
			ajaxList(nadgodziny_tab_active.name, nadgodziny_tab_active.id, 1, nadgodziny_tab_active.nrewid);
		} else {
			ajaxList(nadgodziny_tab_active.name, nadgodziny_tab_active.id, 0, nadgodziny_tab_active.nrewid);
		}
	});

	view.on('click', '.nadgodziny-button-trigger', function(event){
		event.preventDefault();
		var elem = $(this);
		var url = elem.attr('href'),
			run = elem.data('run'),
			data_form = null;

		if (elem.hasClass('disabled')) {
			return;
		}

		var input = $('#' + nadgodziny_tab_active.id + ' .archive > label > input'),
			isArchive = 0;

		if (input.length > 0 && input.is(':checked')) {
			isArchive = 1;
		}

		nadgodziny_tab_active.archive = isArchive;

		if (run == 'filter') {
			var okres = $('#' + nadgodziny_tab_active.id + ' .nadgodziny_filtr'),
				cod = okres.length >= 1 ? $(okres[0]) : null,
				cdo = okres.length >= 2 ? $(okres[1]) : null;
			ajaxList(nadgodziny_tab_active.name, nadgodziny_tab_active.id, nadgodziny_tab_active.archive, nadgodziny_tab_active.nrewid,
				(cod != null ? cod.val() : ''), (cdo != null ? cdo.val() : ''));
			return;
		}

		if (run == 'add' || run == 'add-wybor') {
			url = url + "/" + nadgodziny_tab_active.nrewid;
		}

		if (run == 'remove') {
			if (confirm('Czy na pewno chcesz usunąć?')) {
				ajaxButton(url, run, nadgodziny_tab_active.name, isArchive);
			}
		} else if (run == 'save' || run == 'save-accept' || run == 'save-accept-all') {
			data_form = $('.form-nadgodziny').serializeArray();
			if(data_form.length > 1 && data_form.shift()) {
				if (run == 'save-accept') {
					data_form[data_form.length] = {
						name: 'data[accept]',
						value: 'true'
					}
				}
				if (run == 'save-accept-all') {
					data_form[data_form.length] = {
						name: 'data[accept-all]',
						value: 'true'
					}
				}

				$.ajax({
					url: url + '/test',
					method: 'POST',
					data: {
						formularz: data_form
					},
					success: function (data) {
						var response = JSON.parse(data);
						if(response.error != undefined) {
							$('#formularz_error').text(response.error).css('display', 'block');
						} else {
							$('#modal-nadgodziny').modal('hide');
							ajaxButton(url, run, nadgodziny_tab_active.name, isArchive, 'POST', data_form);
						}
					}
				});
			}
		} else if (run == 'add-wybor') {

			data_form = $('.form-nadgodziny').serializeArray();
			ajaxButton(url, run, nadgodziny_tab_active.name, isArchive, 'POST', data_form);

		} else if (run == 'off') {
			alert('funkcja w przygotowaniu');
		} else {
			ajaxButton(url, run, nadgodziny_tab_active.name, isArchive);
		}
	});

	//=========================================
	//	END INIT LISTENER LISTA
	//=========================================

	//=========================================
	//	START FUNCTIONS LISTA
	//=========================================

	function getRodzaj() {
		var check = 'Realizacja';

		var radioValue = $('input[name="RodzajeZapisuList_' + tabsInfo[nadgodziny_tab_active.id][1] + '"]:checked').val();
		if(radioValue){
			check = radioValue;
		}

		if(check == 'Realizacja') {
			realiz.css('display', 'none');
		} else {
			realiz.css('display', 'inline');
		}

		return check;
	}

	function archiveByTab(tab) {
		return $('#' + tab).find('.archive').find('input');
	}

	function realizByTab(tab) {
		return $('#' + tab).find('.realiz').find('input');
	}

	function ajaxList(filter, tab, archive, nrewid, okresOd, okresDo) {
		var loader = $('#' + tab + ' .custom-loader'),
			container = $('#' + tab + ' .box-body'),
			inputA = archiveByTab(tab),
		    inputR = realizByTab(tab),
			r = 0,
			a = 0;

		if (inputR.length > 0 && inputR.is(':checked')) {
			r = 1;
		}

		if (inputA.length > 0 && inputA.is(':checked')) {
			a = 1;
		}

		$.ajax({
			url: local_url + 'ajax/nadgodziny/lista_' + filter,
			method: 'GET',
			data: {
				archive: archive,
				tab: filter,
				nrewid: nrewid,
				okresOd: okresOd,
				okresDo: okresDo,
				rodzaj: getRodzaj(),
				realiz: r
			},
			beforeSend: function () {
				loader.css('display', 'block');
			},
			success: function (data) {
				if(trim(data) === 'login') {
					window.location = local_url + 'login';
				}
				container.html(data);
			},
			complete: function (xhr, status) {
				loader.css('display', 'none');
			}
		});
	}

	function ajaxButton(url, run, tab, archive, method, data) {
		var container = view;

		$.ajax({
			url: local_url + url,
			method: method !== undefined ? method : 'GET',
			data: {
				tab: tab,
				run: run,
				archive: archive,
				formularz: data
			},
			beforeSend: function () {
				showLoader('show');
			},
			success: function (data) {
				if(trim(data) === 'login') {
					window.location = local_url + 'login';
				}
				if ($.inArray(run,['add', 'edit', 'add-wybor', 'show']) !== -1) {
					var modal = $('#modal-nadgodziny'),
						body = modal.find('.modal-content');
					if (body.length > 0) {
						body.empty();
						body.html(data);
					}
					modal.modal('show');
				} else {
					var response = JSON.parse(data);
					if(response.error != undefined) {
						showError(response.error);
					} else {
						showSuccess(response.message);
					}
					ajaxList(nadgodziny_tab_active.name, nadgodziny_tab_active.id, archive, nadgodziny_tab_active.nrewid);
				}
			},
			complete: function (xhr, status) {
				showLoader('hide');
			}
		});
	}

	function showLoader(show) {
		var loader = $('#table-loading2[data-main-loader]');

		if (show === 'show') {
			loader
				.css('visibility', 'visible')
				.css('opacity', '1')
				.css('display', 'block');
		} else if(show === 'hide') {
			loader
				.css('visibility', 'hidden')
				.css('opacity', '0')
				.css('display', 'none');
		}
	}

	var app = {}

	return app;

})(jQuery);
