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

        protected void Tbt_Select_Click(object sender, ImageClickEventArgs e)
        {
            BindEmail(1);
            PageCount(3);
            setEnable();
        }
        protected void BindEmail(int pageindex)
        {
            List<object> list = bll.SelctObjectByPage(typeof(mail), "mail", "ID,Theme,Recipients,MailContents,SendTime", Condition(), pageindex, 3, "ID", "ID");
            Rp_mail.DataSource = list;
            Rp_mail.DataBind();
            if (list.Count > 0)
            {
                div.Attributes.Add("style", "width:90%;margin:0px auto;");
            }
            else
            {
                div.Attributes.Add("style","width:90%; display:none; margin:0px auto;");
            }
        }
        protected string Condition()
        {
            string str = "Senduser='" + Session["UserName"].ToString() + "' and ReState=0";
            if (Tb_Recipients.Text != "")
            {
                str = str + " and Recipients='" + Tb_Recipients.Text + "'";
            }
            if (Tb_SendTime.Text != "")
            {
                str = str + " and SendTime>'" + Tb_SendTime.Text + "'";
            }
            return str;
        }
        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("mail", Condition(), Page).ToString();
            Lb_Now.Text = "1";
        }

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

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

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

        protected void Ibt_Last_Click(object sender, ImageClickEventArgs e)
        {
            int pageindex=Convert.ToInt32(Lb_Total.Text);
           BindEmail(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)
                {
                    BindEmail(go);
                    Lb_Now.Text = go.ToString();
                    setEnable();
                    Tb_goto.Text = "";
                }
                else
                {
                    Tb_goto.Text = "";
                }
            }
            catch
            {
                Tb_goto.Text = "";
            }
        }

        protected void Lb_Del_Click(object sender, EventArgs e)
        {
            string ID = "";
            foreach (RepeaterItem item in Rp_mail.Items)
            {
                CheckBox ck = (CheckBox)item.FindControl("Ck_Check");
                if (ck.Checked)
                {
                    Label lb = (Label)item.FindControl("Lb_ID");
                    ID = ID + lb.Text + ",";
                }
            }
            ID = ID.Substring(0, ID.Length - 1);
            if (ubll.update("mail", "ReState=1", "ID in(" + ID + ")") == 0)
            {
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alert", "alert('删除成功！')", true);
                BindEmail(1);
                Lb_Now.Text = "1";
                setEnable();
            }
            else
            {
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alert", "alert('删除失败！')", true);
              
            }
        }
    }
}

    

       

    

      