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_ClassSessionEdit : System.Web.UI.Page
{
    //*********************************
    SqlConnection con;
    SqlCommand cmd;
    //*********************************
    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 weekDay = "", day = "", month = "", year = "", sessionDate = "", classNumber = "", sessionTime = "";
                int sessionId = Int32.Parse(Request.QueryString["sessionId"].ToString());
                string conString = RamanConnectionString.LocalConnectionString();
                con = new SqlConnection(conString);
                cmd = con.CreateCommand();
                try
                {
                    con.Open();
                    cmd.CommandText = "select sessionDate, sessionTime, classNumber FROM t_ClassSessions Where sessionId = " + sessionId;
                    SqlDataReader rdr = cmd.ExecuteReader();
                    if (rdr.HasRows)
                    {
                        while (rdr.Read())
                        {
                            sessionDate = (rdr.GetValue(0) != DBNull.Value ? rdr.GetString(0) : "");
                            sessionTime = (rdr.GetValue(1) != DBNull.Value ? rdr.GetString(1) : "");
                            classNumber = (rdr.GetValue(2) != DBNull.Value ? rdr.GetString(2) : "");
                        }
                    }
                    rdr.Close();
                    weekDay = C_myClass.DateSplit(sessionDate)[0];
                    day = C_myClass.DateSplit(sessionDate)[1];
                    month = C_myClass.DateSplit(sessionDate)[2];
                    year = C_myClass.DateSplit(sessionDate)[3];
                    ListItem itemWeek = new ListItem(weekDay);
                    cmbDayOfWeek.SelectedIndex = cmbDayOfWeek.Items.IndexOf(itemWeek);
                    ListItem itemDay = new ListItem(day);
                    cmbDay.SelectedIndex = cmbDay.Items.IndexOf(itemDay);
                    ListItem itemMonth = new ListItem(month);
                    cmbMonth.SelectedIndex = cmbMonth.Items.IndexOf(itemMonth);
                    ListItem itemYear = new ListItem(year);
                    cmbYear.SelectedIndex = cmbYear.Items.IndexOf(itemYear);
                    tbSessionTime.Text = sessionTime;
                    tbClassNumber.Text = classNumber;
                }
                catch
                {
                }
                finally
                {
                    if (con.State != ConnectionState.Closed)
                        con.Close();
                }
            }
        }
        catch
        {
        }
        finally
        {
        }
    }

    protected void btnOk_Click(object sender, EventArgs e)
    {
        try
        {
            System.Threading.Thread.Sleep(2000);
            string weekDay = (cmbDayOfWeek.SelectedIndex > 0 ? cmbDayOfWeek.SelectedItem.ToString() : "");
            string day = (cmbDay.SelectedIndex > 0 ? cmbDay.SelectedItem.ToString() : "");
            string month = (cmbMonth.SelectedIndex > 0 ? cmbMonth.SelectedItem.ToString() : "");
            string year = (cmbYear.SelectedIndex > 0 ? cmbYear.SelectedItem.ToString() : "");
            string sessionTime = tbSessionTime.Text;
            string classNumber = tbClassNumber.Text;
            string sessionDate = weekDay + " " + day + " " + month + " " + year;
            int sessionId = Int32.Parse(Request.QueryString["sessionId"].ToString());
            //lblMessage.Text = classId.ToString() + "<br />" + sessionDate + "<br />" + sessionTime + "<br />" + classNumber + "<br />";
            string conString = RamanConnectionString.LocalConnectionString();
            con = new SqlConnection(conString);
            cmd = con.CreateCommand();
            try
            {
                con.Open();
                cmd.CommandText = "update t_ClassSessions SET " +
                    "sessionDate = N'" + sessionDate + "' , " +
                    "sessionTime = N'" + sessionTime + "' , " +
                    "classNumber = N'" + classNumber + "' " +
                    "Where sessionId = " + sessionId;
                cmd.ExecuteNonQuery();
                lblMessage.ForeColor = System.Drawing.Color.Green;
                lblMessage.Text = "با موفقیت ذخیره شد.";
            }
            catch
            {
            }
            finally
            {
                if (con.State != ConnectionState.Closed)
                    con.Close();
            }
        }
        catch
        {
        }
    }

    protected void btnBack_Click(object sender, EventArgs e)
    {
        try
        {
            string returnUrl = Request.QueryString["returnUrl"].ToString();
            int classId = Int32.Parse(Request.QueryString["classId"].ToString());
            Response.Redirect(returnUrl + "?classId=" + classId + "&returnUrl=pg_ClassSessionsEdit.aspx"); 
        }
        catch
        {
        }
    }
}

