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

public partial class DetailRepair : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["TamirID2"] == null)
        {
            Btn_Save.Enabled = false;
        }
        else
        {
            Btn_Save.Enabled = true;
        }
    }
    protected void Btn_InsertInList_Click(object sender, EventArgs e)
    {
        if (Session["TamirID2"] != null)
        {
            int int_Allow = 0;
            DataTable dt = new DataTable();
            dt = (DataTable)Session["TamirID2"];
            foreach (DataRow Row in dt.Rows)
            {
                if (Decimal.Parse(Row[0].ToString()) == Decimal.Parse(Drp_Details.SelectedValue.ToString()) && Row[2].ToString() == Txt_Note.Text.Trim())
                {
                    int_Allow = 1;
                }
            }
            if (int_Allow == 0)
            {
                dt.Rows.Add(new Object[] {Decimal.Parse(Drp_Details.SelectedValue.ToString()), Drp_Details.SelectedItem.ToString() , Txt_Note.Text.Trim() , Txt_TotalCost.Text.Trim() });
            }
            Session.Add("TamirID2", dt);
            Grd_DetailsRepair.DataSource = dt;
            Grd_DetailsRepair.DataBind();
        }
        else
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("TamirID", typeof(Decimal));
            dt.Columns.Add("information", typeof(String));
            dt.Columns.Add("DetailNote", typeof(String));
            dt.Columns.Add("TotalCost", typeof(String));
            dt.Rows.Add(new Object[] { Decimal.Parse(Drp_Details.SelectedValue.ToString()), Drp_Details.SelectedItem.ToString() , Txt_Note.Text.Trim() , Txt_TotalCost.Text.Trim() });

            Session.Add("TamirID2", dt);

            Grd_DetailsRepair.DataSource = dt;
            Grd_DetailsRepair.DataBind();

        }
        if (Session["TamirID2"] == null)
        {
            Btn_Save.Enabled = false;
        }
        else
        {
            Btn_Save.Enabled = true;
        }
    }
    protected void Btn_Save_Click(object sender, EventArgs e)
    {
        if (Session["TamirID2"] != null)
        {
            DataTable dt = new DataTable();
            dt = (DataTable)Session["TamirID2"];

            if (dt.Rows.Count > 0)
            {
                foreach (DataRow Row in dt.Rows)
                {
                    Class_tblDetails ClsDtl = new Class_tblDetails(Decimal.Parse(Row[0].ToString()), Row[2].ToString(), float.Parse(Row[3].ToString()));
                    ClsDtl.UDF_Save();
                    Lbl_Error.Visible = true;
                    Lbl_Error.Text = "جزئیات تعمیرات با موفقیت ثبت گردید";
                    Grd_DetailsRepair.DataBind();
                    Txt_Note.Text = String.Empty;
                    Txt_TotalCost.Text = String.Empty;
                }

                Session.Remove("TamirID2");
                 }
            else
            {
                Session.Remove("TamirID2");
                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_DetailsRepair_SelectedIndexChanged(object sender, EventArgs e)
    {
        DataTable dt = new DataTable();
        dt = (DataTable)Session["TamirID2"];
        foreach (DataRow Row in dt.Rows)
        {
            if (Decimal.Parse(Row[0].ToString()) == Decimal.Parse(Grd_DetailsRepair.SelectedDataKey.Value.ToString()))
            {
                Row.Delete();
                Row.EndEdit();
                break;
            }
        }
        Grd_DetailsRepair.DataSource = dt;
        Grd_DetailsRepair.DataBind();
        if (Session["TamirID2"] == null)
        {
            Btn_Save.Enabled = false;
        }
        else
        {
            Btn_Save.Enabled = true;
        }
    }
}