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_MessageSendNew : 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 = "ارسال پیام خصوصی";
                string recieverRole = (Request.QueryString["recieverRole"] != null ? Request.QueryString["recieverRole"].ToString() : "");
                int id = (Request.QueryString["id"] != null ? Int32.Parse(Request.QueryString["id"].ToString()) : -1);
                string recieverUserName = (Request.QueryString["recieverUserName"] != null ? Request.QueryString["recieverUserName"].ToString() : "");
                tbRecieverId.Text = ( id != -1 ? id.ToString() : "");
                tbRecieverUserName.Text = (recieverUserName.Length > 0 ? recieverUserName : "");
                tbRecieverRole.Text = (recieverRole == "manager" ? "مدیر سایت" : (recieverRole == "admin" ? "کارمند" : (recieverRole == "teacher" ? "مدرس" : (recieverRole == "student" ? "دانشجو" : (recieverRole == "user" ? "کاربر سایت" : "")))));
            }
        }
        catch
        {
        }
        finally
        {
        }
    }
    protected void btnBack_Click(object sender, EventArgs e)
    {
        try
        {
            string returnUrl = Request.QueryString["returnUrl"].ToString();
            Response.Redirect(returnUrl);
        }
        catch
        {
        }
    }

    protected void upRecs_Load(object sender, EventArgs e)
    {
    }
    protected void btnOk_Click(object sender, EventArgs e)
    {
        try
        {
            if ((tbRecieverId.Text.Length > 0) && (tbSubject.Text.Length > 0) && (tbContext.Text.Length > 0) && (tbRecieverUserName.Text.Length > 0))
            {
                string conString = RamanConnectionString.LocalConnectionString();
                con = new SqlConnection(conString);
                cmd = con.CreateCommand();
                string senderRole = (Roles.IsUserInRole("manager") ? "manager" : ( Roles.IsUserInRole("admin") ? "admin" : ( Roles.IsUserInRole("teacher") ? "teacher" : (Roles.IsUserInRole("student")? "student" : (Roles.IsUserInRole("user")? "user" : "")))));
                string senderFullName = Session["fullName"].ToString();
                string senderUserName = User.Identity.Name;
                string recieverRole = (Request.QueryString["recieverRole"] != null ? Request.QueryString["recieverRole"].ToString() : "");
                string recieverFullName = "";
                string recieverUserName = tbRecieverUserName.Text;
                string subject = tbSubject.Text;
                string context = tbContext.Text;
                string dateOfSend = _UseFull.C_Usefull.FarsiShamsiDate(DateTime.Now);
                string status = "unread";
                bool senderDelete = false;
                bool recieverDelete = false;
                if (recieverRole == "manager")
                {
                    recieverFullName = _UseFull.C_Usefull.FindStringWithStringShart(con, cmd, "t_Managers", "fullName", "userName", recieverUserName);
                }
                else if (recieverRole == "admin")
                {
                    recieverFullName = _UseFull.C_Usefull.FindStringWithStringShart(con, cmd, "t_Admins", "fullName", "userName", recieverUserName);
                }
                else if (recieverRole == "teacher")
                {
                    recieverFullName = _UseFull.C_Usefull.FindStringWithStringShart(con, cmd, "t_Teachers", "fullName", "userName", recieverUserName);
                }
                else if (recieverRole == "student")
                {
                    recieverFullName = _UseFull.C_Usefull.FindStringWithStringShart(con, cmd, "t_Students", "fullName", "userName", recieverUserName);
                }
                else if (recieverRole == "user")
                {
                    recieverFullName = _UseFull.C_Usefull.FindStringWithStringShart(con, cmd, "t_Users", "fullName", "userName", recieverUserName);
                }
                try
                {
                    con.Open();
                    cmd.CommandText = t_MessagesInsertCommand(senderRole, senderFullName, senderUserName, recieverRole, recieverFullName, recieverUserName, subject, context, dateOfSend, status, senderDelete, recieverDelete);
                    cmd.ExecuteNonQuery();
                    lblMessage.ForeColor = System.Drawing.Color.Green;
                    lblMessage.Text = "با موفقیت ارسال شد.";
                }
                catch
                {
                }
                finally
                {
                    con.Close();
                }
            }
        }
        catch(Exception ex)
        {
            lblMessage.ForeColor = System.Drawing.Color.Red;
            lblMessage.Text = ex.Message;//"خطا در ارسال.";
        }
    }

    private string t_MessagesInsertCommand(string SenderRole, string SenderFullName, string SenderUserName, string RecieverRole, string RecieverFullName, string RecieverUserName, string Subject, string Context, string DateOfSend, string Status, bool SenderDelete, bool RecieverDelete)
    {
        string comm = "insert into t_Messages (senderRole, senderFullName, senderUserName, recieverRole, recieverFullName, recieverUserName, subject, context, dateOfSend, status, senderDelete, recieverDelete) values (N'" +
            SenderRole + "', N'" +
            SenderFullName + "', N'" +
            SenderUserName + "', N'" +
            RecieverRole + "', N'" +
            RecieverFullName + "', N'" +
            RecieverUserName + "', N'" +
            Subject + "', N'" +
            Context + "', N'" +
            DateOfSend + "', N'" +
            Status + "', " +
            (SenderDelete ? 1 : 0) + " , " +
            (RecieverDelete ? 1 : 0) + " )";
        return comm;
    }

    protected void panelStudents_Load(object sender, EventArgs e)
    {
        
    }

    protected string FindSqlSelectCommand()
    {
        string comm = "";
        try
        {
            System.Collections.Generic.List<int> studentClasses = new System.Collections.Generic.List<int>();
            string conString = RamanConnectionString.LocalConnectionString();
            con = new SqlConnection(conString);
            cmd = con.CreateCommand();
            try
            {
                con.Open();
                int studentId = Int32.Parse(Session["iden"].ToString());
                cmd.CommandText = "select classId from ClassStudents Where studentId = " + studentId;
                SqlDataReader rdr = cmd.ExecuteReader();
                if (rdr.HasRows)
                {
                    while (rdr.Read())
                    {
                        int classId = (rdr.GetValue(0) != DBNull.Value ? rdr.GetInt32(0) : 0);
                        studentClasses.Add(classId);
                    }
                }
                rdr.Close();
                comm = "SELECT t_Students.studentId, t_Students.fullName, t_Students.gender, t_Students.userName FROM t_Students INNER JOIN t_ClassStudents ON t_Students.studentId = t_ClassStudents.studentId WHERE (t_ClassStudents.classId IN " + _UseFull.C_Usefull.FilesIN(studentClasses) + ") ORDER BY t_Students.studentId";
            }
            catch (Exception ex)
            {
                comm = ex.Message;
            }
            finally
            {
                con.Close();
            }
        }
        catch(Exception exc)
        {
            comm = exc.Message;
        }
        return comm;
    }
}
