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.UI.Control;
using JuSNS.Home;
using JuSNS.Model;
using JuSNS.Common;

namespace JuSNS.Web.home.vote
{
    public partial class index : JuSNS.UI.Page.UserPage
    {
        Vote v = new Vote();
        VoteOption vo = new VoteOption();
        protected void Page_Load(object sender, EventArgs e)
        {
            Response.CacheControl = "no-cache";
            DataBind();
            LoadList();
        }

        /// <summary>
        /// 加载列表
        /// </summary>
        protected void LoadList()
        {
            int c = 0, p = 0, PageIndex = 0;
            PageIndex = Convert.ToInt32(Request.QueryString["Page"]);
            string r = Request.QueryString["r"];
            int uID = 0;
            if (JuSNS.Common.Input.IsInteger(r))
            {
                uID = Convert.ToInt32(r);
            }
            else
            {
                uID = this.UserID;
            }
            SqlConditionInfo st = new SqlConditionInfo("@UserID", uID, TypeCode.Int32);
            DataTable dt = null;
            if (string.IsNullOrEmpty(r))
            {
                dt = UtilPage.GetPage("vote_new_aspx", PageIndex, 20, out c, out p, st);
                this.v1.Attributes.Add("class", "current");
                this.v2.Attributes.Add("class", "");
                this.v3.Attributes.Add("class", "");
                this.v4.Attributes.Add("class", "");
                this.v5.Attributes.Add("class", "");
                this.ptitle.InnerText = "最新投票";
                this.bName.InnerHtml = "最新投票";
            }
            else
            {
                if (!JuSNS.Common.Input.IsInteger(r))
                {
                    switch (r)
                    {
                        case "friend":
                            this.ptitle.InnerText = "朋友的投票";
                            this.bName.InnerHtml = "朋友的投票";
                            this.v1.Attributes.Add("class", "");
                            this.v2.Attributes.Add("class", "current");
                            this.v3.Attributes.Add("class", "");
                            this.v4.Attributes.Add("class", "");
                            this.v5.Attributes.Add("class", "");
                            dt = UtilPage.GetPage("vote_friend_aspx", PageIndex, 20, out c, out p, st);
                            break;
                        case "my":
                            this.ptitle.InnerText = "我的投票";
                            this.bName.InnerHtml = "我的投票";
                            this.v1.Attributes.Add("class", "");
                            this.v2.Attributes.Add("class", "");
                            this.v3.Attributes.Add("class", "current");
                            this.v4.Attributes.Add("class", "");
                            this.v5.Attributes.Add("class", "");
                            dt = UtilPage.GetPage("vote_my_aspx", PageIndex, 20, out c, out p, st);
                            break;
                        case "myc":
                            this.ptitle.InnerText = "我参与的投票";
                            this.bName.InnerHtml = "我参与的投票";
                            this.v1.Attributes.Add("class", "");
                            this.v2.Attributes.Add("class", "");
                            this.v3.Attributes.Add("class", "");
                            this.v4.Attributes.Add("class", "current");
                            this.v5.Attributes.Add("class", "");
                            dt = UtilPage.GetPage("vote_myc_aspx", PageIndex, 20, out c, out p, st);
                            break;
                        case "hot":
                            this.ptitle.InnerText = "热点投票";
                            this.bName.InnerHtml = "热点投票";
                            this.v1.Attributes.Add("class", "");
                            this.v2.Attributes.Add("class", "");
                            this.v3.Attributes.Add("class", "");
                            this.v4.Attributes.Add("class", "");
                            this.v5.Attributes.Add("class", "current");
                            dt = UtilPage.GetPage("vote_hot_aspx", PageIndex, 20, out c, out p, st);
                            break;
                    }
                }
                else
                {
                    UserInfo mdl = new UserInfo();
                    User us = new User();
                    mdl = us.GetUserInfo(Convert.ToInt32(r));
                    this.ptitle.InnerText =mdl.Username + "的投票";
                    this.bName.InnerHtml = mdl.Username + "的投票";
                    this.v1.Attributes.Add("class", "");
                    this.v2.Attributes.Add("class", "current");
                    this.v2.InnerHtml = "<a href=\"../vote?r=" + r + "\">" + mdl.Username + "的投票</a>";
                    this.v3.Attributes.Add("class", "");
                    this.v4.Attributes.Add("class", "");
                    this.v5.Attributes.Add("class", "");
                    dt = UtilPage.GetPage("vote_ID_aspx", PageIndex, 20, out c, out p, st);
                }
            }
            Rpt_newvote.DataSource = dt;
            Rpt_newvote.DataBind();
            this.LtrNum.Text = c.ToString();
            if (c> 20)
            {
                divPage.InnerHtml = PageNavigator.Pagination(PageIndex, p, "../vote/?r=" + r + "");
            }
        }

        /// <summary>
        /// 加载介绍
        /// </summary>
        /// <param name="Intro">介绍</param>
        /// <returns></returns>
        protected string getIntro(object Intro)
        {
            string s =string.Empty;
            if (!string.IsNullOrEmpty(Intro.ToString()))
            {
                s = "<div>" + Intro.ToString() + "</div>";
            }
            return s;
        }

        /// <summary>
        /// 取得投票选项
        /// </summary>
        /// <param name="ID">投票编号</param>
        /// <param name="Mode">投票的选择类型</param>
        /// <returns>返回选项</returns>
        protected string getOption(object ID, object Mode)
        {
            bool tf = false;
            StringBuilder s = new StringBuilder();
            List<VoteOptionInfo> list = vo.OptionList(Convert.ToInt32(ID), this.UserID, out tf);

            for (int i = 0; i < list.Count && i < 3; i++)
            {
                string o = string.Empty;
                if (Mode.ToString() == "1")//单选
                {
                    o = "<img src=\""+rooDir+"/template/"+JuSNS.Config.UiConfig.SkinStyle+"/images/site/dyn/radio.gif\" style=\"border:0px;\" align=\"absmiddle\" /> ";
                    s.Append(o + Input.HtmlDecode(list[i].OptionName) + "<br />");
                }
                else//复选
                {
                    o = "<img src=\"" + rooDir + "/template/" + JuSNS.Config.UiConfig.SkinStyle + "/images/site/dyn/checkbox.gif\" style=\"border:0px;\" align=\"absmiddle\" />";
                    s.Append(o + Input.HtmlDecode(list[i].OptionName) + "<br />");
                }
            }
            s.Append(" &nbsp; …");
            return s.ToString();
        }
    }
}
