using System;
using System.Data;
using System.Data.OleDb;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Text;
using System.Text.RegularExpressions;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Drawing.Imaging;
using System.Drawing;
using System.Drawing.Drawing2D;

namespace WebCode
{
    /// <summary>
    /// 对外的
    /// </summary>
    public class contentPT : Base
    {
        #region 通用函数
        private PageContent pc = new PageContent();
        public contentPT()
        {
            //
            // TODO: 在此处添加构造函数逻辑
            //
        }

        /// <summary>
        /// 获得文件的名字 small_test.jpg
        /// </summary>
        /// <param name="s_fn"></param>
        /// <returns></returns>
        public string GetImageName(string s_fn)
        {
            string s_ret = s_fn.ToLower().Replace("small_", "");
            s_ret = s_ret.Replace(".jpg", "");

            return s_ret;
        }

        /// <summary>
        /// 获得显示的模板
        /// </summary>
        /// <returns></returns>
        public string GetTemplate()
        {

            string s_ret = "";
            //调试状态 和 手工设置debug状态一样
            if ((HttpHandlerType.GetInstance().Debug) || (HttpContext.Current.Request.Url.ToString().ToLower().IndexOf("debug=1") > -1))
                s_ret = ds.GetDataString("select T_Path,iif( T_Type=0,'分类模板','内容模板') as Type from User_Template where T_ID=0" + table["C_Template"], "<!-- @T_Path@ @Type@ -->");
            return s_ret;
        }

        



        /// <summary>
        /// 获得相关数据
        /// </summary>
        /// <param name="i_sel">0相关文章 1相关产品</param>
        /// <returns></returns>
        /// 
        public string GetRelation(int i_sel, int i_length, string s_temp)
        {
            return GetRelation(i_sel, s_temp);
        }

        /// <summary>
        /// 新函数
        /// </summary>
        /// <param name="i_sel"></param>
        /// <param name="s_temp"></param>
        /// <returns></returns>
        private string GetRelation(int i_sel, string s_temp)
        {
            string s_idlist = i_sel == 0 ? table["C_Correlate"] : table["C_Product"];
            if (string.IsNullOrEmpty(s_idlist))
                return "";

            string[] s_con = s_idlist.Split(',');
            StringBuilder sb_ret = new StringBuilder();
            Dictionary<string, string> dic_value = new Dictionary<string, string>();

            //解析模板
            s_temp = ts.TranslateAll(s_temp);


            OleDbDataReader reader = ds.GetDataReader("select *,1 as itemnumber," + GetFieldFromTemp(s_temp) + " from User_Content where c_id in (" + s_idlist + ")");
            while (reader.Read())
            {
                string s_get = s_temp;

                //获得值替换字符串
                for (int i = 0; i < reader.FieldCount; i++)
                {
                    s_get = Regex.Replace(s_get, "@" + reader.GetName(i) + "@", ds.GetLastData(reader.GetName(i), reader[i].ToString()), RegexOptions.IgnoreCase);
                }

                if (!dic_value.ContainsKey(reader["C_ID"].ToString()))
                {
                    dic_value.Add(reader["C_ID"].ToString(), s_get);
                }
                
            }
            reader.Close();

            //按顺序发布
            for (int i = 0; i < s_con.Length; i++)
            {
                if (dic_value.ContainsKey(s_con[i]))
                    sb_ret.Append(dic_value[s_con[i]]);
            }

            return sb_ret.ToString();
        }

        /// <summary>
        /// 获得上一页下一页
        /// </summary>
        /// <returns></returns>
        public string GetPreNext()
        {
            return GetPreNext(table["C_ID"], table["C_CID"], "C_Update desc");
        }

        public string ShowContentPreNext()
        {
            return GetPreNext();
        }


        /// <summary>
        /// 获得大图
        /// </summary>
        /// <returns></returns>
        public string GetImage()
        {
            return GetFieldImage("C_Image");
        }

