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_ClassesList : System.Web.UI.Page
{
    //************************************
    SqlConnection con;
    SqlDataAdapter adp;
    SqlCommand cmd;
    DataSet ds;
    SqlTransaction tr;
    static string selectCommand = "";
    //************************************
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            C_myClass.MenueItemsCreate(User, panelMenues);
            Label lblTitle = (Label)this.Master.FindControl("lblTitle");
            lblTitle.Text = "کلاس ها";
            if (!Page.IsPostBack)
            {
                string conString = RamanConnectionString.LocalConnectionString();
                con = new SqlConnection(conString);
                cmd = con.CreateCommand();
                adp = new SqlDataAdapter(cmd);
                try
                {
                    con.Open();
                    adp.SelectCommand.CommandText = "SELECT * FROM t_Classes INNER JOIN t_Teachers ON t_Classes.teacherId = t_Teachers.teacherId INNER JOIN t_Courses ON t_Classes.courseId = t_Courses.courseId";
                    ds = new DataSet();
                    adp.Fill(ds, "tbl");
                    GridView1.DataSource = ds;
                    GridView1.DataBind();
                }
                catch
                {
                }
                finally
                {
                    if (con.State != ConnectionState.Closed)
                    {
                        con.Close();
                    }
                }
            }
        }
        catch
        {
        }
    }
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        try
        {
            if (e.CommandName == "EditPress")
            {
                int classId = Int32.Parse(e.CommandArgument.ToString());
                Response.Redirect("~/rl_admins/pg_ClassEdit.aspx?classId=" + classId.ToString() + "&returnUrl=pg_ClassesList.aspx");
            }
            else if (e.CommandName == "DeletePress")
            {
                int classId = Int32.Parse(e.CommandArgument.ToString());
                string conString = RamanConnectionString.LocalConnectionString();
                con = new SqlConnection(conString);
                cmd = con.CreateCommand();
                try
                {
                    con.Open();
                    tr = con.BeginTransaction();
                    cmd.Transaction = tr;
                    cmd.CommandText = "delete from t_ClassStudents Where classId = " + classId;
                    cmd.ExecuteNonQuery();
                    cmd.CommandText = "delete from t_Classes Where classId = " + classId;
                    cmd.ExecuteNonQuery();
                    tr.Commit();
                    adp = new SqlDataAdapter(cmd);
                    adp.SelectCommand.CommandText = "SELECT * FROM t_Classes INNER JOIN t_Teachers ON t_Classes.teacherId = t_Teachers.teacherId INNER JOIN t_Courses ON t_Classes.courseId = t_Courses.courseId";
                    ds = new DataSet();
                    adp.Fill(ds, "tbl");
                    GridView1.DataSource = ds;
                    GridView1.DataBind();
                }
                catch(Exception ex)
                {
                    tr.Rollback();
                    Alert.Show(ex.Message);
                }
                finally
                {
                    if (con.State != ConnectionState.Closed)
                        con.Close();
                }
            }
            else if (e.CommandName == "Students")
            {
                int classId = Int32.Parse(e.CommandArgument.ToString());
                ////////////////////////////////////
                Response.Redirect("~/rl_admins/pg_ClassStudentsEdit.aspx?classId=" + classId.ToString() + "&returnUrl=pg_ClassesList.aspx");
                ////////////////////////////////////
            }
        }
        catch
        {
        }
    }
    protected void btnNewClass_Click(object sender, EventArgs e)
    {
        try
        {
            Response.Redirect("pg_ClassDefine.aspx?returnUrl=pg_ClassesList.aspx");
        }
        catch
        {
        }
    }

    protected string GetClassInfo(object ClassId)
    {
        string res = "";
        try
        {
            string courseName = "", teacherName = "";
            int classId = Int32.Parse(ClassId.ToString());
            string conString = RamanConnectionString.LocalConnectionString();
            con = new SqlConnection(conString);
            cmd = con.CreateCommand();
            con.Open();
            cmd.CommandText = "select fullName, courseName 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.classId = " + ClassId.ToString();
            SqlDataReader rdr = cmd.ExecuteReader();
            if (rdr.HasRows)
            {
                while(rdr.Read())
                {
                    teacherName = (rdr.GetValue(0) != DBNull.Value ? rdr.GetString(0) : "");
                    courseName = (rdr.GetValue(1) != DBNull.Value ? rdr.GetString(1) : "");
                }
            }
            rdr.Close();
            cmd.CommandText = "select Count(*) FROM t_ClassStudents Where classId = " + classId;
            int stuCount = ( cmd.ExecuteScalar() != DBNull.Value ? Int32.Parse(cmd.ExecuteScalar().ToString()) : 0);
            res = "نام دوره : " + courseName + "<br />" +
                "نام مدرس : " + teacherName + "<br />" +
                "تعداد دانشجویان ثبت شده : " + stuCount.ToString() + "<br />";
        }
         catch
        {
        }
        finally
        {
            if (con.State != ConnectionState.Closed)
                con.Close();
        }
        return res;
    }

    protected string GetSelectCommand()
    {
        string res = "";
        return res;
    }

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        try
        {
        }
        catch
        {
        }
    }
}
