using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using xabaraCOM;

public partial class webXabara_adminUser_userPopedom : System.Web.UI.Page
{
    admin a = new admin();
    sqlServer sql = new sqlServer();
    javasrcipt js = new javasrcipt();
    classify c = new classify();

    protected void Page_Load(object sender, EventArgs e)
    {
        a.check("|admins|");

        if (!IsPostBack)
        {
            this.newsTree.Attributes.Add("onclick", "OnTreeNodeChecked()");
            this.helpTree.Attributes.Add("onclick", "OnTreeNodeChecked()");

            string strSqlread = "select UserID,newsIDs,helpIDs,UserName,UserAdmin from AdminUser where userID='" + xabara.safeSql(Request.QueryString["id"].Trim()) + "'";
            DataTable dtRead = sql.getDataTable(strSqlread);
            if (dtRead.Rows.Count > 0 && xabara.safeSql(Request.QueryString["id"].Trim()) != a.uid)
            {
                if (dtRead.Rows[0]["UserAdmin"].ToString().Trim().Equals("xabaraCOM"))
                {
                    js.div(false, "", "text", dtRead.Rows[0]["UserID"].ToString().Trim() + " 已是超级管理员，不需设置相应权限！", 380, 0, "userList.aspx?page=" + Request.QueryString["page"], "goto");
                }
                else
                {
                    string userAdminStr = dtRead.Rows[0]["UserAdmin"].ToString();
                    string newIDs = dtRead.Rows[0]["newsIDs"].ToString();
                    string helpIDs = dtRead.Rows[0]["helpIDs"].ToString();

                    uid.Text = dtRead.Rows[0]["UserName"].ToString() + "[" + dtRead.Rows[0]["UserID"].ToString() + "]";
                    DataTable dt;

                    dt = sql.getDataTable("select * from classAdmin where isFlag=1 order by listID ASC ");
                    if (dt.Rows.Count > 0)
                    {
                        string ifType = string.Empty;
                        string bgColor = string.Empty;
                        selectPopedomHtm.Text = "<table border='0' align='left' class='tableAdminLine'><tr>";
                        for (int p = 0; p < dt.Rows.Count; p++)
                        {
                            if (!ifType.Equals(dt.Rows[p]["listClass"].ToString().Trim()) && p > 0)  //控制回车
                            {
                                selectPopedomHtm.Text += "</tr><tr>";
                            }
                            ifType = dt.Rows[p]["listClass"].ToString().Trim();

                            string selectValue = dt.Rows[p]["classCode"].ToString().Trim();
                            string selectBool = string.Empty;
                            if (userAdminStr.IndexOf(@"|" + selectValue + "|") >= 0)
                            {
                                selectBool = @" checked='checked'";
                            }

                            selectPopedomHtm.Text += "<td style='background-color:#f5fdfe;'><input name='adminPopedomSelect' type='checkbox' id='adminPopedomSelect' value='" + selectValue + "'" + selectBool + " />" + dt.Rows[p]["className"].ToString().Trim() + "</td>";

                        }
                        selectPopedomHtm.Text += "</tr></table>";
                    }
                    dt.Dispose();

                    c.bindingTreeList(0, "news", newsTree, "", "", true, true, newIDs);
                    c.bindingTreeList(0, "help", helpTree, "", "", true, true, helpIDs);
                }
            }
            else
            {
                js.div(false, "", "text", "您的操作有误！", 300, 0, "userList.aspx", "goto");
            }
            dtRead.Dispose();
        }
    }

    protected void setAdmin_Click(object sender, EventArgs e)
    {
        string selectValue = Request["adminPopedomSelect"];
        if (!string.IsNullOrEmpty(selectValue))
        {
            selectValue = selectValue.Replace(",", "|");
            string[] strValue = new string[5];
            strValue[0] = "|" + selectValue + "|";
            strValue[1] = "2";
            strValue[2] = string.Empty;
            strValue[3] = string.Empty;

            c.readTreeSelect(newsTree);
            if (!string.IsNullOrEmpty(Session["treeSelectValue"] as string))
            {
                strValue[2] = "|" + Session["treeSelectValue"];
            }
            c.readTreeSelect(helpTree);
            if (!string.IsNullOrEmpty(Session["treeSelectValue"] as string))
            {
                strValue[3] = "|" + Session["treeSelectValue"];
            }

            string dbStr = sql.update("AdminUser", "@UserAdmin,@loginFlag,@newsIDs,@helpIDs", strValue, "userID='" + xabara.safeSql(Request["id"].Trim()) + "'");

            sql.log(a.uid, "admin", "修改管理员权限");

            js.div(false, "设置提示", "text", dbStr, 300, 0, "userList.aspx?page=" + Request.QueryString["page"], "goto");
        }
        else
        {
            js.div(false, "", "text", "请选择相应权限后再设置！", 300, 150, "", "");
        }
    }
}