        /// <summary>
        /// 获得不同位置的大图
        /// </summary>
        /// <param name="s_name"></param>
        /// <returns></returns>
        public string GetFieldImage(string s_name)
        {
            string s_image = table[s_name];
            return s_image.Replace("small_", "");
        }


        #endregion

        #region 标签化 编辑也可以修改

        
        /// <summary>
        /// 显示分类信息
        /// </summary>
        /// <param name="s_temp"></param>
        /// <param name="s_cid"></param>
        /// <returns></returns>
        public string ShowClassInfo(string s_temp, string s_cid)
        {
            //解析模板
            
            s_temp = this[s_temp];

            return ds.GetDataString("select *,1 as itemnumber," + GetFieldFromTemp(s_temp) + " from User_Class where  C_ID in (" + s_cid + ") ", s_temp);


        }

        /// <summary>
        /// 显示内容信息
        /// </summary>
        /// <param name="s_temp"></param>
        /// <param name="s_cid"></param>
        /// <returns></returns>
        public string ShowContentInfo(string s_temp, string s_cid)
        {
            //解析模板
            s_temp = this[s_temp];

            return ds.GetDataString("select *,1 as itemnumber," + GetFieldFromTemp(s_temp) + " from User_Content where  C_ID in (" + s_cid + ") ", s_temp);


        }

        /// <summary>
        /// 简单分类列表页
        /// </summary>
        /// <returns></returns>
        public string ShowClassList(string s_temp, string s_pid, string s_top)
        {
            return ShowClassList(s_temp, s_pid, s_top, "", "");
        }


        /// <summary>
        /// 简单分类列表页
        /// </summary>
        /// <returns></returns>
        public string ShowClassList(string s_temp, string s_pid)
        {
            return ShowClassList(s_temp, s_pid, "1000", "", "");
        }


        /// <summary>
        /// 详细分类列表页
        /// </summary>
        /// <param name="s_pid"></param>
        /// <param name="s_top"></param>
        /// <param name="s_temp"></param>
        /// <param name="s_where"></param>
        /// <param name="s_order"></param>
        /// <returns></returns>
        public string ShowClassList(string s_temp, string s_pid, string s_top, string s_where, string s_order)
        {
            if (string.IsNullOrEmpty(s_where))
            {
                s_where = " 1=1";
            }

            if (string.IsNullOrEmpty(s_order))
            {
                s_order = " C_Order asc";
            }

            //解析模板
            s_temp = this[s_temp];

            return ds.GetDataString("select top " + GetDefaultTop(s_top) + " *,1 as itemnumber," + GetFieldFromTemp(s_temp) + " from User_Class where  " + s_where + " and C_ParentID in (" + s_pid + ") order by  " + s_order, s_temp);

        }

        public string ShowClassPage(string s_temp, string s_cid)
        {
            return ShowClassPage(s_temp, s_cid, "10", "", Para("ord"), false);
        }

        public string ShowClassPage(string s_temp, string s_cid, string s_size)
        {
            return ShowClassPage(s_temp, s_cid, s_size, "", Para("ord"), false);
        }

        public string ShowClassPage(string s_temp, string s_cid, string s_size, string s_where, string s_order, bool b_imagebar)
        {
            //解析模板
            s_temp = ts.TranslateAll(s_temp);

            int i_page = function.GetNumber(Para("pagenow"));
            if (i_page == 0)
                i_page = 1;

            

            if (string.IsNullOrEmpty(s_where))
            {
                s_where = " 1=1";
            }

            if (string.IsNullOrEmpty(s_order))
            {
                s_order = " C_order asc";
            }

            Table tabclass = new Table("user_class", s_cid);


            pc.Table = "User_class";
            pc.PageSize = function.GetNumber(s_size);
            pc.ImagePageBar = b_imagebar;
            pc.PageBarType = 2;
            pc.PageNow = i_page;
            pc.Url = GetPageUrl(tabclass["c_url"]);
            pc.Order = Para("ord");
            pc.SQL = "select *," + GetFieldFromTemp(s_temp) + " from User_class where C_ParentID  in (" + s_cid + ")  " + s_where;

            pc.PageBody("@content@", "@@Const.PageBar('')@@", "", "@content@");
            pc.AddList("", s_temp);

            return pc.ShowContent();
        }


