using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

public partial class DarkhastTamirat : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["UserName"] == null)
        {
            Response.Redirect("LoginPage.aspx?ID=" + DateTime.Now.Ticks.ToString());
        }
        if (Session["TamirID1"] == null)
        {
            Btn_Save.Enabled = false;
        }
        else
        {
            Btn_Save.Enabled = true;
        }
    }
    protected void Btn_InsertInList_Click(object sender, EventArgs e)
    {
        if (Session["TamirID1"] != null)
        {
            int int_Alw = 0;
            DataTable dt = new DataTable();
            dt = (DataTable)Session["TamirID1"];

            foreach (DataRow Row in dt.Rows)
            {
                if (Row[0].ToString() == Drp_PelakCarName.SelectedValue.ToString())
                {
                    int_Alw = 1;
                }
            }
            if (int_Alw == 0)
            {
                dt.Rows.Add(new Object[] { Drp_PelakCarName.SelectedValue.ToString(), Drp_PelakCarName.SelectedItem.ToString(), Txt_Note.Text.Trim() });
                Txt_Note.Text = String.Empty;
            }
            Session.Add("TamirID1", dt);
            Grd_Tamirat.DataSource = dt;
            Grd_Tamirat.DataBind();
        }

        else
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("PelakNumber", typeof(String));
            dt.Columns.Add("PelahAndCarName", typeof(String));
            dt.Columns.Add("TamirNote", typeof(String));
            dt.Rows.Add(new Object[] { Drp_PelakCarName.SelectedValue.ToString() ,Drp_PelakCarName.SelectedItem.ToString(), Txt_Note.Text.Trim() });
            Session.Add("TamirID1", dt);
            Grd_Tamirat.DataSource = dt;
            Grd_Tamirat.DataBind();
        }
        if (Session["TamirID1"] == null)
        {
            Btn_Save.Enabled = false;
        }
        else
        {
            Btn_Save.Enabled = true;
        }
    }
    protected void Btn_Save_Click(object sender, EventArgs e)
    {
        if (Session["TamirID1"] != null)
        {
            DataTable dt = new DataTable();
            dt = (DataTable)Session["TamirID1"];
            if (dt.Rows.Count > 0)
            {
                foreach (DataRow Row in dt.Rows)
                {
                    Class_tblDarkhastTamir ClsDkhst = new Class_tblDarkhastTamir(Row[0].ToString(), Row[2].ToString(), true, "0", "0");
                    ClsDkhst.UDF_Save();
                    Lbl_Error.Visible = true;
                    Lbl_Error.Text = "درخواست تعمیرات شما با موفقیت ثبت شد";
                    Grd_Tamirat.DataBind();
                }
                Session.Remove("TamirID1");
            }
            else
            {
                Session.Remove("TamirID1");
                Lbl_Error.Visible = true;
                Lbl_Error.Text = "رکوردی برای ثبت موجود نمی باشد";
                Btn_Save.Enabled = false;
            }
        }
        else
        {
            Lbl_Error.Visible = true;
            Lbl_Error.Text = "رکوردی برای ثبت موجود نمی باشد";
            Btn_Save.Enabled = false;
        }
    }
    protected void Grd_Tamirat_SelectedIndexChanged(object sender, EventArgs e)
    {
        DataTable dt = new DataTable();
        dt = (DataTable)Session["TamirID1"];
        foreach (DataRow Row in dt.Rows)
        {
            if (Row[0].ToString() == Grd_Tamirat.SelectedDataKey.Value.ToString())
            {
                Row.Delete();
                Row.EndEdit();
                break;
            }
        }
        Grd_Tamirat.DataSource = dt;
        Grd_Tamirat.DataBind();
        if (Session["TamirID1"] == null)
        {
            Btn_Save.Enabled = false;
        }
        else
        {
            Btn_Save.Enabled = true;
        }
    }
}