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;
using System.IO;

public partial class rl_admins_pg_MessageShow : System.Web.UI.Page
{
    //***************************************
    SqlConnection con;
    SqlCommand cmd;
    //***************************************
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (!Page.IsPostBack)
            {
                string subject = "", recieverFullName = "", recieverRole = "", dateOfSend = "", status = "", context = ""; 
                C_myClass.MenueItemsCreate(User, panelMenues);
                Label lblTitle = (Label)this.Master.FindControl("lblTitle");
                lblTitle.Text = "مشاهده پیام خصوصی";
                int messageId = Int32.Parse(Request.QueryString["messageId"].ToString());
                string conString = RamanConnectionString.LocalConnectionString();
                con = new SqlConnection(conString);
                cmd = con.CreateCommand();
                try
                {
                    con.Open();
                    cmd.CommandText = "select subject, recieverFullName, recieverRole, dateOfSend, status, context FROM t_Messages Where messageId = " + messageId;
                    SqlDataReader rdr = cmd.ExecuteReader();
                    if (rdr.HasRows)
                    {
                        while (rdr.Read())
                        {
                            subject = (rdr.GetValue(0) != DBNull.Value ? rdr.GetString(0) : "");
                            recieverFullName = (rdr.GetValue(1) != DBNull.Value ? rdr.GetString(1) : "");
                            recieverRole = (rdr.GetValue(2) != DBNull.Value ? rdr.GetString(2) : "");
                            dateOfSend = (rdr.GetValue(3) != DBNull.Value ? rdr.GetString(3) : "");
                            status = (rdr.GetValue(4) != DBNull.Value ? rdr.GetString(4) : "");
                            context = (rdr.GetValue(5) != DBNull.Value ? rdr.GetString(5) : "");
                        }
                    }
                    rdr.Close();
                    lblSubject.Text = subject;
                    lblRecieverFullName.Text = recieverFullName;
                    lblDateOfSend.Text = dateOfSend;
                    lblRecieverRole.Text = (recieverRole == "manager" ? "مدیر سایت" : (recieverRole == "admin" ? "کارمند آموزشگاه" : (recieverRole == "student" ? "دانشجو" : (recieverRole == "user" ? "کاربر سایت" : (recieverRole == "teacher" ? "مدرس" : "")))));
                    lblContext.Text = context;
                    lblStatus.Text = ( status == "read" ? "این پیام توسط دریافت کننده پیام مشاهده شده است." : ( status == "unread" ? "این پیام هنوز توسط دریافت کننده پیام خوانده نشده است." : ""));
                    imgSenderPhoto.ImageUrl = FindRecieverImageUrl();
                }
                catch
                {
                }
                finally
                {
                    con.Close();
                }
            }
        }
        catch
        {
        }
        finally
        {
        }
    }

    protected string FindRecieverImageUrl()
    {
        string picUrl = "";
        try
        {
            string picName = "", rootUrl = "~/photoes/";
            string photoesFolderPath = Request.PhysicalApplicationPath + @"photoes\";
            int messageId = Int32.Parse(Request.QueryString["messageId"].ToString());
            string conString = RamanConnectionString.LocalConnectionString();
            con = new SqlConnection(conString);
            cmd = con.CreateCommand();
            string recieverRole = _UseFull.C_Usefull.FindStringWithIntegerShart(con, cmd, "t_Messages", "recieverRole", "messageId", messageId);
            string recieverUserName = _UseFull.C_Usefull.FindStringWithIntegerShart(con, cmd, "t_Messages", "recieverUserName", "messageId", messageId);
            try
            {
                con.Open();
                if (recieverRole == "manager")
                {
                    cmd.CommandText = "select managerPhoto from t_Managers Where userName = N'" + recieverUserName + "'";
                    picName = ( cmd.ExecuteScalar() != DBNull.Value ? cmd.ExecuteScalar().ToString() : "2120ds0djjdfuyeu");
                }
                else if (recieverRole == "admin")
                {
                    cmd.CommandText = "select adminPhotoUrl from t_Admins Where userName = N'" + recieverUserName + "'";
                    picName = (cmd.ExecuteScalar() != DBNull.Value ? cmd.ExecuteScalar().ToString() : "2120ds0djjdfuyeu");
                }
                else if (recieverRole == "teacher")
                {
                    cmd.CommandText = "select teacherPhotoUrl from t_Teachers Where userName = N'" + recieverUserName + "'";
                    picName = (cmd.ExecuteScalar() != DBNull.Value ? cmd.ExecuteScalar().ToString() : "2120ds0djjdfuyeu");
                }
                else if (recieverRole == "student")
                {
                    cmd.CommandText = "select studentPhotoUrl from t_Students Where userName = N'" + recieverUserName + "'";
                    picName = (cmd.ExecuteScalar() != DBNull.Value ? cmd.ExecuteScalar().ToString() : "2120ds0djjdfuyeu");
                }
                else if (recieverRole == "user")
                {
                    cmd.CommandText = "select userPhotoUrl from t_Users Where userName = N'" + recieverUserName + "'";
                    picName = (cmd.ExecuteScalar() != DBNull.Value ? cmd.ExecuteScalar().ToString() : "2120ds0djjdfuyeu");
                }
                if (File.Exists(photoesFolderPath + picName))
                {
                    picUrl = rootUrl + picName;
                }
                else
                {
                    picUrl = rootUrl + @"noPersonalPhoto.gif";
                }
            }
            catch(Exception ex)
            {
            }
            finally
            {
                con.Close();
            }
        }
        catch(Exception exc)
        {
        }
        return picUrl;
    }
    protected void btnBack_Click(object sender, EventArgs e)
    {
        try
        {
            string returnUrl = Request.QueryString["returnUrl"].ToString();
            Response.Redirect(returnUrl);
        }
        catch
        {
        }
    }
}
