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_CoursesEdit : System.Web.UI.Page
{
    //************************************
    SqlConnection con;
    SqlTransaction tr;
    SqlCommand cmd = new SqlCommand();
    //************************************
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            C_myClass.MenueItemsCreate(User, panelMenues);
            Label lblTitle = (Label)this.Master.FindControl("lblTitle");
            lblTitle.Text = "ویرایش دوره ها";
        }
        catch
        {
        }
        finally
        {
        }
    }
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        try
        {
            if (e.CommandName == "EditPress")
            {
                int courseId = Int32.Parse(e.CommandArgument.ToString());
                Response.Redirect("pg_CourseEditDetails.aspx?courseId=" + courseId.ToString() + "&returnUrl=pg_CoursesEdit.aspx");
            }
            else if (e.CommandName == "DeletePress")
            {
                string conString = RamanConnectionString.LocalConnectionString();
                con = new SqlConnection(conString);
                cmd.Connection = con;
                try
                {
                    con.Open();
                    int courseId = Int32.Parse(e.CommandArgument.ToString());
                    cmd.CommandText = "delete from t_Courses Where courseId = " + courseId;
                    cmd.ExecuteNonQuery();
                    GridView1.DataBind();
                }
                catch
                {
                }
                finally
                {
                    if (con.State != ConnectionState.Closed)
                        con.Close();
                }
            }
            else if (e.CommandName == "ShowInList")
            {
                bool show = false, showInList = false;
                int courseId = Int32.Parse(e.CommandArgument.ToString());
                string conString = RamanConnectionString.LocalConnectionString();
                con = new SqlConnection(conString);
                cmd.Connection = con;
                con.Open();
                tr = con.BeginTransaction();
                cmd.Transaction = tr;
                try
                {
                    cmd.CommandText = "select showInList from t_Courses Where courseId = " + courseId;
                    show = (cmd.ExecuteScalar() != DBNull.Value ? (bool)cmd.ExecuteScalar() : false);
                    showInList = !show;
                    int showBit = ( showInList ? 1 : 0);
                    cmd.CommandText = "update t_Courses SET showInList = " + showBit + " WHERE courseId = " + courseId;
                    cmd.ExecuteNonQuery();
                    tr.Commit();
                    GridView1.DataBind();
                }
                catch(Exception ex)
                {
                    try
                    {
                        tr.Rollback();
                    }
                    catch
                    {
                    }
                }
                finally
                {
                    if (con.State != ConnectionState.Closed)
                        con.Close();
                }
            }
        }
        catch
        {
        }
    }

    protected string FindCssClass(object ShowInList)
    {
        string css = "";
        bool showInList = (bool)ShowInList;
        if (showInList)
        {
            css = "gridsmallbuttonstrue";
        }
        else
        {
            css = "gridsmallbuttonsfalse";
        }
        return css;
    }

    protected string FindShowButtonText(object ShowInList)
    {
        string txt = "";
        bool showInList = (bool)ShowInList;
        if (showInList)
        {
            txt = "عدم نمایش";
        }
        else
        {
            txt = "نمایش";
        }
        return txt;
    }
    protected void btnNewCourse_Click(object sender, EventArgs e)
    {
        try
        {
            Response.Redirect("pg_CourseAdd.aspx?returnUrl=pg_CoursesEdit.aspx");
        }
        catch
        {
        }
    }
}
