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

namespace JuSNS.Web.home.ask
{
    public partial class add : JuSNS.UI.Page.UserPage
    {
        public int isjiFen = 0;
        public int myJiFen = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            string a = Request.QueryString["a"];
            if (a == "child")
            {
                GetChildClass(Convert.ToInt32(Request.QueryString["pid"]));
            }
            else
            {
                if (!string.IsNullOrEmpty(Request.QueryString["r"]))
                {
                    this.DivMsg1.Visible = true;
                    this.DivMsg1.InnerHtml = Request.QueryString["r"];
                }
                JuSNS.Home.User us = new JuSNS.Home.User();
                JuSNS.Model.UserInfo mdl = new JuSNS.Model.UserInfo();
                mdl = us.GetUserInfo(this.UserID);
                myJiFen = mdl.integral;
                int intel = Convert.ToInt32(JuSNS.Common.Public.GetXMLValue("intel"));
                isjiFen = intel * 20;

                for (int i = 0; i <= 10; i++)
                {
                    string str = (i * intel * 10).ToString();
                    this.TxtJiFen.Items.Add(new ListItem(str + "分", str));
                    this.TxtJiFen.DataBind();
                }
                GetClass(this.TxtClassID, 0);
            }
        }

        protected void GetChildClass(int Pid)
        {
            JuSNS.Home.Ask ask = new JuSNS.Home.Ask();
            DataTable dt = ask.GetAskClass(Pid, 1);
            string listSTR = "<select id=\"TxtchildClassID\" name=\"TxtchildClassID\">";
            if (dt != null && dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    listSTR += "<option value=\"" + dt.Rows[i]["ID"].ToString() + "\">" + dt.Rows[i]["ClassName"].ToString() + "</option>";
                }
            }
            else
            {
                listSTR += "<option value=\"\">无下级分类</option>";
            }
            listSTR += "</select>";
            dt.Clear(); dt.Dispose();
            Response.Write(listSTR);
            Response.End();
        }

        protected void GetClass(DropDownList l, int Pid)
        {
            JuSNS.Home.Ask ask = new JuSNS.Home.Ask();
            DataTable dt = ask.GetAskClass(Pid, 1);
            if (dt != null && dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    l.Items.Add(new ListItem(dt.Rows[i]["ClassName"].ToString(), dt.Rows[i]["ID"].ToString()));
                }
            }
            dt.Clear(); dt.Dispose();
            l.DataBind();
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            JuSNS.Model.AskInfo askinfo = new JuSNS.Model.AskInfo();
            JuSNS.Home.Ask ask = new JuSNS.Home.Ask();
            string ERR = string.Empty;
            int ParentID = 0;
            string Title = Request.Form["TxtTitle"];
            if (!string.IsNullOrEmpty(Request.Form["TxtTitle"]))
            {
                Title = Request.Form["TxtTitle"];
            }
            else
            {
                ERR += "<li>栏目不能为空</li>";
            }
            string Content = string.Empty;
            if (!string.IsNullOrEmpty(Request.Form["TxtContent"]))
            {
                Content = Request.Form["TxtContent"];
            }
            else
            {
                ERR += "<li>内容不能为空</li>";
            }

            string Tag = string.Empty;
            if (!string.IsNullOrEmpty(Request.Form["TxtTag"]))
            {
                Tag = Request.Form["TxtTag"];
            }

            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;
            }
            int JiFen=0;
            if (!string.IsNullOrEmpty(Request.Form["TxtJiFen"]))
            {
                JiFen = Convert.ToInt32(Request.Form["TxtJiFen"]);
            }
            int isJinji = 0;
            if (this.TxtJinji.Checked)
            {
                isJinji = 1;
            }
            int rJiFen = isjiFen + JiFen;
            if (myJiFen < rJiFen)
            {
                ERR += "<li>你积分不够</li>";
            }
            int ClassID = 0;
            if (!string.IsNullOrEmpty(Request.Form["TxtchildClassID"]))
            {
                ClassID = Convert.ToInt32(Request.Form["TxtchildClassID"]);
            }
            else
            {
                if (!string.IsNullOrEmpty(Request.Form["TxtClassID"]))
                {
                    ClassID = Convert.ToInt32(Request.Form["TxtClassID"]);
                }
                else
                {
                    ERR += "<li>栏目不能为空</li>";
                }
            }
            if (ERR != string.Empty)
            {
                Response.Redirect("add.aspx?r=" + ERR + "");
            }
            askinfo.UserID = this.UserID;
            askinfo.Title = Title;
            askinfo.Tag = Tag;
            askinfo.Pic = Pic;
            askinfo.ParentID = ParentID;
            askinfo.jiFen = JiFen;
            askinfo.IsLock = Convert.ToInt32(JuSNS.Common.Public.GetXMLValue("isCheckAsk"));
            askinfo.IsJinji = isJinji;
            askinfo.isClose = 0;
            askinfo.CreatTime = DateTime.Now;
            askinfo.Content = Content;
            askinfo.Click = 0;
            askinfo.ClassID = ClassID;
            if (ask.Add(askinfo, rJiFen, 0) > 0)
            {
                PageRight("问题已经提交。", rooDir + "/home/ask?r=my");
                //Response.Redirect("../ask?r=my");
            }
        }
    }
}
