using System;
using System.Data;
using System.Globalization;
using System.IO;
using System.Resources;
using System.Text;
using System.Threading;
using System.Web.UI;
using System.Web.UI.WebControls;
/****************************************************************************************************************************
* Project Name    : TimeCheck                                   Started Date  : 12-Aug-2007
* Module          : Additional WeeklyOff Settings               Last Modified : 14-Aug-2007
* Programmer      : Janakaraj.P
* Current Storage : \\Angler56\Projects\Inprogress\SCREEN1206\code\Time_AttendanceApp
 * Modified By      : Manjula.P
 * Modified On      : 28-Sep-2013
 * Descriptions     : checking levelrights additonal weekly off for the login employee group in GetNodes() and getChildNodes : Mantis ;0019097
****************************************************************************************************************************/
public partial class Organization_Settings_Organization_AddlWeekoff_Add : Base.BasePage
{
    #region Variable declaration
    AddlWeekOffEventData objAddlWeekOff = new AddlWeekOffEventData();
    ResourceManager rm;
    CultureInfo ci;
    userRightsEventData objClass = new userRightsEventData();
    StringBuilder sb = new StringBuilder();
    int AddWeekOffId = 0;
    int auditEmpId;
    Cls_CheckUser objCheckUser = new Cls_CheckUser();
    DataSet ds_Checkuser = new DataSet();
    #endregion
    #region Page Load Event
    protected override void Page_Load(object sender, EventArgs e)
    {
        base.Page_Load(sender, e);
        GC.Collect();
        if (Request.QueryString["AddlWeekoff_Id"] != null)
        {
            AddWeekOffId = Convert.ToInt32(Request.QueryString["AddlWeekoff_Id"]);
        }
        if (!Page.IsPostBack)
        {
            base.CheckPages(0);
            ddl_Day.Focus();
            GetNodes();
        }
        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;
        string strSetAlert = "";
        strSetAlert = strSetAlert + "<script language='javascript'>";
        strSetAlert = strSetAlert + "SetAlertMessages('" + rm.GetString("Alert_SelectWeeklyoffDay", ci) + "','" + rm.GetString("ReqField_SelectWeek", ci) + "');";
        strSetAlert = strSetAlert + "</script>";
        Page.RegisterStartupScript("SetAlertMessages", strSetAlert);
        site.SiteMapProvider = Convert.ToString(Session["SiteMapProvider"]);
        if (source_Language == "Resources.Resource_AR")
        {
            tvAdditionalWeeklyOff.LineImagesFolder = "../dist/img/TreeLineImage_RTL";
        }
       
        LoadStrings(ci);
        auditEmpId = Convert.ToInt32(Session["EmpAutoID"]);
        UserRights();
    }
    #endregion
    #region User Defined Functions
    public void GetNodes()
    {
        try
        {
            userRightsEventData objClass = new userRightsEventData();
            DataSet ds = new DataSet();
            string strLevelID = string.Empty;
            if (AddWeekOffId != 0)
            {
                btn_Save.Visible = false;
                ds = objAddlWeekOff.SelectAdditionalWeekOffLevelRights(AddWeekOffId);
                if (ds != null)
                {
                    if (ds.Tables.Count > 0)
                    {
                        if (ds.Tables[0].Rows.Count > 0)
                        {
                            ddl_Day.SelectedValue = Convert.ToString(ds.Tables[0].Rows[0]["Addl_WeekOff_Day"]);
                            ddl_Week.SelectedValue = Convert.ToString(ds.Tables[0].Rows[0]["Addl_WeekOff_Week"]);
                            if (ds.Tables[0].Rows[0]["LevelId"] != DBNull.Value)
                            {
                                strLevelID = Convert.ToString(ds.Tables[0].Rows[0]["LevelId"]);
                            }
                        }
                    }
                }
            }
            else
            {
                btn_Save.Visible = true;
            }
            string str = strLevelID;
            char[] comma = { ',' };
            string[] str1 = str.Split(comma);
            DataSet dsRightsLevel = new DataSet();
            if (Session["OrgLevels"] != null)
                dsRightsLevel = (DataSet)Session["OrgLevels"];
            GetReports levl = new GetReports();
            DataSet dsLev = new DataSet();
            dsLev = levl.SelectLevelDetails();
            int count;
            for (count = 0; count <= dsLev.Tables[0].Rows.Count - 1; count++)
            {
                TreeNode root = new TreeNode();
                if (Convert.ToInt32(dsLev.Tables[0].Rows[count]["parentid"]) == 0)
                {
                    DropDownList dd = new DropDownList();
                    dd.DataTextField = Convert.ToString(dsLev.Tables[0].Rows[count]["Name"]);
                    dd.DataBind();
                    root.Text = Convert.ToString(dsLev.Tables[0].Rows[count]["Name"]);
                    root.Value = Convert.ToString(dsLev.Tables[0].Rows[count]["id"]);
                    root.NavigateUrl = "";
                    getChildNodes(root, Convert.ToInt32(dsLev.Tables[0].Rows[count]["id"]), strLevelID);
                    if (AddWeekOffId != 0)
                    {
                        foreach (DataRow drLevel in ds.Tables[0].Rows)
                        {
                            if (drLevel["LevelId"].ToString() == root.Value)
                            {
                                root.Checked = true;
                            }
                        }
                    }
                    this.tvAdditionalWeeklyOff.Nodes.Add(root);
                    DataRow[] row = dsRightsLevel.Tables[0].Select("id='" + Convert.ToString(dsLev.Tables[0].Rows[count]["Id"]) + "'");
                    if (row.Length > 0)
                    {
                        if (Convert.ToString(row[0].ItemArray[0]) != string.Empty)
                        {
                        }
                    }
                    else
                    {
                        root.SelectAction = TreeNodeSelectAction.None;
                    }
                }
            }
            dsLev.Dispose();
        }
        catch (Exception ex)
        {
            Response.Write(ex);
        }
    }
    public void getChildNodes(TreeNode parent, Int32 menuNo, string strstrLevelID)
    {
        DataSet ds = new DataSet();
        string strLevelID = string.Empty;
        if (AddWeekOffId != 0)
        {
            ds = objAddlWeekOff.SelectAdditionalWeekOffLevelRights(AddWeekOffId);
            if (ds != null)
            {
                if (ds.Tables.Count > 0)
                {
                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        if (ds.Tables[0].Rows[0]["LevelId"] != DBNull.Value)
                        {
                            strLevelID = Convert.ToString(ds.Tables[0].Rows[0]["LevelId"]);
                        }
                        string str = strLevelID;
                        char[] comma = { ',' };
                        string[] str1 = str.Split(comma);
                    }
                }
            }
        }
        DataSet dsRightsLevel = new DataSet();
        if (Session["OrgLevels"] != null)
            dsRightsLevel = (DataSet)Session["OrgLevels"];
        GetReports levl = new GetReports();
        DataSet dsLev1 = new DataSet();
        dsLev1 = levl.SelectLevelDetails();
        TreeNode childNode;
        int rowCount = 0, kloop = 0;
        for (kloop = 0; kloop <= dsLev1.Tables[0].Rows.Count - 1; kloop++)
        {
            if (Convert.ToInt32(dsLev1.Tables[0].Rows[kloop]["parentId"]) == menuNo)
            {
                rowCount += 1;
            }
        }
        if (rowCount != 0)
        {
            for (kloop = 0; kloop <= dsLev1.Tables[0].Rows.Count - 1; kloop++)
            {
                if (Convert.ToInt32(dsLev1.Tables[0].Rows[kloop]["parentId"]) == menuNo)
                {
                    childNode = new TreeNode();
                    childNode.Text = Convert.ToString(dsLev1.Tables[0].Rows[kloop]["Name"]);
                    childNode.Value = Convert.ToString(dsLev1.Tables[0].Rows[kloop]["Id"]);
                    childNode.NavigateUrl = "";
                    if (AddWeekOffId != 0)
                    {
                        foreach (DataRow drLevel in ds.Tables[0].Rows)
                        {
                            if (drLevel["LevelId"].ToString() == childNode.Value)
                            {
                                childNode.Checked = true;
                            }
                        }
                    }
                    parent.ChildNodes.Add(childNode);
                    DataRow[] row = dsRightsLevel.Tables[0].Select("id='" + Convert.ToString(dsLev1.Tables[0].Rows[kloop]["Id"]) + "'");
                    if (row.Length > 0)
                    {
                        if (Convert.ToString(row[0].ItemArray[0]) != string.Empty)
                        {
                        }
                    }
                    else
                    {
                        childNode.SelectAction = TreeNodeSelectAction.None;
                    }
                    getChildNodes(childNode, Convert.ToInt32(dsLev1.Tables[0].Rows[kloop]["id"]), strLevelID);
                }
            }
        }
        dsLev1.Dispose();
    }
    public void InsertNodes()
    {
        TreeNodeCollection trcollection = tvAdditionalWeeklyOff.CheckedNodes;
        if (trcollection.Count > 0)
        {
            TreeNode[] trc = new TreeNode[trcollection.Count];
            trcollection.CopyTo(trc, 0);
            DataTable dtAddWeekOffLevels = new DataTable("TA_Tbl_Additional_WeeklyOff_Levels");
            DataColumn dcLevelId = new DataColumn("Addl_WeeklyOff_Id", System.Type.GetType("System.Int32"));
            dtAddWeekOffLevels.Columns.Add(dcLevelId);
            DataRow drAddWeek;
            foreach (TreeNode tr in trc)
            {
                if (tr.SelectAction == TreeNodeSelectAction.None)
                {
                    string str = "alert('" + rm.GetString("Additonal_WeeklyOff_Rights", ci) + "');";
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", str, true);
                    return;
                }
                drAddWeek = dtAddWeekOffLevels.NewRow();
                drAddWeek.BeginEdit();
                drAddWeek["Addl_WeeklyOff_Id"] = Convert.ToInt32(tr.Value);
                drAddWeek.EndEdit();
                dtAddWeekOffLevels.Rows.Add(drAddWeek);
            }
            if (dtAddWeekOffLevels.Rows.Count > 0)
            {
                StringWriter strXml = new StringWriter();
                dtAddWeekOffLevels.WriteXml(strXml);
                DataSet dsResult = new DataSet();
                if (AddWeekOffId != 0)
                {
                    dsResult = objClass.InsertAddWeeklyOff(ddl_Day.SelectedValue, ddl_Week.SelectedValue, Convert.ToInt32(Session["EmpAutoID"]), strXml.ToString(), AddWeekOffId);
                }
                else
                {
                    dsResult = objClass.InsertAddWeeklyOff(ddl_Day.SelectedValue, ddl_Week.SelectedValue, Convert.ToInt32(Session["EmpAutoID"]), strXml.ToString(), 0);
                }
                if (dsResult.Tables[0].Rows[0][0].ToString() == "1")
                {
                    string str = "alert('" + rm.GetString("Additional_WeekOff_AddedSuccess", ci) + "');location.replace('Organization_AddlWeekoff.aspx?');";
                    Session["parentLevelId"] = 0;
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", str, true);
                }
                else if (dsResult.Tables[0].Rows[0][0].ToString() == "0")
                {
                    string str = "alert('" + rm.GetString("AdditionalWeeklyOff_Already_Exists", ci) + "');";
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", str, true);
                }
                else if (dsResult.Tables[0].Rows[0][0].ToString() == "Exists")
                {
                    string str = "alert('" + rm.GetString("Additional_WeekOff_AlreadyExists", ci) + "')";
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", str, true);
                }
            }
        }
        else
        {
            string str = "alert('" + rm.GetString("Additional_WeekOff_EmptySelection", ci) + "');";
            ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", str, true);
        }
    }
    private void LoadStrings(CultureInfo ci)
    {
        lbl_selectedweek.Text = rm.GetString("AdditionalWeeklyOff_SelectedWeek", ci);
        lbl_Weekof.Text = rm.GetString("AdditionalWeeklyOff_WeeklyOffDay", ci);
        lbl_Header.Text = rm.GetString("Org_Additional_Weeklyoff", ci);
        btn_Save.Text = "&nbsp;&nbsp;" + rm.GetString("btn_Save", ci);
        btn_Cancel.Text = "&nbsp;&nbsp;" + rm.GetString("btn_Back", ci);
        ddl_Day.Items[0].Text = rm.GetString("Select", ci);
        ddl_Day.Items[1].Text = rm.GetString("Sunday", ci);
        ddl_Day.Items[2].Text = rm.GetString("Monday", ci);
        ddl_Day.Items[3].Text = rm.GetString("Tuesday", ci);
        ddl_Day.Items[4].Text = rm.GetString("Wednesday", ci);
        ddl_Day.Items[5].Text = rm.GetString("Thursday", ci);
        ddl_Day.Items[6].Text = rm.GetString("Friday", ci);
        ddl_Day.Items[7].Text = rm.GetString("Saturday", ci);
        ddl_Week.Items[0].Text = rm.GetString("Select", ci);
        ddl_Week.Items[1].Text = rm.GetString("FirstWeek", ci);
        ddl_Week.Items[2].Text = rm.GetString("SecondWeek", ci);
        ddl_Week.Items[3].Text = rm.GetString("ThirdWeek", ci);
        ddl_Week.Items[4].Text = rm.GetString("FourthWeek", ci);
        ddl_Week.Items[5].Text = rm.GetString("FifthWeek", ci);
        lblLoading.Text = rm.GetString("ProgressBarText", ci);
    }
    public void UserRights()
    {
        try
        {
            ds_Checkuser = objCheckUser.checkRights(49, 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"]) == 0)
                {
                    Response.Redirect("OrganizationLandingPage.aspx");
                }
            }
            else
            {
                Response.Redirect("OrganizationLandingPage.aspx");
            }
        }
        catch
        {
        }
    }
    private void SelectParentNodes()
    {
        TreeNodeCollection tnCollection = tvAdditionalWeeklyOff.CheckedNodes;
        TreeNode[] trc = new TreeNode[tnCollection.Count];
        tnCollection.CopyTo(trc, 0);
        foreach (TreeNode tnode in trc)
        {
            string str = sb.ToString();
            char[] comma = { ',' };
            string[] str1 = str.Split(comma);
            int flag = 0;
            foreach (string st in str1)
            {
                if (st == tnode.Value)
                {
                    flag = 1;
                }
            }
            if (flag == 0)
            {
                sb.Append(tnode.Value + ",");
                tnode.Checked = true;
            }
            if (tnode != null)
            {
                selectparentnode(tnode.Parent);
            }
        }
    }
    private void selectparentnode(TreeNode tnode)
    {
        if (tnode != null)
        {
            string str = sb.ToString();
            char[] comma = { ',' };
            string[] str1 = str.Split(comma);
            int flag = 0;
            foreach (string st in str1)
            {
                if (st == tnode.Value)
                {
                    flag = 1;
                }
            }
            if (flag == 0)
            {
                sb.Append(tnode.Value + ",");
                tnode.Checked = true;
            }
            if (tnode.Parent != null)
            {
                selectparentnode(tnode.Parent);
            }
            else
                return;
        }
    }
    #endregion
    #region Button Click Events
    protected void btn_Save_Click(object sender, EventArgs e)
    {
        try
        {
            InsertNodes();
        }
        catch
        {
        }
    }
    protected void btn_Cancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("Organization_AddlWeekoff.aspx");
    }
    #endregion
}
