using System;
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_ShiftRoasterView : Base.BasePage
{
    #region Local Variables
    ResourceManager rm;
    //get the culture info to set the language
    CultureInfo ci;
    int ShiftRoasterId, Nodays;
    DataSet ds_Shiftpattern = new DataSet();
    DataSet ds_ShiftRoaster = new DataSet();
    ShiftRoasterEventData objShiftRoaster = new ShiftRoasterEventData();
    Cls_CheckUser objCheckUser = new Cls_CheckUser();
    DataSet ds_Checkuser = new DataSet();
    cls_Common objCommon = new cls_Common();
    string gstrUserGroupName = string.Empty, strScript = string.Empty;
    #endregion
    #region Page Load Event
    protected override void Page_Load(object sender, EventArgs e)
    {
        // session checking
        base.Page_Load(sender, e);
        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);
        gstrUserGroupName = Convert.ToString(Session["UserGroupName"]);
        // set user rights
        UserRights();
        loadcss();
        if (gstrUserGroupName == "SuperAdmin")
        {
            btn_Restore.Visible = true;
            btn_PermanentDelete.Visible = true;
        }
        else
        {
            btn_Restore.Visible = false;
            btn_PermanentDelete.Visible = false;
            lblDeleted.Visible = false;
            lblDeletedData.Visible = false;
        }
        if (!Page.IsPostBack)
        {
            btn_Delete.OnClientClick = "return confirm('" + rm.GetString("Alert_ConfirmDelete", ci) + "')";
            btn_Restore.OnClientClick = "return confirm('" + rm.GetString("Alert_Areyousuretorestore", ci) + "')";
            btn_PermanentDelete.OnClientClick = "return confirm('" + rm.GetString("Alert_AreyouwantDeleteRecordPermanently", ci) + "')";
            try
            {
                ShiftRoasterId = Convert.ToInt16(Request.QueryString["RoasterId"].ToString());
                Nodays = Convert.ToInt16(Request.QueryString["Nodays"].ToString());
                ViewState["ShiftRoasterId"] = ShiftRoasterId;
                ViewState["NoDays"] = Nodays;
                GenerateHtmlTable();
                FillValues(Nodays);
            }
            catch
            {
            }
            objCommon.menu("ShiftMaster", "69");
        }
    }
    #endregion
    #region Button Events
    protected void btn_Delete_Click(object sender, EventArgs e)
    {
        try
        {
            if (Convert.ToString(ViewState["ShiftRoasterId"]) != "0" && Convert.ToString(ViewState["ShiftRoasterId"]) != null)
            {
                objShiftRoaster.DeleteShiftRoaster(Convert.ToString(ViewState["ShiftRoasterId"]), false);
                string str = "alert('" + rm.GetString("Alert_Shiftrosterdeletedsuccessfully", ci) + "');location.replace('TA_UI_Master_ShiftRoaster.aspx');";
                Page.ClientScript.RegisterStartupScript(this.GetType(), "alertBox", str, true);
            }
            else
            {
                strScript = "<script language='javascript'>";
                strScript = strScript + "alert('" + rm.GetString("DeletionRecordAlert", ci) + "')";
                strScript = strScript + "</script>";
                Page.RegisterStartupScript("DelAlert1", strScript);
            }
        }
        catch (Exception Ex)
        {
            strScript = "<script language='javascript'>";
            strScript = strScript + "alert('" + Ex.Message + "')";
            strScript = strScript + "</script>";
            Page.RegisterStartupScript("Error", strScript);
        }
    }
    protected void btn_Restore_Click(object sender, EventArgs e)
    {
        try
        {
            if (Convert.ToString(ViewState["ShiftRoasterId"]) != "0" && Convert.ToString(ViewState["ShiftRoasterId"]) != null)
            {
                DataSet dst = objShiftRoaster.RestoreShiftRoaster(Convert.ToString(ViewState["ShiftRoasterId"]));
                string strMessage = "Alert_ShiftrosterRestoreFailed";
                if (dst.Tables.Count > 0)
                {
                    if (dst.Tables[0].Rows.Count > 0)
                    {
                        strMessage = Convert.ToString(dst.Tables[0].Rows[0][0]);
                        strMessage = strMessage.Contains("Failed") ? "Alert_ShiftrosterRestoreFailed" : strMessage.Contains("Exists") ? "Message_Cannot_Restore" : "Alert_ShiftrosterRestoresuccessfully";
                    }
                }
                string str = "alert('" + rm.GetString(strMessage, ci) + "');location.replace('TA_UI_Master_ShiftRoaster.aspx');";
                Page.ClientScript.RegisterStartupScript(this.GetType(), "alertBox", str, true);
            }
            else
            {
                strScript = "<script language='javascript'>";
                strScript = strScript + "alert('" + rm.GetString("DeletionRecordAlert", ci) + "')";
                strScript = strScript + "</script>";
                Page.RegisterStartupScript("DelAlert1", strScript);
            }
        }
        catch (Exception Ex)
        {
            strScript = "<script language='javascript'>";
            strScript = strScript + "alert('" + Ex.Message + "')";
            strScript = strScript + "</script>";
            Page.RegisterStartupScript("Error", strScript);
        }
    }
    protected void btn_PermanentDelete_Click(object sender, EventArgs e)
    {
        try
        {
            if (Convert.ToString(ViewState["ShiftRoasterId"]) != "0" && Convert.ToString(ViewState["ShiftRoasterId"]) != null)
            {
                objShiftRoaster.DeleteShiftRoaster(Convert.ToString(ViewState["ShiftRoasterId"]), true);
                string str = "alert('" + rm.GetString("Alert_Shiftrosterdeletedsuccessfully", ci) + "');location.replace('TA_UI_Master_ShiftRoaster.aspx');";
                Page.ClientScript.RegisterStartupScript(this.GetType(), "alertBox", str, true);
            }
            else
            {
                strScript = "<script language='javascript'>";
                strScript = strScript + "alert('" + rm.GetString("DeletionRecordAlert", ci) + "')";
                strScript = strScript + "</script>";
                Page.RegisterStartupScript("DelAlert1", strScript);
            }
        }
        catch (Exception Ex)
        {
            strScript = "<script language='javascript'>";
            strScript = strScript + "alert('" + Ex.Message + "')";
            strScript = strScript + "</script>";
            Page.RegisterStartupScript("Error", strScript);
        }
    }
    protected void btn_Modify_Click(object sender, EventArgs e)
    {
        Response.Redirect("TA_UI_Master_ShiftRoasterModify.aspx?Id=" + Convert.ToInt16(ViewState["ShiftRoasterId"]) + "&" + "Nodays=" + Convert.ToInt16(ViewState["NoDays"]) + "&" + "companyId=" + ViewState["companyId"]);
    }
    protected void btn_Back_Click(object sender, EventArgs e)
    {
        Response.Redirect("TA_UI_Master_ShiftRoaster.aspx");
    }
    #endregion
    #region User Define Function
    private void LoadStrings(CultureInfo ci)
    {
        lbl_OrganizationLevel.Text = rm.GetString("EntranceMaster_Company", 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);
        lblDeleted.Text = rm.GetString("DeleteStatus", ci);
        btn_Modify.Text = rm.GetString("btn_Modify", ci);
        btn_Delete.Text = "&nbsp;&nbsp;" + rm.GetString("btn_Delete", ci);
        btn_Restore.Text = "&nbsp;&nbsp;" + rm.GetString("btn_Restore", ci);
        btn_PermanentDelete.Text = "&nbsp;&nbsp;" + rm.GetString("btn_PermanentDelete", ci);
        btn_Back.Text = "&nbsp;&nbsp;" + rm.GetString("btn_Back", ci);
    }
    private void GenerateHtmlTable()
    {
        try
        {
            int numrows;
            int numcells;
            int i;
            int j;
            int count = 1;
            int cellcount;
            // Generate rows and cells
            numrows = Nodays;
            cellcount = Nodays;
            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", "50px");
            }
            else if (numrows == 3 || numrows == 4)
            {
                tbl_ShiftRoaster.Style.Add("height", "90px");
            }
            else if (numrows == 5 || numrows == 6)
            {
                tbl_ShiftRoaster.Style.Add("height", "130px");
            }
            else if (numrows == 7 || numrows == 8)
            {
                tbl_ShiftRoaster.Style.Add("height", "170px");
            }
            else if (numrows == 9 || numrows == 10)
            {
                tbl_ShiftRoaster.Style.Add("height", "210px");
            }
            if (numrows >= 1)
                if ((numrows % 1) == 0)
                    numrows = numrows / 1;
                else
                    numrows = numrows + 1;
            else
                numrows = 1;
            //Generate Rows and Cols based on the selected value
            for (j = 1; j <= numrows; j++)
            {
                TableRow r = new TableRow();
                for (i = 1; i <= numcells; i++)
                {
                    if (count <= cellcount)
                    {
                        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");
                        Label lbl_Value = new Label();
                        lbl_Value.ID = "lbl" + count;
                        lbl_Value.Text = count.ToString(); ;
                        lbl_Value.CssClass = "form-control";
                        count = count + 1;
                        div1.Controls.Add(lbl_Value);
                        tbl_ShiftRoaster.Controls.Add(div1);
                        tbl_ShiftRoaster.Controls.Add(new LiteralControl("<div class='col-md-1'></div>"));
                    }
                }
            }
        }
        catch
        {
        }
    }
    private void FillValues(int NoDays)
    {
        try
        {
            //Get ShiftPattern Id 
            ds_Shiftpattern = GetShiftPatternId();
            //Get ShiftRoaster Details
            ds_ShiftRoaster = GetshiftRoasterDeatails();
            //Display Shift Roaster Details
            lbl_OrgLevel.Text = Convert.ToString(ds_ShiftRoaster.Tables[0].Rows[0]["Name"]);
            ViewState["companyId"] = Convert.ToString(ds_ShiftRoaster.Tables[0].Rows[0]["Level_Id"]);
            lblRoasterName.Text = Convert.ToString(ds_ShiftRoaster.Tables[0].Rows[0]["ShiftRoaster_Name"]);
            lbl_Patterns.Text = Convert.ToString(ds_ShiftRoaster.Tables[0].Rows[0]["NumberOfPatterns"]);
            if (Convert.ToString(ds_ShiftRoaster.Tables[0].Rows[0]["IsDeleted"]) != string.Empty)
            {
                string strText = Convert.ToString(ds_ShiftRoaster.Tables[0].Rows[0]["IsDeleted"]).Contains("1") ? rm.GetString("Grid_True", ci) : rm.GetString("Grid_False", ci);
                lblDeletedData.Text = strText;
                btn_Delete.Visible = Convert.ToString(ds_ShiftRoaster.Tables[0].Rows[0]["IsDeleted"]).Contains("0");
                btn_Restore.Visible = Convert.ToString(ds_ShiftRoaster.Tables[0].Rows[0]["IsDeleted"]).Contains("1");
            }
            int i = 0; // for iterating the total selected days
            int j = 0; // for iterating the shift patterns
            for (i = 0; i < NoDays; i++) //iterating the total days
            {
                for (j = 0; j < ds_Shiftpattern.Tables[0].Rows.Count; j++) // iterating the total shifts
                {
                    string dayShiftName;
                    dayShiftName = "Pattern" + (i + 1);
                    if (Convert.ToString(ds_Shiftpattern.Tables[0].Rows[j]["ShiftPattern_Id"]) == Convert.ToString(ds_ShiftRoaster.Tables[0].Rows[0][dayShiftName]))
                    {
                        Label lblShiftPatternName = new Label();
                        string LabelId = "lbl" + (i + 1);
                        lblShiftPatternName = (Label)tbl_ShiftRoaster.FindControl(LabelId);
                        lblShiftPatternName.Text = Convert.ToString(ds_Shiftpattern.Tables[0].Rows[j]["Pattern_Name"]);
                    }
                }
            }
        }
        catch
        {
        }
    }
    public DataSet GetshiftRoasterDeatails()
    {
        DataSet ds = new DataSet();
        try
        {
            ds = objShiftRoaster.GetShiftRoasterDetailsById(ShiftRoasterId);
        }
        catch
        {
        }
        return ds;
    }
    public DataSet GetShiftPatternId()
    {
        DataSet ds_ShiftpatternId = new DataSet();
        try
        {
            ds_ShiftpatternId = objShiftRoaster.SelectShiftPattern(gstrUserGroupName, Convert.ToString(Session["LevelRights"]), Convert.ToInt16(Session["parentLevelId"]));
        }
        catch
        {
        }
        return ds_ShiftpatternId;
    }
    private void UserRights()
    {
        ds_Checkuser = objCheckUser.checkRights(69, Convert.ToInt32(Session["userGroup_Id"]), "");
        if (ds_Checkuser.Tables[0].Rows.Count > 0)
        {
            if (ds_Checkuser.Tables[0].Rows[0]["viewPrv"] == DBNull.Value || Convert.ToInt32(ds_Checkuser.Tables[0].Rows[0]["viewPrv"]) != 1)
            {
                Response.Redirect("TA_UI_Master_ShiftRoaster.aspx");
            }
            if (ds_Checkuser.Tables[0].Rows[0]["modifyPrv"] != DBNull.Value && Convert.ToInt32(ds_Checkuser.Tables[0].Rows[0]["modifyPrv"]) == 1)
            {
                this.btn_Modify.Visible = true;
            }
            else
            {
                this.btn_Modify.Visible = false;
            }
            if (ds_Checkuser.Tables[0].Rows[0]["deletePrv"] != DBNull.Value && Convert.ToInt32(ds_Checkuser.Tables[0].Rows[0]["deletePrv"]) == 1)
            {
                this.btn_Delete.Visible = true;
            }
            else
            {
                this.btn_Delete.Visible = false;
            }
        }
        else
        {
            Response.Redirect("MasterLandingPage.aspx");
        }
    }
    private void loadcss()
    {
        CultureInfo ci;
        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;
        //if Standard Demo
        htmlShiftRoaster.Attributes.Add("dir", rm.GetString("Direction", ci));
        if (source_Language == "Resources.Resource_AR")
        {
            stylebootstrap.Href = "../bootstrap/css/bootstrap-rtl.min.css";
            stylefontawesome.Href = "../bootstrap/font-awesome/css/font-awesome-rtl.css";
            styleAdminLTE.Href = "../dist/css/AdminLTE.min-rtl.css";
            stylecustom.Href = "../dist/css/custom-rtl.css";
            stylefonts.Href = "../dist/css/fonts.css";
        }
        else
        {
            //htmlShiftRoaster.Attributes.Add("dir", "rtl");
            //stylebootstrap.Href = "../bootstrap/css/bootstrap-rtl.min.css";
            //stylefontawesome.Href = "../bootstrap/font-awesome/css/font-awesome-rtl.css";
            //styleAdminLTE.Href = "../dist/css/AdminLTE.min-rtl.css";
            //stylecustom.Href = "../dist/css/custom-rtl.css";
            //stylefonts.Href = "../dist/css/fonts.css";
            stylebootstrap.Href = "../bootstrap/css/bootstrap.min.css";
            stylefontawesome.Href = "../bootstrap/font-awesome/css/font-awesome.css";
            styleAdminLTE.Href = "../dist/css/AdminLTE.min.css";
            stylecustom.Href = "../dist/css/custom.css";
            stylefonts.Href = "../dist/css/fonts.css";
        }
    }
    #endregion
}
