using System;
using System.Data;
using System.Text;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
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;
using JuSNS.Home;
using JuSNS.Model;
using JuSNS.Common;
using JuSNS.UI.Control;

namespace JuSNS.Web.home.ask
{
    public partial class index : JuSNS.UI.Page.BasePage
    {
        JuSNS.Home.Ask ask = new JuSNS.Home.Ask();
        public string r = string.Empty;
        protected void Page_Load(object sender, EventArgs e)
        {
            r = Request.QueryString["r"];
            string action = Request.QueryString["action"];
            if (action == "del")
            {
                if (ask.Del(this.GetUserID(), Convert.ToInt32(Request.QueryString["id"])) > 0)
                {
                    this.DivMsg.Visible = true;
                    this.DivMsg.InnerHtml = "删除成功。";
                }
            }
            int ClassID = 0;
            if (JuSNS.Common.Input.IsInteger(Request.QueryString["classid"]))
            {
                ClassID = Convert.ToInt32(Request.QueryString["classid"]);
                string nask = GetAskClass(ClassID, 1);
                this.ChildClassList.InnerHtml = nask;
            }
            else
            {
                this.ChildClassList.Visible = false;
            }
            if (this.GetUserID() == 0)
            {
                 this.li_my.InnerHtml = "<a href=\"../ask/?r=my\" onclick=\"javascript:{unlogin();return false}\">我的问题</a>";
                this.li_none.InnerHtml = "<a href=\"../ask/\" onclick=\"javascript:{unlogin();return false}\">朋友的问题</a>";
            }
            this.askClass.InnerHtml = GetAskClass(0, 0);
            GetContent();
            GetXinZhi(this.uncloseHTML, 0, 5, ClassID);
            GetXinZhi(this.hiJiFenHTML, 1, 5, ClassID);
        }

        protected void GetXinZhi(HtmlGenericControl T, int flg,int num,int classid)
        {
            DataTable dt=ask.GetXAskList(flg,num,classid);
            string listSTR = string.Empty;
            if (dt != null && dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    listSTR += "<li>• <a href=\"" + rooDir + "/ask/q" + dt.Rows[i]["ID"].ToString() + ".aspx\" title=\"" + dt.Rows[i]["Title"].ToString() + "\">" + JuSNS.Common.Input.GetSubString(dt.Rows[i]["Title"].ToString(), 46) + "</a> <span class=\"c333 font12\">[<a href=\"../ask/?r=all&classID=" + dt.Rows[i]["ClassID"].ToString() + "\">" + dt.Rows[i]["ClassName"].ToString() + "</a>]</span></li>";
                }
            }
            dt.Clear(); dt.Dispose();
            T.InnerHtml = listSTR;
        }


