using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace MSE
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!Page.IsPostBack)
            {
                FetchUserRole();
            }
        }
        private void FetchUserRole()
        {
            try
            {
                System.Security.Principal.IPrincipal myPrincipal = this.Context.User;
                txtUser.Value = myPrincipal.Identity.Name;

                //txtUser.Text = HttpContext.Current.User.Identity.Name.ToString();
                if (Request.Form["User"] != "")
                {
                    MSE_LIB.MSE lcMSE = new MSE_LIB.MSE();
                    List<MSE_LIB.MSE.UserRole> ldRtn = new List<MSE_LIB.MSE.UserRole>();
                    ldRtn = lcMSE.SelectUserRole(Request.Form["User"].ToString());

                    if (ldRtn != null && ldRtn.Count > 0)
                    {
                        foreach (MSE_LIB.MSE.UserRole ldRcd in ldRtn)
                        {
                            txtRole.Value = ldRcd.Role;
                        }
                    }
                }
            }
            catch(Exception ex)
            {
                string msg = ex.Message;
            }
        }
        protected void btnSubmit_ServerClick(object sender, EventArgs e)
        {
            int RunRate = 0;
            int PerformLevel1 = 0, PerformLevel2 = 0;
            string AssemblyLine = Request.Form["AssemblyLine"];
            string MfgGate = Request.Form["MfgGate"];
            RunRate = int.Parse(Request.Form["RunRate"]);
            DateTime StartDate = Convert.ToDateTime(Request.Form["StartDate"]);
            DateTime EndDate = Convert.ToDateTime(Request.Form["EndDate"]);
            PerformLevel1 = int.Parse(Request.Form["Green"]);
            PerformLevel2 = int.Parse(Request.Form["Yellow"]);
            MSE_LIB.MSE lcMSE = new MSE_LIB.MSE();
            MSE_LIB.MSE.MSEConfigurationSPResultRecord ldRtn = new MSE_LIB.MSE.MSEConfigurationSPResultRecord();
            ldRtn = lcMSE.InsertMSEConfigurationRecord(AssemblyLine,MfgGate,RunRate, StartDate, EndDate,PerformLevel1,PerformLevel2);
            if (ldRtn.ROW_COUNT > 0)
            {
                Response.Write("<script>alert('Record saved successfully.');</script>");
            }
            else
            {
                Response.Write("<script>alert('Error occured.');</script>");
            }
        }

        protected void btnSearch_ServerClick(object sender, EventArgs e)
        {
            try
            {
                string AssemblyLine = Request.Form["ScheduleAssemblyLine"].ToString();
                string MfgGate = Request.Form["ScheduleMfgGate"].ToString();
                string Report = Request.Form["Report"].ToString();
                MSE_LIB.MSE lcMSE = new MSE_LIB.MSE();
                List<MSE_LIB.MSE.ScheduleDetails> ldRtn = new List<MSE_LIB.MSE.ScheduleDetails>();
                ldRtn = lcMSE.SelectScheduleDetails(AssemblyLine, MfgGate, Report);
                
                //if (ldRtn != null && ldRtn.Count > 0)
                //{
                //    hdfieldTotalRecord.Value = ldRtn.Count.ToString();
                //    grdMSM.PageSize = Pagesize;
                //    grdMSM.DataSource = ldRtn;
                //    grdMSM.DataBind();
                //}
                //else if (ldRtn.Count == 0)
                //{
                //    grdMSM.PageSize = Pagesize;
                //    grdMSM.DataSource = ldRtn;
                //    grdMSM.DataBind();
                //}
                //else
                //{

                //}
            }
            catch(Exception ex)
            {
                string msg = ex.Message;
            }
        }
    }
}