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_students_pg_RegisterCourse : System.Web.UI.Page
{
    SqlConnection con;
    SqlDataAdapter adp;
    DataSet ds;
    SqlTransaction tr;
    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);
                SqlCommand cmd = con.CreateCommand();
                adp = new SqlDataAdapter(cmd);
                adp.SelectCommand.CommandText = "select * from t_Courses Where showInList = 1";
                ds = new DataSet();
                adp.Fill(ds, "tbl");
                GridView1.DataSource = ds;
                GridView1.DataBind();
                
            }
        }
        catch(Exception ex)
        {
        }
        finally
        {
        }
    }
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        try
        {
            if (e.CommandName == "Register")
            {
                string regStatus = "";
                bool reged = false;
                int regId = 0;
                int courseId = Int32.Parse(e.CommandArgument.ToString());
                string userName = HttpContext.Current.User.Identity.Name;
                string conString = RamanConnectionString.LocalConnectionString();
                con = new SqlConnection(conString);
                SqlCommand cmd = con.CreateCommand();
                try
                {
                    con.Open();
                    int studentId = _UseFull.C_Usefull.FindIntegerWithStringShart(con, cmd, "t_Students", "studentId", "userName", userName);
                    cmd.CommandText = "select regStatus, regId from t_Registers Where studentId = " + studentId + " AND courseId = " + courseId ;
                    SqlDataReader rdr = cmd.ExecuteReader();
                    if (rdr.HasRows)
                    {
                        while (rdr.Read())
                        {
                            reged = true;
                            regStatus = (rdr.GetValue(0) != DBNull.Value ? rdr.GetString(0) : "");
                            regId = (rdr.GetValue(1) != DBNull.Value ? rdr.GetInt32(1) : 0);
                        }
                    }
                    else
                    {
                        reged = false;
                    }
                    rdr.Close();
                    if ((reged) && (regStatus == "انتظار"))
                    {
                        cmd.CommandText = "delete from t_Registers Where studentId = " + studentId + " AND courseId = " + courseId;
                        cmd.ExecuteNonQuery();
                        adp = new SqlDataAdapter(cmd);
                        adp.SelectCommand.CommandText = "select * from t_Courses Where showInList = 1";
                        ds = new DataSet();
                        adp.Fill(ds, "tbl");
                        GridView1.DataSource = ds;
                        GridView1.DataBind();
                    }
                    else if ((reged) && (regStatus == "رد"))
                    {
                        Response.Redirect("pg_RegisterCancelReason.aspx?regId=" + regId.ToString() + "&returnUrl=pg_RegisterCourse.aspx");
                    }
                    else if ((reged) && (regStatus == "تأیید"))
                    {
                    }
                    else if (!reged)
                    {
                        string regDate = _UseFull.C_Usefull.FarsiShamsiDate(DateTime.Now);
                        string regStatus2 = "انتظار";
                        string commitDate = "";
                        string cancelDate = "";
                        string cancelReason = "";
                        cmd.CommandText = t_RegistersInsertCommand(studentId, courseId, regDate, regStatus2, commitDate, cancelDate, cancelReason);
                        cmd.ExecuteNonQuery();
                        adp = new SqlDataAdapter(cmd);
                        adp.SelectCommand.CommandText = "select * from t_Courses Where showInList = 1";
                        ds = new DataSet();
                        adp.Fill(ds, "tbl");
                        GridView1.DataSource = ds;
                        GridView1.DataBind();
                    }
                }
                catch(Exception ex)
                {
                }
                finally
                {
                    if (con.State != ConnectionState.Closed)
                        con.Close();
                }
            }
        }
        catch
        {
        }
    }

    private string t_RegistersInsertCommand(int StudentId, int CourseId, string RegDate, string RegStatus, string CommitDate, string CancelDate, string CancelReason)
    {
        string comm = "insert into t_Registers (studentId, courseId, regDate, regStatus, commitDate, cancelDate, cancelReason) VALUES(" +
            StudentId + " , " +
            CourseId + " , N'" +
            RegDate + "' , N'" +
            RegStatus + "' , N'" +
            CommitDate + "' , N'" +
            CancelDate + "', N'" +
            CancelReason + "')";
        return comm;
    }

    protected string FindCssClass(object CourseId)
    {
        string regStatus = "";
        string css = "";
        string userName = HttpContext.Current.User.Identity.Name;
        int courseId = Int32.Parse(CourseId.ToString());
        string conString = RamanConnectionString.LocalConnectionString();
        con = new SqlConnection(conString);
        SqlCommand cmd = con.CreateCommand();
        try
        {
            con.Open();
            int studentId = _UseFull.C_Usefull.FindIntegerWithStringShart(con, cmd, "t_Students", "studentId", "userName", userName);
            cmd.CommandText = "select courseId , regStatus from t_Registers Where studentId = " + studentId + " AND courseId = " + courseId;
            SqlDataReader rdr = cmd.ExecuteReader();
            bool reged = false;
            if (rdr.HasRows)
            {
                while (rdr.Read())
                {
                    if ((rdr.GetValue(0) != DBNull.Value) && (rdr.GetValue(1) != DBNull.Value))
                    {
                        reged = true;
                        regStatus = rdr.GetString(1);
                    }
                }
            }
            else
            {
                reged = false;
            }
            rdr.Close();
            if ((reged) && (regStatus == "انتظار"))
            {
                css = "gridsmallbuttonstrue";
            }
            else if ((reged) && (regStatus == "رد"))
            {
                css = "gridlankaslabelred";
            }
            else if ((reged) && (regStatus == "تأیید"))
            {
                css = "gridlankaslabelgreen";
            }
            else
            {
                css = "gridsmallbuttons";
            }
        }
        catch (Exception ex)
        {
        }
        finally
        {
            if (con.State != ConnectionState.Closed)
                con.Close();
        }
        return css;
    }

    protected string FindLinkButtonText(object CourseId)
    {
        string regStatus = "";
        string txt = "";
        string userName = HttpContext.Current.User.Identity.Name;
        int courseId = Int32.Parse(CourseId.ToString());
        string conString = RamanConnectionString.LocalConnectionString();
        con = new SqlConnection(conString);
        SqlCommand cmd = con.CreateCommand();
        try
        {
            con.Open();
            int studentId = _UseFull.C_Usefull.FindIntegerWithStringShart(con, cmd, "t_Students", "studentId", "userName", userName);
            cmd.CommandText = "select courseId , regStatus from t_Registers Where studentId = " + studentId + " AND courseId = " + courseId;
            SqlDataReader rdr = cmd.ExecuteReader();
            bool reged = false;
            if (rdr.HasRows)
            {
                while (rdr.Read())
                {
                    if ((rdr.GetValue(0) != DBNull.Value) && (rdr.GetValue(1) != DBNull.Value))
                    {
                        reged = true;
                        regStatus = rdr.GetString(1);
                    }
                }
            }
            else
            {
                reged = false;
            }
            rdr.Close();
            if ((reged) && (regStatus == "انتظار"))
            {
                txt = "حذف ثبت نام";
            }
            else if ((reged) && (regStatus == "رد"))
            {
                txt = "رد شد";
            }
            else if ((reged) && (regStatus == "تأیید"))
            {
                txt = "تأیید شد";
            }
            else if (!reged)
            {
                txt = "ثبت نام";
            }
        }
        catch (Exception ex)
        {
        }
        finally
        {
            if (con.State != ConnectionState.Closed)
                con.Close();
        }
        return txt;
    }

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        /*
        try
        {
            if (Roles.IsUserInRole("student"))
            {
                string regStatus = "";
                string userName = HttpContext.Current.User.Identity.Name;
                Label1.Text = userName;
                TableCell cell = e.Row.Cells[0];
                Label1.Text = userName;
                //int courseId = Convert.ToInt32(cell.Text);
                string conString = RamanConnectionString.LocalConnectionString();
                con = new SqlConnection(conString);
                SqlCommand cmd = con.CreateCommand();
                try
                {
                    con.Open();
                    int studentId = _UseFull.C_Usefull.FindIntegerWithStringShart(con, cmd, "t_Students", "studentId", "userName", userName);
                    cmd.CommandText = "select courseId, regStatus from t_Registers Where studentId = " + studentId;
                    SqlDataReader rdr = cmd.ExecuteReader();
                    bool reged = false;
                    if (rdr.HasRows)
                    {
                        while (rdr.Read())
                        {
                            if (rdr.GetValue(0) != DBNull.Value)
                            {
                                if (rdr.GetInt32(0) == courseId)
                                {
                                    reged = true;
                                    //regStatus = ( rdr.GetValue(1) != DBNull.Value ? rdr.GetString(1).ToString() : "" );
                                }
                            }
                        }
                    }
                    rdr.Close();
                    Label1.Text = reged.ToString() + regStatus;
                    if ((reged) && (regStatus == "انتظار"))
                    {
                        e.Row.Style.Add("background-color", "rgb(130,215,130)");
                    }
                    else if ((reged) && (regStatus == "رد"))
                    {
                        e.Row.Style.Add("background-color", "rgb(215,130,130)");
                    }
                    else if ((reged) && (regStatus == "تأیید"))
                    {
                        e.Row.Style.Add("background-color", "rgb(80,255,80)");
                    }
                    else if (!reged)
                    {
                        e.Row.Style.Add("background-color", "White");
                    }
                }
                catch(Exception ex)
                {
                    Label1.Text = ex.Message;
                }
                finally
                {
                    if (con.State != ConnectionState.Closed)
                        con.Close();    
                }
            }
        }
        catch(Exception exc)
        {
            Label1.Text = exc.Message;
        }
        */
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        try
        {
            GridView1.PageIndex = e.NewPageIndex;
            string conString = RamanConnectionString.LocalConnectionString();
            con = new SqlConnection(conString);
            SqlCommand cmd = con.CreateCommand();
            adp = new SqlDataAdapter(cmd);
            adp.SelectCommand.CommandText = "select * from t_Courses Where showInList = 1";
            ds = new DataSet();
            adp.Fill(ds, "tbl");
            GridView1.DataSource = ds;
            GridView1.DataBind();
        }
        catch
        {
        }
    }
}
