using System;
using System.Data;
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.Model;

namespace JuSNS.Web.home.ask
{
    public partial class content : JuSNS.UI.Page.BasePage
    {
        JuSNS.Home.Ask ask = new JuSNS.Home.Ask();
        JuSNS.Model.AskInfo askinfo = new JuSNS.Model.AskInfo();
        protected int qID = 0;
        protected int myUserID = 0;
        protected int gUserID = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string a = Request.QueryString["a"];
                if (a == "update")
                {
                    ask.UpdateAskBest(Convert.ToInt32(Request.QueryString["id"]), this.GetUserID());
                }
                else
                {
                    if (JuSNS.Common.Input.IsInteger(Request.QueryString["id"]))
                    {
                        if (Request.QueryString["id"] == "0")
                        {
                            Response.Redirect(rooDir + "/home/ask?r=my");
                        }
                        qID = Convert.ToInt32(Request.QueryString["id"]);
                        askinfo = ask.GetAskInfo(qID);
                        ask.UpdateAskClick(qID);
                        this.ptitle.InnerText = askinfo.Title;
                        this.qqID.Value = askinfo.ID.ToString();
                        this.askTitle.InnerHtml = askinfo.Title;
                        this.askClass.InnerHtml = GetAskClass(0, 0);
                        myUserID = askinfo.UserID;
                        gUserID = this.GetUserID();
                        if (this.GetUserID() == 0)
                        {
                            this.li_my.InnerHtml = "<a href=\"" + rooDir + "/home/ask/?r=my\" onclick=\"javascript:{unlogin();return false}\">我的问题</a>";
                            this.li_none.InnerHtml = "<a href=\"" + rooDir + "/home/ask/\" onclick=\"javascript:{unlogin();return false}\">朋友的问题</a>";
                        }
                        else
                        {
                            this.li_my.InnerHtml = "<a href=\"" + rooDir + "/home/ask/?r=my\">我的问题</a>";
                            this.li_none.InnerHtml = "<a href=\"" + rooDir + "/home/ask/\">朋友的问题</a>";
                            if (ask.GetChildCount(this.GetUserID(), Convert.ToInt32(this.qqID.Value)) > 0)
                            {
                                this.ReplayForm.InnerHtml = "<span class=\"reshow\">你已经回复过此问题了。</span>";
                            }
                        }
                        if (askinfo.isClose == 1)
                        {
                            this.ReplayForm.InnerHtml = "<span class=\"reshow\">问题已经关闭，不能回复</span>";
                        }
                        if (askinfo.UserID != this.GetUserID())
                        {
                            this.ReplayForm.Visible = true;
                        }
                        if (this.GetUserID() == 0)
                        {
                            this.ReplayForm.Visible = false;
                        }
                        GetXinZhi(this.uncloseHTML, 0, 5, askinfo.ClassID);
                        GetXinZhi(this.hiJiFenHTML, 1, 5, askinfo.ClassID);
                        GetInfo();
                        GetChildAsk();
                    }
                    else
                    {
                        Response.Redirect(rooDir + "/home/ask?r=my");
                    }
                }
            }
        }

        protected void GetInfo()
        {
            string g = string.Empty;
            if (askinfo.jiFen > 0)
            {
                g = " <img src=\"" + rooDir + "/template/" + JuSNS.Config.UiConfig.SkinStyle + "/images/g.gif \" align=\"absmiddle\" alt=\"悬赏" + askinfo.jiFen + "积分\" /><span class=\"normal font12\">" + askinfo.jiFen+"</span>";
            }
            if (askinfo.isClose == 0)
            {
                if (askinfo.UserID == this.GetUserID())
                {
                    g += " <span class=\"normal font12\"><a href=\"javascript:void(0);\" onclick=\"EditAskInfo('" + askinfo.ID + "')\">编辑</a></span>";
                }
            }
            this.askTitleSpan.InnerHtml = askinfo.Title + g;
            string tags = string.Empty;
            if (!string.IsNullOrEmpty(askinfo.Tag))
            {
                string[] arr = askinfo.Tag.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
                if (arr!=null)
                {
                    tags += "<div>标签：";
                    for (int i = 0; i < arr.Length; i++)
                    {
                        tags += "<a href=\"" + rooDir + "/home/ask/index.aspx?r=all&key=" + JuSNS.Common.Input.URLEncode(arr[i]) + "\">" + arr[i] + "</a> ";
                    }
                    tags += "</div>";
                }
            }
            string Pic = string.Empty;
            if (!string.IsNullOrEmpty(askinfo.Pic))
            {
                Pic = "<a href=\"" + rooDir + "/" + askinfo.Pic + "\" target=\"_blank\"><img src=\"" + rooDir + "/" + askinfo.Pic + "\" align=\"left\" alt=\"点击放大\" style=\"padding:3px;border:1px solid #eeeeee;width:120px;margin:0 10px 5px 0;\" /></a>";
            }
            this.askContentSpan.InnerHtml = tags + Pic + JuSNS.Common.Public.CheckSma(askinfo.Content);
            string AuthorSTR = string.Empty;
            JuSNS.Home.User us = new JuSNS.Home.User();
            JuSNS.Model.UserInfo mdl = new JuSNS.Model.UserInfo();
            mdl = us.GetUserInfo(askinfo.UserID);
            AuthorSTR += "提问者：<a href=\"" + rooDir + "/user/" + JuSNS.Common.Public.URLWrite("", "r", askinfo.UserID.ToString()) + "\">" + mdl.Username + "</a> &nbsp; <span class=\"c999\">人气:" + askinfo.Click + " &nbsp; 提问时间:" + askinfo.CreatTime.ToString("yyyy-MM-dd hh:mm") + "</span>";
            this.askAuthor.InnerHtml = AuthorSTR;
            GetChildAsk();
        }

        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=\""+rooDir+"/home/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 GetChildAsk()
        {
            string listSTR = string.Empty;
            List<AskInfo> InfoList = ask.GetChildAskList(qID);
            JuSNS.Home.User us = new JuSNS.Home.User();
            JuSNS.Model.UserInfo mdl = new JuSNS.Model.UserInfo();
            for (int i = 0; i < InfoList.Count; i++)
            {
                listSTR += "<fieldset>";
                string myPost = string.Empty;
                if (askinfo.isClose != 1)
                {
                    if (myUserID == this.GetUserID())
                    {
                        if (InfoList[i].UserID != this.GetUserID())
                        {
                            myPost = " <a href=\"javascript:void(0);\" onclick=\"setBest(" + InfoList[i].ID + ")\" />设为最佳答案</a>";
                        }
                    }
                }
                string Pic = string.Empty;
                if (!string.IsNullOrEmpty(InfoList[i].Pic))
                {
                    Pic = "<span class=\"image\"><a href=\"" + rooDir + "/" + InfoList[i].Pic + "\" target=\"_blank\"><img src=\"" + rooDir + "/" + InfoList[i].Pic + "\" align=\"left\" alt=\"点击放大\" style=\"padding:3px;border:1px solid #eeeeee;width:60px;height:60px;margin:0 10px 5px 0;\" /></a></span>";
                }
                if (InfoList[i].isBest == 1)
                {
                    listSTR += "<legend>最佳答案</legend>";
                    listSTR += "<div class=\"askcontent\">" + Pic + JuSNS.Common.Public.CheckSma(InfoList[i].Content) + "</div>";
                }
                else
                {
                    listSTR += "<legend class=\"c999 font14 normal\">答案" + (i+1) + "</legend>";
                    listSTR += "<div class=\"askcontent\">" + Pic + JuSNS.Common.Public.CheckSma(InfoList[i].Content) + "</div>";
                }
                mdl = us.GetUserInfo(InfoList[i].UserID);
                listSTR += "<div class=\"c\"></div><a href=\"" + rooDir + "/user/" + JuSNS.Common.Public.URLWrite("", "r", InfoList[i].UserID.ToString()) + "\">" + mdl.Username + "</a> &nbsp; <span class=\"c999\"> 回复时间:" + JuSNS.Common.Public.getTimeEXTSpan(InfoList[i].CreatTime) + "</span>" + myPost + "";
                listSTR += "</fieldset>";
                listSTR += "<div class=\"spline\"></div>";
            }
            this.askRePlayContent.InnerHtml = listSTR;
        }

        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=\""+rooDir+"/home/ask/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=\"" + rooDir + "/home/ask/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=\"" + rooDir + "/home/ask/index.aspx?r=all&classID=" + dt.Rows[i]["ID"] + "\">" + dt.Rows[i]["ClassName"] + "</a>" + askCount + "<br />\n";
                        }
                        else
                        {
                            listSTR += "<a href=\"" + rooDir + "/home/ask/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=\"" + rooDir + "/home/ask/index.aspx?r=all&classID=" + dt.Rows[i]["ID"] + "\">...</a>";
                }
            }
            return listSTR;
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                if (GetUserID() != 0)
                {
                    JuSNS.Model.AskInfo ainfo = new JuSNS.Model.AskInfo();
                    string Content = this.TxtContent.Text;
                    string Pic = string.Empty;
                    string filename = string.Empty;
                    string path = "uploads/ask";
                    if (this.TxtPic.HasFile)
                    {
                        JuSNS.Common.Input.UploadFile(this.TxtPic.PostedFile, path, out filename);
                        Pic = path + "/" + filename;
                    }
                    ainfo.UserID = this.GetUserID();
                    ainfo.Title = string.Empty;
                    ainfo.Tag = string.Empty;
                    ainfo.Pic = Pic;
                    ainfo.ParentID = Convert.ToInt32(this.qqID.Value);
                    ainfo.jiFen = 0;
                    ainfo.IsLock = 0;
                    ainfo.IsJinji = 0;
                    ainfo.isClose = 0;
                    ainfo.CreatTime = DateTime.Now;
                    ainfo.Content = this.TxtContent.Text;
                    ainfo.Click = 0;
                    ainfo.ClassID = askinfo.ClassID;
                    int intel = Convert.ToInt32(JuSNS.Common.Public.GetXMLValue("intel"));
                    int nJiFen = 2 * intel;
                    if (ask.GetChildCount(this.GetUserID(), Convert.ToInt32(this.qqID.Value)) > 0)
                    {
                        nJiFen = 10000;
                        this.ErrSTRDiv.Visible = true;
                    }
                    else
                    {
                        if (ask.Add(ainfo, nJiFen, 1) > 0)
                        {
                            Response.Redirect("q" + this.qqID.Value + ".aspx#startForm");
                        }
                        Response.Redirect("q" + this.qqID.Value + ".aspx#startForm");
                    }
                }
            }
        }
    }
}
