<%@ Page Language="C#" MasterPageFile="~/Site.Master" %>
<%@ Import Namespace="Brevity" %>
<%@ Import Namespace="Castle.Core.Internal" %>
<%@ Import Namespace="eDocument.Core.Services" %>
<%@ Import Namespace="eDocument.Server.Administrator" %>

<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" Runat="Server">

<script runat="server">
	protected void Page_Load(object sender, System.EventArgs e)
	{
			if (!Authorizor.IsAuthorized())
			{
				return;
			}

		if(!IsPostBack)	
			this.BindProcess();
	}

	private void BindProcess()
	{
			var service = eDocument.Server.Administrator.ServiceFactory.GetService();
			var ds = service.GetFlowProcessesByConsumerID(SessionData.ConsumerID);
			ddlFlowProcess.DataSource = ds.FlowProcess.Select(null, "Name ASC");
			ddlFlowProcess.DataBind();
			ddlFlowProcess.Items.Insert(0, new ListItem("---", "---"));
	}

	protected void btnExecute_Click(object sender, EventArgs e)
	{
		var service = eDocument.Server.Administrator.ServiceFactory.GetService();

		var instanceIDs = Session["eDocumentInstanceIDs"] as string;

		if(string.IsNullOrEmpty(instanceIDs))
			return;

		var instanceID = instanceIDs.Split(new[] {';'}, StringSplitOptions.RemoveEmptyEntries);
		var instanceGuids = instanceID.ConvertAll(input => new Guid(input));

		var response = service.SetWorkflowStatusForInstances(SessionData.CurrentConsumer.Username, SessionData.CurrentConsumer.Password, instanceGuids, true, new Guid(ddlFlowStatus.SelectedValue), txtComment.Text.Trim());

		lblResponse.Text = response.ToJson();
	}

		protected void ddlFlowProcess_SelectedIndexChanged(object sender, EventArgs e)
		{
			if(ddlFlowProcess.SelectedIndex == 0)
			{
				ddlFlowStatus.Items.Clear();
				return;
			}

			var service = eDocument.Server.Administrator.ServiceFactory.GetService();
			var ds = service.GetFlowStatusesByFlowProcessID(new Guid(ddlFlowProcess.SelectedValue));
			ddlFlowStatus.DataSource = ds.FlowStatusView.Select(null, "Name ASC");
			ddlFlowStatus.DataBind();
			ddlFlowStatus.Items.Insert(0, new ListItem("---", "---"));
		}
</script>


			<div>
				<table>
					<tr>
						<td>
							Arbeidsflyt *
						</td>
						<td>
							<asp:DropDownList AutoPostBack="true" runat="server" ID="ddlFlowProcess" DataValueField="FlowProcessID" DataTextField="Name" OnSelectedIndexChanged="ddlFlowProcess_SelectedIndexChanged"/>
						</td>
						<td>
							<asp:RequiredFieldValidator runat="server" ControlToValidate="ddlFlowProcess" ForeColor="Red" InitialValue="---" Text="Angi prosess" />
						</td>
					</tr>
					<tr>
						<td>
							Status *
						</td>
						<td>
							<asp:DropDownList runat="server" ID="ddlFlowStatus" DataTextField="Name" DataValueField="FlowStatusID"/>
						</td>
						<td>
							<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="ddlFlowStatus" ForeColor="Red" InitialValue="---" Text="Angi status" />
						</td>
					</tr>
					<tr>
						<td>
							Kommentar (brukes på FlowHistory)
						</td>
						<td>
							<asp:TextBox runat="server" ID="txtComment" TextMode="MultiLine" Columns="30" Rows="2"></asp:TextBox>
						</td>
					</tr>
					<tr>
						<td colspan="2" align="right">
							<asp:Button runat="server" ID="btnExecute" Text="Kjør!" OnClick="btnExecute_Click"/>
						</td>
					</tr>
				</table>
				<asp:Label runat="server" ID="lblResponse" />
			</div>
</asp:Content>