using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Model;
using BLL;
namespace ComResProSystem
{
    public partial class PermissionsEdit : BasePage
    {
        BaseBLL bll = new BaseBLL();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindPer();
            }
        }
        protected void BindPer()
        {
            string ID = Request.QueryString["ID"];
            List<object> list = bll.SelectObject(typeof(Model.Authority), "Authority", "ID,NodeText,NodeDes", "1=1");
            List<object> userlist = bll.SelectObject(typeof(Model.UserPermissions), "UserPermissions", "AuthorityID", " UserID=" + ID);
            Rp_Permission.DataSource = list;
            Rp_Permission.DataBind();
            if (userlist.Count > 0)
            {
                foreach (RepeaterItem item in Rp_Permission.Items)
                {
                    Label lb = (Label)item.FindControl("Lb_ID");
                    string value = lb.Text;
                    for (int i = 0; i < userlist.Count; i++)
                    {
                        Model.UserPermissions user=(Model.UserPermissions)userlist[i];
                        if (value == user.AuthorityID)
                        {
                            CheckBox ck = (CheckBox)item.FindControl("Ck_Check");
                            ck.Checked = true;
                        }
                    }
                }
            }

        }

        protected void Itb_Submit_Click(object sender, ImageClickEventArgs e)
        {
            string ID = Request.QueryString["ID"];
            if (bll.Delete0bject("UserPermissions", "UserID=" + ID) == 0)
            {
                foreach (RepeaterItem item in Rp_Permission.Items)
                {
                    CheckBox ck = (CheckBox)item.FindControl("Ck_Check");
                    if (ck.Checked)
                    {
                        Label lb = (Label)item.FindControl("Lb_ID");
                        Dictionary<string,string> dic=new Dictionary<string,string>();
                        dic.Add("UserID", ID);
                        dic.Add("AuthorityID", lb.Text);
                        object obj = bll.CreateObject(typeof(Model.UserPermissions), dic);
                        bll.SaveObject(obj, "UserPermissions");
                 
                    }
                }
            }
        }
    }
}