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_Messages : System.Web.UI.Page
{
    //*********************************************
    SqlConnection con;
    SqlCommand cmd;
    //*********************************************
    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 = "پیام ها";
                SqlDataSource1.SelectCommand = "select * from t_Messages Where senderUserName = N'" + User.Identity.Name + "' AND senderDelete = 0";
                GridView1.DataBind();
            }
        }
        catch
        {
        }
        finally
        {
        }
    }


    protected void btnNewClass_Click(object sender, EventArgs e)
    {
        try
        {
            Response.Redirect("pg_MessageSendNew.aspx?returnUrl=pg_Messages.aspx");
        }
        catch
        {
        }
    }
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        try
        {
            if (e.CommandName == "ContextShow")
            {
                int messageId = Int32.Parse(e.CommandArgument.ToString());
                Response.Redirect("pg_MessageSendedShow.aspx?messageId=" + messageId + "&returnUrl=pg_Messages.aspx");
            }
            else if (e.CommandName == "DeletePress")
            {
                bool recieverDelete = 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 recieverDelete from t_Messages Where messageId = " + messageId;
                    SqlDataReader rdr = cmd.ExecuteReader();
                    if (rdr.HasRows)
                    {
                        while (rdr.Read())
                        {
                            recieverDelete = (rdr.GetValue(0) != DBNull.Value ? rdr.GetBoolean(0) : false);
                        }
                    }
                    rdr.Close();
                    if (recieverDelete)
                    {
                        cmd.CommandText = "delete from t_Messages where messageId = " + messageId;
                        cmd.ExecuteNonQuery();
                    }
                    else
                    {
                        cmd.CommandText = "update t_Messages SET " +
                            "senderDelete = 1 " +
                            "WHERE messageId = " + messageId;
                        cmd.ExecuteNonQuery();
                    }
                    SqlDataSource1.SelectCommand = "select * from t_Messages Where senderUserName = N'" + User.Identity.Name + "' AND senderDelete = 0";
                    GridView1.DataBind();
                }
                catch
                {
                }
                finally
                {
                    con.Close();
                }
            }
        }
        catch
        {
        }
    }
    protected void btnSendAll_Click(object sender, EventArgs e)
    {
        try
        {
            Response.Redirect("pg_MessageSendToAllUsers.aspx?returnUrl=pg_Messages.aspx");
        }
        catch
        {
        }
    }
    protected void btnSendRole_Click(object sender, EventArgs e)
    {
        try
        {
            Response.Redirect("pg_MessageSendToRole.aspx?returnUrl=pg_Messages.aspx");
        }
        catch
        {
        }
    }
    protected void btnSendClass_Click(object sender, EventArgs e)
    {
        try
        {
            Response.Redirect("pg_MessageSendToClassStudents.aspx?returnUrl=pg_Messages.aspx");
        }
        catch
        {
        }
    }
    protected void btnRecieved_Click(object sender, EventArgs e)
    {
        try
        {
            Response.Redirect("pg_MessagesInbox.aspx?returnUrl=pg_Messages.aspx");
        }
        catch
        {
        }
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        try
        {
            SqlDataSource1.SelectCommand = "select * from t_Messages Where senderUserName = N'" + User.Identity.Name + "' AND senderDelete = 0";
            GridView1.DataBind();
        }
        catch
        {
        }
    }
}
