using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class permissions : System.Web.UI.Page
{
    public string userid = string.Empty;
    protected void Page_Load(object sender, EventArgs e)
    {
        HttpCookie GetCookie = Request.Cookies["cook_admin"];
        if (Request.Cookies["cook_admin"] == null)
        {
            Response.Redirect("index.aspx");
        }
        if (Request.QueryString["userid"] != null)
        {
            userid = Request.QueryString["userid"];
            if (!IsPostBack)
            {
                bindData();
                DataSet dss = DbHelperSQL.Query("select * from etw_admin where id=" + userid + "");
                if (dss.Tables[0].Rows.Count > 0)
                {
                    if (dss.Tables[0].Rows[0]["Purview"].ToString() != "" && dss.Tables[0].Rows[0]["Purview"].ToString() != null)
                    {
                        string viewstr = "," + dss.Tables[0].Rows[0]["Purview"].ToString() + ",";
                        for (int i = 0; i < CheckBoxList1.Items.Count; i++)
                        {
                            if (viewstr.IndexOf("," + CheckBoxList1.Items[i].Value + ",") >= 0)
                            {
                                CheckBoxList1.Items[i].Selected = true;
                            }
                        }
                    }
                }
            }
        }
        else
        {
            CheckBoxList1.Visible = false;
            Button1.Visible = false;
        }
        if (!IsPostBack)
        {
            DataSet ds = DbHelperSQL.Query("select * from etw_admin where roleid=2");
            if (ds.Tables[0].Rows.Count > 0)
            {
                Repeater1.DataSource = ds.Tables[0].DefaultView;
                Repeater1.DataBind();
            }
        }
    }
    protected void bindData()
    {
        DataSet ds = DbHelperSQL.Query("select * from etw_menu where id not in (select parentid from etw_menu) order by orderby ");
        if (ds.Tables[0].Rows.Count > 0)
        {
            CheckBoxList1.DataSource = ds.Tables[0].DefaultView;
            CheckBoxList1.DataTextField = "menuname";
            CheckBoxList1.DataValueField = "id";
            CheckBoxList1.DataBind();
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string listvalue = string.Empty;
        for (int i = 0; i < CheckBoxList1.Items.Count; i++)
        {
            if (CheckBoxList1.Items[i].Selected == true)
            {
                listvalue += CheckBoxList1.Items[i].Value + ",";
            }
        }
        if (listvalue != "")
        {
            listvalue = listvalue.Substring(0, listvalue.Length - 1);
        }
        etw.etw_admin lead = new etw.etw_admin();
        lead.id = Convert.ToInt32(userid);
        lead.Purview = listvalue;
        lead.Uppurview();
        Response.Write("<script language=\"javascript\">window.location='permissions.aspx'</" + "script>");
    }
}