        /* 显示内容 */

        /// <summary>
        /// 显示内容标签
        /// </summary>
        /// <returns></returns>
        public string ShowContentTags()
        {

            return GetTags(false);
        }

        /// <summary>
        /// 显示相关产品
        /// </summary>
        /// <param name="s_temp"></param>
        /// <returns></returns>
        public string ShowContentRelationProduct(string s_temp)
        {
            return GetRelation(1, s_temp);
        }

        

        /// <summary>
        /// 显示相关信息
        /// </summary>
        /// <param name="s_temp"></param>
        /// <returns></returns>
        public string ShowContentRelationInfo(string s_temp)
        {
            return GetRelation(0, s_temp);
        }

        /// <summary>
        /// 翻页
        /// </summary>
        /// <param name="s_cid"></param>
        /// <param name="s_temp"></param>
        /// <returns></returns>
        public string ShowContentPage(string s_temp, string s_cid, string s_size)
        {
            return ShowContentPage(s_temp, s_cid, s_size, "", Para("ord"), false);
        }

        /// <summary>
        /// 翻页
        /// </summary>
        /// <param name="s_cid"></param>
        /// <param name="s_temp"></param>
        /// <returns></returns>
        public string ShowAllContentPage(string s_temp, string s_cid, string s_size, string s_where, string s_order, bool b_imagebar, string s_shell)
        {
            return ShowContentPage(s_temp, s_cid+","+GetClassChildId(s_cid), s_size, "", Para("ord"), false, s_shell);
        }

       

        public string ShowContentPage(string s_temp, string s_cid, string s_size, string s_shell)
        {
            return ShowContentPage(s_temp, s_cid, s_size, "", Para("ord"), false, s_shell);
        }

        public string ShowContentPage(string s_temp, string s_cid, string s_size, string s_where, string s_order, bool b_imagebar, string s_shell)
        {
            //解析模板
            s_temp = ts.TranslateAll(s_temp);

            int i_page = function.GetNumber(Para("pagenow"));
            if (i_page == 0)
                i_page = 1;

            

            if (string.IsNullOrEmpty(s_where))
            {
                s_where = " 1=1";
            }

            if (string.IsNullOrEmpty(s_order))
            {
                s_order = " C_update desc";
            }

            Table tabclass = new Table("user_class", s_cid);

            int i_size = function.GetNumber(s_size);
            if (i_size == 0)
                i_size = 10;

            pc.Table = "User_Content";
            pc.PageSize = i_size;
            pc.ImagePageBar = b_imagebar;
            pc.PageBarType = 2;
            pc.PageNow = i_page;
            pc.Url = GetPageUrl(tabclass["c_url"]);
            pc.Order = Para("ord");
            pc.SQL = "select *," + GetFieldFromTemp(s_temp) + " from User_Content where C_CID in ( " + s_cid + ") and (C_Status=1 or C_Status=2) and " + s_where;

            pc.PageBody(s_shell, "", "", "@content@");
            pc.AddList("", s_temp);

            return pc.ShowContent();
        }

        public string ShowContentPage(string s_temp, string s_cid, string s_size, string s_where, string s_order, bool b_imagebar)
        {
            return ShowContentPage(s_temp, s_cid, s_size, s_where, s_order, b_imagebar, "@content@");
        }

        /// <summary>
        /// 默认字符翻页
        /// </summary>
        /// <returns></returns>
        public string ShowPageBar()
        {
            return ShowPageBar(false);
        }

        public string ShowPageBar(bool b_imagebar)
        {
            return ShowPageBar(b_imagebar,"@@Const.PageBar('')@@");
        }

        public string ShowPageBar(bool b_imagebar,string s_temp)
        {
            pc.ImagePageBar = b_imagebar;
            return pc.ShowCount(this[s_temp]);
        }

        /// <summary>
        /// 获得不同位置的分类
        /// </summary>
        /// <returns></returns>
        public string GetNavigateClass(string s_cid,string s_layer)
        {
            return UserClass.GetInstance().GetTopId(s_cid, function.GetNumber(s_layer));
        }

