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 Administration_AddUserWebAuthority : System.Web.UI.Page {
    //the function code for localization on this page
    protected override void InitializeCulture() {
        ProfileCommon p = (ProfileCommon)this.Context.Profile;

        if (Request.Form.Get("ctl00$selLanguage") != null)
            p.lang = Request.Form.Get("ctl00$selLanguage").ToString();
        if (p.lang == String.Empty)
            p.lang = Session["Localization"].ToString();

        if (p.lang.Length > 0) {
            this.Culture = p.lang;
            this.UICulture = System.Globalization.CultureInfo.CreateSpecificCulture(p.lang).Name;
        }
    }
    //select theme
    protected void Page_PreInit(object sender, EventArgs e) {
        Page.Theme = Profile.UI.Theme;

        // 沒有權限
        if (!SES_Common.Authorize(Request.Url.ToString(), Profile.UserName)) {
            Response.Redirect("/SEST/Common/NoAuthorization.aspx");
        }
    }
    
    protected void Page_Load(object sender, EventArgs e) {
    }
    protected void Page_PreRender(object sender, EventArgs e) {
    }
    protected void TreeView1_DataBound(object sender, EventArgs e) {
        TreeView1.CollapseAll();

        Expand(TreeView1.Nodes[0], 2);      // expand tree view ( level )
    }
    private void Expand(TreeNode tn, int Layer) {
        tn.Expand();
        if (Layer > 1) {
            for (int i = 0; i < tn.ChildNodes.Count; i++) {
                Expand(tn.ChildNodes[i], Layer-1);
            }
        }
    }
    
    protected void btnApply_Click(object sender, EventArgs e) {
        ArrayList arlChedkedNode = new ArrayList();
        addCheckedItem( TreeView1.Nodes[0], ref arlChedkedNode);

        // 依所選之套用人員與套用路徑依序新增至資料庫
        foreach (GridViewRow gvr in GridView1.Rows) { 
            if (((CheckBox)gvr.FindControl("chkApply")).Checked) {
                string sAccount = ((Label)gvr.FindControl("lblAccount")).Text; // Account
                for (int i = 0; i < arlChedkedNode.Count; i++) {
                    new WebAuthorityData().Insert(sAccount, "User", arlChedkedNode[i].ToString().Replace("/#", "") );
                }
            }
        }
    }
    // 取得所選取的節點
    private void addCheckedItem(TreeNode tn, ref ArrayList arlChedkedNode) {
        // 根目錄已選擇 (擁有全部功能)
        if ( tn.Parent == null && tn.Checked){
            arlChedkedNode.Add("DEFAULT.ASPX");
            return;
        }

        //// 有子節點的處理
        //if (tn.ChildNodes.Count > 0) {
            foreach (TreeNode tnchild in tn.ChildNodes) {
                if (tnchild.Checked) {
                    // 有子節點同時該節點有連結網頁時, 將該連結網頁排除
                    if (tnchild.ChildNodes.Count > 0 && (tnchild.NavigateUrl.ToLower().EndsWith(".aspx") 
                        || tnchild.NavigateUrl.ToLower().EndsWith("#")) ) {
                        arlChedkedNode.Add(
                            tnchild.NavigateUrl.Substring(0, tnchild.NavigateUrl.LastIndexOf("/")));
                    }
                    else {
                        arlChedkedNode.Add(tnchild.NavigateUrl);
                    }
                }
                else {
                    addCheckedItem(tnchild, ref arlChedkedNode);
                }
            }
        //}
        //else { //葉節點
        //    if (tn.Checked) { 
        //        arlChedkedNode.Add( tn.NavigateUrl);
        //    }
        //}
    }

    protected void btnDelete_Click(object sender, EventArgs e) {
        // 依所選之套用人員與套用路徑依序新增至資料庫
        foreach (GridViewRow gvr in GridView1.Rows) {
            if (((CheckBox)gvr.Cells[0].FindControl("chkApply")).Checked) {
                string sAccount = ((Label)gvr.Cells[2].FindControl("lblAccount")).Text; // Account
                new WebAuthorityData().Delete( sAccount);
            }
        }
    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) {
        if (e.Row.FindControl("lblAccount") != null) {
            if (((Label)e.Row.FindControl("lblAccount")).Text == "")
                ((CheckBox)e.Row.FindControl("chkApply")).Enabled = false;
        }
    }
}
