using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using JuSNS.Model;
using JuSNS.Common;

namespace JuSNS.Web.inc.Ajax.vote
{
    public partial class vote : JuSNS.UI.Page.UserPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Response.CacheControl = "no-cache";
            start();
        }

        /// <summary>
        ///  开始操作
        /// </summary>
        protected void start()
        {
            string t = Request.QueryString["t"];
            string id = Request.QueryString["id"];
            string option = Request.QueryString["option"];
            string comm = Request.QueryString["comm"];
            string pid = Request.QueryString["pid"];
            string uid = Request.QueryString["uid"];

            int VoteID = Convert.ToInt32(id);
            if (VoteID == 0)
                Response.End();
            switch (t)
            {
                case "disvote":
                    DisVote(VoteID);
                    break;
                case "tovote":
                    ToVote(VoteID, comm, option);
                    break;
                case "getcomm":
                    GetComm(VoteID);
                    break;
                case "savecomm":
                    SaveComm(VoteID, comm);
                    break;
                case "replylist":
                    ReplyList(Convert.ToInt32(pid));
                    break;
                case "goreply":
                    VGoreply(Convert.ToInt32(pid), VoteID, comm, Convert.ToInt32(uid));
                    break;
                case "delreply":
                    DelReply(Convert.ToInt32(id));
                    break;
                case "delvote":
                    DelVote(Convert.ToInt32(id));
                    break;
            }
        }

        /// <summary>
        /// 删除投票
        /// </summary>
        /// <param name="ID">投票编号</param>
        protected void DelVote(int ID)
        {
            JuSNS.Home.Vote vote = new JuSNS.Home.Vote();
            vote.Del(ID, this.UserID);
        }

        /// <summary>
        /// 删除回复
        /// </summary>
        /// <param name="ID">回复编号</param>
        protected void DelReply(int ID)
        {
            JuSNS.Home.VoteComm votecomm = new JuSNS.Home.VoteComm();
            votecomm.Del(ID, this.UserID);
        }

        /// <summary>
        /// 添加评论回复
        /// </summary>
        /// <param name="PID">评论编号</param>
        /// <param name="VoteID">投票编号</param>
        /// <param name="comm">回复内容</param>
        protected void VGoreply(int PID, int VoteID, string comm,int uid)
        {
            JuSNS.Home.VoteComm votecomm = new JuSNS.Home.VoteComm();
            VoteCommInfo comminfo = new VoteCommInfo();

            comminfo.ID = 0;
            comminfo.PID = PID;
            comminfo.UserID = this.UserID;
            comminfo.VoteID = VoteID;
            comminfo.Content = comm;
            comminfo.AddTime = DateTime.Now;

            votecomm.Add(comminfo, uid);

            ReplyList(PID);
        }


        /// <summary>
        /// 取得评论列表
        /// </summary>
        /// <param name="PID"></param>
        protected void ReplyList(int PID)
        {
            StringBuilder s = new StringBuilder();
            if (PID == 0)
            {
                s.Append("");
            }
            else
            {
                JuSNS.Home.VoteComm comm = new JuSNS.Home.VoteComm();
                JuSNS.Home.User user = new JuSNS.Home.User();
                List<VoteCommInfo> list = comm.List(PID);
                for (int i = 0; i < list.Count; i++)
                {
                    UserInfo uinfo = new UserInfo();
                    uinfo = user.GetUserInfo(list[i].UserID);
                    if (uinfo.UserID == this.UserID)
                    {
                        s.Append("<div id=\"sapnreply_" + list[i].ID + "\" class=\"nl\"><span class=\"nl\"><a href=\""+rooDir+"/user/?r=" + uinfo.UserID + "\">" + uinfo.Username + "</a>：" + Input.HtmlEncode(list[i].Content) + " <span class=\"time\">" + Public.getTimeEXSpan(list[i].AddTime) + "</span></span> <span class=\"delete\"><a href=\"javascript:void(0)\" onclick=\"javascript:Confirm(this,'确定删除此回复吗？','删除',function(){delvreply((" + list[i].ID + "))})\" title=\"删除此信息\">删除</a></span><div class=\"c\"></div></div>");
                    }
                    else
                    {
                        s.Append("<div class=\"nl\"><a href=\"" + rooDir + "/user/?r=" + uinfo.UserID + "\">" + uinfo.Username + "</a>：" + Input.HtmlEncode(list[i].Content) + " <span class=\"time\">" + Public.getTimeEXSpan(list[i].AddTime) + "</span></div><div class=\"c\"></div>");
                    }
                }
            }
            Response.Write(s.ToString());
            Response.End();
        }

        /// <summary>
        /// 取得评论内容
        /// </summary>
        /// <param name="ID">投票编号</param>
        protected void GetComm(int ID)
        {
            JuSNS.Home.ToVote tovote = new JuSNS.Home.ToVote();
            ToVoteInfo tovoteinfo = new ToVoteInfo();
            tovoteinfo = tovote.Info(ID, this.UserID);
            Response.Write(Input.HtmlEncode(tovoteinfo.Content));
            Response.End();
        }

        /// <summary>
        /// 保存评论内容
        /// </summary>
        /// <param name="ID">投票编号</param>
        /// <param name="Comm">评论内容</param>
        /// <param name="uid">用户</param>
        protected void SaveComm(int ID, string Comm)
        {
            JuSNS.Home.ToVote tovote = new JuSNS.Home.ToVote();
            ToVoteInfo tovoteinfo = new ToVoteInfo();

            tovoteinfo.ID = 0;
            tovoteinfo.OptionID = "";
            tovoteinfo.UserID = this.UserID;
            tovoteinfo.VoteID = ID;
            tovoteinfo.Content = Comm;

            tovote.Add(tovoteinfo);

            Response.Write(Input.HtmlEncode(Comm));
            Response.End();
        }


        /// <summary>
        /// 投票不感兴趣
        /// </summary>
        /// <param name="ID">投票编号</param>
        protected void DisVote(int ID)
        {
            JuSNS.Home.DisVote dv = new JuSNS.Home.DisVote();
            JuSNS.Home.Vote vote = new JuSNS.Home.Vote();
            DisVoteInfo dvinfo = new DisVoteInfo();
            VoteInfo vinfo = new VoteInfo();

            dvinfo.ID = 0;
            dvinfo.UserID = this.UserID;
            dvinfo.VoteID = ID;
            dv.Add(dvinfo);

            vinfo = vote.Info(ID);

            Response.Write("<p><a href=\"vote.aspx?r=" + ID + "\">" + Input.HtmlEncode(vinfo.Title) + "</a></p><p>不感兴趣。</p>");
            Response.End();
        }

        /// <summary>
        /// 开始投票
        /// </summary>
        /// <param name="ID">投票编号</param>
        /// <param name="Comm">评论内容</param>
        /// <param name="Option">选项编号</param>
        protected void ToVote(int ID, string Comm, string Option)
        {
            JuSNS.Home.Vote vote = new JuSNS.Home.Vote();
            VoteInfo vinfo = new VoteInfo();
            bool tf = true;
            if (Comm.Trim() == "" && Option.Trim() == "")
                tf = false;
            vinfo = vote.Info(ID);
            DateTime dnow = DateTime.Now;
            DateTime dvote = vinfo.EndTime;
            TimeSpan ts = dnow.Subtract(dvote);
            if (ts.Days > 0)
            {
                Response.Write("0|此投票已过期。");
                Response.End();
            }

            //判断是否已经投票
            JuSNS.Home.ToVote tovote = new JuSNS.Home.ToVote();
            if (tovote.IsVote(this.UserID, ID))
            {
                Response.Write("0|已经投过了，不能再投票了。");
                Response.End();
            }

            ToVoteInfo tovoteinfo = new ToVoteInfo();

            tovoteinfo.ID = 0;
            tovoteinfo.OptionID = Option;
            tovoteinfo.UserID = this.UserID;
            tovoteinfo.VoteID = ID;
            tovoteinfo.Content = Comm;

            tovote.Add(tovoteinfo);

            string s = string.Empty;
            if (tf)
            {
                s = "1|投票成功 。";
            }
            else
            {
                s = "0|>投票失败。参数传递错误！";
            }
            Response.Write(s);
            Response.End();
        }
    }
}
