using System;
using System.Collections;
using System.Data;
using System.Transactions;
using System.Web.UI;
using System.Web.UI.WebControls;
using DeiNiu.wms.Logical;
using DeiNiu.Utils;
public partial class RoleAuth : PageBase
{
    private LRoleAuthority _logic;
   
    protected void Page_Load(object sender, EventArgs e)
    {
       
       
        if (!IsPostBack)
        {
            _logic = new LRoleAuthority();
            Databound(); 
            bindAuth();
            bindRoles();
           
        }
        SessionSeting();
    }
    /// 
    /// bound the query list.
    /// 
    private void Databound()
    {
    //    bindAuth();
    //    bindRoles();
         bindGV();
         UpdateSession();
 
        
    }
   
    /// 
    /// 绑定权限
    /// 
    void bindAuth()
    {
        treeAuth.Nodes.Clear(); 
        BindAuthTree("0", treeAuth.SelectedNode, _logic.QueryNonePublicAuthority().DefaultView);
    }
 
    /// 
    ///绑定角色
    /// 
    private void bindRoles()
    {
        DataView dv = _logic.GetAllRoles().DefaultView;
        dv.RowFilter = "role_4Approve =0";
        listRoles.DataSource =dv;
        listRoles.DataTextField = "role_name";
        listRoles.DataValueField = "ID";
        listRoles.DataBind();
    }
 
   
    public void BindAuthTree(string id, TreeNode pNode, DataView  dv )
    {
        dv.RowFilter = "auth_uplevel = '" + id + "'";
        foreach (DataRowView row in  dv)
        {
            TreeNode node = new TreeNode();
            string authLink = row["auth_link"].ToString();
            string authForm = row["auth_class"].ToString();
            node.Value = row["ID"].ToString();
            node.Expanded = true;
            string authLevel = row["auth_uplevel"].ToString();
           // if (authForm == string.Empty && (authLink == string.Empty || authLink.Trim().Length < 6)) //not *.aspx
            if (authLevel.Equals("0"))
            {
                node.Text = "" + row["auth_name"] + ""; 
                node.SelectAction = TreeNodeSelectAction.None;
              //  node.ShowCheckBox = false;
            }   else 
 
            {
                node.Text =   row["auth_name"].ToString()  ;
            }
           
          
            if (pNode == null)
            //说明是根节点
            {
                treeAuth.Nodes.Add(node);
              
            }
            else
            {
                
                pNode.ChildNodes.Add(node);
 
            }
            BindAuthTree(node.Value, node, new DataView(dv.Table));
        }
    }
    private void SessionSeting()
    {
        if (IsPostBack)
        {
            _logic = (LRoleAuthority)Session[appScope.PagelevelObj];
        }
    }
    private void UpdateSession()
    {
        Session[appScope.PagelevelObj] = _logic;
    }
    
    /************************************** protected methods (event listeners) ********************************/
 
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        lbmsg.Text = " ";
        if (listRoles.SelectedValue == string.Empty) return;
        //角色,权限
        ArrayList al = new ArrayList();
        GetCheckedNode(treeAuth.Nodes, al);
        _logic.Initialize();
//        using (TransactionScope trans = new TransactionScope())
        {
            try
            {
                
                _logic.GetRoleAuthority.Delete(Convert.ToInt32(listRoles.SelectedValue));
                //   _logic.Initialize();
                foreach (string id in al)
                {
                    _logic.GetRoleAuthority.ra_role = Convert.ToInt32(listRoles.SelectedValue);
                    _logic.GetRoleAuthority.ra_authority = Convert.ToInt32(id);
                    _logic.GetRoleAuthority.Add();
                }
                lbmsg.Text = "保存成功!";
//                trans.Complete();
            }
            catch (MyException)
            {
                throw;
            }
            catch (Exception)
            {
                throw new Exception("出现异常,保存失败。");
            }
        }
        Databound();
    }
   
    //reset the page
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        _logic.Initialize();
        Page_Load(this, e);
        ClearDetail();
    }
    /***************************************** internal methods ********************************************/
    /// 
    /// set data from page to shift object
    /// 
    private void SetDataBack()
    { 
       
    }
    void GetCheckedNode(TreeNodeCollection tnc, ArrayList checkedvalues)
    {
        foreach (TreeNode node in tnc)
        {
            if (node.Checked)
            {
                checkedvalues.Add(node.Value);
            }
            GetCheckedNode(node.ChildNodes, checkedvalues);
        }
      
    }
    void SetCheckedNode(TreeNodeCollection tnc, ArrayList setcheckedvalues)
    {
        foreach (TreeNode node in tnc)
        { 
            node.Checked = setcheckedvalues.Contains(Convert.ToInt32(node.Value)); 
             
            SetCheckedNode(node.ChildNodes, setcheckedvalues);
        }
    }
    private void chgOperTxt(bool addNew)
    {
       
        btnSubmit.Enabled = true;
        
    }
    private void ClearDetail()
    { 
       
    }
    protected void listRoles_SelectedIndexChanged(object sender, EventArgs e)
    {
        lbmsg.Text = " ";
 /*
        if (treeAuth.SelectedNode != null) treeAuth.SelectedNode.Selected = false;
        lbAuthRole.Text = "权限角色清单" ;
        GvRole.DataSource = null;
        GvRole.DataBind();
  */
  
  //--重置权限树的选择
            ArrayList autList = new ArrayList();
            DataView dv = _logic.GetRoleAuthority.QueryActived().Tables[0].DefaultView;
            dv.RowFilter = "ra_role ='" + listRoles.SelectedValue + "'";
            foreach (DataRowView drv in dv)
            {
                autList.Add(drv["ra_authority"]);
            }
            SetCheckedNode(treeAuth.Nodes, autList);
       bindGV();
    }
   
     
    protected void treeAuth_SelectedNodeChanged(object sender, EventArgs e)
    {
        bindGV();
            
    }
    void bindGV()
    { 
        //角色对应的人员清单
        if (listRoles.SelectedItem != null)
        {
            lbRoleEm.Text = listRoles.SelectedItem.Text + " 角色人员清单";
            GvEm.DataSource = _logic.GetEmListByRole(Convert.ToInt32(listRoles.SelectedValue));
        }
        GvEm.DataBind();
         
      
        //权限对应的角色,人员清单
        if (treeAuth.SelectedNode != null) 
        { 
            lbAuthEm.Text = treeAuth.SelectedNode.Text + " 权限人员清单";
            GvAuthEm.DataSource = _logic.GetEmListByAuth(Convert.ToInt32(treeAuth.SelectedValue));
            lbAuthRole.Text =   treeAuth.SelectedNode.Text + " 权限角色清单";
            GvRole.DataSource = _logic.GetRoleListByAuth(Convert.ToInt32(treeAuth.SelectedValue));
            
        }
        GvAuthEm.DataBind();
        GvRole.DataBind();
    }
 
}