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

namespace JuSNS.Web.home.vote
{
    public partial class add : JuSNS.UI.Page.UserPage
    {
        string[] option = new string[21];
        protected void Page_Load(object sender, EventArgs e)
        {
            GetIsNotEmail();
            Response.CacheControl = "no-cache";
            DataBind();
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                bool tf = check();
                if (tf)
                    goto Finish;

                Vote v = new Vote();
                VoteInfo Info = new VoteInfo();
                VoteOption vo = new VoteOption();

                Info.ID = 0;
                Info.Title = Title.Text;
                Info.Intro = Intro.Text;
                Info.UserID = this.UserID;
                Info.AddTime = DateTime.Now;
                Info.Mode = Convert.ToInt32(Mode.Text);
                if (isEnd.Checked)
                    Info.EndTime = Convert.ToDateTime(EndTime.Value);
                else
                    Info.EndTime = Convert.ToDateTime("3000-01-01");
                Info.JCnt = 0;
                if (IsFriend.Checked)
                    Info.IsFriend = 1;
                else
                    Info.IsFriend = 0;
                int VoteID = v.Add(Info);
                for (int i = 1; i < option.Length; i++)
                {
                    if (option[i] != null && option[i].Trim() != "")
                    {
                        VoteOptionInfo oInfo = new VoteOptionInfo();
                        oInfo.ID = 0;
                        oInfo.OptionName = option[i];
                        oInfo.VoteID = VoteID;
                        oInfo.Cnt = 0;
                        vo.Add(oInfo);
                    }
                }
                Response.Redirect("vote.aspx?r=" + VoteID + "");
            Finish: ;
            }

        }

        /// <summary>
        /// 检测参数是否合法
        /// </summary>
        protected bool check()
        {
            string OpNum = Request.Form["OpNum"];
            int j = 0;
            for (int i = 1; i <= Convert.ToInt32(OpNum); i++)
            {
                string s = Request.Form["Option_" + i];
                if (!(s == null || s.Trim() == ""))
                {
                    j++;
                }
                option[i] = s;
            }
            if (j == 0)
            {
                ShowErr("请填写投票选项", div_Error);
                return true;
            }
            if (isEnd.Checked)
            {
                if (EndTime.Value.Trim() == "")
                {
                    ShowErr("结束时间不能为空", div_Error);
                    return true;
                }
                DateTime d = DateTime.Now;
                try
                {
                    d = Convert.ToDateTime(EndTime.Value);
                }
                catch
                {
                    ShowErr("结束时间格式错误", div_Error);
                    return true;
                }
                TimeSpan ts = DateTime.Now - d;

                if (ts.Days < 0)
                {
                    ShowErr("结束时间不能小于今天", div_Error);
                    return true;
                }
            }
            return false;
        }
    }
}
