using System;
using System.Data;
using System.Globalization;
using System.Resources;
using System.Threading;
using System.Web.UI;
using System.Web.UI.WebControls;
using DevExpress.Web;
using System.Web.UI.HtmlControls;
public partial class Masters_TA_UI_Master_ShiftRoaster : Base.BasePage
{
    #region VariableDeclaration
    ShiftRoasterEventData obj_ShiftRoaster = new ShiftRoasterEventData();
    ResourceManager rm;
    CultureInfo ci;
    Cls_CheckUser objCheckUser = new Cls_CheckUser();
    DataSet ds_Checkuser = new DataSet();
    cls_Common objCommon = new cls_Common();
    DataSet ds_Roster = new DataSet();
    int ShiftRoasterId;
    string strScript = string.Empty, source_Language = string.Empty, gstrUserGroupName = string.Empty;
    #endregion
    #region PageLoad
    protected override void Page_Load(object sender, EventArgs e)
    {
        // session checking
        base.Page_Load(sender, e);
        grv_ShiftRoaster.Styles.CssFilePath = Convert.ToString(Session["GridCSS"]);
        // get the culture info to set the language
        if (!Page.IsPostBack)
        {
            base.CheckPages(0);
            objCommon.menu("ShiftMaster", "69");
        }
        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;
        site.SiteMapProvider = Convert.ToString(Session["SiteMapProvider"]);
        LoadStrings(ci);
        string strSetAlert = "";
        strSetAlert = strSetAlert + "<script language='javascript'>";
        strSetAlert = strSetAlert + "RestoreAlert('" + rm.GetString("Alert_Norecordsfound", ci) + "','" + rm.GetString("Alert_Areyousuretorestore", ci) + "','" + rm.GetString("Alert_Selecteddataisnotindeletedstatus", ci) + "','" + rm.GetString("Alert_Selectdatatorestore", ci) + "','" + rm.GetString("Alert_Nodeletedrecordsavailable", ci) + "');";
        strSetAlert = strSetAlert + "</script>";
        Page.RegisterStartupScript("RestoreAlert", strSetAlert);
        //Check UserRights
        UserRights();
        btn_Add.Focus();
        gstrUserGroupName = Convert.ToString(Session["UserGroupName"]);
        if (gstrUserGroupName == "SuperAdmin")
        {
            btn_Restore.Visible = false;            
            //grv_ShiftRoaster.Columns[5].Visible = true;
        }
        else
        {
            btn_Restore.Visible = false;            
            //grv_ShiftRoaster.Columns[5].Visible = false;
        }
    }
    #endregion
    #region ButtonEvents
    protected void btn_Add_Click(object sender, EventArgs e)
    {
        Response.Redirect("TA_UI_Master_ShiftRoasterAdd.aspx");
    }
    protected void btn_Delete_Click(object sender, EventArgs e)
    {
        try
        {
            if (hdnSelectRow.Value != string.Empty && hdnSelectRow.Value != null)
            {
                obj_ShiftRoaster.DeleteShiftRoaster(hdnSelectRow.Value, 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 (hdnSelectRow.Value != string.Empty && hdnSelectRow.Value != null)
            {
                DataSet dst = obj_ShiftRoaster.RestoreShiftRoaster(hdnSelectRow.Value);
                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 (hdnSelectRow.Value != string.Empty && hdnSelectRow.Value != null)
            {
                obj_ShiftRoaster.DeleteShiftRoaster(hdnSelectRow.Value, 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);
        }
    }
    #endregion
    #region UserDefinedFunctions
    //Getting Information from resource file and display it in label
    private void LoadStrings(CultureInfo ci)
    {
        lblHeader_ShiftRoster.Text = rm.GetString("Masters_ShiftRoster", ci);
        btn_Add.Text = "&nbsp;&nbsp;" + rm.GetString("btn_Add", ci);
        btn_Delete.Text = "&nbsp;&nbsp;" + rm.GetString("btn_Delete", ci);
        btn_PermanentDelete.Text = "&nbsp;&nbsp;" + rm.GetString("btn_Delete", ci);
        btn_Restore.Text = "&nbsp;&nbsp;" + rm.GetString("btn_Restore", ci);
        grv_ShiftRoaster.Columns[2].Caption = rm.GetString("ShiftRoster_Name", ci);
        grv_ShiftRoaster.Columns[3].Caption = rm.GetString("ShiftRoster_NofPatterns", ci);
        grv_ShiftRoaster.Columns[4].Caption = rm.GetString("EntranceMaster_Company", ci);
        //grv_ShiftRoaster.Columns[5].Caption = rm.GetString("DeleteStatus", ci);
        lblDataView.Text = rm.GetString("lblDataView", ci);
    }
    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]["addPrv"] != DBNull.Value && Convert.ToInt32(ds_Checkuser.Tables[0].Rows[0]["addPrv"]) == 1)
            {
                this.btn_Add.Visible = true;
            }
            else
            {
                this.btn_Add.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 = false;
                //this.btn_PermanentDelete.Visible = false;
                this.btn_Restore.Visible = false;
                this.grv_ShiftRoaster.Columns[0].Visible = true;
                this.grv_ShiftRoaster.Columns[7].Visible = true;
            }
            else
            {
                this.btn_Delete.Visible = false;
                //this.btn_PermanentDelete.Visible = false;
                this.btn_Restore.Visible = false;
                this.grv_ShiftRoaster.Columns[0].Visible = false;
                this.grv_ShiftRoaster.Columns[7].Visible = false;
            }
            if (ds_Checkuser.Tables[0].Rows[0]["modifyPrv"] != DBNull.Value && Convert.ToInt32(ds_Checkuser.Tables[0].Rows[0]["modifyPrv"]) == 1)
            {
                this.grv_ShiftRoaster.Columns[6].Visible = true;
            }
            else
            {
                this.grv_ShiftRoaster.Columns[6].Visible = false;
            }
            if (ds_Checkuser.Tables[0].Rows[0]["viewPrv"] != DBNull.Value && Convert.ToInt32(ds_Checkuser.Tables[0].Rows[0]["viewPrv"]) == 1)
            {
                this.grv_ShiftRoaster.Columns[5].Visible = true;
            }
            else
            {
                this.grv_ShiftRoaster.Columns[5].Visible = false;
            }
        }
        else
        {
            Response.Redirect("MasterLandingPage.aspx");
        }
    }
    #endregion
    #region GridviewEvents
    protected void grv_ShiftRoaster_HtmlRowCreated(object sender, DevExpress.Web.ASPxGridViewTableRowEventArgs e)
    {
        if (e.RowType == GridViewRowType.Data)
        {
            source_Language = Convert.ToString(Session["Session_ActiveLanguage"]);
            rm = new ResourceManager(source_Language, System.Reflection.Assembly.Load("App_GlobalResources"));
            ci = Thread.CurrentThread.CurrentCulture;
            Label DeletedRecord = (Label)grv_ShiftRoaster.FindRowCellTemplateControl(e.VisibleIndex, null, "lblIsDeleted");
            HtmlInputCheckBox deleteChkBxItem = (HtmlInputCheckBox)grv_ShiftRoaster.FindRowCellTemplateControl(e.VisibleIndex, null, "chkSelect");
            LinkButton LnkbtnDelete = (LinkButton)grv_ShiftRoaster.FindRowCellTemplateControl(e.VisibleIndex, null, "btnDelete");
            if (deleteChkBxItem != null)
            {
                if (deleteChkBxItem.Disabled == false)
                {
                    LnkbtnDelete.Enabled = true;
                }
                else
                {
                    LnkbtnDelete.Enabled = false;
                    LnkbtnDelete.OnClientClick = null;
                }
            }
            if (DeletedRecord != null)
            {
                if (DeletedRecord.Text == "True" || DeletedRecord.Text == rm.GetString("Grid_True", ci))
                {
                    DeletedRecord.ForeColor = System.Drawing.Color.Red;
                    DeletedRecord.Text = rm.GetString("Grid_True", ci);
                    if (hdnDeletedData.Value == string.Empty)
                    {
                        hdnDeletedData.Value = Convert.ToString(e.GetValue("ShiftRoaster_Id"));
                    }
                    else
                    {
                        hdnDeletedData.Value = hdnDeletedData.Value + "," + Convert.ToString(e.GetValue("ShiftRoaster_Id"));
                    }
                }
                else if (DeletedRecord.Text == "False")
                {
                    DeletedRecord.Text = rm.GetString("Grid_False", ci);
                }
            }
        }
    }
    #endregion
    protected void EditLinkButton_Click(object sender, EventArgs e)
    {
        LinkButton btn = sender as LinkButton;
        int Id = Convert.ToInt32(btn.CommandName);
        int intNodays = Convert.ToInt32(btn.CommandArgument);
        ShiftRoasterId = Id;
        DataSet ds_ShiftRoaster = new DataSet();
        ds_ShiftRoaster = GetshiftRoasterDeatails();
        ViewState["companyId"] = Convert.ToString(ds_ShiftRoaster.Tables[0].Rows[0]["Level_Id"]);
        Response.Redirect("TA_UI_Master_ShiftRoasterModify.aspx?Id=" + Convert.ToInt16(ShiftRoasterId) + "&" + "Nodays=" + Convert.ToInt16(intNodays) + "&" + "companyId=" + ViewState["companyId"]);
    }
    protected void btn_delete_Click_1(object sender, EventArgs e)
    {
        cls_Master_Transportation objTransportationRoute = new cls_Master_Transportation();
        LinkButton btn = sender as LinkButton;
        int intRoaster = Convert.ToInt32(btn.CommandName);
        try
        {
            obj_ShiftRoaster.DeleteShiftRoaster(Convert.ToString(intRoaster), true);
            string str = "alert('" + rm.GetString("Alert_Shiftrosterdeletedsuccessfully", ci) + "');location.replace('TA_UI_Master_ShiftRoaster.aspx');";
            Page.ClientScript.RegisterStartupScript(this.GetType(), "alertBox", str, true);
        }
        catch (Exception Ex)
        {
            strScript = "<script language='javascript'>";
            strScript = strScript + "alert('" + Ex.Message + "')";
            strScript = strScript + "</script>";
            Page.RegisterStartupScript("Error", strScript);
        }
    }
    public DataSet GetshiftRoasterDeatails()
    {
        DataSet ds = new DataSet();
        try
        {
            ds = obj_ShiftRoaster.GetShiftRoasterDetailsById(ShiftRoasterId);
        }
        catch
        {
        }
        return ds;
    }
}
