using System;
using System.Data;
using System.Globalization;
using System.Resources;
using System.Text;
using System.Threading;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Masters_TA_UI_Master_LevelRights : Base.BasePage
{
    #region VariableDeclaration
    StringBuilder sb = new StringBuilder();
    int gid = 0;
    ResourceManager rm;
    CultureInfo ci;
    userRightsEventData objClass = new userRightsEventData();
    cls_Common objCommon = new cls_Common();
    #endregion
    #region PageLoad
    protected override void Page_Load(object sender, EventArgs e)
    {
        int employeeId = 0;
        // session checking
        base.Page_Load(sender, e);
        string source_Language;
        source_Language = Convert.ToString(Session["Session_ActiveLanguage"]);
        employeeId = Int32.Parse(Session["EmpAutoID"].ToString());
        rm = new ResourceManager(source_Language, System.Reflection.Assembly.Load("App_GlobalResources"));
        ci = Thread.CurrentThread.CurrentCulture;
        site.SiteMapProvider = Convert.ToString(Session["SiteMapProvider"]);
        if (source_Language == "Resources.Resource_AR")
        {
            tvLevel.LineImagesFolder = "../dist/img/TreeLineImage_RTL";
        }
        if (!Page.IsPostBack)
        {
            // LoadStrings Function fills the Label from the Resource File
            LoadStrings(ci);
            string strSetAlert = "";
            strSetAlert = strSetAlert + "<script language='javascript'>";
            strSetAlert = strSetAlert + "SetAlertMessages('" + rm.GetString("Alert_Are_you_sure_to_Save", ci) + "');";
            strSetAlert = strSetAlert + "</script>";
            Page.RegisterStartupScript("SetAlertMessages", strSetAlert);
            if (Request.QueryString["gid"] != null)
            {
                gid = Convert.ToInt32(Request.QueryString["gid"]);
                DataSet dsusergroupName = new DataSet();
                dsusergroupName = objClass.getUserGroupNameById(gid);
                if (dsusergroupName != null)
                {
                    if (dsusergroupName.Tables.Count > 0)
                    {
                        if (dsusergroupName.Tables[0].Rows.Count > 0)
                        {
                            this.lblGroupName.Text = Convert.ToString(dsusergroupName.Tables[0].Rows[0]["groupName"]);
                        }
                    }
                }
                Session["gid"] = gid;
            }
            getNodes();
            objCommon.menu("Previlege", "21");
        }
    }
    #endregion
    #region UserDefinedFunctions
    //Getting Information from resource file and display it in label
    private void LoadStrings(CultureInfo ci)
    {
        lblHeading.Text = rm.GetString("Menu_Masters_UserGroups", ci);
        btn_save.Text = "&nbsp;&nbsp;" + rm.GetString("btn_Save", ci);
        btn_cancel.Text = "&nbsp;&nbsp;" + rm.GetString("btn_Back", ci);
        lbl_UserGroup.Text = rm.GetString("UserGroups_UserGroupName", ci) + " :";
        lblLevelRight.Text = rm.GetString("LevelWiseRights", ci);
    }
    public void getNodes()
    {
        try
        {
            userRightsEventData objClass = new userRightsEventData();
            DataSet ds = new DataSet();
            ds = objClass.SelectLevelRights(gid);
            string strLevelID = "";
            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);
            GetReports levl = new GetReports();
            DataSet dsLev = new DataSet();
            dsLev = levl.SelectLevelDetails();
            int count;
            for (count = 0; count <= dsLev.Tables[0].Rows.Count - 1; count++)
            {
                string tag = "<ul>  <li>  <a href='#'>";
                tvLevel.Style.Add("Padding-top", "0px");
                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 = "&nbsp;" + Convert.ToString(dsLev.Tables[0].Rows[count]["Name"]);
                    root.Value = Convert.ToString(dsLev.Tables[0].Rows[count]["id"]);
                    getChildNodes(root, Convert.ToInt32(dsLev.Tables[0].Rows[count]["id"]), strLevelID);
                    //included by sashma on 04-Sep-2010
                    foreach (DataRow drLevel in ds.Tables[0].Rows)
                    {
                        if (drLevel["LevelId"].ToString() == root.Value)
                        {
                            root.Checked = true;
                        }
                    }
                    //end of check for selection                    
                    this.tvLevel.Nodes.Add(root);
                }
            }
            dsLev.Dispose();
        }
        catch (Exception ex)
        {
            Response.Write(ex);
        }
    }
    public void getChildNodes(TreeNode parent, Int32 menuNo, string strLevelID)
    {
        DataSet ds = new DataSet();
        ds = objClass.SelectLevelRights(gid);
        string str = strLevelID;
        char[] comma = { ',' };
        string[] str1 = str.Split(comma);
        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;
                break;
            }
        }
        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 = "&nbsp;" + Convert.ToString(dsLev1.Tables[0].Rows[kloop]["Name"]);
                    childNode.Value = Convert.ToString(dsLev1.Tables[0].Rows[kloop]["Id"]);
                    foreach (DataRow drLevel in ds.Tables[0].Rows)
                    {
                        if (drLevel["LevelId"].ToString() == childNode.Value)
                        {
                            childNode.Checked = true;
                        }
                    }
                    parent.ChildNodes.Add(childNode);
                    getChildNodes(childNode, Convert.ToInt32(dsLev1.Tables[0].Rows[kloop]["id"]), strLevelID);
                }
            }
        }
        dsLev1.Dispose();
    }
    private void LoadOrgnaisationLevel()
    {
        cls_master_levels class1 = new cls_master_levels();
        DataSet ds = new DataSet();
        if (Session["UserGroupName"] != null)
        {
            ds = class1.getOrgLevelUpdated(0, Convert.ToString(Session["LevelRights"]), 0, Convert.ToString(Session["UserGroupName"]));
        }
        Session["OrgLevels"] = ds;
    }
    //Included by sashma on 04-Sep-2010
    //Inserts the Level wise rights into TA_Tbl_LevelRights
    private void InsertLevels()
    {
        TreeNodeCollection tnCollection = tvLevel.CheckedNodes;
        TreeNode[] trc = new TreeNode[tnCollection.Count];
        tnCollection.CopyTo(trc, 0);
        DataTable dtLevelRights = new DataTable("Ta_Tbl_LevelRights");
        DataColumn dcGroupID = new DataColumn("GroupId", System.Type.GetType("System.Int32"));
        DataColumn dcLevelId = new DataColumn("LevelId", System.Type.GetType("System.String"));
        DataColumn dcEmpId = new DataColumn("emp_id", System.Type.GetType("System.String"));
        dtLevelRights.Columns.Add(dcGroupID);
        dtLevelRights.Columns.Add(dcLevelId);
        dtLevelRights.Columns.Add(dcEmpId);
        DataRow drLevelRights;
        foreach (TreeNode tnode in trc)
        {
            drLevelRights = dtLevelRights.NewRow();
            drLevelRights.BeginEdit();
            drLevelRights["GroupId"] = Convert.ToInt32(Session["gid"]);
            drLevelRights["LevelId"] = Convert.ToInt32(tnode.Value);
            drLevelRights["emp_id"] = Convert.ToString(Session["EmpAutoID"]);
            drLevelRights.EndEdit();
            dtLevelRights.Rows.Add(drLevelRights);
        }
        //Deletes existing level rights before inserting the new level rights.
        objClass.DeleteLevelWiseRights(Convert.ToInt32(Session["gid"]));
        //Inserts the level rights into TA_Tbl_Levelrights.
        objClass.InsertInto_TATblLevelRights(dtLevelRights);
    }
    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)
            {
                tnode.Checked = true;
            }
            if (tnode.Parent != null)
            {
                selectparentnode(tnode.Parent);
            }
            else
                return;
        }
    }
    private void SelectParentNodes()
    {
        TreeNodeCollection tnCollection = tvLevel.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)
            {
                tnode.Checked = true;
            }
            if (tnode != null)
            {
                selectparentnode(tnode.Parent);
            }
        }
    }
    private void getSelectedChildNodes(TreeNode[] orderedtrc, int vLoop, TreeNode trnode)
    {
        foreach (TreeNode trchildnode in trnode.ChildNodes)
        {
            if (trchildnode.Checked == true)
            {
                orderedtrc[vLoop] = trchildnode;
                vLoop++;
                getSelectedChildNodes(orderedtrc, vLoop, trchildnode);
            }
        }
    }
    private void CheckBoxParent()
    {
        userRightsEventData objClass = new userRightsEventData();
        DataSet ds = new DataSet();
        ds = objClass.SelectLevelRights(gid);
        if (ds != null)
        {
            if (ds.Tables.Count > 0)
            {
                if (ds.Tables[0].Rows.Count > 0)
                {
                    if (ds.Tables[0].Rows[0]["LevelId"] != DBNull.Value)
                    {
                        string strLevelID = Convert.ToString(ds.Tables[0].Rows[0]["LevelId"]);
                        TreeNodeCollection tnCollection = tvLevel.Nodes;
                        TreeNode[] trc = new TreeNode[tnCollection.Count];
                        tnCollection.CopyTo(trc, 0);
                        foreach (TreeNode tnode in trc)
                        {
                            string str = strLevelID;
                            char[] comma = { ',' };
                            string[] str1 = str.Split(comma);
                            int flag = 0;
                            foreach (string st in str1)
                            {
                                if (st == tnode.Value)
                                {
                                    tnode.Checked = true;
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    private void CheckBoxNodes(TreeNode tnode, string LevelIDs)
    {
        if (tnode != null)
        {
            string str = LevelIDs;
            char[] comma = { ',' };
            string[] str1 = str.Split(comma);
            int flag = 0;
            foreach (string st in str1)
            {
                if (st == tnode.Value)
                {
                    tnode.Checked = true;
                }
            }
            if (tnode.Parent != null)
            {
            }
            else
                return;
        }
    }
    #endregion
    #region ButtonEvents
    protected void TreeView1_TreeNodeCheckChanged(object sender, TreeNodeEventArgs e)
    {
        Response.Write("test");
    }
    protected void but_Click(object sender, EventArgs e)
    {
        SelectParentNodes();
        InsertLevels();
        LoadOrgnaisationLevel();
        string strScript = "";
        strScript = strScript + "<script language='javascript'>";
        strScript = strScript + "alert('" + rm.GetString("Alert_LevelwiseRightssavedsuccessfully", ci) + "');";
        strScript = strScript + "location.replace('TA_UI_Master_groupView.aspx');";
        strScript = strScript + "</script>";
        ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", strScript, false);
    }
    protected void btn_cancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("TA_UI_Master_groupView.aspx");
    }
    #endregion
}