using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using NBear.Common;
using Aspxcn.function;

public partial class _Msg : NBear.Web.UI.Page
{
    protected static Aspxcn.ServiceImpls.MessageService cmd = new Aspxcn.ServiceImpls.MessageService();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //Response.Cache.SetExpires(DateTime.Now);
            Response.Expires = -1;
            if (User.Identity.Name!="")
            {
                if (Request.QueryString["user"] != null)
                {
                    send.BackColor = System.Drawing.Color.FromName("#EFF3FB");
                    send.Text = "[发消息]";
                    sendBox.Text = "发件箱";
                    inBox.Text = "收件箱";
                    send.BorderStyle = BorderStyle.Dashed;
                    sendBox.BorderStyle = BorderStyle.NotSet;
                    inBox.BorderStyle = BorderStyle.NotSet;

                    viewMSgContent.Visible = false;
                    viewMSgTitle.Visible = false;
                    sendMsgContent.Visible = true;
                    sendToUsername.Text = Request.QueryString["user"].ToString();
                }
                else
                {
                    LoadMsgTitlePage(User.Identity.Name);
                    selectAllBtn.Focus();
                }
            }
            else
            {
                Response.Redirect("notfound.htm");
            }

        }
    }

    protected void Page_PreRender(object sender, EventArgs e)
    {
        if (send.Text == "[发消息]")
        {
            send.BackColor = System.Drawing.Color.FromName("#EFF3FB");
        }

        if (inBox.Text == "[收件箱]")
        {
            inBox.BackColor = System.Drawing.Color.FromName("#EFF3FB");
        }

        if (sendBox.Text == "[发件箱]")
        {
            sendBox.BackColor = System.Drawing.Color.FromName("#EFF3FB");
        }   
        
    }

    private void LoadMsgTitlePage(string userName)
    {
        try
        {

            inBox.BackColor = System.Drawing.Color.FromName("#EFF3FB");
            inBox.Text = "[收件箱]";
            sendBox.Text = "发件箱";
            send.Text = "发消息";
            inBox.BorderStyle = BorderStyle.Dashed;
            sendBox.BorderStyle = BorderStyle.NotSet;
            send.BorderStyle = BorderStyle.NotSet;
            ListGrid.DataSource = cmd.GetMsgContent(30, userName);
            ListGrid.DataBind();

            if (cmd.GetMsgPost(userName, "TO") > 30)
                Anthem.Manager.RegisterClientScriptBlock(typeof(Page),"Key", "alert('您的收件箱已满30条,请尽快清理,否则无法接收信息!')",true);
        }
        catch
        {
            ListGrid.DataBind();
        }
    }

    private void LoadSendBoxMsgTitlePage(string userName)
    {
        try
        {
            sendBox.BackColor = System.Drawing.Color.FromName("#EFF3FB");
            sendBox.Text = "[发件箱]";
            inBox.Text = "收件箱";
            send.Text = "发消息";
            sendBox.BorderStyle = BorderStyle.Dashed;
            inBox.BorderStyle = BorderStyle.NotSet;
            send.BorderStyle = BorderStyle.NotSet;
            ListGrid.DataSource = cmd.GetMsgContent(50, userName,"sendBox");
            ListGrid.DataBind();
            if (cmd.GetMsgPost(userName, "From") > 50)
                Anthem.Manager.RegisterClientScriptBlock(typeof(Page), "Key", "alert('您的发件箱已满50条,请尽快清理,否则无法保存信息!')", true);
        }
        catch
        {
            ListGrid.DataBind();
        }
    }

    private void LoadMsgContentPage(string userName,string Msg_ID)
    {
        try
        {
            if (inBox.Text == "[收件箱]")
            {
                Aspxcn.Entities.Msg MsgContent = cmd.GetMsgContent(userName, Int32.Parse(Msg_ID));
                Msg_userName.Text = MsgContent.From_userName;
                Msg_title.Text = MsgContent.Msg_Title;
                Msg_Content.Text = MsgContent.Msg_Content;
                Msg_dateTime.Text = MsgContent.Msg_DateTime.ToString("yyyy/MM/dd HH:mm");
                replyMsg.Enabled = true;
                replyMsg.CommandArgument = MsgContent.From_userName;
                replyMsg.CommandName = MsgContent.Msg_Title;
                delBtn.CommandArgument = MsgContent.ID.ToString();
                if (MsgContent.IsRead == false)
                {
                    cmd.Update_Msg(Int32.Parse(Msg_ID), true);
                }
            }
            else
            {
                Aspxcn.Entities.Msg MsgContent = cmd.GetMsgContent(userName, Int32.Parse(Msg_ID),"sendBox");
                Msg_userName.Text = MsgContent.From_userName;
                Msg_title.Text = MsgContent.Msg_Title;
                Msg_Content.Text = MsgContent.Msg_Content;
                Msg_dateTime.Text = MsgContent.Msg_DateTime.ToString("yyyy/MM/dd HH:mm");
                replyMsg.Text = "转发";
                replyMsg.CommandName = MsgContent.Msg_Content;
                replyMsg.CommandArgument = MsgContent.Msg_Title;
                delBtn.CommandArgument = MsgContent.ID.ToString();
            }
            
        }
        catch
        {

        }
    }

    protected void checkAll(object sender, EventArgs e)
    {
        CheckBox cb = (CheckBox)sender;
        if (cb.Text == "全选")
        {
            foreach (GridViewRow gvr in ListGrid.Rows)
            {
                CheckBox cb1 = (CheckBox)gvr.FindControl("check");
                cb1.Checked = cb.Checked;
            }
            if (cb.Checked)
            {
                selectAllBtn.Text = "反选";
            }
            else
            {
                selectAllBtn.Text = "全选";
            }
        }
        
    }

    protected void deleteBtn_Click(object sender, EventArgs e)
    {
        if (deleteBtn.CommandName == "inBox")
        {
            foreach (GridViewRow dl in ListGrid.Rows)
            {
                CheckBox cb = (CheckBox)dl.FindControl("check");

                if (cb.Checked)
                {
                    GridViewRow row = (GridViewRow)cb.NamingContainer;
                    cmd.Update_Msg(Int32.Parse(ListGrid.DataKeys[row.RowIndex].Value.ToString()),1);
                }
            }
        }
        else
        {
            foreach (GridViewRow dl in ListGrid.Rows)
            {
                CheckBox cb = (CheckBox)dl.FindControl("check");

                if (cb.Checked)
                {
                    GridViewRow row = (GridViewRow)cb.NamingContainer;
                    cmd.Update_Msg(Int32.Parse(ListGrid.DataKeys[row.RowIndex].Value.ToString()), 2);
                }
            }
        }
        cmd.Delete_Msg();
        LoadMsgTitlePage(User.Identity.Name);
    }

    protected void selectAllBtn_Click(object sender, EventArgs e)
    {
        if (selectAllBtn.Text == "全选")
        {
            CheckBox cb = (CheckBox)ListGrid.HeaderRow.FindControl("checkall");
            cb.Checked = true;
            foreach (GridViewRow gvr in ListGrid.Rows)
            {
                CheckBox cb1 = (CheckBox)gvr.FindControl("check");
                cb1.Checked = true;
            }
            selectAllBtn.Text = "反选";
        }
        else
        {
            CheckBox cb = (CheckBox)ListGrid.HeaderRow.FindControl("checkall");
            cb.Checked = false;
            foreach (GridViewRow gvr in ListGrid.Rows)
            {
                CheckBox cb1 = (CheckBox)gvr.FindControl("check");
                cb1.Checked = false;
            }
            selectAllBtn.Text = "全选";
        }
    }
    protected void ListGrid_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        ListGrid.PageIndex = e.NewPageIndex;
        LoadMsgTitlePage(User.Identity.Name);
    }
    protected void ListTitle_Command(object sender, CommandEventArgs e)
    {
        viewMSgContent.Visible = true;
        sendMsgContent.Visible = false;
        viewMSgTitle.Visible = false;
        
        LoadMsgContentPage(User.Identity.Name,e.CommandArgument.ToString());
    }

    protected void delBtn_command(object sender, CommandEventArgs e)
    {
        
        if (inBox.Text == "[收件箱]")
        {
            cmd.Update_Msg(Int32.Parse(e.CommandArgument.ToString()), 1);
        }
        else
        {
            cmd.Update_Msg(Int32.Parse(e.CommandArgument.ToString()), 2);
        }
        cmd.Delete_Msg();
        viewMSgContent.Visible = false;
        sendMsgContent.Visible = false;
        viewMSgTitle.Visible = true;
        LoadMsgTitlePage(User.Identity.Name);
    }


    protected void inBox_Click(object sender, EventArgs e)
    {
        viewMSgContent.Visible = false;
        sendMsgContent.Visible = false;
        viewMSgTitle.Visible = true;
        deleteBtn.CommandName = "inBox";
        LoadMsgTitlePage(User.Identity.Name);
    }

    protected void sendBox_click(object sender, EventArgs e)
    {
        viewMSgContent.Visible = false;
        sendMsgContent.Visible = false;
        viewMSgTitle.Visible = true;
        deleteBtn.CommandName = "sendBox";
        LoadSendBoxMsgTitlePage(User.Identity.Name);
    }

    protected void sendMsgBtn_Click(object sender, EventArgs e)
    {
        sendMsgBtn.Enabled = false;
        if (Membership.FindUsersByName(sendToUsername.Text.Trim()).Count > 0)
        {
            Response.Cache.SetExpires(DateTime.Now);
            if (User.Identity.Name != sendToUsername.Text.Trim())
            {
                Add_Msg();
            }
            else
            {
                sendToUsername.Text = "";
                Anthem.Manager.AddScriptForClientSideEval("alert('发送失败,不能给自己发短消息!')");
            }

        }
        else
        {
            sendToUsername.Text = "";
            Anthem.Manager.AddScriptForClientSideEval("alert('发送失败,用户名不存在!')");

        }
        sendMsgBtn.Enabled = true;
    }

    protected void clearAll_Click(object sender, EventArgs e)
    {
        MsgContent.Text = "";
    }

    protected void Add_Msg()
    {
        
        if (cmd.GetMsgPost(TextToHtml(sendToUsername.Text),"TO") <= 30)
        {
            Aspxcn.Entities.Msg _Msg = new Aspxcn.Entities.Msg();
            _Msg.From_userName = User.Identity.Name;
            _Msg.To_userName = TextToHtml(sendToUsername.Text);
            _Msg.Msg_Title = TextToHtml(MsgTitle.Text);
            _Msg.Msg_Content = TextToHtml(MsgContent.Text);
            _Msg.Msg_DateTime = DateTime.Now;
            _Msg.ToIsDel = false;
            if (saveBox.Checked&cmd.GetMsgPost(Server.HtmlEncode(sendToUsername.Text), "FROM") <= 50)
            {
                _Msg.FromIsDel = false;
            }
            else
            {
                if (saveBox.Checked)
                {
                    Anthem.Manager.AddScriptForClientSideEval("alert('您的发件箱已满50条,必须清理后才能继续保存信息到发件箱!')");
                }
                _Msg.FromIsDel = true;
            }

            cmd.insert_MsgContent(_Msg);
            viewMSgContent.Visible = false;
            sendMsgContent.Visible = false;
            viewMSgTitle.Visible = true;
            LoadMsgTitlePage(User.Identity.Name);
            Anthem.Manager.AddScriptForClientSideEval("alert('您的短消息已经成功发送!')");
        }
        else
        {
            Anthem.Manager.AddScriptForClientSideEval("alert('发送信息的接收方收件箱已满,无法再发送信息给该用户!')");
        }        
    }

    protected void send_Command(object sender, CommandEventArgs e)
    {
        send.BackColor = System.Drawing.Color.FromName("#EFF3FB");
        send.Text = "[发消息]";
        sendBox.Text = "发件箱";
        inBox.Text = "收件箱";
        send.BorderStyle = BorderStyle.Dashed;
        sendBox.BorderStyle = BorderStyle.NotSet;
        inBox.BorderStyle = BorderStyle.NotSet;
        viewMSgContent.Visible = false;
        viewMSgTitle.Visible = false;
        sendMsgContent.Visible = true;
        if (e.CommandArgument.ToString() != "")
        {
            sendToUsername.Text = e.CommandArgument.ToString();
            MsgTitle.Text = "RE:" + TextToHtml(e.CommandName);
        }
        if (replyMsg.Text == "转发")
        {
            MsgTitle.Text = TextToHtml(e.CommandArgument.ToString());
            MsgContent.Text = e.CommandName;
        }
    }

}