        protected void GetContent()
        {
            int ReCount = 0;
            int PgCount = 0;
            int PageIndex = 1;
            if (JuSNS.Common.Input.IsInteger(Request.QueryString["Page"]))
            {
                PageIndex = Convert.ToInt32(Request.QueryString["Page"]);
            } 
            string classID = Request.QueryString["classid"];
            string key = Request.QueryString["key"];
            DataTable dt = null;
            if (!string.IsNullOrEmpty(r))
            {
                #region 条件
                switch (r)
                {
                    case "all":
                        this.li_all.Attributes.Add("class", "current");
                        this.li_my.Attributes.Add("class", "");
                        this.li_none.Attributes.Add("class", "");
                        this.li_unclose.Attributes.Add("class", "");
                        this.li_isclose.Attributes.Add("class", "");
                        this.li_ji.Attributes.Add("class", "");
                        if (!string.IsNullOrEmpty(key))
                        {
                            this.ptitle.InnerText = key + "__问题";
                            SqlConditionInfo st = new SqlConditionInfo("@key", key, TypeCode.String);
                            st.Blur = 3;
                            dt = UtilPage.GetPage("ask_all_key_aspx", PageIndex, 40, out ReCount, out PgCount, st);
                        }
                        else
                        {
                            this.xinzhiDiv.Visible = true;
                            this.ptitle.InnerText = "所有问题";
                            this.askTitle.InnerHtml = "所有问题";
                            if (JuSNS.Common.Input.IsInteger(classID))
                            {
                                dt = UtilPage.GetPage("ask_all_class_aspx", PageIndex, 40, out ReCount, out PgCount, new SqlConditionInfo("@ClassID", Convert.ToInt32(classID), TypeCode.Int32));
                            }
                            else
                            {
                                dt = UtilPage.GetPage("ask_all_aspx", PageIndex, 40, out ReCount, out PgCount, null);
                            }
                        }
                        break;
                    case "my":
                        if (this.GetUserID() == 0){Response.Redirect("../ask?r=all");}
                        this.ptitle.InnerText = "我的问题";
                        this.askTitle.InnerHtml = "我的问题";
                        this.li_all.Attributes.Add("class", "");
                        this.li_my.Attributes.Add("class", "current");
                        this.li_none.Attributes.Add("class", "");
                        this.li_unclose.Attributes.Add("class", "");
                        this.li_isclose.Attributes.Add("class", "");
                        this.li_ji.Attributes.Add("class", "");
                        dt = UtilPage.GetPage("ask_my_aspx", PageIndex, 40, out ReCount, out PgCount, new SqlConditionInfo("@UserID", this.GetUserID(), TypeCode.Int32));
                        break;
                    case "unclose":
                        this.ptitle.InnerText = "待解决的问题";
                        this.askTitle.InnerHtml = "待解决的问题";
                        this.li_all.Attributes.Add("class", "");
                        this.li_my.Attributes.Add("class", "");
                        this.li_none.Attributes.Add("class", "");
                        this.li_unclose.Attributes.Add("class", "current");
                        this.li_isclose.Attributes.Add("class", "");
                        this.li_ji.Attributes.Add("class", "");
                        dt = UtilPage.GetPage("ask_unclose_aspx", PageIndex, 40, out ReCount, out PgCount, null);
                        break;
                    case "isclose":
                        this.ptitle.InnerText = "已解决的问题";
                        this.askTitle.InnerHtml = "已解决的问题";
                        this.li_all.Attributes.Add("class", "");
                        this.li_my.Attributes.Add("class", "");
                        this.li_none.Attributes.Add("class", "");
                        this.li_unclose.Attributes.Add("class", "");
                        this.li_isclose.Attributes.Add("class", "current");
                        this.li_ji.Attributes.Add("class", "");
                        dt = UtilPage.GetPage("ask_isclose_aspx", PageIndex, 40, out ReCount, out PgCount, null);
                        break;
                    case "ji":
                        this.ptitle.InnerText = "紧急问题";
                        this.askTitle.InnerHtml = "紧急问题";
                        this.li_all.Attributes.Add("class", "");
                        this.li_my.Attributes.Add("class", "");
                        this.li_none.Attributes.Add("class", "");
                        this.li_unclose.Attributes.Add("class", "");
                        this.li_isclose.Attributes.Add("class", "");
                        this.li_ji.Attributes.Add("class", "current");
                        this.xinzhiDiv.Visible = true;
                        dt = UtilPage.GetPage("ask_ji_aspx", PageIndex, 40, out ReCount, out PgCount, null);
                        break;
                    default:
                        this.li_all.Attributes.Add("class", "");
                        this.li_my.Attributes.Add("class", "");
                        this.li_none.Attributes.Add("class", "current");
                        this.li_unclose.Attributes.Add("class", "");
                        this.li_isclose.Attributes.Add("class", "");
                        this.li_ji.Attributes.Add("class", "");
                        break;
                }
                #endregion 条件
            }
            else
            {
                if (this.GetUserID() == 0) { Response.Redirect("../ask?r=all"); }
                this.askTitle.InnerHtml = "朋友的问题";
                dt = UtilPage.GetPage("ask_friend_aspx", PageIndex, 40, out ReCount, out PgCount, new SqlConditionInfo("@UserID", this.GetUserID(), TypeCode.Int32));
            }
            DataTable data = new DataTable();
            data.Columns.Add("d", typeof(string));
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                StringBuilder sb = new StringBuilder();
                DataRow dr = data.NewRow();
                string isJI = string.Empty;
                if (dt.Rows[i]["isClose"].ToString() == "1")
                {
                    isJI += "<span class=\"green font12\">(已解决)</span>";
                }
                if (dt.Rows[i]["isJinji"].ToString() == "1")
                {
                    isJI += "<span class=\"reshow font12\">[急]</span>";
                }
                //green
                string DelSTR = string.Empty;
                if (dt.Rows[i]["UserID"].ToString() == this.GetUserID().ToString())
                {
                    if (dt.Rows[i]["isClose"].ToString() != "1")
                    {
                        DelSTR = "<span class=\"c999 font12\"><a href=\"edit.aspx?ID=" + dt.Rows[i]["ID"].ToString() + "\">编辑</a>|<a href=\"index.aspx?r=" + r + "&ID=" + dt.Rows[i]["ID"].ToString() + "&action=del\" onclick=\"return delask();\">删除</a></span>";
                    }
                }
                sb.Append("<li>• <a href=\"" + rooDir + "/ask/q" + dt.Rows[i]["ID"].ToString() + ".aspx\" title=\"" + dt.Rows[i]["Title"].ToString() + "\">" + isJI + "" + JuSNS.Common.Input.GetSubString(dt.Rows[i]["Title"].ToString(), 46) + "</a> <span class=\"c333 font12\">[<a href=\"../ask/?r=all&classID=" + dt.Rows[i]["ClassID"].ToString() + "\">" + dt.Rows[i]["ClassName"].ToString() + "</a>]</span> " + DelSTR + "</li>");
                dr["d"] = sb.ToString();
                data.Rows.Add(dr);
            }
            dt.Clear(); dt.Dispose();
            this.ContentList.Visible = true;
            this.ContentList.DataSource = data;
            this.ContentList.DataBind();
            this.LtrNum.Text = ReCount.ToString();
            if (ReCount > 40)
            {
                this.DivPage.InnerHtml = PageNavigator.Pagination(PageIndex, PgCount, "/home/ask/?r=" + r + "&classID="+Request.QueryString["classID"]+"");
            }
            else
            {
                this.DivPage.Visible = false;
            }
        }


        public string GetAskClass(int pid, int Num)
        {
            string listSTR = string.Empty;
            DataTable dt = ask.GetAskClass(pid, Num);
            int gRowscount = dt.Rows.Count;
            for (int i = 0; i < gRowscount; i++)
            {
                string askCount = string.Empty;
                if (Num == 1)
                {
                    askCount = "(" + dt.Rows[i]["AskCount"].ToString() + ")";
                }
                if (pid == 0)
                {
                    listSTR += "<div class=\"font14 bold underline\" style=\"line-height:25px;\"><a href=\"index.aspx?r=all&classID=" + dt.Rows[i]["ID"] + "\">" + dt.Rows[i]["ClassName"] + "</a>" + askCount + "</div>\n";
                }
                else
                {
                    if (Num == 1)
                    {
                        listSTR += "<li style=\"height:30px;\"><a href=\"index.aspx?r=all&classID=" + dt.Rows[i]["ID"] + "\">" + dt.Rows[i]["ClassName"] + "</a>" + askCount + "</li>\n";
                    }
                    else
                    {
                        if ((i + 1) % 3 == 0)
                        {
                            listSTR += "<a href=\"index.aspx?r=all&classID=" + dt.Rows[i]["ID"] + "\">" + dt.Rows[i]["ClassName"] + "</a>" + askCount + "<br />\n";
                        }
                        else
                        {
                            listSTR += "<a href=\"index.aspx?r=all&classID=" + dt.Rows[i]["ID"] + "\">" + dt.Rows[i]["ClassName"] + "</a>" + askCount + "&nbsp;\n";
                        }
                    }
                }
                listSTR += GetAskClass(Convert.ToInt32(dt.Rows[i]["ID"].ToString()), 0);
                if (!string.IsNullOrEmpty(GetAskClass(Convert.ToInt32(dt.Rows[i]["ID"].ToString()), 0)))
                {
                    listSTR += "<a href=\"index.aspx?r=all&classID=" + dt.Rows[i]["ID"] + "\">...</a>";
                }
            }
            return listSTR;
        }
    }
}
