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_CourseEditDetails : System.Web.UI.Page
{
    //*************************************
    SqlConnection con;
    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 courseName = "", beginDate = "", endDate = "", duration = "";
                int amount = 0;
                bool showInList = false;
                int courseId = Int32.Parse(Request.QueryString["courseId"].ToString());
                string conStrint = RamanConnectionString.LocalConnectionString();
                con = new SqlConnection(conStrint);
                SqlCommand cmd = con.CreateCommand();
                try
                {
                    con.Open();
                    cmd.CommandText = "select courseName, beginDate, duration, endDate, amount, showInList from t_Courses Where courseId = " + courseId;
                    SqlDataReader rdr = cmd.ExecuteReader();
                    if (rdr.HasRows)
                    {
                        while (rdr.Read())
                        {
                            courseName = ( rdr.GetValue(0) != DBNull.Value ? rdr.GetString(0) : "");
                            beginDate = (rdr.GetValue(1) != DBNull.Value ? rdr.GetString(1) : "");
                            duration = (rdr.GetValue(2) != DBNull.Value ? rdr.GetString(2) : "");
                            endDate = (rdr.GetValue(3) != DBNull.Value ? rdr.GetString(3) : "");
                            amount = (rdr.GetValue(4) != DBNull.Value ? rdr.GetInt32(4) : 0);
                            showInList = (rdr.GetValue(5) != DBNull.Value ? rdr.GetBoolean(5) : false);
                        }
                    }
                    rdr.Close();
                    /////////////////////////////////////// set textboxes :
                    tbCourseName.Text = courseName;
                    tbDuration.Text = duration;
                    tbAmount.Text = amount.ToString();
                    checkShowInList.Checked = showInList;
                    ListItem beginDay = new ListItem(C_myClass.DateSplit(beginDate)[0]);
                    cmbBeginDay.SelectedIndex = cmbBeginDay.Items.IndexOf(beginDay);
                    ListItem beginMonth = new ListItem(C_myClass.DateSplit(beginDate)[1]);
                    cmbBeginMonth.SelectedIndex = cmbBeginMonth.Items.IndexOf(beginMonth);
                    ListItem beginYear = new ListItem(C_myClass.DateSplit(beginDate)[2]);
                    cmbBeginYear.SelectedIndex = cmbBeginYear.Items.IndexOf(beginYear);
                    ////////
                    ListItem endDay = new ListItem(C_myClass.DateSplit(endDate)[0]);
                    cmbEndDay.SelectedIndex = cmbEndDay.Items.IndexOf(endDay);
                    ListItem endMonth = new ListItem(C_myClass.DateSplit(endDate)[1]);
                    cmbEndMonth.SelectedIndex = cmbEndMonth.Items.IndexOf(endMonth);
                    ListItem endYear = new ListItem(C_myClass.DateSplit(endDate)[2]);
                    cmbEndYear.SelectedIndex = cmbEndYear.Items.IndexOf(endYear);
                }
                catch(Exception ex)
                {
                    lblMessage.ForeColor = System.Drawing.Color.Red;
                    lblMessage.Text = ex.Message;
                }
                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 conStrint = RamanConnectionString.LocalConnectionString();
            con = new SqlConnection(conStrint);
            SqlCommand cmd = con.CreateCommand();
            try
            {
                con.Open();
                tr = con.BeginTransaction();
                cmd.Transaction = tr;
                int courseId = Int32.Parse(Request.QueryString["courseId"].ToString());
                string courseName = tbCourseName.Text;
                string beginDay = (cmbBeginDay.SelectedIndex > 0 ? cmbBeginDay.SelectedItem.ToString() :"");
                string beginMonth = ( cmbBeginMonth.SelectedIndex > 0 ? cmbBeginMonth.SelectedItem.ToString() :"");
                string beginYear = (cmbBeginYear.SelectedIndex > 0 ? cmbBeginYear.SelectedItem.ToString() :"");
                string endDay = (cmbEndDay.SelectedIndex > 0 ? cmbEndDay.SelectedItem.ToString() : "");
                string endMonth = (cmbEndMonth.SelectedIndex > 0 ? cmbEndMonth.SelectedItem.ToString() : "");
                string endYear = (cmbEndYear.SelectedIndex > 0 ? cmbEndYear.SelectedItem.ToString() : "");
                string beginDate = beginDay + " " + beginMonth + " " + beginYear;
                string endDate = endDay + " " + endMonth + " " + endYear;
                string duration = tbDuration.Text;
                int amount = Int32.Parse(tbAmount.Text);
                bool showInList = (checkShowInList.Checked ? true : false);
                cmd.CommandText = t_CoursesUpdate(courseId, courseName, beginDate, duration, endDate, amount, showInList);
                cmd.ExecuteNonQuery();
                tr.Commit();
                lblMessage.ForeColor = System.Drawing.Color.Green;
                lblMessage.Text = "با موفقیت ذخیره شد";
            }
            catch(Exception ex)
            {
                try
                {
                    lblMessage.ForeColor = System.Drawing.Color.Red;
                    lblMessage.Text = ex.Message;
                    tr.Rollback();
                }
                catch
                {
                }
            }
            finally
            {
                if (con.State != ConnectionState.Closed)
                    con.Close();
            }
        }
        catch
        {
        }
    }

    private string t_CoursesUpdate(int CourseId, string CourseName, string BeginDate, string Duration,string EndDate, int Amount, bool ShowInList)
    {
        string comm = "Update t_Courses SET " +
            "courseName = N'" + CourseName + "' , " +
            "beginDate = N'" + BeginDate + "' , " +
            "duration = N'" + Duration + "' , " +
            "endDate = N'" + EndDate + "' , " +
            "amount = " + Amount + " , " +
            "showInList = " + (ShowInList ? 1 : 0) + " " +
            "Where courseId = " + CourseId;
        return comm;
    }
    
    protected void btnBack_Click(object sender, EventArgs e)
    {
        try
        {
            string returnUrl = Request.QueryString["returnUrl"].ToString();
            Response.Redirect(returnUrl);
        }
        catch
        {
        }
    }
}
