using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Diagnostics;
using System.Text;
using EKETEAM.FrameWork;
using EKETEAM.Data;


public class ApplicationMenu
{
    private eUser _user;
    private eUser User
    {
        get
        {
            return _user;
        }
    }
    private string _modelid = "";
    public string ModelID
    {
        get
        {
            return _modelid;
        }
    }
    private string _applicationid = "";
    public string ApplicationID
    {
        get
        {
            if (_applicationid.Length == 0 && Applications.Rows.Count > 0)
            {
                DataRow[] rs = Applications.Select("", "px,addTime");
                if (rs.Length > 0) _applicationid = rs[0]["ApplicationID"].ToString();
            }
            return _applicationid;
        }
    }
    private string _appitem = "";
    public string AppItem
    {
        get
        {
            return _appitem;
        }
    }
    public string ApplicationItemID
    {
        get
        {
            return AppItem;
        }
    }
    private object _lockMe = new object();
    private string _apptype;// = "1";//PC
    public string AppType
    {
        get
        {
            if (_apptype == null)
            {
                _apptype = !eBase.IsMobile() ? "1" : "2";
            }
            return _apptype;
        }
    }
    private string _homeurl;
    public string HomeURL
    {
        get
        {
            if (_homeurl == null)
            {
                if (HttpContext.Current.Items.Contains("homeurl"))
                {
                    _homeurl = HttpContext.Current.Items["homeurl"].ToString();
                    return _homeurl;
                }
                /*
                DataRow[] rs = (from row in AppItems.AsEnumerable()
                                where row["ApplicationID"].ToString() == ApplicationID && row["ParentID"] == DBNull.Value && MenuIDS.Contains(row["ApplicationItemID"].ToString())
                                orderby row["px"]
                                select row).Take(1).ToArray();
                if (rs.Length == 0)
                {
                    rs = (from row in AppItems.AsEnumerable()
                          where row["ApplicationID"].ToString() == ApplicationID && MenuIDS.Contains(row["ApplicationItemID"].ToString())
                          orderby row["px"]
                          select row).Take(1).ToArray();
                }
                */
                //if (rs.Length == 0) return "javascript:;";
                //return rs[0]["href"].ToString();
                return "javascript:;";
            }
            return _homeurl;
        }
    }    
    public DataRow getTopModel(string appid)
    {
        DataRow dr = new DataTable().NewRow();
        DataRow[] rs;
        if (_apptype == "1")
        {
            rs = (from row in AllApplicationItems.AsEnumerable()
                  where row["ApplicationID"].ToString() == appid && row["ParentID"] == DBNull.Value && row["Show"].ToString().ToLower() == "true" && row["ModelID"] != DBNull.Value && MenuIDS.Contains(row["ApplicationItemID"].ToString())
                  orderby row["px"]
                  select row).Take(1).ToArray();
        }
        else
        {
            rs = (from row in AllApplicationItems.AsEnumerable()
                  where row["ApplicationID"].ToString() == appid && row["ParentID"] == DBNull.Value && row["mShow"].ToString().ToLower() == "true" && row["ModelID"] != DBNull.Value && MenuIDS.Contains(row["ApplicationItemID"].ToString())
                  orderby row["px"]
                  select row).Take(1).ToArray();
        }
        if (rs.Length == 0)
        {
            if (_apptype == "1")
            {
                rs = (from row in AllApplicationItems.AsEnumerable()
                      where row["ApplicationID"].ToString() == appid && row["ModelID"] != DBNull.Value && row["Show"].ToString().ToLower() == "true" && MenuIDS.Contains(row["ApplicationItemID"].ToString())
                      orderby row["px"]
                      select row).Take(1).ToArray();
            }
            else
            {
                rs = (from row in AllApplicationItems.AsEnumerable()
                      where row["ApplicationID"].ToString() == appid && row["ModelID"] != DBNull.Value && row["mShow"].ToString().ToLower() == "true" && MenuIDS.Contains(row["ApplicationItemID"].ToString())
                      orderby row["px"]
                      select row).Take(1).ToArray();
            }
        }
        if (rs.Length > 0) dr = rs[0];
        return dr;
       
    }
    public string getTopModelUrl(string appid)
    {
        DataRow row = getTopModel(appid);
        if (row.Table.Columns.Count == 0) return "javascript:;";
        return row["href"].ToString() + (HttpContext.Current.Request.QueryString["debug"] != null ? "&debug=1" : "");
    }
    private void addMenuIDS(List<string> list, string appitem)
    {
        DataRow[] rows = AllApplicationItems.Select("ApplicationItemID='" + appitem + "'");
        if (rows.Length > 0)
        {
            if (!list.Contains(appitem)) list.Add(appitem);

            string ParentID = rows[0]["ParentID"].ToString();
            if (ParentID.Length > 0)
            {
                addMenuIDS(list, ParentID);
            }
        }
    }
    private List<string> _curmenuids;
    public List<string> curMenuIDS
    {
        get
        {
            if (_curmenuids == null)
            {
                _curmenuids = new List<string>();
                getCurMenuIDS(_curmenuids, AppItem);
            }
            return _curmenuids;
        }
    }
    private string _menuid;
    public string MenuID
    {
        get
        {
            if (_menuid == null)
            {
                _menuid = AppItem;
                if (AppItem.Length > 0)
                {
                    DataRow[] rows = AllApplicationItems.Select("ApplicationItemID='" + AppItem + "'");
                    if (rows.Length > 0)
                    {
                        if (rows[0]["PackID"].ToString().Length > 0) _menuid = rows[0]["PackID"].ToString();
                    }
                }
            }
            return _menuid;
        }
    }
    private void getCurMenuIDS(List<string> list, string appitem)
    {
        if (appitem.Length == 0) return;
        DataRow[] rows = AllApplicationItems.Select("ApplicationItemID='" + appitem + "'");
        if (rows.Length == 0) return;
        if (rows[0]["PackID"].ToString().Length > 0) appitem = rows[0]["PackID"].ToString();
        list.Add(appitem);
        string pid = rows[0]["ParentID"].ToString();
        if (pid.Length > 0) getCurMenuIDS(list, pid);
    }
    private void updateHref(DataRow dr)
    {
        string url = "";
        bool auto = true;
        DataRow[] rs = eBase.a_eke_sysModels.Select("ModelID='"+ dr["ModelID"].ToString() +"'");
        if (rs.Length > 0) auto = eBase.parseBool(rs[0]["auto"].ToString());
        if (dr["Finsh"].ToString() == "False")
        {
            dr["href"] = "Model.aspx?AppItem=" + dr["ApplicationItemID"].ToString() + "&act=building";
            dr["mhref"] = "Model.aspx?AppItem=" + dr["ApplicationItemID"].ToString() + "&act=building";
        }
        else
        {
            if (dr["URL"].ToString().Length > 0)
            {
                url = dr["URL"].ToString();
                url = eParameters.Replace(url, null, User);
                url = url.Replace("{year}", DateTime.Now.Year.ToString());
                url = url.Replace("{month}", DateTime.Now.Month.ToString());
                url = url.Replace("{day}", DateTime.Now.Day.ToString());
                if (!url.ToLower().StartsWith("http") && url.ToLower().IndexOf("appitem=") == -1)
                {
                    url += (url.IndexOf("?") == -1 ? "?" : "&") + "AppItem=" + dr["ApplicationItemID"].ToString();
                }
                dr["href"] = url;
            }
            else
            {

                string file = "Model.aspx";

                //if (!eBase.parseBool(dr["Auto"]))
                if(!auto)
                {
                    file = "Custom.aspx";
                    if (dr["AutoLayout"].ToString() == "2")
                    {
                        file = dr["AspxFile"].ToString();
                        if (file.StartsWith("/"))
                        {
                            file = eBase.getAbsolutePath() + file.Substring(1);
                        }
                    }
                }
                url = file + "?AppItem=" + dr["ApplicationItemID"].ToString();
                dr["href"] = url;
            }
            if (!dr.Contains("murl")) return;
            if (dr["mURL"].ToString().Length > 0)
            {
                url = dr["mURL"].ToString();
                url = eParameters.Replace(url, null, User);
                url = url.Replace("{year}", DateTime.Now.Year.ToString());
                url = url.Replace("{month}", DateTime.Now.Month.ToString());
                url = url.Replace("{day}", DateTime.Now.Day.ToString());
                if (!url.ToLower().StartsWith("http") && url.ToLower().IndexOf("appitem=") == -1)
                {
                    url += (url.IndexOf("?") == -1 ? "?" : "&") + "AppItem=" + dr["ApplicationItemID"].ToString();
                }
                dr["mhref"] = url;
            }
            else
            {

                string file = "Model.aspx";
                //if (!eBase.parseBool(dr["Auto"]))
                if (!auto)
                {
                    file = "Custom.aspx";
                    if (dr["mAutoLayout"].ToString() == "2")
                    {
                        file = dr["mAspxFile"].ToString().Length > 0 ? dr["mAspxFile"].ToString() : dr["AspxFile"].ToString();
                        if (file.StartsWith("/"))
                        {
                            file = eBase.getAbsolutePath() + file.Substring(1);
                        }
                    }
                }
                url = file + "?AppItem=" + dr["ApplicationItemID"].ToString();
                dr["mhref"] = url;
            }
        }
    }
    private DataTable _applications;
    public DataTable Applications
    {
        get
        {
            if (_applications == null)
            {
                _applications = eBase.a_eke_sysApplications.Copy();
                
                _applications.Columns.Add("href", typeof(string));
                _applications.Columns.Add("DoMain", typeof(string)); //使用域名


                _applications.Columns.Add("ApplicationItemID", typeof(string));//AppFrame ApplicationItemID
                _applications.Columns.Add("ModelName", typeof(string));//AppFrame名称
               
                DataRow[] rs;
                #region 删除没有权限的应用
                for (int i = _applications.Rows.Count - 1; i >= 0; i--)
                {
                    rs = eBase.a_eke_sysUserPowers.Select("UserID='" + User.ID + "' and ApplicationID='" + _applications.Rows[i]["ApplicationID"].ToString() + "'");
                    if (rs.Length == 0)
                    {
                        _applications.Rows.Remove(_applications.Rows[i]);                        
                    }
                }
                #endregion
                #region 企业未授权的应用
                for (int i = _applications.Rows.Count - 1; i >= 0; i--)
                {
                    rs = eBase.a_eke_sysSiteItems.Select("SiteID='" + User["SiteID"].ToString() + "' and ApplicationID='" + _applications.Rows[i]["ApplicationID"].ToString() + "'");
                    if (rs.Length == 0)
                    {
                        _applications.Rows.Remove(_applications.Rows[i]);
                    }
                    //if (!SiteApps.Contains(_applications.Rows[i]["ApplicationID"].ToString())) _applications.Rows.Remove(_applications.Rows[i]);
                }
                #endregion
                foreach (DataRow dr in _applications.Rows)
                {
                    if (dr["pic"].ToString().Length > 0) dr["pic"] = "../" + dr["pic"].ToString();
                    string _name = _apptype == "1" ? "homeUrl" : "mhomeUrl";
                    if (dr.Contains(_name) && dr[_name].ToString().Length > 0)
                    {
                        string cusurl = dr[_name].ToString();
                        string _appitem = cusurl.getQuery("appitem");
                        if (MenuIDS.Contains(_appitem))
                        {
                            dr["href"] = cusurl;
                            DataRow[] _rs3 = AllApplicationItems.Select("href='" + cusurl + "'");
                            if (_rs3.Length > 0)
                            {
                                dr["ApplicationItemID"] = _appitem;
                                dr["ModelName"] = _rs3[0]["ModelName"].ToString();
                            }
                        }
                    }
                    if (dr["href"].ToString().Length == 0)
                    {
                        dr["href"] = getTopModelUrl(dr["ApplicationID"].ToString());
                        DataRow row = getTopModel(dr["ApplicationID"].ToString());
                        if (row.Table.Columns.Count > 0)
                        {
                            dr["ModelName"] = row["MC"].ToString();
                            dr["ApplicationItemID"] = row["ApplicationItemID"].ToString();
                        }
                    }
                    #region 更新应用授权的域名
                    if (User["SiteID"].ToString().Length > 0)
                    {
                        if (eBase.a_eke_sysSiteItems.Rows.Count > 0)
                        {
                            rs = eBase.a_eke_sysSiteItems.Select("SiteID='" + User["SiteID"].ToString() + "' and ApplicationID='" + dr["ApplicationID"].ToString() + "'");
                            if (rs.Length > 0)
                            {
                                if (rs[0]["DoMain"].ToString().Length > 0) dr["DoMain"] = rs[0]["DoMain"].ToString();
                            }
                        }
                    }
                    #endregion
                }
               
                #region 域名过滤
                string domain = HttpContext.Current.Request.Url.Host;
                if (_applications.Columns.Contains("ApplicationID"))
                {
                    _applications = _applications.Select("(Type=3 or Type=" + _apptype + ") and (len(isnull(domain,'')) = 0 or domain='" + domain + "')", "PX,addTime").toDataTable();
                }
                #endregion
                //eBase.PrintDataTable(_applications);
            }
            return _applications;
        }
    }   
    private DataTable _allapplicationitems;
    /// <summary>
    /// 所有应用项
    /// </summary>
    public DataTable AllApplicationItems
    {
        get
        {
            string cacheName = "DataCache_Tables_AllApplicationItems";
            _allapplicationitems = (DataTable)HttpRuntime.Cache.Get(cacheName);
            if (_allapplicationitems == null)
            {
                _allapplicationitems = eBase.a_eke_sysApplicationItems.Copy();
                _allapplicationitems.Columns.Add("href", typeof(string));
                _allapplicationitems.Columns.Add("mhref", typeof(string));
                _allapplicationitems.Columns.Add("ModelName", typeof(string));
                _allapplicationitems.Columns.Add("auto", typeof(string));
                _allapplicationitems.Columns.Add("AutoLayout", typeof(int));
                _allapplicationitems.Columns.Add("mAutoLayout", typeof(int));
                _allapplicationitems.Columns.Add("AspxFile", typeof(string));
                _allapplicationitems.Columns.Add("mAspxFile", typeof(string));
                foreach (DataRow dr in _allapplicationitems.Rows)
                {
                    dr["ModelName"] = dr["MC"];
                    DataRow[] rs = eBase.a_eke_sysModels.Select("ModelID='" + dr["ModelID"].ToString() + "'");
                    if (rs.Length > 0)
                    {
                        dr["auto"] = rs[0]["auto"].ToString();
                        dr["AutoLayout"] = rs[0]["AutoLayout"];
                        dr["mAutoLayout"] = rs[0]["mAutoLayout"];

                        dr["AspxFile"] = rs[0]["AspxFile"];
                        dr["mAspxFile"] = rs[0]["mAspxFile"];
                    }
                    //dr["href"] = dr["URL"];
                    updateHref(dr);
                }
                if (_allapplicationitems.Rows.Count > 0)
                {
                    HttpRuntime.Cache.Insert(cacheName, _allapplicationitems);
                }
            }

            return _allapplicationitems;
        }
    }
    private DataTable _applicationitems;
    /// <summary>
    /// 用户在当前应用下有权限的模块
    /// </summary>
    public DataTable ApplicationItems
    {
        get
        {
            if (_applicationitems == null)
            {
                _applicationitems = AllApplicationItems.Select("ApplicationID='" + ApplicationID + "'").toDataTable();
                #region 删除条件禁用的模块
                for (int i = _applicationitems.Rows.Count - 1; i >= 0; i--)
                {
                    string cond = _applicationitems.Rows[i]["condDisable"].ToString();
                    if (cond.Length > 0)
                    {
                        cond = eParameters.Replace(cond, null, User);
                        string result = eVsa.Eval(cond);
                        if (result.ToLower() == "true")
                        {
                            _applicationitems.Rows.Remove(_applicationitems.Rows[i]);
                            continue;
                        }
                    }
                }
                #endregion
            }
            return _applicationitems;
        }
    }
    public DataTable UserApps
    {
        get
        {
            return ApplicationItems;
        }
    }
    //企业的应用
    private List<string> _siteapps;
    private List<string> SiteApps
    {
        get
        {
            if (_siteapps == null)
            {
                _siteapps = new List<string>();
                DataRow[] rows = new DataTable().Select();
                if (User["siteid"].Length > 0) rows = eBase.a_eke_sysSiteItems.Select("SiteID=" + User["siteid"]);
                foreach (DataRow dr in rows)
                {
                    if (!_siteapps.Contains(dr["ApplicationID"].ToString()))
                    {
                        _siteapps.Add(dr["ApplicationID"].ToString());
                    }
                }
            }
            return _siteapps;
        }
    }
    private List<string> _menuids;
    public List<string> MenuIDS
    {
        get
        {
            if (_menuids == null)
            {
                _menuids = new List<string>();
                //_menuids = (from row in UserApps.AsEnumerable() where row["ApplicationID"].ToString() == ApplicationID select row["ApplicationItemID"].ToString()).ToList();
                //string[] ids = (from row in UserApplicationItems.AsEnumerable() where row["ApplicationID"].ToString() == ApplicationID select row["ApplicationItemID"].ToString()).ToArray();
                string[] ids = (from row in eBase.a_eke_sysUserPowers.AsEnumerable() where row["UserID"].ToString() == User.ID select row["ApplicationItemID"].ToString()).ToArray();
                _menuids = ids.ToList();
                foreach (string appitem in ids)
                {
                    addMenuIDS(_menuids, appitem);
                }

                //连接类型模块
                ids = (from row in AllApplicationItems.AsEnumerable() where row["URL"].ToString().ToLower().StartsWith("http") select row["ApplicationItemID"].ToString()).ToArray();
                foreach (string appitem in ids)
                {
                    addMenuIDS(_menuids, appitem);
                }

            }
            return _menuids;
        }
    }
    public ApplicationMenu(string apptype)
	{
        if (eItems.Get("eUser") == null)
        {
            eBase.Writeln("请在使用模块前先验证用户登录状态!");
            eBase.End();
        }
        _apptype = apptype;
        _user = eItems.Get("eUser");
        _appitem = eParameters.QueryString("AppItem");
        _applicationid = eParameters.QueryString("AppID");
        if (_appitem.Length > 0)
        {
            DataRow[] rows = eBase.a_eke_sysApplicationItems.Select("ApplicationItemID='" + _appitem + "'");
            if (rows.Length > 0)
            {
                _modelid = rows[0]["ModelID"].ToString();
                _applicationid = rows[0]["ApplicationID"].ToString();
            }
            if (!HttpContext.Current.Items.Contains("homeurl")) HttpContext.Current.Items["homeurl"] = HomeURL;
        }
        if (_applicationid.Length == 0)
        {
            /*
            DataRow[] rs = (from row in ApplicationItems.AsEnumerable()
                            where row["apptype"].ToString() == _apptype || row["apptype"].ToString()=="3"
                            orderby row["appsort"],row["appAddTime"]
                            select row).Take(1).ToArray();
            */
            DataRow[] rs = Applications.Select("", "px,addTime");
            if (rs.Length > 0) _applicationid = rs[0]["ApplicationID"].ToString();
        }        
    }
    public ApplicationMenu(eUser user, string apptype)
    {
        if (eItems.Get("eUser") == null)
        {
            eBase.Writeln("请在使用模块前先验证用户登录状态!");
            eBase.End();
        }
        _apptype = apptype;
        _user = user;
        _appitem = eParameters.QueryString("AppItem");
        _applicationid = eParameters.QueryString("AppID");
        if (_appitem.Length > 0)
        {
            DataRow[] rows = eBase.a_eke_sysApplicationItems.Select("ApplicationItemID='" + _appitem + "'");
            if (rows.Length > 0)
            {
                _modelid = rows[0]["ModelID"].ToString();
                _applicationid = rows[0]["ApplicationID"].ToString();               
            }
            if (!HttpContext.Current.Items.Contains("homeurl")) HttpContext.Current.Items["homeurl"] = HomeURL;
        }
        if (_applicationid.Length == 0 && Applications.Rows.Count > 0)
        {
            /*
            DataRow[] rs = (from row in ApplicationItems.AsEnumerable()
                            orderby row["px"], row["addTime"]
                            select row).Take(1).ToArray();
            */

            DataRow[] rs = Applications.Select("", "px,addTime");          
            if (rs.Length > 0) _applicationid = rs[0]["ApplicationID"].ToString();
            //eBase.End();

        }    
    }
    public string getMenus(string ParentID, int Level = 1, string target = "")
    {
        if (AppType == "1")
        {
            return getPCMenus(ParentID, Level, target);
        }
        else
        {
            return getMobileMenus();
        }
    }
    private string getPCMenus(string ParentID, int Level = 1, string target = "")
    {
        //eBase.PrintDataTable(ApplicationItems);
        StringBuilder sb = new StringBuilder();
        if (ParentID.Length == 0)
        {
            sb.Append("<ul class=\"emenu\">\r\n");
        }
        else
        {
            sb.Append("<ul" + (curMenuIDS.IndexOf(ParentID) == -1 ? " style=\"display:none;\"" : "") + ">\r\n");
        }

        DataRow[] rows = ApplicationItems.Select(ParentID.Length == 0 ? "ParentID is Null and Show=1" : "ParentID='" + ParentID + "' and Show=1", "PX,addTime");

        //eBase.PrintDataRow(rows);

        for (int i = 0; i < rows.Length; i++)
        {
            if (!MenuIDS.Contains(rows[i]["ApplicationItemID"].ToString())) continue; //没有权限的模块不输出
            string curids = rows[i]["ApplicationItemID"].ToString();
            if (rows[i]["PackID"].ToString().Length > 0) curids = rows[i]["PackID"].ToString();
            sb.Append("<li><div class=\"level" + Level.ToString() + (MenuID == curids ? " cur" : "") + "\">");

            if (rows[i]["ModelID"].ToString().Length == 0)
            {
                if (rows[i]["url"].ToString().Length > 0)
                {
                    sb.Append("<a href=\"" + rows[i]["url"].ToString() + "\"");
                    if (target.Length > 0) sb.Append(" target=\"" + target + "\"");
                }
                else
                {
                    sb.Append("<a href=\"javascript:;\" onclick=\"showmenu(this);\"");
                    sb.Append(curMenuIDS.IndexOf(rows[i]["ApplicationItemID"].ToString()) > -1 ? " class=\"open\"" : " class=\"close\"");
                }
            }
            else
            {
                sb.Append("<a href=\"" + rows[i]["href"].ToString() + (HttpContext.Current.Request.QueryString["debug"] != null ? "&debug=1" : "") + "\"");
            }
            //sb.Append(rows[i]["Finsh"].ToString() == "False" ? " style=\"color:#666;\"" : "");
            sb.Append(rows[i]["Finsh"].ToString() == "False" ? " class=\"disable\"" : "");

            sb.Append(" title=\"" + rows[i]["mc"].ToString() + "\"");
            sb.Append(" onfocus=\"this.blur();\">");
            string iconhtml = rows[i]["IconHTML"].ToString();
            if (iconhtml.Length > 10)
            {
                if (rows[i].Contains("IconSize") && Convert.ToInt32(rows[i]["IconSize"]) > 0)
                {
                    iconhtml = iconhtml.Replace("<i", "<i style=\"font-size:" + rows[i]["IconSize"].ToString() + "px;\"");
                }
                sb.Append(iconhtml);
            }
            else
            {
                string iconpath = rows[i]["Icon"].ToString();
                string iconactivepath = rows[i]["IconActive"].ToString().Length > 10 ? rows[i]["IconActive"].ToString() : iconpath;
                if (iconpath.Length > 0)
                {
                    //sb.Append("<img" + (rows[i]["Finsh"].ToString() == "False" ? " class=\"gray\"" : "") + " src=\"../" + (appmenu.curMenuIDS.IndexOf(curids) > -1 ? iconactivepath : iconpath) + "\"align=\"absmiddle\" border=\"0\" >");
                }
                if (iconpath.Length > 0)
                {
                    sb.Append("<img");
                    if (rows[i].Contains("IconWidth") && rows[i].Contains("IconHeight") && (Convert.ToInt32(rows[i]["IconWidth"]) > 0 || Convert.ToInt32(rows[i]["IconHeight"]) > 0))
                    {
                        sb.Append(" style=\"" + (Convert.ToInt32(rows[i]["IconWidth"]) > 0 ? "width:" + rows[i]["IconWidth"].ToString() + "px;" : "") + (Convert.ToInt32(rows[i]["IconHeight"]) > 0 ? "height:" + rows[i]["IconHeight"].ToString() + "px;" : "") + "\"");
                    }
                    sb.Append(" class=\"def\" src=\"../" + iconpath + "\" onerror=\"this.src='../images/none.gif';\">");
                }
                if (iconactivepath.Length > 0)
                {
                    sb.Append("<img");
                    if (rows[i].Contains("IconWidth") && rows[i].Contains("IconHeight") && (Convert.ToInt32(rows[i]["IconWidth"]) > 0 || Convert.ToInt32(rows[i]["IconHeight"]) > 0))
                    {
                        sb.Append(" style=\"" + (Convert.ToInt32(rows[i]["IconWidth"]) > 0 ? "width:" + rows[i]["IconWidth"].ToString() + "px;" : "") + (Convert.ToInt32(rows[i]["IconHeight"]) > 0 ? "height:" + rows[i]["IconHeight"].ToString() + "px;" : "") + "\"");
                    }
                    sb.Append(" class=\"cur\" src=\"../" + iconactivepath + "\" onerror=\"this.src='../images/none.gif';\">");
                }

            }
            sb.Append(rows[i]["mc"].ToString());
            sb.Append("</a></div>");

            if (rows[i]["ModelID"].ToString().Length == 0)
            {
                sb.Append(getPCMenus(rows[i]["ApplicationItemID"].ToString(), Level + 1, target));
            }
            sb.Append("</li>\r\n");
        }
        sb.Append("</ul>\r\n");
        return sb.ToString();
    }
    private string getMobileMenus()
    {
        StringBuilder sb = new StringBuilder();
        DataRow[] rows, rs;
        #region 已分组
        rows = ApplicationItems.Select("ModelID is Null and mURL is Null and mShow=1", "PX,addTime");
        for (int i = 0; i < rows.Length; i++)
        {
            if (!MenuIDS.Contains(rows[i]["ApplicationItemID"].ToString()) && rows[i]["mURL"] == DBNull.Value) continue;
            sb.Append("<dl class=\"eFunGroup\">\r\n");
            sb.Append("<dt>" + rows[i]["MC"].ToString() + "</dt>\r\n");
            sb.Append("<dd>\r\n");
            rs = ApplicationItems.Select("ParentID='" + rows[i]["ApplicationItemID"].ToString() + "' and (ModelID is not Null or URL is not Null) and mShow=1", "PX,addTime");
            for (int j = 0; j < rs.Length; j++)
            {
                if (!MenuIDS.Contains(rs[j]["ApplicationItemID"].ToString()) && rs[j]["mURL"] == DBNull.Value) continue;
                if (rows[i]["ModelID"].ToString().Length == 0 && rs[j]["murl"].ToString().Length > 0)
                {
                    sb.Append("<a href=\"" + rs[j]["murl"].ToString() + "\">");
                }
                else
                {
                    sb.Append("<a href=\"" + rs[j]["mhref"].ToString() + "\">");
                }
                string iconhtml = rs[j]["IconHTML"].ToString();
                if (iconhtml.Length > 10)
                {
                    if (rs[j].Contains("IconSize") && Convert.ToInt32(rs[j]["IconSize"]) > 0)
                    {
                        iconhtml = iconhtml.Replace("<i", "<i style=\"font-size:" + rs[j]["IconSize"].ToString() + "px;\"");
                    }
                    sb.Append(iconhtml);
                }
                else
                {
                    string iconpath = rs[j]["icon"].ToString();
                    if (iconpath.Length == 0) iconpath = "images/noicon.png";
                    sb.Append("<img");
                    if (rs[j].Contains("IconWidth") && rs[j].Contains("IconHeight") && (Convert.ToInt32(rs[j]["IconWidth"]) > 0 || Convert.ToInt32(rs[j]["IconHeight"]) > 0))
                    {
                        sb.Append(" style=\"" + (Convert.ToInt32(rs[j]["IconWidth"]) > 0 ? "width:" + rs[j]["IconWidth"].ToString() + "px;" : "") + (Convert.ToInt32(rs[j]["IconHeight"]) > 0 ? "height:" + rs[j]["IconHeight"].ToString() + "px;" : "") + "\"");
                    }
                    sb.Append((rs[j]["Finsh"].ToString() == "False" ? " class=\"gray\"" : "") + " src=\"../" + iconpath + "\"  />");
                }
                sb.Append("<p" + (rs[j]["Finsh"].ToString() == "False" ? " style=\"color:#ccc;\"" : "") + ">" + rs[j]["MC"].ToString() + "</p></a>\r\n");
            }
            sb.Append("</dd>\r\n");
            sb.Append("</dl>\r\n");
            sb.Append("<div class=\"clear\"></div>\r\n");
        }
        #endregion
        #region 未分组
        rs = ApplicationItems.Select("(ModelID is not Null or mURL is not Null) and ParentID is null and Show=1", "PX,addTime");
        if (rs.Length > 0)
        {
            sb.Append("<dl class=\"eFunGroup\">\r\n");
            sb.Append("<dt>未分组</dt>\r\n");
            sb.Append("<dd>\r\n");
            for (int j = 0; j < rs.Length; j++)
            {
                if (!MenuIDS.Contains(rs[j]["ApplicationItemID"].ToString()) && rs[j]["mURL"] == DBNull.Value) continue;
                if (rs[j]["ModelID"].ToString().Length == 0 && rs[j]["murl"].ToString().Length > 0)
                {
                    sb.Append("<a href=\"" + rs[j]["murl"].ToString() + "\">");
                }
                else
                {
                    sb.Append("<a href=\"" + rs[j]["mhref"].ToString() + "\">");
                }
                string iconhtml = rs[j]["IconHTML"].ToString();
                if (iconhtml.Length > 10)
                {
                    if (rs[j].Contains("IconSize") && Convert.ToInt32(rs[j]["IconSize"]) > 0)
                    {
                        iconhtml = iconhtml.Replace("<i", "<i style=\"font-size:" + rs[j]["IconSize"].ToString() + "px;\"");
                    }
                    sb.Append(iconhtml);
                }
                else
                {
                    string iconpath = rs[j]["icon"].ToString();
                    if (iconpath.Length == 0) iconpath = "images/noicon.png";
                    sb.Append("<img");
                    if (rs[j].Contains("IconWidth") && rs[j].Contains("IconHeight") && (Convert.ToInt32(rs[j]["IconWidth"]) > 0 || Convert.ToInt32(rs[j]["IconHeight"]) > 0))
                    {
                        sb.Append(" style=\"" + (Convert.ToInt32(rs[j]["IconWidth"]) > 0 ? "width:" + rs[j]["IconWidth"].ToString() + "px;" : "") + (Convert.ToInt32(rs[j]["IconHeight"]) > 0 ? "height:" + rs[j]["IconHeight"].ToString() + "px;" : "") + "\"");
                    }
                    sb.Append((rs[j]["Finsh"].ToString() == "False" ? " class=\"gray\"" : "") + " src=\"../" + iconpath + "\"  />");
                }
                sb.Append("<p" + (rs[j]["Finsh"].ToString() == "False" ? " style=\"color:#ccc;\"" : "") + ">" + rs[j]["MC"].ToString() + "</p></a>\r\n");
            }
            sb.Append("</dd>\r\n");
            sb.Append("</dl>\r\n");
            sb.Append("<div class=\"clear\"></div>\r\n");
        }
        #endregion
        return sb.ToString();
    }
}


