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_LettersRecieved : System.Web.UI.Page
{
    //************************************
    SqlConnection con;
    SqlCommand cmd;
    static string lastQuery = "";
    //************************************
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            C_myClass.MenueItemsCreate(User, panelMenues);
            Label lblTitle = (Label)this.Master.FindControl("lblTitle");
            lblTitle.Text = "نامه های رسیده";
            lastQuery = "SELECT [letterId], [subject], [context], [letterDate], [letterType], [senderName] FROM [t_Letters] ORDER BY [letterId]";
        }
        catch
        {
        }
        finally
        {
        }
    }

    protected string FindImageUrl(object LetterId)
    {
        string imageUrl = "";// "~/images/letterread.gif";
        string conString = RamanConnectionString.LocalConnectionString();
        con = new SqlConnection(conString);
        cmd = con.CreateCommand();
        try
        {
            con.Open();
            int letterId = Int32.Parse(LetterId.ToString());
            string status = _UseFull.C_Usefull.FindStringWithIntegerShart(con, cmd, "t_Letters", "status", "letterId", letterId);
            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 == "ShowContext")
            {
                int letterId = Int32.Parse(e.CommandArgument.ToString());
            }
            else if (e.CommandName == "DeletePress")
            {
                int letterId = Int32.Parse(e.CommandArgument.ToString());
                string conString = RamanConnectionString.LocalConnectionString();
                con = new SqlConnection(conString);
                cmd = con.CreateCommand();
                try
                {
                    con.Open();
                    cmd.CommandText = "delete from t_Letters Where letterId = " + letterId;
                    cmd.ExecuteNonQuery();
                    GridView1.DataBind();
                }
                catch
                {
                }
                finally
                {
                    con.Close();
                }
            }
            else if (e.CommandName == "ReadPress")
            {
                string conString = RamanConnectionString.LocalConnectionString();
                con = new SqlConnection(conString);
                cmd = con.CreateCommand();
                try
                {
                    con.Open();
                    int letterId = Int32.Parse(e.CommandArgument.ToString());
                    string status = _UseFull.C_Usefull.FindStringWithIntegerShart(con, cmd, "t_Letters", "status", "letterId", letterId);
                    if (status == "unread")
                    {
                        cmd.CommandText = "update t_Letters SET status = N'read' Where letterId = " + letterId;
                        cmd.ExecuteNonQuery();
                        GridView1.DataBind();
                    }
                }
                catch
                {
                }
                finally
                {
                    con.Close();
                }
            }
        }
        catch
        {
        }
    }


    protected void btnUnRead_Click(object sender, EventArgs e)
    {
        try
        {
            lastQuery = "SELECT [letterId], [subject], [context], [letterDate], [letterType], [senderName] FROM [t_Letters] Where status = N'unread' ORDER BY [letterId]";
            SqlDataSource1.SelectCommand = lastQuery;
            GridView1.DataBind();
        }
        catch
        {
        }
    }
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        try
        {
            lastQuery = "SELECT [letterId], [subject], [context], [letterDate], [letterType], [senderName] FROM [t_Letters] ORDER BY [letterId]";
            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
        {
        }
    }
    protected void btnEnteghad_Click(object sender, EventArgs e)
    {
        try
        {
            lastQuery = "SELECT [letterId], [subject], [context], [letterDate], [letterType], [senderName] FROM [t_Letters] Where letterType = N'انتقاد' ORDER BY [letterId]";
            SqlDataSource1.SelectCommand = lastQuery;
            GridView1.DataBind();
        }
        catch
        {
        }
    }
    protected void btnNazar_Click(object sender, EventArgs e)
    {
        try
        {
            lastQuery = "SELECT [letterId], [subject], [context], [letterDate], [letterType], [senderName] FROM [t_Letters] Where letterType = N'نظر' ORDER BY [letterId]";
            SqlDataSource1.SelectCommand = lastQuery;
            GridView1.DataBind();
        }
        catch
        {
        }
    }
    protected void btnPishnahadat_Click(object sender, EventArgs e)
    {
        try
        {
            lastQuery = "SELECT [letterId], [subject], [context], [letterDate], [letterType], [senderName] FROM [t_Letters] Where letterType = N'پیشنهاد' ORDER BY [letterId]";
            SqlDataSource1.SelectCommand = lastQuery;
            GridView1.DataBind();
        }
        catch
        {
        }
    }
}
