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_Registers : System.Web.UI.Page
{
    //*********************************
    SqlConnection con;
    SqlCommand cmd;
    SqlDataAdapter adp;
    DataSet ds;
    private static string lastQuery = "";
    //*********************************
    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 = "ثبت نام ها";
                string conString = RamanConnectionString.LocalConnectionString();
                con = new SqlConnection(conString);
                cmd = con.CreateCommand();
                adp = new SqlDataAdapter(cmd);
                lastQuery = "select * from t_Registers Inner Join t_Courses ON t_Registers.courseId = t_Courses.courseId " +
                    "Inner Join t_Students On t_Registers.studentId = t_Students.studentId";
                adp.SelectCommand.CommandText = lastQuery;
                ds = new DataSet();
                adp.Fill(ds, "tbl");
                GridView1.DataSource = ds;
                GridView1.DataBind();
            }
        }
        catch
        {
        }
        finally
        {
        }
    }
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        try
        {
            if (e.CommandName == "CommitPress")
            {
                try
                {
                    int regId = Int32.Parse(e.CommandArgument.ToString());
                    string conString = RamanConnectionString.LocalConnectionString();
                    con = new SqlConnection(conString);
                    con.Open();
                    cmd = con.CreateCommand();
                    string regStatus = _UseFull.C_Usefull.FindStringWithIntegerShart(con, cmd, "t_Registers", "regStatus", "regId", regId);
                    if (regStatus == "انتظار")
                    {
                        cmd.CommandText = "update t_Registers set regStatus = N'تأیید' Where regId = " + regId;
                        cmd.ExecuteNonQuery();
                    }
                    else if (regStatus == "رد")
                    {
                        cmd.CommandText = "update t_Registers set regStatus = N'انتظار' Where regId = " + regId;
                        cmd.ExecuteNonQuery();
                    }
                    else if (regStatus == "تأیید")
                    {
                        cmd.CommandText = "update t_Registers set regStatus = N'انتظار' Where regId = " + regId;
                        cmd.ExecuteNonQuery();
                    }
                    adp = new SqlDataAdapter(cmd);
                    adp.SelectCommand.CommandText = lastQuery;
                    ds = new DataSet();
                    adp.Fill(ds, "tbl");
                    GridView1.DataSource = ds;
                    GridView1.DataBind();
                }
                catch
                {
                }
                finally
                {
                    con.Close();
                }
            }
            else if (e.CommandName == "CancelPress")
            {
                try
                {
                    int regId = Int32.Parse(e.CommandArgument.ToString());
                    string conString = RamanConnectionString.LocalConnectionString();
                    con = new SqlConnection(conString);
                    con.Open();
                    cmd = con.CreateCommand();
                    string regStatus = _UseFull.C_Usefull.FindStringWithIntegerShart(con, cmd, "t_Registers", "regStatus", "regId", regId);
                    if (regStatus == "انتظار")
                    {
                        cmd.CommandText = "update t_Registers set regStatus = N'رد' Where regId = " + regId;
                        cmd.ExecuteNonQuery();
                    }
                    else if (regStatus == "رد")
                    {
                        cmd.CommandText = "update t_Registers set regStatus = N'انتظار' Where regId = " + regId;
                        cmd.ExecuteNonQuery();
                    }
                    else if (regStatus == "تأیید")
                    {
                        cmd.CommandText = "update t_Registers set regStatus = N'انتظار' Where regId = " + regId;
                        cmd.ExecuteNonQuery();
                    }
                    adp = new SqlDataAdapter(cmd);
                    adp.SelectCommand.CommandText = lastQuery;
                    ds = new DataSet();
                    adp.Fill(ds, "tbl");
                    GridView1.DataSource = ds;
                    GridView1.DataBind();
                }
                catch
                {
                }
                finally
                {
                    con.Close();
                }
            }
            else if (e.CommandName == "DeletePress")
            {
                try
                {
                    int regId = Int32.Parse(e.CommandArgument.ToString());
                    string conString = RamanConnectionString.LocalConnectionString();
                    con = new SqlConnection(conString);
                    con.Open();
                    cmd = con.CreateCommand();
                    cmd.CommandText = "delete from t_Registers Where regId = " + regId;
                    cmd.ExecuteNonQuery();
                    adp = new SqlDataAdapter(cmd);
                    adp.SelectCommand.CommandText = lastQuery;
                    ds = new DataSet();
                    adp.Fill(ds, "tbl");
                    GridView1.DataSource = ds;
                    GridView1.DataBind();
                }
                catch
                {
                }
                finally
                {
                    con.Close();
                }
            }
        }
        catch
        {
        }
    }
    protected string FindStatusText(object RegId)
    {
        string res = "";
        try
        {
            int regId = Int32.Parse(RegId.ToString());
            string conString = RamanConnectionString.LocalConnectionString();
            con = new SqlConnection(conString);
            con.Open();
            cmd = con.CreateCommand();
            string regStatus = _UseFull.C_Usefull.FindStringWithIntegerShart(con, cmd, "t_Registers", "regStatus", "regId", regId);
            if (regStatus == "انتظار")
            {
                res = "در انتظار تأیید";
            }
            else if (regStatus == "رد")
            {
                res = "توسط آموزشگاه رد شد";
            }
            else if (regStatus == "تأیید")
            {
                res = "ثبت نام تأیید شد";
            }
        }
        catch
        {
        }
        finally
        {
            con.Close();
        }
        return res;
    }

    protected string FindShowButtonText(object RegStatus)
    {
        string res = "";
        string regStatus = RegStatus.ToString();
        if (regStatus == "انتظار")
        {
            res = "تأیید";
        }
        else if (regStatus == "تأیید")
        {
            res = "انتظار";
        }
        else if (regStatus == "رد")
        {
            res = "انتظار";
        }
        return res;
    }

    protected string FindShowButtonText2(object RegStatus)
    {
        string res = "";
        string regStatus = RegStatus.ToString();
        if (regStatus == "انتظار")
        {
            res = "رد";
        }
        else if (regStatus == "تأیید")
        {
            res = "انتظار";
        }
        else if (regStatus == "رد")
        {
            res = "انتظار";
        }
        return res;
    }

    protected string FindCssClass(object RegStatus)
    {
        string res = "";
        string regStatus = RegStatus.ToString();
        if (regStatus == "انتظار")
        {
            res = "gridsmallbuttonstrue";
        }
        else if (regStatus == "تأیید")
        {
            res = "gridsmallbuttonsfalse";
        }
        else if (regStatus == "رد")
        {
            res = "gridsmallbuttonsfalse";
        }
        return res;
    }

    protected string FindCssClass2(object RegStatus)
    {
        string res = "";
        string regStatus = RegStatus.ToString();
        if (regStatus == "انتظار")
        {
            res = "gridsmallbuttons";
        }
        else if (regStatus == "تأیید")
        {
            res = "gridsmallbuttonsfalse";
        }
        else if (regStatus == "رد")
        {
            res = "gridsmallbuttonsfalse";
        }
        return res;
    }

    protected string FindStatusCssClass(object RegId)
    {
        string res = "";
        try
        {
            int regId = Int32.Parse(RegId.ToString());
            string conString = RamanConnectionString.LocalConnectionString();
            con = new SqlConnection(conString);
            con.Open();
            cmd = con.CreateCommand();
            string regStatus = _UseFull.C_Usefull.FindStringWithIntegerShart(con, cmd, "t_Registers", "regStatus", "regId", regId);
            if (regStatus == "انتظار")
            {
                res = "labelyellow";
            }
            else if (regStatus == "رد")
            {
                res = "labelred";
            }
            else if (regStatus == "تأیید")
            {
                res = "labelgreen";
            }
        }
        catch
        {
        }
        finally
        {
            con.Close();
        }
        return res;
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        try
        {
            System.Threading.Thread.Sleep(2000);
            if (tbSearch.Text.Length > 0)
            {
                string fullName = tbSearch.Text;
                string conString = RamanConnectionString.LocalConnectionString();
                con = new SqlConnection(conString);
                cmd = con.CreateCommand();
                adp = new SqlDataAdapter(cmd);
                lastQuery = "select * from t_Registers Inner Join t_Courses ON t_Registers.courseId = t_Courses.courseId " +
                    "Inner Join t_Students On t_Registers.studentId = t_Students.studentId where t_Students.fullName LIKE N'%" + fullName + "%'";
                adp.SelectCommand.CommandText = lastQuery;
                ds = new DataSet();
                adp.Fill(ds, "tbl");
                GridView1.DataSource = ds;
                GridView1.DataBind();
            }
        }
        catch(Exception ex)
        {
        }
    }
    protected void btnAllShow_Click(object sender, EventArgs e)
    {
        try
        {
            string conString = RamanConnectionString.LocalConnectionString();
            con = new SqlConnection(conString);
            cmd = con.CreateCommand();
            adp = new SqlDataAdapter(cmd);
            lastQuery = "select * from t_Registers Inner Join t_Courses ON t_Registers.courseId = t_Courses.courseId " +
                "Inner Join t_Students On t_Registers.studentId = t_Students.studentId";
            adp.SelectCommand.CommandText = lastQuery;
            ds = new DataSet();
            adp.Fill(ds, "tbl");
            GridView1.DataSource = ds;
            GridView1.DataBind();
        }
        catch (Exception ex)
        {
        }
        finally
        {
        }
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        try
        {
            GridView1.PageIndex = e.NewPageIndex;
            string conString = RamanConnectionString.LocalConnectionString();
            con = new SqlConnection(conString);
            cmd = con.CreateCommand();
            adp = new SqlDataAdapter(cmd);
            adp.SelectCommand.CommandText = lastQuery;
            ds = new DataSet();
            adp.Fill(ds, "tbl");
            GridView1.DataSource = ds;
            GridView1.DataBind();
        }
        catch
        {
        }
    }
}
