using System;
using System.Collections;
using System.Data;
using System.Globalization;
using System.Resources;
using System.Threading;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
public partial class Masters_TA_UI_Master_ShiftRoasterAdd : Base.BasePage
{
    #region"Variable Declaration"
    ResourceManager rm;
    CultureInfo ci;
    ShiftRoasterEventData obj_ShiftRoaster = new ShiftRoasterEventData();
    DataSet ds_Shift = new DataSet();
    DataSet ds_CheckShiftname = new DataSet();
    int numrows, numcells, i, j, count = 1, intCompanyId;
    string strPatternIds = "";
    Hashtable hs = new Hashtable();
    Cls_CheckUser objCheckUser = new Cls_CheckUser();
    DataSet ds_Checkuser = new DataSet();
    ShiftEvenData objShift = new ShiftEvenData();
    cls_Common objCommon = new cls_Common();
    string gstrUserGroupName = string.Empty;
    #endregion
    #region PageLoad
    protected override void Page_Load(object sender, EventArgs e)
    {
        // session checking
        base.Page_Load(sender, e);
        //get the culture info to set the language            
        string source_Language;
        source_Language = Convert.ToString(Session["Session_ActiveLanguage"]);
        rm = new ResourceManager(source_Language, System.Reflection.Assembly.Load("App_GlobalResources"));
        ci = Thread.CurrentThread.CurrentCulture;
        LoadStrings(ci);
        site.SiteMapProvider = Convert.ToString(Session["SiteMapProvider"]);
        gstrUserGroupName = Convert.ToString(Session["UserGroupName"]);
        string strSetAlert = "";
        strSetAlert = strSetAlert + "<script language='javascript'>";
        strSetAlert = strSetAlert + "MaxLengthh('" + rm.GetString("Alert_MaximumLengthis", ci) + "');";
        strSetAlert = strSetAlert + "SetAlertMessages('" + rm.GetString("Alert_SelectShiftPattern", ci) + "','" + rm.GetString("Alert_ConfirmSave", ci) + "');";
        strSetAlert = strSetAlert + "</script>";
        Page.RegisterStartupScript("SetAlertMessages", strSetAlert);
        // set user rights
        UserRights();
        if (!IsPostBack)
        {
            ddlCompany.Focus();
            CompanyBinding();
            objCommon.menu("ShiftMaster", "69");
            //Alerts
        }
        else
        {
            GenerateHtmlTable();
        }
    }
    #endregion
    #region UserDefinedFunctions
    private void CompanyBinding()
    {
        DataSet dsCompany = new DataSet();
        dsCompany = objShift.SelectCompanyWithApprovalSettings(gstrUserGroupName, Convert.ToString(Session["LevelRights"]), 0, "R");
        if (dsCompany != null)
        {
            if (dsCompany.Tables[0].Rows.Count > 0)
            {
                ddlCompany.DataSource = dsCompany;
                ddlCompany.DataTextField = "CompanyName";
                ddlCompany.DataValueField = "CompanyId";
                ddlCompany.DataBind();
            }
        }
        foreach (ListItem item in ddlCompany.Items)
        {
            item.Attributes.Add("title", item.Text);
        }
    }
    //Getting Information from resource file and display it in label
    private void LoadStrings(CultureInfo ci)
    {
        lbl_Header.Text = rm.GetString("Masters_ShiftRoster", ci);
        lbl_RoasterName.Text = rm.GetString("ShiftRoster_Name", ci);
        lbl_Nopatterns.Text = rm.GetString("ShiftRoster_NofPatterns", ci);
        lbl_IndicatesMandatoryFields.Text = rm.GetString("IndicatesMandatoryFields", ci);
        RequiredFieldValidator1.ErrorMessage = rm.GetString("Req_ShiftRoasterName", ci);
        RequiredFieldValidator2.ErrorMessage = rm.GetString("Req_NumberofPatterns", ci);
        RequiredFieldValidator3.ErrorMessage = rm.GetString("Alert_Select_Company", ci);
        lblCompany.Text = rm.GetString("EntranceMaster_Company", ci);
        btn_save.Text = "&nbsp;&nbsp;" + rm.GetString("btn_Save", ci);
        btn_Back.Text = "&nbsp;&nbsp;" + rm.GetString("btn_Back", ci);
        ddlCompany.Items[0].Text = rm.GetString("Select", ci);
        ddl_Nopatterns.Items[0].Text = rm.GetString("Select", ci);
    }
    private void GenerateHtmlTable()
    {
        try
        {
            // Generate rows and cells
            if (ddl_Nopatterns.SelectedValue != "-Select-")
                numrows = Convert.ToInt16(ddl_Nopatterns.SelectedValue);
            numcells = 1;
            if (numrows != 0)
            {
                tbl_ShiftRoaster.Style.Add("border", "1px solid #c5dffc;");                
            }
            if (numrows == 0)
            {
                tbl_ShiftRoaster.Style.Add("height", "0px");
                tbl_ShiftRoaster.Style.Add("border", "none");
            }
            else if (numrows == 1 || numrows == 2)
            {
                tbl_ShiftRoaster.Style.Add("height", "80px");
            }
            else if (numrows == 3 || numrows == 4)
            {
                tbl_ShiftRoaster.Style.Add("height", "120px");
            }
            else if (numrows == 5 || numrows == 6)
            {
                tbl_ShiftRoaster.Style.Add("height", "160px");
            }
            else if (numrows == 7 || numrows == 8)
            {
                tbl_ShiftRoaster.Style.Add("height", "200px");
            }
            else if (numrows == 9 || numrows == 10)
            {
                tbl_ShiftRoaster.Style.Add("height", "210px");
            }
            else
            {
                tbl_ShiftRoaster.Style.Add("height", "207px");
                tbl_ShiftRoaster.Style.Add("border", "1px solid #c5dffc;");
            }
            if (numrows >= 1)
                if ((numrows % 1) == 0)
                    numrows = numrows / 1;
                else
                    numrows = numrows + 1;
            else
                numrows = 1;
            //Generate Rows and Columns based on the selected value
            if (ddl_Nopatterns.SelectedValue != "-Select-")
            {
                for (j = 1; j <= numrows; j++)
                {
                    //Create Dynamic Rows
                    TableRow r = new TableRow();
                    for (i = 1; i <= numcells; i++)
                    {
                        if (count <= Convert.ToInt16(ddl_Nopatterns.SelectedValue))
                        {
                            //Create Dynamic dropdowns
                            TableCell c = new TableCell();
                            HtmlGenericControl div = new HtmlGenericControl("div");
                            div.Attributes.Add("class", "col-md-2");
                            div.Style.Add("padding-top", "10px");
                            if (count < 10)
                            {
                                div.Controls.Add(new LiteralControl(rm.GetString("ShiftPattern", ci) + " " + count));
                            }
                            else
                            {
                                div.Controls.Add(new LiteralControl(rm.GetString("ShiftPattern", ci) + " " + count));
                            }
                            tbl_ShiftRoaster.Controls.Add(div);
                            HtmlGenericControl div1 = new HtmlGenericControl("div");
                            div1.Attributes.Add("class", "col-md-3");
                            div1.Style.Add("padding-top", "10px");
                            div1.Style.Add("padding-left", "14px");
                            div1.Style.Add("padding-right", "14px");
                            DropDownList ddl_SRoaster = new DropDownList();
                            ddl_SRoaster.CssClass = "form-control select2";
                            ddl_SRoaster.ID = "ddl_SRoaster" + count;
                            //Fiil dorpdown with Shiftpatttern Name
                            ddl_SRoaster.DataSource = FillDropDown();
                            ddl_SRoaster.Attributes.Add("onchange", "CheckShift(this," + count + "," + Convert.ToInt16(ddl_Nopatterns.SelectedValue) + ");");
                            ddl_SRoaster.DataValueField = "ShiftPattern_Id";
                            ddl_SRoaster.DataTextField = "Pattern_Name";
                            ddl_SRoaster.DataBind();
                            ListItem liSelect = new ListItem();
                            liSelect.Text = rm.GetString("Select", ci);
                            liSelect.Value = "-Select-";
                            ddl_SRoaster.Items.Insert(0, liSelect);
                            //ddl_SRoaster.Items.Insert(0, "-Select-");
                            //Set Cell width property
                            foreach (ListItem item in ddl_SRoaster.Items)
                            {
                                item.Attributes.Add("title", item.Text);
                            }
                            div1.Controls.Add(ddl_SRoaster);
                            tbl_ShiftRoaster.Controls.Add(div1);
                            tbl_ShiftRoaster.Controls.Add(new LiteralControl("<div class='col-md-1'></div>"));
                            count = count + 1;
                        }
                    }
                }
            }
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message.ToString());
        }
    }
    private DataSet FillDropDown()
    {
        //Fills the dropdowns with Shift Name        
        try
        {
            ds_Shift = obj_ShiftRoaster.SelectShiftPatternForShiftRoster(Convert.ToInt32(ddlCompany.SelectedValue));
        }
        catch
        {
            //
        }
        return ds_Shift;
    }
    private string GetShiftPatternIds()
    {
        string strPatternId = "";
        DropDownList ddl_list = new DropDownList();
        for (int i = 1; i <= Convert.ToInt16(ddl_Nopatterns.SelectedValue); i++)
        {
            ddl_list = (DropDownList)tbl_ShiftRoaster.FindControl("ddl_SRoaster" + i);
            if (strPatternId == null || strPatternId == "")
            {
                strPatternId = ddl_list.SelectedValue.ToString();
                if (strPatternId == "-Select-")
                {
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", "alert('" + rm.GetString("Alert_Selectappropriatecompanyandpattern", ci) + "');", true);
                    return string.Empty;
                }
            }
            else
            {
                strPatternId = strPatternId + "," + ddl_list.SelectedValue.ToString();
            }
        }
        return strPatternId;
    }
    private void UserRights()
    {
        ds_Checkuser = objCheckUser.checkRights(69, Convert.ToInt32(Session["userGroup_Id"]), "addPrv");
        if (ds_Checkuser.Tables[0].Rows.Count == 0)
        {
            Response.Redirect("MasterLandingPage.aspx");
        }
        else
        {
            //   
        }
    }
    private void DropdownTooltip(DropDownList ddl)
    {
        if (ddl != null)
        {
            foreach (ListItem li in ddl.Items)
            {
                li.Attributes["title"] = li.Text;
            }
        }
    }
    #endregion
    #region ButtonEvents
    protected void btn_save_Click(object sender, EventArgs e)
    {
        if (ddlCompany.SelectedValue != "0")
        {
            intCompanyId = Convert.ToInt32(ddlCompany.SelectedValue);
        }
        else
        {
            ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", "alert('" + rm.GetString("Alert_Select_Company", ci) + "');", true);
            return;
        }
        string[] strpatternId = new string[10] { "0", "0", "0", "0", "0", "0", "0", "0", "0", "0" };
        //int ddlcount;
        strPatternIds = GetShiftPatternIds();
        if (strPatternIds == string.Empty)
        {
            ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", "alert('" + rm.GetString("Req_NumberofPatterns", ci) + "');", true);
            return;
        }
        char[] delimiter = { ',' };
        //Spliting string using Delimiter 
        strpatternId = strPatternIds.Split(delimiter);
        for (int i = 0; i < 10; i++)
            // Adding ShiftpatternId in hashtable untill Number of patterns reaches the selected value else insert 0 into hash table untill 
            //value of variale i reaches 10 
            if (i <= Convert.ToInt16(ddl_Nopatterns.SelectedValue) - 1)
                hs.Add(i, strpatternId[i]);
            else
                hs.Add(i, "0");
        try
        {
            //Checking for duplicate value
            if (obj_ShiftRoaster.CheckShiftRoasterName(intCompanyId, txt_Roastername.Text.Trim().Replace("'", "''")) == 0)
            {
                obj_ShiftRoaster.InsertShiftRoaster(intCompanyId, txt_Roastername.Text.Trim().Replace("'", "''"), Convert.ToInt16(ddl_Nopatterns.SelectedValue), Convert.ToInt32(hs[0]), Convert.ToInt32(hs[1]), Convert.ToInt32(hs[2]),
                                                 Convert.ToInt32(hs[3]), Convert.ToInt32(hs[4]), Convert.ToInt32(hs[5]), Convert.ToInt32(hs[6]), Convert.ToInt32(hs[7]),
                                                 Convert.ToInt32(hs[8]), Convert.ToInt32(hs[9]), Session["EmpAutoID"].ToString());
                string strScript = "";
                strScript = strScript + "<script language='javascript'>";
                strScript = strScript + "alert('" + rm.GetString("Alert_Shiftrostersavedsuccessfully", ci) + "');";
                strScript = strScript + "location.replace('TA_UI_Master_ShiftRoaster.aspx');";
                strScript = strScript + "</script>";
                ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", strScript, false);
            }
            else
            {
                //Display Duplicate Error Message from Resource File
                ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", "alert('" + rm.GetString("Errormsg_Roster", ci) + "');", true);
            }
        }
        catch
        {
        }
    }
    protected void btn_Back_Click1(object sender, EventArgs e)
    {
        Response.Redirect("TA_UI_Master_ShiftRoaster.aspx");
    }
    protected void ddlCompany_DataBound(object sender, EventArgs e)
    {
        DropDownList ddl = sender as DropDownList;
        DropdownTooltip(ddl);
    }
    #endregion
}
