using System;
using System.Data;
using System.Web;
using System.Text;
using System.Web.UI.WebControls;
using ASPNET.StarterKit.TimeTracker.BusinessLogicLayer;

namespace ASPNET.StarterKit.TimeTracker.Web
{
	//*********************************************************************
	//
	// ProjectList.aspx
	//
	// Report launching page for the project and resource reports.
	//
	//*********************************************************************

	public class Reports : System.Web.UI.Page
	{
		protected System.Web.UI.WebControls.ListBox ProjectList;
		protected System.Web.UI.WebControls.ListBox UserList;
		protected System.Web.UI.WebControls.Button GenProjectRpt;
		protected System.Web.UI.WebControls.Button GenResourceRpt;
		protected System.Web.UI.WebControls.TextBox StartDate;
		protected System.Web.UI.WebControls.TextBox EndDate;
		protected System.Web.UI.WebControls.RequiredFieldValidator ProjectListRequiredFieldValidator;
		protected System.Web.UI.WebControls.RequiredFieldValidator UserListRequiredFieldValidator;

		private string _pageIndex = string.Empty;

		private void Page_Load(object sender, System.EventArgs e)
		{
			// Ensure that the visiting user has access to view the current page
			if (TTSecurity.IsInRole(TTUser.UserRoleAdminPMgr) == false) 
			{
				Response.Redirect("AccessDenied.aspx?Index=-1", true);
			}

			DateTime startingDate = DateTime.Today;
			DateTime endingDate = DateTime.Today;

			_pageIndex = Request.QueryString["Index"];

			if (!this.IsPostBack)
			{   
				ProjectList.DataSource = Project.GetProjects(TTSecurity.GetUserID(), TTSecurity.GetUserRole());
				ProjectList.DataTextField = "Name";
				ProjectList.DataValueField = "ProjectID";
				ProjectList.DataBind();

				UserList.DataSource = TTUser.GetUsers(TTSecurity.GetUserID(), TTSecurity.GetUserRole());
				UserList.DataTextField = "Name";
				UserList.DataValueField = "UserID";
				UserList.DataBind();

				BusinessLogicLayer.TimeEntry.FillCorrectStartEndDates(DateTime.Today, ref startingDate, ref endingDate);
				StartDate.Text = startingDate.ToShortDateString();
				EndDate.Text = endingDate.ToShortDateString();
			}
		}

		//*********************************************************************
		//
		// Converts values contained in the provided ListItemsCollection to a 
		// comma-delimited string. 
		//
		//*********************************************************************

		private string BuildValueList(ListItemCollection items)
		{
			// Build up list of IDs of selected users
			StringBuilder idList = new StringBuilder();
			foreach (ListItem item in items)
			{
				if (item.Selected)
				{
					if (idList.ToString() != string.Empty)
						idList.Append(",");
					idList.Append(item.Value.ToString());
				}
			}

			return idList.ToString();
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
			this.GenProjectRpt.Click += new System.EventHandler(this.GenProjectRpt_Click);
			this.GenResourceRpt.Click += new System.EventHandler(this.GenResourceRpt_Click);
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

		//*********************************************************************
		//
		// Launches the project report using the selected projects as input.
		//
		//*********************************************************************

		private void GenProjectRpt_Click(object sender, System.EventArgs e)
		{
			ProjectListRequiredFieldValidator.Validate();

			// Go to the project report page using the selected project IDs
			if (ProjectListRequiredFieldValidator.IsValid)
				Server.Transfer("ProjectReport.aspx?IDs=" + BuildValueList(ProjectList.Items) + 
					"&index=" + _pageIndex);
		}

		//*********************************************************************
		//
		// Launches the resource report using the selected consultants and date range as input.
		//
		//*********************************************************************

		private void GenResourceRpt_Click(object sender, System.EventArgs e)
		{
			UserListRequiredFieldValidator.Validate();

			// Go to the resource report page using the selected user IDs
			if (UserListRequiredFieldValidator.IsValid)
				Response.Redirect("ResourceReport.aspx?IDs=" + BuildValueList(UserList.Items) + 
					"&Start=" + StartDate.Text + "&End=" + EndDate.Text + "&index=" + _pageIndex, false);		
		}
	}
}
