using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Text;
using DSWeb.Models;
using DSWeb.EntityDA;
using DSWeb.Filter;

namespace DSWeb.Shipping
{
    public partial class SeaeOrderEdit : System.Web.UI.Page
    {
        StringBuilder leftNav = new StringBuilder();
        StringBuilder leftTree = new StringBuilder();
        StringBuilder tmpLeftTree = new StringBuilder();
        private string strHandleType;
        private string strGID;
        private string strISAudit;

        protected void Page_Load(object sender, EventArgs e)
        {
            HttpRequest request = Page.Request;
            LeftTreeCssFilter myFilter = new LeftTreeCssFilter(request);

            if (Request.QueryString["handle"] != null)
            {
                strHandleType = Request.QueryString["handle"].Trim().ToString().ToLower();
                if (Request.QueryString["id"] != null)
                {
                    strGID = Request.QueryString["id"].ToString().Trim();
                }
                if (Request.QueryString["isAudit"] != null)
                {
                    strISAudit = Request.QueryString["isAudit"].ToString().Trim();
                }
            }
            DoGet();

        }
        /// <summary>
        /// 只显示用户设置
        /// </summary>
        /// <param name="moduleEntites"></param>
        /// <returns></returns>
        private List<ModuleEntity> ActionFilter(List<ModuleEntity> moduleEntites)
        {
            List<ModuleEntity> ActionFilter = new List<ModuleEntity>();
            foreach (ModuleEntity moduleEntity in moduleEntites)
            {
                if (moduleEntity.Gid == "0791bbcc-8079-49de-8175-6063b9826ac2")
                {
                    ActionFilter.Add(moduleEntity);
                }
            }

            return ActionFilter;
        }