        public string ShowMultiContent(string s_cid, string s_firsttemp, string s_secondtemp)
        {
            return ShowMultiContent( s_cid,  s_firsttemp,  s_secondtemp, " 1=1 ");
        }

        public string ShowMultiContent(string s_cid, string s_firsttemp, string s_secondtemp,string s_where)
        {
            StringBuilder sb_ret = new StringBuilder();
            s_firsttemp = this[s_firsttemp];
            s_secondtemp = this[s_secondtemp];

            OleDbDataReader reader = ds.GetDataReader("select *,1 as itemnumber," + GetFieldFromTemp(s_firsttemp) + " from User_Class where C_parentID in (" + s_cid + ") order by C_order asc ");
            while (reader.Read())
            {
                string s_ret = ds.GetDataString("select *,1 as itemnumber," + GetFieldFromTemp(s_secondtemp) + " from User_Content where C_cID=" + reader["C_ID"].ToString()+" and "+s_where+" order by c_update desc ", s_secondtemp);

                string s_tmp =s_firsttemp;

                for (int i = 0; i < reader.FieldCount; i++)
                {
                    string s_name = reader.GetName(i);
                    s_tmp = Regex.Replace(s_tmp, "@" + s_name + "@", ds.GetLastData(s_name, reader.GetValue(i).ToString()), RegexOptions.IgnoreCase);
                }

                //替换子菜单
                s_tmp = s_tmp.Replace("@child@", s_ret);

                sb_ret.Append(s_tmp);

            }
            reader.Close();

            return sb_ret.ToString();
        }


        /// <summary>
        /// 显示多级菜单
        /// </summary>
        /// <param name="s_cid"></param>
        /// <returns></returns>
        public string ShowMultiMenu(string s_cid,string s_firsttemp,string s_secondtemp)
        {
            StringBuilder sb_ret = new StringBuilder();
            s_firsttemp = this[s_firsttemp];
            s_secondtemp = this[s_secondtemp];
            OleDbDataReader reader = ds.GetDataReader("select *,1 as itemnumber," + GetFieldFromTemp(s_firsttemp) + " from User_Class where C_ParentID in (" + s_cid + ") order by C_order asc " );
            while (reader.Read())
            {
                string s_ret = ds.GetDataString("select *,1 as itemnumber," + GetFieldFromTemp(s_secondtemp) + " from User_Class where C_ParentID=" + reader["C_ID"].ToString(), s_secondtemp);

                string s_tmp = s_firsttemp;
                for (int i = 0; i < reader.FieldCount; i++)
                {
                    string s_name=reader.GetName(i);
                    s_tmp = Regex.Replace(s_tmp,"@"+ s_name+"@", ds.GetLastData(s_name,reader.GetValue(i).ToString()),RegexOptions.IgnoreCase);
                }

                //替换子菜单
                s_tmp = s_tmp.Replace("@child@", s_ret);

                sb_ret.Append(s_tmp);

            }
            reader.Close();

            return sb_ret.ToString();
        }

        /// <summary>
        /// 获得内容图片
        /// </summary>
        /// <returns></returns>
        public string ShowContentImage()
        {
            return ShowContentImage("C_Image");
        }

        public string ShowContentImage(string s_name)
        {
            return GetFieldImage(s_name);
        }

