<%@ Page Language="C#" AutoEventWireup="true" %>
<%@ Import Namespace="eDocument.Server.Administrator" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
	protected void Page_Load(object sender, EventArgs e)
	{
		if(!Authorizor.IsAuthorized()) 
			Response.Redirect("~/Forbidden.aspx");

		var service = ServiceFactory.GetService();
		var ds = service.GetAllDevices();

		dgDevices.DataSource = ds.Device.Select(null, "Name ASC"); ;
		dgDevices.DataBind();
	}

	private void dgDevices_ItemCommand(object source, DataGridCommandEventArgs e)
	{
		if(e.CommandName == "Delete")
		{
					var service = ServiceFactory.GetService();
					var ds = service.GetAllDevices();
					ds.Device.FindByDeviceId(new Guid(e.Item.Cells[0].Text)).Delete();
					service.Update(ds);
					Page_Load(null, null);
					Response.Redirect("List.aspx");	
		}
		else
		{
			Response.Redirect("Update.aspx?DeviceId=" + e.Item.Cells[0].Text);
		}
	}
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
	<head runat="server">
		<title>Title</title>
		<LINK rel="stylesheet" type="text/css" href="../css/FrameWork.css">
		<LINK rel="stylesheet" type="text/css" href="../css/Main.css">
	</head>
	<body>
		<form id="HtmlForm" runat="server">
			<div>
							<h1>Oversikt over enheter</h1>

<asp:DataGrid runat="server" ID="xxx"></asp:DataGrid>

			<asp:datagrid id="dgDevices" runat="server" AutoGenerateColumns="False" Width="100%" HeaderStyle-CssClass="HeaderStyle"
				ItemStyle-CssClass="ItemStyle" AlternatingItemStyle-CssClass="AlternatingItemStyle" AllowSorting="False" Height="98px" OnItemCommand="dgDevices_ItemCommand">
				<AlternatingItemStyle CssClass="AlternatingItemStyle"></AlternatingItemStyle>
				<ItemStyle CssClass="ItemStyle"></ItemStyle>
				<HeaderStyle CssClass="HeaderStyle"></HeaderStyle>
				<Columns>
					<asp:BoundColumn Visible="True" DataField="DeviceID" HeaderText="Id"></asp:BoundColumn>
					<asp:BoundColumn DataField="Name" SortExpression="Name ASC" HeaderText="Navn"></asp:BoundColumn>
					<asp:BoundColumn DataField="Description" SortExpression="Description ASC" HeaderText="Beskrivelse"></asp:BoundColumn>
					<asp:ButtonColumn Text="Rediger" CommandName="Edit"></asp:ButtonColumn>
					<asp:ButtonColumn Text="Slett" CommandName="Delete"></asp:ButtonColumn>
				</Columns>
			</asp:datagrid>
			</div>
		</form>
	</body>
</html>
