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

namespace JuSNS.Web.home.vote
{
    public partial class vote : JuSNS.UI.Page.UserPage
    {
        Vote v = new Vote();
        VoteInfo voteinfo = new VoteInfo();
        VoteOption vo = new VoteOption();
        protected string r = "";
        protected int userid = 0;
        protected string username = "";
        protected string option = string.Empty;
        protected int jcnt = 0;
        protected int vcnt = 0;
        protected string answer = "";
        protected string comm = "";
        protected string intro = "";
        protected string voteharf = "";
        protected string mycomm = "发表你的意见...";
        protected int pid = 0;
        protected string rcnt = "";
        protected string tempreplycnt = "";
        protected string del = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            Response.CacheControl = "no-cache";
            getInfo();
            DataBind();
        }

        protected void getInfo()
        {
            r = Request.QueryString["r"];
            string t = Request.QueryString["t"];
            voteinfo = v.Info(Convert.ToInt32(r));
            if (voteinfo.ID == 0)
            {
                PageError("参数传递错误", "");
            }
            this.ptitle.InnerText = "投票：" + voteinfo.Title;
            this.Title.Text = Input.HtmlDecode(voteinfo.Title);
            DateTime dnow = DateTime.Now;
            DateTime dvote = voteinfo.EndTime;
            TimeSpan ts = dnow.Subtract(dvote);
            if (ts.Days > 0)
            {
                this.Button1.Disabled = true;
                this.Button1.Value = "此投票已过期。";
            }
            else
            {
                this.Button1.Attributes.Add("onclick", "govote(" + r + ");");
            }
            this.AddTime.Text = JuSNS.Common.Public.getTimeSpan(voteinfo.AddTime);
            jcnt = voteinfo.JCnt;
            vcnt = voteinfo.VCnt;
            userid = voteinfo.UserID;
            intro = "<div class=\"content c999\" style=\"padding:20px 10px 10px 100px;\">" + Input.HtmlEncode(voteinfo.Intro) + "</div>";
            User user = new User();
            UserInfo userinfo = new UserInfo();
            userinfo = user.GetUserInfo(userid);
            username = userinfo.Username;
            this.lefter.Visible = true;
            this.righter.Visible = true;

            VoteComm votecomm = new VoteComm();
            ToVote tv = new ToVote();
            ToVoteInfo tvinfo = new ToVoteInfo();
            tvinfo = tv.Info(voteinfo.ID, UserID);
            if (tv.IsVote(this.UserID, voteinfo.ID))
            {
                this.Button1.Disabled = true;
                this.Button1.Value = "你已经投过此票了。";
            }

            if (tvinfo.ID > 0)
            {
                myvoteinfo.Visible = true;
                if (tvinfo.Content != null && tvinfo.Content.Trim() != "")
                {
                    pid = tvinfo.ID;
                    int tempcnt = votecomm.ReplyCnt(pid);
                    if (tempcnt > 0)
                        rcnt = "(" + tempcnt + ")";
                }
            }

            getfjoinvote();
            getuservote();
            getfriendvote(voteinfo.ID);
            getothervote(voteinfo.ID);
            option = getOption(voteinfo.ID, voteinfo.Mode);
            answer = getAnswer(voteinfo.ID, UserID);
            comm = getComm(voteinfo.ID, UserID);
            getdel(voteinfo.ID);
        }

        /// <summary>
        /// 得到是否可以删除
        /// </summary>
        /// <param name="VoteID">投票编号</param>
        /// <returns></returns>
        protected void getdel(int VoteID)
        {
            if (userid == this.UserID)
                del = " | <a href=\"javascript:void(0);\" onclick=\"javascript:Confirm(this,'确定删除此投票吗？','删除',function(){delvote(" + VoteID + "," + this.UserID + ")})\">删除</a>";
        }

        /// <summary>
        /// 取得评论回复数
        /// </summary>
        /// <param name="VoteID">评论编号</param>
        /// <param name="UID">用户编号</param>
        /// <param name="PID"></param>
        /// <returns></returns>
        protected string getReplyCnt(object VoteID, object UID, object PID)
        {
            tempreplycnt = string.Empty;
            VoteComm votecomm = new VoteComm();
            ToVote tv = new ToVote();
            ToVoteInfo tvinfo = new ToVoteInfo();
            tvinfo = tv.Info(Convert.ToInt32(VoteID), Convert.ToInt32(UID));
            if (!string.IsNullOrEmpty(tvinfo.Content))
            {
                int tempcnt = votecomm.ReplyCnt(Convert.ToInt32(PID));
                if (tempcnt > 0)
                    tempreplycnt = "(" + tempcnt + ")";
            }
            return tempreplycnt;
        }

