using System;
using System.Data;
using System.Globalization;
using System.Resources;
using System.Threading;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using DevExpress.Web;
/****************************************************************************************************************************
* Project Name    : TimeCheck                                         Started Date  : 12-Aug-2007
* Module          : Shift Pattern                                     Last Modified : 20-Aug-2007
* Programmer      : Janakaraj.P
* Current Storage : \\Angler56\Projects\Inprogress\SCREEN1206\code\Time_AttendanceApp
* BackUp          :
****************************************************************************************************************************/
public partial class Masters_TA_UI_Master_ShiftPattern : Base.BasePage
{
    #region "variable Declartion"
    ResourceManager rm;
    CultureInfo ci;
    LeaveEventData objLeave = new LeaveEventData();
    Cls_CheckUser objCheckUser = new Cls_CheckUser();
    DataSet ds_Checkuser = new DataSet();
    DataSet ds_PatternName = new DataSet();
    cls_Common objCommon = new cls_Common();
    ShiftPatternEventData obj = new ShiftPatternEventData();
    ShiftRoasterEventData obj_ShiftRoaster = new ShiftRoasterEventData();
    string source_Language;
    string gstrUserGroupName = string.Empty;
    int PatternId;
    string strScript = string.Empty;
    #endregion
    #region PageLoad
    protected override void Page_Load(object sender, EventArgs e)
    {
        // session checking
        base.Page_Load(sender, e);
        Grv_ShiftPattern.Styles.CssFilePath = Convert.ToString(Session["GridCSS"]);
        if (!Page.IsPostBack)
        {
            base.CheckPages(20);
            objCommon.menu("ShiftMaster", "20");
        }
        //get the culture info to set the 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();
        this.btn_Add.Focus();
        gstrUserGroupName = Convert.ToString(Session["UserGroupName"]);
        if (gstrUserGroupName == "SuperAdmin")
        {
            btn_Restore.Visible = false;
            btn_Delete.Visible = false;
            btn_PermanentDelete.Visible = true;
            //Grv_ShiftPattern.Columns[5].Visible = true;
        }
        else
        {
            btn_Restore.Visible = false;
            btn_Delete.Visible = false;
            btn_PermanentDelete.Visible = false;
            //Grv_ShiftPattern.Columns[5].Visible = false;
        }
    }
    #endregion
    #region ButtonEvents
    protected void btn_Add_Click(object sender, EventArgs e)
    {
        Response.Redirect("../Masters/TA_UI_Master_ShiftPatternAdd.aspx");
    }
    protected void btn_Delete_Click(object sender, EventArgs e)
    {
        string[] ChkBoxIDs;
        bool checkDeleteFlag = true;
        int k = 0;
        int PatternId = 0;
        DataSet ds_ShiftRoaster = new DataSet();
        if (hdnSelectRow.Value != string.Empty && hdnSelectRow.Value != null)
        {
            ChkBoxIDs = objLeave.commaStringToArray(hdnSelectRow.Value);
            for (int i = 0; i < ChkBoxIDs.Length; i++)
            {
                ds_ShiftRoaster = obj_ShiftRoaster.SelectShiftRoaster(gstrUserGroupName, Convert.ToString(Session["LevelRights"]), Convert.ToInt16(Session["parentLevelId"]));
                for (int Rowcount = 0; Rowcount < ds_ShiftRoaster.Tables[0].Rows.Count; Rowcount++)
                {
                    for (k = 1; k <= 10; k++)
                    {
                        string str_ShiftPattern = "Pattern" + k;
                        int intShiftPatternId = 0;
                        if (ds_ShiftRoaster.Tables[0].Rows.Count > 0)
                        {
                            if (ds_ShiftRoaster.Tables[0].Rows[0][str_ShiftPattern] != DBNull.Value)
                            {
                                intShiftPatternId = Convert.ToInt32(ds_ShiftRoaster.Tables[0].Rows[0][str_ShiftPattern]);
                            }
                        }
                        if (intShiftPatternId == Convert.ToUInt32(ChkBoxIDs[i]))
                        {
                            checkDeleteFlag = false;
                            base.Message(rm.GetString("Masters_ShiftPattern_Cannot_Delete", ci));
                            hdnSelectRow.Value = "";
                            return;
                        }
                    }
                }
                if (checkDeleteFlag == false)
                {
                    hdnSelectRow.Value = "";
                    base.Message(rm.GetString("Masters_ShiftPattern_Cannot_Delete", ci));
                }
                else
                {
                    obj.DeleteShiftPattern(Convert.ToInt32(ChkBoxIDs[i]));
                }
            }
            string str = "alert('" + rm.GetString("Alert_ShiftPatterndeletedsuccessfully", ci) + "');location.replace('TA_UI_Master_ShiftPattern.aspx');";
            Page.ClientScript.RegisterStartupScript(this.GetType(), "alertBox", str, true);
        }
        else
        {
            strScript = "";
            strScript = strScript + "<script language='javascript'>";
            strScript = strScript + "alert('" + rm.GetString("DeletionRecordAlert", ci) + "')";
            strScript = strScript + "</script>";
            Page.RegisterStartupScript("DelAlert1", strScript);
            return;
        }
    }
    protected void btn_Restore_Click(object sender, EventArgs e)
    {
        string[] ChkBoxIDs;
        string SelectedChkBoxIDs = "";
        int count = 0;
        if (hdnSelectRow.Value != string.Empty && hdnSelectRow.Value != null)
        {
            ChkBoxIDs = objLeave.commaStringToArray(hdnSelectRow.Value);
            for (int i = 0; i < ChkBoxIDs.Length; i++)
            {
                count = obj.CheckShiftPattern_Restore(Convert.ToInt32(ChkBoxIDs[i]));
                if (count > 0)
                {
                    base.Message(rm.GetString("Message_Cannot_Restore", ci));
                    Grv_ShiftPattern.DataBind();
                    hdnSelectRow.Value = "";
                    return;
                }
                if (SelectedChkBoxIDs == "")
                {
                    SelectedChkBoxIDs = ChkBoxIDs[i];
                }
                else
                {
                    SelectedChkBoxIDs = SelectedChkBoxIDs + "," + ChkBoxIDs[i];
                }
            }
            obj.RestoreShiftPattern(SelectedChkBoxIDs);
            base.MessageWithRedirect(rm.GetString("Alert_ShiftPatternrestoredsuccessfully", ci), "TA_UI_Master_ShiftPattern.aspx");
        }
        else
        {
            strScript = "";
            strScript = strScript + "<script language='javascript'>";
            strScript = strScript + "alert('" + rm.GetString("DeletionRecordAlert", ci) + "')";
            strScript = strScript + "</script>";
            Page.RegisterStartupScript("DelAlert1", strScript);
            return;
        }
    }
    protected void btn_PermanentDelete_Click(object sender, EventArgs e)
    {
        string[] ChkBoxIDs;
        bool checkDeleteFlag = true;
        int k = 0;
        int PatternId = 0; int intShiftPatternId = 0;
        DataSet ds_ShiftRoaster = new DataSet();
        if (hdnSelectRow.Value != string.Empty && hdnSelectRow.Value != null)
        {
            ChkBoxIDs = objLeave.commaStringToArray(hdnSelectRow.Value);
            for (int i = 0; i < ChkBoxIDs.Length; i++)
            {
                ds_ShiftRoaster = obj_ShiftRoaster.SelectShiftRoaster(gstrUserGroupName, Convert.ToString(Session["LevelRights"]), Convert.ToInt16(Session["parentLevelId"]));
                for (int Rowcount = 0; Rowcount < ds_ShiftRoaster.Tables[0].Rows.Count; Rowcount++)
                {
                    for (k = 1; k <= 10; k++)
                    {
                        string str_ShiftPattern = "Pattern" + k;
                        if (ds_ShiftRoaster.Tables[0].Rows.Count > 0)
                        {
                            if (ds_ShiftRoaster.Tables[0].Rows[0][str_ShiftPattern] != DBNull.Value)
                            {
                                intShiftPatternId = Convert.ToInt32(ds_ShiftRoaster.Tables[0].Rows[0][str_ShiftPattern]);
                            }
                        }
                        if (intShiftPatternId == Convert.ToInt32(ChkBoxIDs[i]))
                        {
                            checkDeleteFlag = false;
                            base.Message(rm.GetString("Masters_ShiftPattern_Cannot_Delete", ci));
                            hdnSelectRow.Value = "";
                            return;
                        }
                    }
                }
                if (checkDeleteFlag == false)
                {
                    hdnSelectRow.Value = "";
                    base.Message(rm.GetString("Masters_ShiftPattern_Cannot_Delete", ci));
                }
                else
                {
                    obj.PermanentDeleteShiftPattern(Convert.ToInt16(ChkBoxIDs[i]));
                }
            }
            string str = "alert('" + rm.GetString("Alert_ShiftPatterndeletedsuccessfully", ci) + "');location.replace('TA_UI_Master_ShiftPattern.aspx');";
            Page.ClientScript.RegisterStartupScript(this.GetType(), "alertBox", str, true);
        }
        else
        {
            strScript = "";
            strScript = strScript + "<script language='javascript'>";
            strScript = strScript + "alert('" + rm.GetString("DeletionRecordAlert", ci) + "')";
            strScript = strScript + "</script>";
            Page.RegisterStartupScript("DelAlert1", strScript);
            return;
        }
    }
    #endregion
    #region UserDefinedFunctions
    //Getting Information from resource file and display it in label
    private void LoadStrings(CultureInfo ci)
    {
        lbl_HeaderShiftPattern.Text = rm.GetString("Masters_ShiftPattern", 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 = rm.GetString("btn_Restore", ci);
        Grv_ShiftPattern.Columns[2].Caption = rm.GetString("ShiftPattern_PatternName", ci);
        Grv_ShiftPattern.Columns[3].Caption = rm.GetString("ShiftPattern_NumberOfDays", ci);
        Grv_ShiftPattern.Columns[4].Caption = rm.GetString("EntranceMaster_Company", ci);
        //Grv_ShiftPattern.Columns[5].Caption = rm.GetString("DeleteStatus", ci);
        lblDataView.Text = rm.GetString("lblDataView", ci);
    }
    private void UserRights()
    {
        ds_Checkuser = objCheckUser.checkRights(20, 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 = true;
                this.btn_Restore.Visible = false;
                this.Grv_ShiftPattern.Columns[0].Visible = true;
                this.Grv_ShiftPattern.Columns[7].Visible = true;
            }
            else
            {
                this.btn_Delete.Visible = false;
                this.btn_PermanentDelete.Visible = false;
                this.btn_Restore.Visible = false;
                this.Grv_ShiftPattern.Columns[0].Visible = false;
                this.Grv_ShiftPattern.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_ShiftPattern.Columns[6].Visible = true;
            }
            else
            {
                this.Grv_ShiftPattern.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_ShiftPattern.Columns[5].Visible = true;
            }
            else
            {
                this.Grv_ShiftPattern.Columns[5].Visible = false;
            }
        }
        else
        {
            Response.Redirect("MasterLandingPage.aspx");
        }
    }
    #endregion
    #region GridviewEvents
    protected void Grv_ShiftPattern_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_ShiftPattern.FindRowCellTemplateControl(e.VisibleIndex, null, "lblIsDeleted");
            if (DeletedRecord != null)
            {
                if (DeletedRecord.Text == "True" || DeletedRecord.Text == rm.GetString("Grid_True", ci))
                {
                    DeletedRecord.ForeColor = System.Drawing.Color.Red;
                }
            }
            HtmlInputCheckBox deleteChkBxItem = (HtmlInputCheckBox)Grv_ShiftPattern.FindRowCellTemplateControl(e.VisibleIndex, null, "chkSelect");
            LinkButton LnkbtnDelete = (LinkButton)Grv_ShiftPattern.FindRowCellTemplateControl(e.VisibleIndex, null, "btnDelete");
            if (deleteChkBxItem != null)
            {
                int PatternId = Convert.ToInt16(e.GetValue("ShiftPattern_Id"));
                ds_PatternName = obj.SelectPatternById();
                int k, i;
                string Days = "";
                bool checkPattern = false;
                int RosterCount;
                RosterCount = ds_PatternName.Tables[0].Rows.Count;
                for (i = 0; i < RosterCount; i++)
                {
                    for (k = 1; k <= 10; k++)
                    {
                        Days = "Pattern" + Convert.ToString(k);
                        int intShiftId = 0;
                        if (ds_PatternName.Tables[0].Rows.Count > 0)
                        {
                            if (ds_PatternName.Tables[0].Rows[i][Days] != DBNull.Value)
                            {
                                intShiftId = Convert.ToInt32(ds_PatternName.Tables[0].Rows[i][Days]);
                            }
                        }
                        if (intShiftId == PatternId)
                        {
                            deleteChkBxItem.Disabled = true;
                            LnkbtnDelete.Enabled = false;
                            LnkbtnDelete.OnClientClick = null;
                            checkPattern = true;
                            break;
                        }
                        else
                        {
                            deleteChkBxItem.Disabled = false;
                            LnkbtnDelete.Enabled = true;
                        }
                    }
                    if (checkPattern == true)
                    {
                        break;
                    }
                }
            }
            if (DeletedRecord != null)
            {
                if (DeletedRecord.Text == "True")
                {
                    DeletedRecord.Text = rm.GetString("Grid_True", ci);
                    if (hdnDeletedData.Value == string.Empty)
                    {
                        hdnDeletedData.Value = Convert.ToString(e.GetValue("ShiftPattern_Id"));
                    }
                    else
                    {
                        if (hdnDeletedData.Value.Contains(Convert.ToString(e.GetValue("ShiftPattern_Id"))) == false)
                        {
                            hdnDeletedData.Value = hdnDeletedData.Value + "," + Convert.ToString(e.GetValue("ShiftPattern_Id"));
                        }
                    }
                }
                else if (DeletedRecord.Text == "False")
                {
                    DeletedRecord.Text = rm.GetString("Grid_False", ci);
                }
            }
        }
    }
    #endregion
    private DataSet GetPatternName()
    {
        try
        {
            ds_PatternName = obj.SelectPatternDetails(PatternId);
        }
        catch
        {
        }
        return ds_PatternName;
    }
    public bool CheckShiftPatternMapping()
    {
        bool checkDeleteFlag = true;
        DataSet ds_ShiftRoaster = new DataSet();
        try
        {
            ds_ShiftRoaster = obj_ShiftRoaster.SelectShiftRoaster(gstrUserGroupName, Convert.ToString(Session["LevelRights"]), Convert.ToInt16(Session["parentLevelId"]));
            for (int Rowcount = 0; Rowcount < ds_ShiftRoaster.Tables[0].Rows.Count; Rowcount++)
            {
                for (int i = 1; i <= 10; i++)
                {
                    string str_ShiftPattern = "Pattern" + i;
                    if (PatternId == Convert.ToInt16(Convert.ToString(ds_ShiftRoaster.Tables[0].Rows[Rowcount][str_ShiftPattern]).Trim()))
                    {
                        checkDeleteFlag = false;
                        base.Message(rm.GetString("Masters_ShiftPattern_Cannot_Modified", ci));
                        return false;
                    }
                }
            }
        }
        catch (Exception ex)
        {
            // 
        }
        return (checkDeleteFlag);
    }
    protected void EditLinkButton_Click(object sender, EventArgs e)
    {
        LinkButton btn = sender as LinkButton;
        int Id = Convert.ToInt32(btn.CommandName);
        PatternId = Id;
        bool checkDeleteFlag = true;
        DataSet ds_pat = new DataSet();
        ds_pat = GetPatternName();
        if (ds_pat.Tables[0].Rows.Count > 0)
        {
            ViewState["companyId"] = Convert.ToString(ds_pat.Tables[0].Rows[0]["Level_Id"]);
        }
        checkDeleteFlag = CheckShiftPatternMapping();
        if (checkDeleteFlag == false)
        {
            base.Message(rm.GetString("Masters_ShiftPattern_Cannot_Modified", ci));
        }
        else
        {
            Response.Redirect("TA_UI_Master_ShiftPatternModify.aspx?PatternId=" + Convert.ToInt16(PatternId) + "&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;
        PatternId = Convert.ToInt32(btn.CommandName);
        bool checkDeleteFlag = true;
        // no need to check shift while deleting shift pattern  
        DataSet ds_ShiftRoaster = new DataSet();
        ds_ShiftRoaster = obj_ShiftRoaster.SelectShiftRoaster(gstrUserGroupName, Convert.ToString(Session["LevelRights"]), Convert.ToInt16(Session["parentLevelId"]));
        if (ds_ShiftRoaster.Tables[0].Rows.Count > 0)
        {
            for (int Rowcount = 0; Rowcount < ds_ShiftRoaster.Tables[0].Rows.Count; Rowcount++)
            {
                for (int i = 1; i <= 10; i++)
                {
                    string str_ShiftPattern = "Pattern" + i;
                    if (PatternId == Convert.ToInt16(Convert.ToString(ds_ShiftRoaster.Tables[0].Rows[Rowcount][str_ShiftPattern]).Trim()))
                    {
                        checkDeleteFlag = false;
                        base.Message(rm.GetString("Masters_ShiftPattern_Cannot_Delete", ci));
                        return;
                    }
                }
            }
        }
        if (checkDeleteFlag == false)
        {
            base.Message(rm.GetString("Masters_ShiftPattern_Cannot_Delete", ci));
        }
        else
        {
            obj.PermanentDeleteShiftPattern(Convert.ToInt16(PatternId));
            base.MessageWithRedirect(rm.GetString("Alert_ShiftPatterndeletedsuccessfully", ci), "TA_UI_Master_ShiftPattern.aspx");
        }
    }
}
