using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using TopWinCMS.Common;
using TopWinCMS.Model;
using TopWinCMS.BLL;
namespace TopWinCMS.WebUI.Admin.Vote
{
    public partial class Vote_Add : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                RightsCheck.CheckWorkPower("P_3");
                if (Request["Action"] == null)
                {
                    AddUI();
                }
                else if (Request["Action"] == "Edit")
                {
                    EditUI();
                }
            }

        }

        private void AddUI()
        {
            UiTitle.InnerText = "添加投票";

            //List<string> list = new List<string>();
            //list.Add(""); list.Add(""); list.Add(""); list.Add(""); list.Add("");

            //Repeater1.DataSource = list;
            //Repeater1.DataBind();
        }

        private void EditUI()
        {
            UiTitle.InnerText = "修改投票";

            VoteInfo model = new VoteManage().EditLoad(Request["ID"]);
            TB_Title.Text = model.Title;
            CB_Pass.Checked = model.Pass == 1 ? true : false;

            //CB_SingleSelect.Checked = model.VoteType == 1 ? true : false;
            RBL_SingleSelect.SelectedValue = model.VoteType.ToString();

            //TB_PicUrl.Text = model.PicUrl;
            CD_EndData.Value = model.EndData;
            //TB_windowsize.Text = model.windowsize;
            DDL_Template.SelectedValue = model.TemplateFileName;
            CB_LimitIP.Checked = model.LimitIP == 1 ? true : false;

            VoteChoiceInfo modelChoice = new VoteChoiceManage().ForVoteLoad(Request["ID"], UpdatePanel1);

            HF_ID.Value = modelChoice.ID;
            TB_ChoiceTitle.Text = modelChoice.ChoiceTitle;
            NTB_VoteNum.Text = modelChoice.VoteNum;
        }

        protected void BT_AddChoice_Click(object sender, EventArgs e)
        {
            VoteInfo modelvote = new VoteInfo();
            if (Request["ID"] == null)
            {
                modelvote.ID = "0";
            }
            else
            {
                modelvote.ID = Request["ID"];
            }
            modelvote.Title = TB_Title.Text;
            modelvote.VoteType = int.Parse(RBL_SingleSelect.SelectedValue);
            modelvote.Pass = CB_Pass.Checked ? 1 : 0;
            modelvote.EndData = CD_EndData.Value;
            modelvote.LimitIP = CB_LimitIP.Checked ? 1 : 0;
            modelvote.TemplateFileName = DDL_Template.SelectedValue;

            string strErr = null;

            new VoteManage().Add(modelvote, Request.Form["HF_ID"], Request.Form["TB_ChoiceTitle"], Request.Form["NTB_VoteNum"], ref strErr);


            if (string.IsNullOrEmpty(strErr) == false)
            {
                MsgBox.Alert(strErr, UpdatePanel1);
            }
            else
            {

                Response.Redirect("Vote.aspx");

            }
        }
    }
}