using System;
using System.Collections;
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 digioz.PollSystem;
using System.Collections.Generic;

public partial class controls_poll_PollList : System.Web.UI.UserControl
{
    public int PageSize
    {
        get
        {
            object pageSize = ViewState["PageSize"];
            return ((pageSize == null) ? 1 : (int)pageSize);
        }
        set
        {
            ViewState["PageSize"] = value;
        }
    }

    public int CurrentPage
    {
        get
        {
            object currentPage = ViewState["CurrentPage"];
            return ((currentPage == null) ? 1 : (int)currentPage);
        }
        set
        {
            ViewState["CurrentPage"] = value;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        //TODO: Use a DataGrid or any appropriate server control.
        //        That way, it won't be necessary to reinitialize on post back
        //initialize pagesize to 25 and currnet page to 1
       // if (!IsPostBack)
       // {
            PageSize = 25;
            CurrentPage = 1;
            show();
        //}
    }

    private void show()
    {
        tblPollList.Rows.Clear();

        PollSubjectInfoList subjects = PollSubjectInfoManagement.GetPollSubjectsPaging(PageSize, CurrentPage);

        //check if the index is out of bound
        
        //check the up bound
        if (subjects.Count < PageSize)
        {
            hbNext.Enabled = false;
        }
        else
        {
            hbNext.Enabled = true;
        }

        //also check the lower bound
        if (CurrentPage > 1)
        {
            hbPrevious.Enabled = true;
        }
        else
        {
            hbPrevious.Enabled = false;
        }

        string csURLEncryptionKey = ConfigurationManager.AppSettings["URLEncryptKey"];


        foreach (PollSubjectInfo subject in subjects)
        {
            HtmlTableRow row;
            HtmlTableCell cell;

            row = new HtmlTableRow();
            cell = new HtmlTableCell();
            string url = "Default.aspx?page=" + Server.UrlEncode( digioz.Encryption64.Encrypt("Poll List", csURLEncryptionKey)) +
                            "&pollsubject=" + subject.ID.ToString();
            cell.InnerHtml = "<a href=\"" + url + "\">" + subject.SubjectName + "</a>";
            row.Cells.Add(cell);
            tblPollList.Rows.Add(row);

            row = new HtmlTableRow();
            cell = new HtmlTableCell();
            cell.InnerHtml = subject.SubjectDescription;
            row.Cells.Add(cell);
            tblPollList.Rows.Add(row);

            row = new HtmlTableRow();
            cell = new HtmlTableCell();
            cell.InnerHtml = "DeadLine:" + subject.SubjectDeadline.ToString();
            row.Cells.Add(cell);
            tblPollList.Rows.Add(row);
        }
    }

    /// <summary>
    /// Display previous page
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void hbPrevious_Click(object sender, EventArgs e)
    {
        CurrentPage--;
        show();
    }

    /// <summary>
    /// Display next page
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void hbNext_Click(object sender, EventArgs e)
    {
        CurrentPage++;
        show();
    }
}
