using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
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 System.Xml.Linq;
using System.Data.SqlClient;

public partial class rl_admins_pg_MessagesInbox : System.Web.UI.Page
{
    //**************************************
    SqlConnection con;
    SqlCommand cmd;
    static string lastQuery = "";
    //**************************************
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (!Page.IsPostBack)
            {
                C_myClass.MenueItemsCreate(User, panelMenues);
                Label lblTitle = (Label)this.Master.FindControl("lblTitle");
                lblTitle.Text = "صندوق ورودی";
                lastQuery = "select * from t_Messages Where recieverUserName = N'" + User.Identity.Name + "' AND recieverDelete = 0";
                SqlDataSource1.SelectCommand = lastQuery;
                GridView1.DataBind();
            }
        }
        catch
        {
        }
        finally
        {
        }
    }

    protected string FindImageUrl(object MessageId)
    {
        string imageUrl = "";// "~/images/letterread.gif";
        string conString = RamanConnectionString.LocalConnectionString();
        con = new SqlConnection(conString);
        cmd = con.CreateCommand();
        try
        {
            con.Open();
            int messageId = Int32.Parse(MessageId.ToString());
            string status = _UseFull.C_Usefull.FindStringWithIntegerShart(con, cmd, "t_Messages", "status", "messageId", messageId);
            if (status == "unread")
            {
                imageUrl = "~/images/letterunread.gif";
            }
            else if (status == "read")
            {
                imageUrl = "~/images/letterread.gif";
            }
        }
        catch
        {
        }
        finally
        {
            if (con.State != ConnectionState.Closed)
                con.Close();
        }
        return imageUrl;
    }

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        try
        {
            if (e.CommandName == "ContextShow")
            {
                int messageId = Int32.Parse(e.CommandArgument.ToString());
                string conString = RamanConnectionString.LocalConnectionString();
                con = new SqlConnection(conString);
                cmd = con.CreateCommand();
                try
                {
                    con.Open();
                    cmd.CommandText = "update t_Messages SET status = N'read' WHERE messageId = " + messageId;
                    cmd.ExecuteNonQuery();
                    Response.Redirect("pg_MessageRecievedShow.aspx?messageId=" + messageId + "&returnUrl=pg_MessagesInbox.aspx");
                }
                catch
                {
                }
                finally
                {
                    con.Close();
                }
            }
            else if (e.CommandName == "DeletePress")
            {
                bool senderDelete = false;
                int messageId = Int32.Parse(e.CommandArgument.ToString());
                string conString = RamanConnectionString.LocalConnectionString();
                con = new SqlConnection(conString);
                cmd = con.CreateCommand();
                try
                {
                    con.Open();
                    cmd.CommandText = "select senderDelete from t_Messages Where messageId = " + messageId;
                    SqlDataReader rdr = cmd.ExecuteReader();
                    if (rdr.HasRows)
                    {
                        while (rdr.Read())
                        {
                            senderDelete = (rdr.GetValue(0) != DBNull.Value ? rdr.GetBoolean(0) : false);
                        }
                    }
                    rdr.Close();
                    if (senderDelete)
                    {
                        cmd.CommandText = "delete from t_Messages where messageId = " + messageId;
                        cmd.ExecuteNonQuery();
                    }
                    else
                    {
                        cmd.CommandText = "update t_Messages SET " +
                            "recieverDelete = 1 " +
                            "WHERE messageId = " + messageId;
                        cmd.ExecuteNonQuery();
                    }
                    SqlDataSource1.SelectCommand = lastQuery;
                    GridView1.DataBind();
                }
                catch
                {
                }
                finally
                {
                    con.Close();
                }
            }
        }
        catch
        {
        }
    }
    protected void btnBack_Click(object sender, EventArgs e)
    {
        try
        {
            string returnUrl = Request.QueryString["returnUrl"].ToString();
            Response.Redirect(returnUrl);
        }
        catch
        {
        }
    }
    protected void btnUnread_Click(object sender, EventArgs e)
    {
        try
        {
            lastQuery = "select * from t_Messages Where recieverUserName = N'" + User.Identity.Name + "' AND recieverDelete = 0 AND status = N'unread'";
            SqlDataSource1.SelectCommand = lastQuery;
            GridView1.DataBind();
        }
        catch
        {
        }
    }
    protected void btnAllMessages_Click(object sender, EventArgs e)
    {
        try
        {
            lastQuery = "select * from t_Messages Where recieverUserName = N'" + User.Identity.Name + "' AND recieverDelete = 0 ";
            SqlDataSource1.SelectCommand = lastQuery;
            GridView1.DataBind();
        }
        catch
        {
        }
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        try
        {
            GridView1.PageIndex = e.NewPageIndex;
            SqlDataSource1.SelectCommand = lastQuery;
            GridView1.DataBind();
        }
        catch
        {
        }
    }
}
