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

        protected void Tbt_Select_Click(object sender, ImageClickEventArgs e)
        {
            Bindmail(1);
            Pagetotal(3);
        }
        protected void Bindmail(int pageindex)
        {
            List<object> list = bll.SelctObjectByPage(typeof(mail), "mail", "ID, Theme, MailContents, SendTime,Senduser,State", 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%;margin:0px auto;display:none");
           }

        }
        protected void Pagetotal(int pagecount)
        {
           Lb_Total.Text=bll.SelPageCount("mail", Condition(), pagecount).ToString();
           Lb_Now.Text = "1";
        }
        protected string Condition()
        {
            string str = "Recipients='" + Session["UserName"].ToString() + "' and UserState=0";
            if (Tb_Senduser.Text != "")
            {
                str = str + " and Senduser='" +Tb_Senduser.Text+ "'";
            }
            if (Tb_SendTime.Text != "")
            {
                str = str + " and SendTime<'" + Tb_SendTime.Text + "'";
            }
            return str;
        }

        protected void LinkButton1_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_User");
                    ID = ID + lb.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_User");
                    ID =ID + lb.Text + ",";
                }
            }
            ID = ID.Substring(1, ID.Length - 1);
            usebll.update("mail", "UserState=1", "ID in(" + ID + ")");
        }

        protected void Ibt_Firsr_Click(object sender, ImageClickEventArgs e)
        {
            Bindmail(1);
            setEnable();
        }
        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;
        }
        protected void Ibt_Pre_Click(object sender, ImageClickEventArgs e)
        {
            int pageindex = Convert.ToInt32(Lb_Now.Text) - 1;
            Bindmail(pageindex);
            Lb_Now.Text = pageindex.ToString();
            setEnable();
        }

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

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