        public void DoGet()
        {
            ModuleDA moduleDA = new ModuleDA();
            List<ModuleEntity> moduleEntites = ActionFilter(moduleDA.GetModuleByType(ModuleDA.ModuleType.SEAEXPORT));

            StringBuilder htmlBuilder = new StringBuilder();

            LeftTreeCssFilter cssFilter = new LeftTreeCssFilter(Page.Request);

            string strBVersion = cssFilter.Version.Trim();
            string strBName = cssFilter.BrowserName.ToLower().Trim();

            htmlBuilder.Append("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">");
            htmlBuilder.Append("<html><head><title>Manager</title><meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\">");
            //根据不同的浏览器加载不同的css样式
            if (strBVersion.Equals("6.0") && strBName.Equals("ie"))
            {
                htmlBuilder.Append("<link type=\"text/css\" rel=\"stylesheet\" href=\"../../theme/default/styles/default/globle.css\"/>");
                htmlBuilder.Append("<link type=\"text/css\" rel=\"stylesheet\" href=\"../../theme/default/styles/default/icons.css\"/>");
                htmlBuilder.Append("<link type=\"text/css\" rel=\"stylesheet\" href=\"../../theme/button/default/Fee.css\"/>");
            }
            else
            {
                htmlBuilder.Append("<link type=\"text/css\" rel=\"stylesheet\" href=\"../../theme/default/styles/IE8/globle.css\"/>");
                htmlBuilder.Append("<link type=\"text/css\" rel=\"stylesheet\" href=\"../../theme/default/styles/IE8/icons.css\"/>");
                htmlBuilder.Append("<link type=\"text/css\" rel=\"stylesheet\" href=\"../../theme/button/default/Fee.css\"/>");
            }
            htmlBuilder.Append("<link type=\"text/css\" rel=\"stylesheet\" href=\"../../treeview/thickbox.css\" />");
            htmlBuilder.Append("<link type=\"text/css\" rel=\"stylesheet\" href=\"../../styles/jquery.tree.css\"/>");
            htmlBuilder.Append("<style type=\"text/css\">");
            htmlBuilder.Append("#list { width:500px; margin:0px; padding:0px; font-size:14px;vertical-align:middle;}");
            htmlBuilder.Append("#list li { list-style:none; display:block; width:290px; background-color:#D9E8FB; padding:5px; margin:1px; height:20px;}");
            htmlBuilder.Append(".btn{width:95px;BORDER-RIGHT: #2C59AA 1px solid;PADDING-RIGHT: 2px; BORDER-TOP: #2C59AA 1px solid;PADDING-LEFT: 2px; FONT-SIZE: 12px; FILTER: progid:DXImageTransform.Microsoft.Gradient(GradientType=0, StartColorStr=#ffffff, EndColorStr=#C3DAF5);");
            htmlBuilder.Append("BORDER-LEFT: #2C59AA 1px solid; CURSOR: hand; COLOR: black; PADDING-TOP: 2px; BORDER-BOTTOM: #2C59AA 1px solid}</style>");

            htmlBuilder.Append("<script type=\"text/javascript\" src=\"../../js/jquery/js/jquery.js\"></script>");
            htmlBuilder.Append("<script type=\"text/javascript\" src=\"../../js/jquery/js/jquery.tree.js\"></script>");

            htmlBuilder.Append("<script type=\"text/javascript\">var $j = jQuery.noConflict();</script>");
            htmlBuilder.Append("<script type=\"text/javascript\" src=\"../../theme/default/editjs/util.js\"></script>");
            //htmlBuilder.Append("<script type=\"text/javascript\" src=\"../../treeview/MzTreeView12.js\"></script>");
            htmlBuilder.Append("<script type=\"text/javascript\" src=\"../../theme/default/editjs/loaderframework.js\"></script><script type=\"text/javascript\" src=\"../../js/showdialog.js\"></script>");
            htmlBuilder.Append("<script src=\"../js/dhtmlxcommon.js\" type=\"text/javascript\"></script>");

            htmlBuilder.Append("</head><body style=\"background-color:#ecf2ff\">");
            htmlBuilder.Append("<table width=\"100%\" height=\"100%\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\" >");
            htmlBuilder.Append("<tr><td height=\"60\" colspan=\"3\" valign=\"top\">");
            htmlBuilder.Append("<table width=\"100%\" height=\"70\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\">");//
            //htmlBuilder.Append("<tr><td width=\"180\"></td></tr>");
            htmlBuilder.Append("<tr><td width=\"180\" colspan=\"2\" valign=\"top\">");// align=\"center\" 

            //htmlBuilder.Append("<div class=\"btn-content\"><div class=\"btn-blank\"></div><div class=\"xbtn-img\" onclick=\"addPayRow()\">");
            //htmlBuilder.Append("<div class=\"btnimg\"><img src=\"../../images/icons/save.gif\" width=\"16px\" height=\"16px\" title=\"添加应收费用\"/></div></div></div>");//<div style=\"width:150px\" onclick=\"openNewDiv('dvAmend')\">添加费用更改单</div><input type=\"hidden\" id=\"h_id\" value=\""+strGID+"\" />

            htmlBuilder.Append("</td>");
            htmlBuilder.Append("<td align=\"right\" valign=\"top\" height=\"100%\">");
            htmlBuilder.Append("<table width=\"100%\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\">");
            htmlBuilder.Append("<tr><td height=\"37\" valign=\"middle\">");
            htmlBuilder.Append("<table width=\"100%\" border=\"0\" height=\"37\" cellpadding=\"0\" cellspacing=\"0\"><tr>");
            htmlBuilder.Append("<td align=\"right\" valign=\"bottom\">&nbsp;");
            htmlBuilder.Append("<div class=\"HTitle\" style=\"position:absolute;right:0px;top:18px;width:100px;\">");
            htmlBuilder.Append("<a href=\"../SignOn/Close.aspx\">[退出]</a>&nbsp;&nbsp;&nbsp;&nbsp;</div></td></tr></table></td></tr>");
            htmlBuilder.Append("</td></tr>");

            htmlBuilder.Append("<tr><td align=\"left\" height=\"23\"><div id=\"dvTab\"><ul id=\"tags\"></ul></div></td></tr></table>");
            htmlBuilder.Append("</td></tr></table></td></tr>");
            htmlBuilder.Append("<tr><td class=\"space\" colspan=\"3\" valign=\"top\"></td></tr>");

            htmlBuilder.Append("<tr><td width=\"180px\" valign=\"top\"><ul id=\"toolsBig\" style=\"display:\" class=\"bigbtn\">");
            htmlBuilder.Append("<li id=\"btnRec\"><a id = \"ExchangeLab\" href=\"javascript:void(0);\" title=\"\"><b></b></a></li>");

            htmlBuilder.Append("</ul></li></ul>");
            htmlBuilder.Append("<div id=\"tree\"></div><ul id=\"nav\">");
            string strValue = GetTreeView(moduleEntites);
            htmlBuilder.Append(leftNav.ToString());
            htmlBuilder.Append("</ul></td><td width=\"4px\" valign=\"top\" bgcolor=\"#D7EDF4\"></td>");
            htmlBuilder.Append("<td valign=\"top\" align=\"left\" id=\"tdContent\"><div id=\"dvContent\"></div></td></tr></table>");
            htmlBuilder.Append(strValue);

            if (strHandleType != null)
            {
                if (strHandleType.Equals("edit") && !strGID.Equals(""))
                {
                    htmlBuilder.Append("<script type=\"text/javascript\">CreateTab(\"  首页  \",'H','shipping/SeaeOrderInfo.aspx?handle=" + strHandleType + "&isAudit=" + strISAudit + "&id=" + strGID + "');</script></body></html>");
                }
                else
                {
                    htmlBuilder.Append("<script type=\"text/javascript\">CreateTab(\"  首页  \",'H','shipping/SeaeOrderInfo.aspx?handle=add&isAudit=" + strISAudit + "');</script></body></html>");
                }
            }
            else
            {
                Response.End();
            }
            Response.Clear();
            Response.Write(htmlBuilder.ToString());
        }

        /// <summary>
        /// 获取左侧展开树内容
        /// </summary>
        /// <param name="tempEntities">实体类ModuleEntity<List></param>
        /// <returns></returns>
        public String GetTreeView(List<ModuleEntity> tempEntities)
        {
            //StringBuilder leftNav = new StringBuilder();
            StringBuilder leftTree = new StringBuilder();
            StringBuilder tmpLeftTree = new StringBuilder();

            //树型菜单第一级菜单
            StringBuilder leftTreeData = new StringBuilder();
            StringBuilder treesJS = new StringBuilder();

            foreach (ModuleEntity mdel in tempEntities)
            {
                leftNav.Append("<li id=\"nav" + mdel.Gid + "\"");
                leftNav.Append(" title=\"" + mdel.ModuleDescription + "\"");
                leftNav.Append(" onclick=\"selectmainMenu(this);\">");
                leftNav.Append("<img src=\"../../images/icons/" + mdel.ModuleIcon + "\" align=\"absmiddle\" />&nbsp;");
                leftNav.Append(mdel.ModuleDescription);
                leftNav.Append("</li>");

                tmpLeftTree = new StringBuilder();// class=\"TreeMenu\"
                tmpLeftTree.Append("<div class=\"TreeMenu\" style=\"width:178px;height:300px;background:#FFFFEE;overflow-y:auto;overflow-x:auto\" id=\"tree" + mdel.Gid + "\">");
                tmpLeftTree.Append("</div>");

                leftTreeData.Append(LeftTreeNodeData(mdel));
                leftTree.Append(tmpLeftTree.ToString());

            }
            return leftTreeData.ToString();
        }

        public String ConvertToJS(JSTreeNodeEntity tempJSTreeEntity)
        {
            StringBuilder nodeJS = new StringBuilder();
            nodeJS.Append("{");
            nodeJS.Append("\"id\":\"root" + tempJSTreeEntity.Gid + "\"");
            nodeJS.Append(",");
            nodeJS.Append("\"text\":\"" + tempJSTreeEntity.Text + "\"");
            nodeJS.Append(",");
            nodeJS.Append("\"value\":\"" + tempJSTreeEntity.Value + "\"");
            nodeJS.Append(",");
            nodeJS.Append("\"showcheck\":" + (tempJSTreeEntity.SetShowCheck ? 1 : 0).ToString() + "");
            nodeJS.Append(",");
            nodeJS.Append("\"complete\":1");
            nodeJS.Append(",");
            nodeJS.Append("\"isexpand\":1"); //+ (tempJSTreeEntity.IsExpand ? 1 : 0).ToString() + "");
            nodeJS.Append(",");
            nodeJS.Append("\"checkstate\":" + tempJSTreeEntity.CheckState + "");
            nodeJS.Append(",");
            nodeJS.Append("\"hasChildren\":" + (tempJSTreeEntity.SetHasChild ? 1 : 0).ToString() + "");
            //nodeJS.Append("\"hasChildren\":\"1\"");
            nodeJS.Append(",");
            string classes = tempJSTreeEntity.SetClasses;
            if (classes != null && !classes.Equals(""))
            {
                int ilen = classes.IndexOf('.');
                classes = classes.Substring(0, ilen);
                if (classes.Length > 1)
                {
                    nodeJS.Append("\"classes\":\"" + classes + "\"");
                    nodeJS.Append(",");
                }
            }
            //nodeJS.Append("\"ChildNodes\":\"null\"");//root13D3C23DA46E49E5B0C4FB446534244909=
            //string strTemp = "[{\"id\":\"root13D3C23DA46E49E5B0C4FB446534244777\",\"text\":\"系统流程\",\"value\":\"CreateTab('系统流程','H','../users/userinfo.aspx');\","
            //               + "\"showcheck\":0,\"complete\":1,\"isexpand\":0,\"checkstate\":0,\"hasChildren\":0,\"classes\":\"fax\",\"ChildNodes\":\"null\"}]";
            //nodeJS.Append("\"ChildNodes\":" + strTemp);
            nodeJS.Append("\"ChildNodes\":\"null\"");
            //nodeJS.Append("\"ChildNodes\":\"[root399E191892584C938D5D7582027FA391]\"");

            nodeJS.Append("}");
            return nodeJS.ToString();
        }

        /// <summary>
        /// 遍历模块查看此模块是否为父模块
        /// </summary>
        /// <param name="tempEntities"></param>
        /// <param name="strGID"></param>
        /// <returns></returns>
        public bool IsParent(List<ModuleEntity> tempEntities, string strGID)
        {
            bool IsExist = false;
            foreach (ModuleEntity mdle in tempEntities)
            {
                if (mdle.ParentID.Trim().Equals(strGID))
                {
                    IsExist = true;
                }
            }
            return IsExist;
        }

        public String LeftTreeNodeData(ModuleEntity tempEntity)
        {
            StringBuilder appTreeData = new StringBuilder();
            StringBuilder childerData = new StringBuilder();

            ModuleDA moduleDA = new ModuleDA();
            //List<ModuleEntity> subModule = moduleDA.GetSubModule(tempEntity.Gid);
            //List<ModuleEntity> tempEntities = moduleDA.GetAllUserBaseSubModules();
            List<ModuleEntity> subModule = moduleDA.GetSubModule(tempEntity.Gid);
            List<ModuleEntity> tempEntities = moduleDA.GetAllSubModules();

            appTreeData.Append("<script type=\"text/javascript\">");
            String strAppModelGid = "root" + tempEntity.Gid.Replace("-", "");

            JSTreeNodeEntity rootData = new JSTreeNodeEntity();
            rootData.Gid = strAppModelGid;
            rootData.Value = tempEntity.Gid;
            rootData.Text = tempEntity.ModuleDescription;
            rootData.SetShowCheck = false;
            rootData.CheckState = 0;
            rootData.IsExpand = true;
            rootData.SetChildNode = -1;
            rootData.SetHasChild = true;

            StringBuilder resultBuilder = new StringBuilder();
            StringBuilder tempBuilder = new StringBuilder();
            string getStr = GetSubJs(tempEntity.Gid, tempEntities, "");

            if (getStr.Length > 1)
            {
                resultBuilder.Append(getStr);
            }
            else
            {
                //如果模板为主模板,自动生成相关同名子模板
                JSTreeNodeEntity modelData = new JSTreeNodeEntity();

                modelData.Gid = tempEntity.Gid.Replace("-", "");
                modelData.Text = tempEntity.ModuleDescription;
                modelData.SetShowCheck = false;
                modelData.CheckState = 0;
                modelData.IsExpand = false;
                modelData.SetChildNode = 0;
                modelData.SetClasses = tempEntity.ModuleIcon;

                String tmpUrl = tempEntity.ModuleUrl.Trim();

                if (!tmpUrl.Equals("#"))
                {
                    tmpUrl = "../" + tmpUrl;
                    modelData.Value = "CreateTab('" + tempEntity.ModuleDescription + "','H','" + tmpUrl + "');";
                    
                    modelData.SetHasChild = true;

                }
                else
                {

                    modelData.Value = "javascript:void(0);";
                    modelData.SetHasChild = true;
                }
                resultBuilder.Append(ConvertToJS(modelData) + ",");

            }

            String strchilderData = resultBuilder.ToString();

            if (strchilderData.Length > 1 && strchilderData.EndsWith(","))
            {
                strchilderData = strchilderData.Substring(0, strchilderData.Length - 1);
            }
            else
            {
                strchilderData = strchilderData.Substring(0, strchilderData.Length);
            }
            appTreeData.Append("" + strAppModelGid + "=[" + strchilderData + "];");
            appTreeData.Append("</script>");

            return appTreeData.ToString();
        }

        /// <summary>
        /// 返回
        /// </summary>
        /// <param name="parentGid"></param>
        /// <param name="tempEntities"></param>
        /// <returns></returns>
        public String GetSubJs(string parentGid, List<ModuleEntity> tempEntities, string tempJSON)
        {
            StringBuilder valBuilder = new StringBuilder();
            StringBuilder childBuilder = new StringBuilder();
            foreach (ModuleEntity mdle in tempEntities)
            {
                if (parentGid.Trim().Equals(mdle.ParentID))
                {
                    bool isParentNode = IsParent(tempEntities, mdle.Gid);

                    string strTempNode = GetNodeJs(mdle, isParentNode);
                    string strTempValue = "";

                    if (isParentNode)
                    {
                        strTempValue = GetSubJs(mdle.Gid, tempEntities, strTempNode);
                        childBuilder.Append(strTempValue);
                    }
                    if (strTempValue.Trim().Equals(""))
                    {
                        childBuilder.Append(strTempNode);
                    }
                }
            }

            if (childBuilder.Length > 1)
            {
                string strChilds = childBuilder.ToString();
                strChilds = strChilds.Substring(0, strChilds.Length - 1);//去除末尾逗号
                if (!tempJSON.Equals(""))
                {
                    tempJSON = tempJSON.Replace("\"null\"", "[" + strChilds + "]");
                    valBuilder.Append(tempJSON);
                }
                else
                {
                    valBuilder.Append(strChilds);
                }
            }
            return valBuilder.ToString();
        }

        #region 获取节点JSON值
        /// <summary>
        /// 获取节点JSON值
        /// </summary>
        /// <param name="tempEntity"></param>
        /// <param name="hasChild"></param>
        /// <returns></returns>
        public String GetNodeJs(ModuleEntity tempEntity, bool hasChild)
        {
            StringBuilder tempNodeBuilder = new StringBuilder();
            JSTreeNodeEntity modelData = new JSTreeNodeEntity();

            modelData.Gid = tempEntity.Gid.Replace("-", "");
            modelData.Text = tempEntity.ModuleDescription;
            modelData.SetShowCheck = false;
            modelData.CheckState = 0;
            modelData.IsExpand = false;

            modelData.SetClasses = tempEntity.ModuleIcon;

            String tmpUrl = tempEntity.ModuleUrl.Trim();
            //如果数据库模板表Module字段Url设置为"#"则不提供链接
            if (!tmpUrl.Equals("#"))
            {
                tmpUrl = "../" + tmpUrl;
                //modelData.Value = "CreateTab('" + tempEntity.ModuleDescription + "','H','" + tmpUrl + "?handle=edit&id=" + strGID + "');";
                modelData.Value = "CreateTab('" + tempEntity.ModuleDescription + "','H','" + tmpUrl + "?handle=edit&isAudit=" + strISAudit + "&id=" + strGID + "');";
            }
            else
            {
                modelData.Value = "javascript:void(0);";
            }
            //设置是否有子菜单
            if (hasChild)
            {
                modelData.SetChildNode = 0;
                modelData.SetHasChild = true;
            }
            else
            {
                modelData.SetChildNode = 0;
                modelData.SetHasChild = false;
            }
            //recording++;
            // Response.Write(ConvertToJS(modelData) + ",");

            return (ConvertToJS(modelData) + ",");
        }
        #endregion
    }
}