var jQuery = jQuery || $.noConflict();
(function ($, undefined) {
	$(function () {
		var $frmCreate = $("#frmCreate"),
			validate = ($.fn.validate !== undefined),
			multilang = ($.fn.multilang !== undefined),
			datagrid = ($.fn.datagrid !== undefined);
		
		if (multilang && myLabel.isFlagReady == 1) {
			$(".multilang").multilang({
				langs: pjLocale.langs,
				flagPath: pjLocale.flagPath,
				tooltip: "",
				select: function (event, ui) {
					$("input[name='locale_id']").val(ui.index);					
				}
			});
		}
		
		if ($frmCreate.length > 0 && validate) {
			$frmCreate.validate({
				onkeyup: false,
				submitHandler: function (form) {
					var l = Ladda.create( $(form).find(":submit").get(0) );
					l.start();
					$.post("index.php?controller=pjAdminExtraHours&action=pjActionAddExtrahours", $(form).serialize()).done(function (data) {
						var content = $grid.datagrid("option", "content");
						$grid.datagrid("load", "index.php?controller=pjAdminExtraHours&action=pjActionGetExtrahours", content.column, content.direction, content.page, content.rowCount);
						$(form)[0].reset();
						l.stop();
					}).always(function () {
						l.stop();
					});
					return false;
				}
			});
		}

		if ($("#grid").length > 0 && datagrid) {
			console.log(myLabel);
			console.log(myLabel.hours, myLabel.status);

			var $grid = $("#grid").datagrid({
				
				buttons: [
					      { type: "edit", url: "index.php?controller=pjAdminExtraHours&action=pjActionUpdate&id={:id}"},
				          { type: "delete", url: "index.php?controller=pjAdminExtraHours&action=pjActionDelete&id={:id}"}
				          ],
		          columns: [
							{text: myLabel.hours, type: "text", sortable: true, editable: false},
							{text: myLabel.until_time, type: "text", sortable: true, editable: false},
							{text: myLabel.price, type: "text", sortable: true, editable: false},
				            {text: myLabel.status, type: "toggle", sortable: true, editable: pjGrid.hasAccessUpdate, positiveLabel: myLabel.active, positiveValue: "T", negativeLabel: myLabel.inactive, negativeValue: "F"}
				          ],
						  
				dataUrl: "index.php?controller=pjAdminExtraHours&action=pjActionGetExtrahours",
				dataType: "json",
				fields: ['hours', 'until_time', 'price', 'status'],
				paginator: {
					actions: [
					   {text: myLabel.delete_selected, url: "index.php?controller=pjAdminExtraHours&action=pjActionDeleteBulk", render: true, confirmation: myLabel.delete_confirmation},
					],
					gotoPage: true,
					paginate: true,
					total: true,
					rowCount: true
				},
				saveUrl: "index.php?controller=pjAdminExtraHours&action=pjActionSaveExtrahours&id={:id}",
				select: {
					field: "id",
					name: "record[]",
					cellClass: 'cell-width-2'
				}
			});
			
		}
		
		
		$(document).on("submit", ".frm-filter", function (e) {
			if (e && e.preventDefault) {
				e.preventDefault();
			}
			var $this = $(this),
				content = $grid.datagrid("option", "content"),
				cache = $grid.datagrid("option", "cache");
			$.extend(cache, {
				q: $this.find("input[name='q']").val()
			});
			$grid.datagrid("option", "cache", cache);
			$grid.datagrid("load", "index.php?controller=pjAdminExtraHours&action=pjActionGetExtrahours", content.column, content.direction, content.page, content.rowCount);
			return false;
		}).on("click", ".btn-all", function (e) {
			if (e && e.preventDefault) {
				e.preventDefault();
			}
			var $this = $(this),
				content = $grid.datagrid("option", "content"),
				cache = $grid.datagrid("option", "cache");
			$.extend(cache, {
				status: "",
				q: ""
			});
			$this.removeClass("btn-default").addClass("active").addClass("btn-primary").siblings(".btn").removeClass("active").removeClass("btn-primary").addClass("btn-default");
			$grid.datagrid("option", "cache", cache);
			$grid.datagrid("load", "index.php?controller=pjAdminExtraHours&action=pjActionGetExtrahours", content.column, content.direction, content.page, content.rowCount);
			return false;
		}).on("click", ".btn-filter", function (e) {
			if (e && e.preventDefault) {
				e.preventDefault();
			}
			var $this = $(this),
				content = $grid.datagrid("option", "content"),
				cache = $grid.datagrid("option", "cache"),
				obj = {};
			$this.removeClass("btn-default").addClass("active").addClass("btn-primary").siblings(".btn").removeClass("active").removeClass("btn-primary").addClass("btn-default");
			obj.status = "";
			obj[$this.data("column")] = $this.data("value");
			$.extend(cache, obj);
			$grid.datagrid("option", "cache", cache);
			$grid.datagrid("load", "index.php?controller=pjAdminExtraHours&action=pjActionGetExtrahours", content.column, content.direction, content.page, content.rowCount);
			return false;
		}).on("click", ".pj-table-icon-edit", function (e) {
			var $url = $(this).attr("href");
			$.get($url).done(function (data) {
				$(".boxFormSubservice").html(data);
				highlightLanguage();
				$("#frmUpdate").validate({
    				onkeyup: false,
    				submitHandler: function (form) {
    					var l = Ladda.create( $(form).find(":submit").get(0) );
    					l.start();
    					$.post("index.php?controller=pjAdminExtraHours&action=pjActionUpdate", $(form).serialize()).done(function (data) {
    						var content = $grid.datagrid("option", "content");
    						$grid.datagrid("load", "index.php?controller=pjAdminExtraHours&action=pjActionGetExtrahours", content.column, content.direction, content.page, content.rowCount);
    						if(pjGrid.hasAccessCreate == true)
    						{
    							$(".pjBtnCancelUpdateSubservice").trigger("click");
    						}else{
    							$(".boxFormSubservice").html("");
    						}
    						l.stop();
    					}).always(function () {
    						l.stop();
    					});
    					return false;
    				}
    			});
			});
			return false;
		}).on("click", ".pjBtnCancelUpdateSubservice", function (e) {
			if(pjGrid.hasAccessCreate == true)
			{
				var $url = 'index.php?controller=pjAdminExtraHours&action=pjActionCreate';
				$.get($url).done(function (data) {
					$(".boxFormSubservice").html(data);
					highlightLanguage();
					$("#frmCreate").validate({
						onkeyup: false,
						submitHandler: function (form) {
							var l = Ladda.create( $(form).find(":submit").get(0) );
							l.start();
							$.post("index.php?controller=pjAdminExtraHours&action=pjActionAddExtrahours", $(form).serialize()).done(function (data) {
								var content = $grid.datagrid("option", "content");
								$grid.datagrid("load", "index.php?controller=pjAdminExtraHours&action=pjActionGetExtrahours", content.column, content.direction, content.page, content.rowCount);
								$(form)[0].reset();
								l.stop();
							}).always(function () {
	    						l.stop();
	    					});
							return false;
						}
					});
				});
			}else{
				$(".boxFormSubservice").html("");
			}
			return false;
		});
		
		function highlightLanguage()
		{
			$(".pj-form-langbar-item").removeClass('btn-primary').addClass('btn-white');
			$(".pj-form-langbar-item").each(function( index ) {
				if($(this).attr('data-index') == myLabel.localeId)
				{
					$(this).addClass('btn-primary');
				}
			});
			$(".pj-multilang-wrap").each(function( index ) {
				if($(this).attr('data-index') == myLabel.localeId)
				{
					$(this).css('display','');
				}else{
					$(this).css('display','none');
				}
			});
		}
	});
})(jQuery);