#set ($layout = "grid.vm" )
#set ($module = "packagelists" )
#set ($title = "Paketlisten")
#set ($disablenew=false)
#if($params.profile)
#set($viaprofile=true)
#set($profile=$profiles)
#set($r=$profile.load($params.profile))
#else
#set($viaprofile=false)
#end
#if ($viaprofile)
#set ($title = "$title für Profil $params.profile")
#set($disablenew=true)
#set($disablebuttons=true)
#end
#if($request.isUserInRole("packageapprover"))
#set($disableapproval=false)
#else
#set($disableapproval=true)
#end
#if($request.isUserInRole("packageadmin"))
#set($disablebuttons=false)
#else
#set($disablebuttons=true)
#set($disablenew=true)
#end
	<SCRIPT type="text/javascript">

	function post_error_handler(jqXHR, textStatus, errorThrown) {
		alert("AJAX POST Error:"+errorThrown);
	}

	function DiffCountFormatter(row, cell, value, columnDef, dataContext) {
		if (value == null || value === "0" || value==0) {
			return "&nbsp;-&nbsp";
    		} else {
      			return "<span style='color:white;background:#e05050;border-radius:8px'>&nbsp;" + value + "&nbsp;</span>";
		}
	}

	function BulletFormatter(row,cell,value,columnDef,dataContext) {
		return (value) ?  '<span class="ui-icon ui-icon-bullet" style="display:inline-block"></span>' : "";
	}

	function CheckmarkFormatter(row,cell,value,columnDef,dataContext) {
		return (value) ?  '<span class="ui-icon ui-icon-check" style="display:inline-block"></span>' : "";
	}

	function updateFilter(search) {
		dataView.setFilterArgs({
			searchString: search });
		dataView.refresh();
	}

	function myFilter(item, args) {
		if (args.searchString != "" && item["name"].indexOf(args.searchString) == -1) {
			return false;
		}
		return true;
	}


	var grid;
  	var columns = [
		{id: "name", name: "Name", field: "name", cssClass: "mycell"},
		{id: "autoUpdate", name: "Auto-Update", field: "autoUpdate", cssClass: "mycell", formatter: BulletFormatter },
		{id: "priority", name: "Priorit&auml;t", field: "priority", cssClass: "mycell" },
		{id: "approved", name: "Freigegeben", field: "approved", cssClass: "mycell", formatter: CheckmarkFormatter},
		{id: "origin", name: "Vorg&auml;nger", field: "origin", cssClass: "mycell" },
		{id: "diffcount", name: "Updates", field: "diffcount", cssClass: "mycell", formatter: DiffCountFormatter}, 
	];

	$(document).ready(function () {

#js_create_grid("columns")

		grid.onSelectedRowsChanged.subscribe(function() { 
#if(!$disablebuttons)
			$("#button-edit").button("option","disabled",false);
			$("#button-rename").button("option","disabled",false);
			$("#button-delete").button("option","disabled",false);
#end
			$("#button-view").button("option","disabled",false);
	   		row_ids = grid.getSelectedRows();
			var o = dataView.getItem(row_ids[0]);
			// console.log(o.approved+" "+o.approvalRequired);
#if(!$disableapproval)
			$("#button-approve").button("option","disabled",(o.approved==true || o.approvalRequired==false));
#end
#if(!$viaprofile)
			// determine if origin package list has been approved
			var origin=null;
			if(o.origin!=null) {
				origin=dataView.getItemById(o.origin);
			}

			$("#button-update").button("option","disabled",o.origin==null || (origin!=null && origin.approvalRequired==true && origin.approved==false));
#end
 	  		// console.log(row_ids);
		});
		grid.onDblClick.subscribe(function() { $("#button-view").click(); });
#js_dialog_form("new" "Create" "ui-icon-plus" $disablenew false)
#js_dialog_form("edit" "Save" "ui-icon-pencil" true false)
#js_dialog_form("rename" "Rename" "ui-icon-tag" true false)
#js_dialog_form("delete" "Delete" "ui-icon-minus" true false)

#if($viaprofile)
#if($profile.checkApprovals())
#set($disable=false)
#else
#set($disable=true)
#end
#else
#set($disable=true)
#end
#js_dialog_simple("approve","Freigeben", "ui-icon-check" true)
#js_dialog_simple("update","Aktualisieren", "ui-icon-arrowreturnthick-1-n" $disable)

		$("#button-view").button({disabled: true, icons:{ primary: "ui-icon-zoomin"}})
			.click(function() {
				var row=grid.getSelectedRows()[0];
				var item=dataView.getItem(row);
				var name=item.name;
				var updates=item.diffcount;
				var newLocation="$root/packages/index.vm?list="+name;
				if(updates>0) newLocation += "&show=updates";
				window.location=newLocation;
				});
#js_searchbox()
#if($viaprofile)
#js_load_data_key("packagelists","name","profile=$params.profile")
#else
#js_load_data("packagelists","name")
#end
	});

	</SCRIPT>
<DIV class="ui-layout-center">
</DIV>
<DIV class="ui-layout-north">#parse("menu.vm")#searchbox()</DIV>
<DIV class="ui-layout-south">
<div class="buttons">
<button id="button-new">Neu</button>
<button id="button-edit">Bearbeiten</button>
<button id="button-rename">Umbenennen</button>
<button id="button-delete">L&ouml;schen</button>
<span>&nbsp;</span>
<button id="button-approve">Freigeben</button>
<button id="button-view">Pakete anzeigen</button>
#if($viaprofile)
<button id="button-update">Alles aktualisieren</button>
#else
<button id="button-update">Pakete aktualisieren</button>
#end
</div>
</DIV>
#parse("$module/new.inc")
#parse("$module/edit.inc")
#parse("$module/rename.inc")
#parse("$module/delete.inc")
#parse("$module/approve.inc")
#parse("$module/update.inc")

