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("student"))
                {
                    int studentId = Int32.Parse(Session["iden"].ToString());
                    lastQuery = "SELECT t_Classes.* , t_Courses.courseName, t_Teachers.fullName FROM t_Classes INNER JOIN t_ClassStudents ON t_Classes.classId = t_ClassStudents.classId INNER JOIN t_Courses ON t_Classes.courseId = t_Courses.courseId INNER JOIN t_Teachers ON t_Classes.teacherId = t_Teachers.teacherId WHERE (t_ClassStudents.studentId = " + studentId + ")";
                    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_ClassScheduleForStudent.aspx?classId=" + classId + "&returnUrl=pg_StudentSchedule.aspx");
            }
        }
        catch
        {
        }
    }

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        try
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                if (Roles.IsUserInRole("student"))
                {
                    int classId = Int32.Parse(e.Row.Cells[0].Text);
                    string conString = RamanConnectionString.LocalConnectionString();
                    con = new SqlConnection(conString);
                    cmd = con.CreateCommand();
                    int studentId = _UseFull.C_Usefull.FindIntegerWithStringShart(con, cmd, "t_Students", "studentId", "userName", User.Identity.Name);
                    try
                    {
                        if (!IsInClass(studentId, classId))
                        {
                            ids.Add(e.Row.RowIndex);
                        }
                    }
                    catch
                    {
                    }
                    finally
                    {
                        if (con.State != ConnectionState.Closed)
                            con.Close();
                    }
                }
            }
        }
        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 GridView1_DataBound(object sender, EventArgs e)
    {
        try
        {
            for (int i = 0; i < ids.Count; i++)
            {
                GridView1.Rows[ids[i]].Visible = false;
            }
        }
        catch
        {
        }
    }
    protected void btnEdit_Click(object sender, EventArgs e)
    {
        try
        {
            int studentId = Int32.Parse(Session["iden"].ToString());
            lastQuery = "SELECT t_Classes.* , t_Courses.courseName, t_Teachers.fullName FROM t_Classes INNER JOIN t_ClassStudents ON t_Classes.classId = t_ClassStudents.classId INNER JOIN t_Courses ON t_Classes.courseId = t_Courses.courseId INNER JOIN t_Teachers ON t_Classes.teacherId = t_Teachers.teacherId WHERE (t_ClassStudents.studentId = " + studentId + ") AND (t_Classes.status = N'جاری')";
            SqlDataSource1.SelectCommand = lastQuery;
            GridView1.DataBind();
        }
        catch
        {
        }
    }
    protected void btnAllShow_Click(object sender, EventArgs e)
    {
        try
        {
            int studentId = Int32.Parse(Session["iden"].ToString());
            lastQuery = "SELECT t_Classes.* , t_Courses.courseName, t_Teachers.fullName FROM t_Classes INNER JOIN t_ClassStudents ON t_Classes.classId = t_ClassStudents.classId INNER JOIN t_Courses ON t_Classes.courseId = t_Courses.courseId INNER JOIN t_Teachers ON t_Classes.teacherId = t_Teachers.teacherId WHERE (t_ClassStudents.studentId = " + studentId + ")";
            SqlDataSource1.SelectCommand = lastQuery;
            GridView1.DataBind();
        }
        catch
        {
        }
    }
}
