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.Collections.Generic;
using System.Data.SqlClient;

public partial class rl_admins_pg_MessageSendToClassStudents : System.Web.UI.Page
{
    //*-******************************
    static string lastQuery = "";
    SqlConnection con;
    SqlCommand cmd;
    SqlTransaction tr;
    //*-******************************
    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 t_Classes.classId, t_Classes.classTitle, t_Courses.courseName, t_Teachers.fullName FROM t_Classes INNER JOIN t_Courses ON t_Classes.courseId = t_Courses.courseId INNER JOIN t_Teachers ON t_Classes.teacherId = t_Teachers.teacherId";
            }
        }
        catch
        {
        }
        finally
        {
        }
    }

    protected void btnBack_Click(object sender, EventArgs e)
    {
        try
        {
            string returnUrl = Request.QueryString["returnUrl"].ToString();
            Response.Redirect(returnUrl);
        }
        catch
        {
        }
    }

    protected void btnOk_Click(object sender, EventArgs e)
    {
        try
        {
            if ((tbClassId.Text.Length > 0) && (tbSubject.Text.Length > 0) && (tbContext.Text.Length > 0))
            {
                List<Hashtable> students = new List<Hashtable>();
                Hashtable studentInfo;
                string conString = RamanConnectionString.LocalConnectionString();
                con = new SqlConnection(conString);
                cmd = con.CreateCommand();
                int classId = Int32.Parse(tbClassId.Text);
                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 = "student";
                string recieverFullName = "", recieverUserName = "";
                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;
                try
                {
                    con.Open();
                    tr = con.BeginTransaction();
                    cmd.Transaction = tr;
                    cmd.CommandText = "select t_Students.fullName, t_Students.userName FROM t_Students INNER JOIN t_ClassStudents ON t_Students.studentId = t_ClassStudents.studentId Where t_ClassStudents.classId = " + classId;
                    SqlDataReader rdr = cmd.ExecuteReader();
                    if (rdr.HasRows)
                    {
                        while (rdr.Read())
                        {
                            studentInfo = new Hashtable();
                            studentInfo["fullName"] = (rdr.GetValue(0) != DBNull.Value ? rdr.GetString(0) : "");
                            studentInfo["userName"] = (rdr.GetValue(1) != DBNull.Value ? rdr.GetString(1) : "");
                            students.Add(studentInfo);
                        }
                    }
                    rdr.Close();
                    for (int i = 0; i < students.Count; i++)
                    {
                        Hashtable info = students[i];
                        recieverFullName = info["fullName"].ToString();
                        recieverUserName = info["userName"].ToString();
                        cmd.CommandText = t_MessagesInsertCommand(senderRole, senderFullName, senderUserName, recieverRole, recieverFullName, recieverUserName, subject, context, dateOfSend, status, senderDelete, recieverDelete);
                        cmd.ExecuteNonQuery();
                    }
                    //////////////////////////////
                    tr.Commit();
                    lblMessage.ForeColor = System.Drawing.Color.Green;
                    lblMessage.Text = "با موفقیت ارسال شد.";
                }
                catch(Exception ex)
                {
                    tr.Rollback();
                    lblMessage.ForeColor = System.Drawing.Color.Red;
                    lblMessage.Text = ex.Message;
                }
                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 btnSelectUser_Click(object sender, EventArgs e)
    {
        try
        {
            lblClassesTitle.Text = "کلاس مورد نظرتان را ازلیست زیر انتخاب کنید :";
            mpeClasses.Show();
        }
        catch
        {
        }
    }
    protected void btnStudentsCancel_Click(object sender, EventArgs e)
    {
        try
        {
            mpeClasses.Hide();
        }
        catch
        {
        }
    }
    protected void btnCurrentClasses_Click(object sender, EventArgs e)
    {
        try
        {
            lastQuery = "SELECT t_Classes.classId, t_Classes.classTitle, t_Courses.courseName, t_Teachers.fullName FROM t_Classes INNER JOIN t_Courses ON t_Classes.courseId = t_Courses.courseId INNER JOIN t_Teachers ON t_Classes.teacherId = t_Teachers.teacherId Where t_Classes.status = N'جاری'";
            sqlClasses.SelectCommand = lastQuery;
            GridClasses.DataBind();
        }
        catch
        {
        }
    }
    protected void GridClasses_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        try
        {
            GridClasses.PageIndex = e.NewPageIndex;
            sqlClasses.SelectCommand = lastQuery;
            GridClasses.DataBind();
        }
        catch
        {
        }
    }
    protected void GridClasses_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        try
        {
            int classId = Int32.Parse(e.CommandArgument.ToString());
            string conString = RamanConnectionString.LocalConnectionString();
            con = new SqlConnection(conString);
            cmd = con.CreateCommand();
            string classTitle = _UseFull.C_Usefull.FindStringWithIntegerShart(con, cmd, "t_Classes", "classTitle", "classId", classId);
            tbClassId.Text = classId.ToString();
            tbClassTitle.Text = classTitle;
            UpdatePanel1.Update();
            mpeClasses.Hide();
        }
        catch
        {
        }
    }
    protected void btnAllShow_Click(object sender, EventArgs e)
    {
        try
        {
            lastQuery = "SELECT t_Classes.classId, t_Classes.classTitle, t_Courses.courseName, t_Teachers.fullName FROM t_Classes INNER JOIN t_Courses ON t_Classes.courseId = t_Courses.courseId INNER JOIN t_Teachers ON t_Classes.teacherId = t_Teachers.teacherId";
            sqlClasses.SelectCommand = lastQuery;
            GridClasses.DataBind();
        }
        catch
        {

        }
    }
}