        /// <summary>
        /// 加载好友最近参加的投票列表
        /// </summary>
        protected void getfjoinvote()
        {
            DataTable dt = v.FriendNewJoinVoteList(5, this.UserID);
            Rpt_fjoinvote.DataSource = dt;
            Rpt_fjoinvote.DataBind();
        }

        /// <summary>
        /// 加载用户其它投票列表
        /// </summary>
        protected void getuservote()
        {
            DataTable dt = v.MyAddVote(5, userid, Convert.ToInt32(r));
            Rpt_uservote.DataSource = dt;
            Rpt_uservote.DataBind();
        }

        /// <summary>
        /// 加载好友的投票选项
        /// </summary>
        /// <param name="ID">投票编号</param>
        protected void getfriendvote(int ID)
        {
            DataTable dt = v.FriendVote(this.UserID, ID);
            Rpt_friendvote.DataSource = dt;
            Rpt_friendvote.DataBind();
        }

        /// <summary>
        /// 加载其它人投票选项
        /// </summary>
        /// <param name="ID"></param>
        protected void getothervote(int ID)
        {
            DataTable dt = v.OtherVote(this.UserID, ID);
            Rpt_otherdvote.DataSource = dt;
            Rpt_otherdvote.DataBind();
        }

        /// <summary>
        /// 取得我的答案
        /// </summary>
        /// <param name="ID">投票编号</param>
        /// <param name="UID">会员编号</param>
        /// <returns></returns>
        protected string getAnswer(int ID, int UID)
        {
            bool tf = false;
            StringBuilder s = new StringBuilder();
            List<VoteOptionInfo> list = vo.OptionList(ID, UID, out tf);
            if (tf)
            {
                if (list.Count > 0)
                {
                    for (int i = 0; i < list.Count; i++)
                    {
                        s.Append("<div class=\"padding10\">" + list[i].OptionName + "</div>");
                    }
                    return "<p class=\"ansjudgeinfo padding10\">" + s.ToString() + "</p>";
                }
            }
            return string.Empty;
        }

        /// <summary>
        /// 取得评论内容
        /// </summary>
        /// <param name="ID">投票编号</param>
        /// <param name="UID">会员编号</param>
        /// <returns></returns>
        protected string getComm(int ID, int UID)
        {
            ToVote tv = new ToVote();
            ToVoteInfo tvinfo = new ToVoteInfo();
            tvinfo = tv.Info(ID, UID);
            return "<span class=\"blue pstr padding10\">" + Input.HtmlEncode(tvinfo.Content) + "</span>";
        }

        /// <summary>
        /// 取得投票选项
        /// </summary>
        /// <param name="ID">投票编号</param>
        /// <returns>返回结果</returns>
        protected string getOption(int ID, int Mode)
        {
            StringBuilder s = new StringBuilder();
            List<VoteOptionInfo> list = vo.OptionList(ID);
            for (int i = 0; i < list.Count; i++)
            {
                float j = 0;
                if (vcnt != 0)
                {
                    j = ((float)list[i].Cnt / (float)vcnt) * 100;
                }
                string o = string.Empty;
                if (Mode == 1)//单选
                {
                    o = "<input name=\"Option_" + ID.ToString() + "\" type=\"radio\" value=\"" + list[i].ID + "\" /> ";
                }
                else//复选
                {
                    o = "<input name=\"Option_" + ID.ToString() + "\" type=\"checkbox\" value=\"" + list[i].ID + "\" /> ";
                } 
                
                s.Append("<div class=\"voteinfo\">\r\n");
                s.Append(Input.HtmlDecode(list[i].OptionName) + "：</div>\r\n");
                s.Append("<div class=\"voteplan\">\r\n");
                s.Append("        <div style=\"border:1px solid #ababab;\"><div class=\"plan\" style=\"border:1px solid #f4f9fd;background-color:#" + JuSNS.Common.Rand.Str_Color(true) + ";width:" + j + "%;\"></div></div>\r\n");
                s.Append("</div>\r\n");
                s.Append("<div class=\"number\"><span class=\"nl\">");
                s.Append(list[i].Cnt + "(" + Convert.ToInt32(j) + "%)</span><span class=\"nr\">" + o + "</span></div>\r\n");

                s.Append("<div class=\"c\"></div>\r\n");
            }
            return s.ToString();
        }
    }
}
