#set ($layout = "grid.vm" )
#set ($title = "Pakete $params.list")
#set ($module = "packages")
#set($disablebuttons=false)
#if($params.show=='updates')
#set($showupdates=true)
#set($disablebuttons=true)
#else
#set($showupdates=false)
#end
#if($request.isUserInRole("packageadmin"))
#set($isAdmin=true)
#else
#set($disablebuttons=true)
#end
#set($r=$packagelists.load($params.list))
#set($disable=$packagelists.origin == 0 || !$packagelists.checkApproval())
	<!-- isAdmin $isAdmin showUpdates $showupdates -->
	<SCRIPT type="text/javascript">

	var grid;
	var dataView;
  	var columns = [
		{id: "name", name: "Name", field: "name", cssClass: "mycell"},
		{id: "version", name: "Version", field: "version", cssClass: "mycell" },
/*		{id: "description", name: "Beschreibung", field: "description", cssClass: "mycell" },*/
		{id: "arch", name: "Architektur", field: "arch", cssClass: "mycell" },
		{id: "install", name: "Installieren", field: "install", cssClass: "mycell", formatter: Slick.Formatters.Checkmark },
	];

	function enable_buttons(row) {
		var o=dataView.getItem(row);
		if(!o) return;
#if(!$disablebuttons)
		$('#button-install').button("option","disabled",o.install);
		$('#button-uninstall').button("option","disabled",!o.install);
		$('#button-edit').button("option","disabled",false);
		$('#button-delete').button("option","disabled",false);
#end
		$('#button-update').button("option","disabled",$disable);
		$('#button-updateall').button("option","disabled",$disable);
	};


	function radiobutton_handler() {
		var inst=$('#showinst:checked').val();
		var uninst=$('#showuninst:checked').val();
		var updates=$('#showupdates:checked').val();
		if(inst) show="";
		if(uninst) show="&show=uninst";
		if(updates) show="&show=updates";
		window.location="$root/packages/index.vm?list=${params.list}"+show;	
	}

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

	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;
	}

	$(document).ready(function () {

#js_create_grid("columns")

		grid.onSelectedRowsChanged.subscribe(function() {
	   		row_ids = grid.getSelectedRows();
			i=row_ids[0];
			o=dataView.getItem(i);
			if(!o) return;
			enable_buttons(i);
 	  		console.log(row_ids);
		});
		grid.onDblClick.subscribe(function() { $("#button-edit").click(); });

                $('#button-install').button({disabled: true, icons: { primary: "ui-icon-play" }})
                        .click(function() {
	   			row_ids = grid.getSelectedRows();
				i=row_ids[0];
				o=dataView.getItem(i);
				o.install=true;
				dataView.refresh();	
				grid.updateRow(i);
				enable_buttons(i);
				$.post("$root/packages/install-action.vm?p="+o.id)
					.error(post_error_handler);
			});
                $('#button-uninstall').button({disabled: true, icons: { primary: "ui-icon-eject" }})
                        .click(function() {
	   			row_ids = grid.getSelectedRows();
				i=row_ids[0];
				o=dataView.getItem(i);
				o.install=false;
				dataView.refresh();	
				grid.updateRow(i);
				enable_buttons(i);
				$.post("$root/packages/uninstall-action.vm?p="+o.id)
					.error(post_error_handler);
			});

		$('#showuninst').click(radiobutton_handler);
		$('#showinst').click(radiobutton_handler);
		$('#showupdates').click(radiobutton_handler);
#js_dialog_form("new" "Paket anlegen" "ui-icon-plus" $disablebuttons false)
#js_dialog_form("edit" "Speichern" "ui-icon-pencil" true false)
#js_dialog_form("delete" "Entfernen" "ui-icon-minus" true false)
#js_dialog_simple("update","Aktualisieren","ui-icon-arrowreturnthick-1-n" true)
#js_dialog_simple("updateall","Alles aktualisieren","ui-icon-arrowreturnthick-1-n" $disable)
#if($params.show=="updates")
#set ($showinst="&updates=true")
#elseif($params.show=="uninst")
#set($showinst="")
#else
#set($showinst="&installed=true")
#end
#js_searchbox()
#js_load_data_key("packages","id","list=${params.list}$showinst")
		
	});

	</SCRIPT>
<DIV class="ui-layout-center">
</DIV>
<DIV class="ui-layout-north">#parse("menu.vm")&nbsp;
<span class="menu">Anzeigen:</span>
<span class="menu"><input class="menu" type="radio" name="showgroup" value="showinst" #if(!$params.show) checked #end id="showinst">installiert</span>
<span class="menu"><input class="menu" type="radio" name="showgroup" value="showuninst" #if($params.show=="uninst") checked #end id="showuninst">Nicht installiert</span>
<span class="menu"><input class="menu" type="radio" name="showgroup" value="showupdates" #if($params.show=="updates") checked #end id="showupdates">Updates</span>
#searchbox()
<div id="ajaxBusy" class="ajaxBusy"><img class="ajaxBusy" src="$root/images/loading.gif"></div>
</DIV>
<DIV class="ui-layout-south">
<div class="buttons">
<button id="button-new">Neu</button>
<button id="button-edit">Bearbeiten</button>
<button id="button-delete">Entfernen</button>
<span>&nbsp;</span>
<button id="button-install">Installieren</button>
<button id="button-uninstall">Deinstallieren</button>
<button id="button-update">Aktualisieren</button>
<span>&nbsp;</span>
<button id="button-updateall">Alles aktualisieren</button>
</div>
</DIV>
#parse("$module/new.inc")
#parse("$module/edit.inc")
#parse("$module/delete.inc")
#parse("$module/update.inc")
#parse("$module/updateall.inc")

