using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using Model;
namespace ComResProSystem
{
    public partial class AnnouncementManagement:BasePage
    {
        BaseBLL bll = new BaseBLL();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindAnnouncementinfo(1);
                PageCount(3);
                setEnable();
            }
        }

        protected void Ibt_Check_Click(object sender, ImageClickEventArgs e)
        {
            System.Web.UI.ScriptManager.RegisterStartupScript(this, Page.GetType(), "ajaxjs", " var sReturn=window.showModalDialog('SelectPerson.aspx','window','dialogWidth:180px;dialogHeight:300px;help:0;scroll:1;status:no;');document.getElementById('" + this.Tb_Author.UniqueID + "').value=sReturn;", true);
        }
        protected string Contidion()
        {
            string contidion = "1=1";
            if (Tb_Author.Text != "")
            {
                contidion = contidion + " and Author='" + Tb_Author.Text + "'";
            }
            if(Tb_Title.Text!="")
            {
                contidion = contidion + " and Title like '%" + Tb_Title.Text + "%'";
            }
            if (Tb_Begin.Text != "" && Tb_End.Text != "")
            {
                contidion = contidion + " and ReleaseTime between '" + Tb_Begin.Text + "' and '" + Tb_End.Text + "'";
            }
            return contidion;
        }

        protected void Ibt_select_Click(object sender, ImageClickEventArgs e)
        {
            BindAnnouncementinfo(1);
            PageCount(3);
            setEnable();
        }
        public void BindAnnouncementinfo(int pageindex)
        {
            List<object> list = bll.SelctObjectByPage(typeof(Announcement), "Announcement", "*", Contidion(), pageindex, 3, "ID", "ID");
            Rp_Announcement.DataSource = list;
            Rp_Announcement.DataBind();
            if (list.Count > 0)
            {
                preup.Attributes.Add("style", "display:block;margin-right:5%;");
            }
            else
            {
                preup.Attributes.Add("style", "display:none");
            }
        }
        public void setEnable()
        {
            Ibt_Firsr.Enabled = Lb_Now.Text == "1" ? false : true;
            Ibt_Pre.Enabled = Lb_Now.Text == "1" ? false : true; ;
            Ibt_Next.Enabled = Lb_Now.Text == Lb_Total.Text ? false : true;
            Ibt_Last.Enabled = Lb_Now.Text == Lb_Total.Text ? false : true;
        }
        public void PageCount(int Page)
        {
            Lb_Total.Text = bll.SelPageCount("Announcement", Contidion(), Page).ToString();
            Lb_Now.Text = "1";
        }

        protected void Ibt_Firsr_Click(object sender, ImageClickEventArgs e)
        {
            BindAnnouncementinfo(1);
            Lb_Now.Text = "1";
            setEnable();
        }

        protected void Ibt_Pre_Click(object sender, ImageClickEventArgs e)
        {
            int pageindex = Convert.ToInt32(Lb_Now.Text) - 1;
            BindAnnouncementinfo(pageindex);
            Lb_Now.Text = pageindex.ToString();
            setEnable();
        }

        protected void Ibt_Next_Click(object sender, ImageClickEventArgs e)
        {
            int pageindex = Convert.ToInt32(Lb_Now.Text) + 1;
            BindAnnouncementinfo(pageindex);
            Lb_Now.Text = pageindex.ToString();
            setEnable();
        }

        protected void Ibt_Last_Click(object sender, ImageClickEventArgs e)
        {
            int pageindex = Convert.ToInt32(Lb_Total.Text);
            BindAnnouncementinfo(pageindex);
            Lb_Now.Text = pageindex.ToString();
            setEnable();
        }

        protected void Ibt_goto_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                int go = Convert.ToInt32(Tb_goto.Text);
                if (go > 0 && go < Convert.ToInt32(Lb_Total.Text) + 1)
                {
                    BindAnnouncementinfo(go);
                    Lb_Now.Text = go.ToString();
                    setEnable();
                    Tb_goto.Text = "";
                }
                else
                {
                    Tb_goto.Text = "";
                }
            }
            catch
            {
                Tb_goto.Text = "";
            }
        }

        protected void Rp_Announcement_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "DoAduit")
            {
                string ID = e.CommandArgument.ToString();
                string pageindex = Lb_Now.Text;
                System.Web.UI.ScriptManager.RegisterStartupScript(this, Page.GetType(), "ajaxjs", " var sReturn=window.showModalDialog('AnnouncementEdit.aspx?ID=" + ID + "','window','dialogWidth:600px;dialogHeight:560px;help:0;scroll:1;status:no;');if(sReturn=='1'){document.getElementById('" + this.Tb_goto.UniqueID + "').value=" + pageindex + ";document.getElementById('" + this.Ibt_goto.UniqueID + "').click();}", true);
            }
            if (e.CommandName == "DoDelete")
            {
                string ID = e.CommandArgument.ToString();
                string pageindex = Lb_Now.Text;
                if (bll.Delete0bject("Announcement", "ID=" + ID) == 0)
                {
                    Tb_goto.Text = pageindex;
                    object sender=null;
                    ImageClickEventArgs eargs=null;
                    Ibt_goto_Click(sender, eargs);
                }
            }
        }
    }
}