        /// <summary>
        /// 获得面包屑导航
        /// </summary>
        /// <returns></returns>
        public string ShowBreadBar(string s_temp, string s_split)
        {
            Dictionary<string, ClassData> dic_path = UserClass.GetInstance().List;
            string s_cid = "";

            //解析模板
            s_temp = ts.TranslateAll(s_temp);


            if (table.Name.ToLower() == "user_content")
            {
                s_cid = table["C_CID"];
            }
            else
            {
                s_cid = table["C_ParentID"];
            }

            if (dic_path.ContainsKey(s_cid))
            {
                System.Text.StringBuilder sb_ret = new System.Text.StringBuilder();
                ClassData cd = dic_path[s_cid];
                while (true)
                {
                    string s_value = ds.GetDataString("select *,1 as itemnumber," + GetFieldFromTemp(s_temp) + " from User_Class where C_ID=" + cd.Id, s_temp);

                    s_value = Regex.Replace(s_value, "@layer@", cd.Layer.ToString(), RegexOptions.IgnoreCase);


                    sb_ret.Insert(0, s_value);

                    if (cd.Parent.Id == 0)
                        break;

                    sb_ret.Insert(0, s_split);

                    cd = cd.Parent;
                }

                return s_split + sb_ret.ToString() + s_split;
            }
            else
                return s_split;
        }


        /// <summary>
        /// 简单内容列表页
        /// </summary>
        /// <returns></returns>
        public string ShowContentList(string s_temp, string s_cid, string s_top)
        {
            return ShowContentList(s_temp, s_cid, s_top, "", "");
        }


        public string ShowContentList(string s_temp, string s_cid)
        {
            return ShowContentList(s_temp, s_cid, "1000", "", "");
        }

        /// <summary>
        /// 显示推荐列表
        /// </summary>
        /// <param name="s_cid"></param>
        /// <param name="s_top"></param>
        /// <param name="s_temp"></param>
        /// <returns></returns>
        public string ShowContentGoodList(string s_temp, string s_cid, string s_top)
        {
            return ShowContentList(s_temp, s_cid, s_top, " C_Status=2 ", "");
        }

        public string ShowContentGoodList(string s_temp, string s_cid)
        {
            return ShowContentList(s_temp, s_cid, "1000", " C_Status=2 ", "");
        }

        /// <summary>
        /// 获得带参数的信息列表
        /// </summary>
        /// <param name="s_cid"></param>
        /// <param name="s_top"></param>
        /// <param name="s_temp"></param>
        /// <param name="s_where"></param>
        /// <param name="s_order"></param>
        /// <returns></returns>
        public string ShowContentList(string s_temp, string s_cid, string s_top, string s_where, string s_order)
        {
            if (string.IsNullOrEmpty(s_where))
            {
                s_where = " 1=1";
            }

            if (string.IsNullOrEmpty(s_order))
            {
                s_order = " c_update desc";
            }

            //解析模板
            s_temp = ts.TranslateAll(s_temp);


            return ds.GetDataString("select top " + GetDefaultTop(s_top) + " *,1 as itemnumber," + GetFieldFromTemp(s_temp) + " from User_Content where C_Status in (1,2) and " + s_where + " and C_CID in (" + s_cid + ") order by  " + s_order, s_temp);
        }

        /// <summary>
        /// 显示文章内容
        /// </summary>
        /// <returns></returns>
        public string ShowContentContent()
        {
            string s_result = "";
            string s_url = table["C_Url"];
            string[] s_rel = Regex.Split(table["C_Tags"], "\r\n");

            string s_content = table["C_Content"];
            //支持高级编辑里面的分页处理 //分页 <div style="page-break-after: always"><span style="display: none">&nbsp;</span></div>
            s_content = Regex.Replace(s_content, "<div[^>]*?page-break-after.*?</span></div>", "#page#");

            string[] s_con = Regex.Split(s_content, "#page#");

            if (i_page > s_con.Length)
                i_page = s_con.Length;

            s_result = OutputHTML(s_con[i_page - 1]);

            //全部替换tags
            for (int i = 0; i < s_rel.Length; i++)
            {
                if (!string.IsNullOrEmpty(s_rel[i]))
                {
                    s_result = function.ReplaceHTML(s_result, s_rel[i], "<strong>" + s_rel[i] + "</strong>", 10);//仅替换前10个
                }
            }

            //替换内部链接
            s_result = SysInnerLink.GetInstance().ReplaceInnerUrl(s_result);

            s_result = s_result + "<div id='divpage'>" + function.GetNumberPage(GetPageUrl(s_url), i_page, s_con.Length) + " </div>";

            return s_result;
        }


       
        #endregion
    }
}