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.Collections.Generic;

public partial class rl_students_pg_StudentSchedule : System.Web.UI.Page
{
    //**********************************
    SqlConnection con;
    SqlCommand cmd;
    List<int> ids = new List<int>();
    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 = "برنامه کلاسی";
            if (!Page.IsPostBack)
            {
                if (Roles.IsUserInRole("teacher"))
                {
                    int teacherId = Int32.Parse(Session["iden"].ToString());
                    lastQuery = "SELECT t_Classes.classId, t_Classes.teacherId, t_Classes.classTitle, t_Courses.courseName, t_Classes.status FROM t_Classes INNER JOIN t_Courses ON t_Classes.courseId = t_Courses.courseId WHERE ( t_Classes.teacherId = " + teacherId + ")";
                    SqlDataSource1.SelectCommand = lastQuery;
                    GridView1.DataBind();
                }
            }
        }
        catch
        {
        }
        finally
        {
        }
    }
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        try
        {
            if (e.CommandName == "Schedule")
            {
                int classId = Int32.Parse(e.CommandArgument.ToString());
                Response.Redirect("pg_ClassSchedule.aspx?classId=" + classId + "&returnUrl=pg_ScheduleForTeacher.aspx");
            }
        }
        catch
        {
        }
    }

    private bool IsInClass(int StudentId, int ClassId)
    {
        bool res = false;
        string conString = RamanConnectionString.LocalConnectionString();
        con = new SqlConnection(conString);
        cmd = con.CreateCommand();
        try
        {
            con.Open();
            cmd.CommandText = "select * from t_ClassStudents Where classId = " + ClassId + " AND studentId = " + StudentId;
            SqlDataReader rdr = cmd.ExecuteReader();
            if (rdr.HasRows)
            {
                res = true;
            }
            rdr.Close();
        }
        catch
        {
        }
        finally
        {
            con.Close();
        }
        return res;
    }

    protected void btnEdit_Click(object sender, EventArgs e)
    {
        try
        {
            int teacherId = Int32.Parse(Session["iden"].ToString());
            lastQuery = "SELECT t_Classes.classId, t_Classes.teacherId, t_Classes.classTitle, t_Courses.courseName, t_Classes.status FROM t_Classes INNER JOIN t_Courses ON t_Classes.courseId = t_Courses.courseId WHERE ( t_Classes.teacherId = " + teacherId + ") AND (t_Classes.status = N'جاری')";
            SqlDataSource1.SelectCommand = lastQuery;
            GridView1.DataBind();
        }
        catch
        {
        }
    }
    protected void btnAllShow_Click(object sender, EventArgs e)
    {
        try
        {
            int teacherId = Int32.Parse(Session["iden"].ToString());
            lastQuery = "SELECT t_Classes.classId, t_Classes.teacherId, t_Classes.classTitle, t_Courses.courseName, t_Classes.status FROM t_Classes INNER JOIN t_Courses ON t_Classes.courseId = t_Courses.courseId WHERE ( t_Classes.teacherId = " + teacherId + ")";
            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
        {
        }
    }
}
