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;

public partial class ProjectAssetDetails : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["dtOrderDetails5"] == null)
        {
            Btn_Save.Enabled = false;
        }
        else
        {
            Btn_Save.Enabled = true;
        }
    }
    protected void Btn_ViewUserOrders_Click(object sender, EventArgs e)
    {
        if (Session["dtOrderDetails5"] != null)
        {
            int int_Allow = 0;
           

            DataTable dt = new DataTable();
            dt = (DataTable)Session["dtOrderDetails5"];

            foreach (DataRow Row in dt.Rows)
            {
                if ( Decimal.Parse(Row[2].ToString()) == Decimal.Parse(Drp_OrderDetail.SelectedValue.ToString()) )
                {

                        int_Allow = 1;
                      
                }
            }
            if (int_Allow == 0 )
            {
                dt.Rows.Add(new Object[] { Decimal.Parse(Drp_PrjName.SelectedValue.ToString()), Drp_PrjName.SelectedItem.ToString(), Decimal.Parse(Drp_OrderDetail.SelectedValue.ToString()), Drp_OrderDetail.SelectedItem.ToString(), Drp_UserName.SelectedItem.ToString(), Txt_UseCase.Text.Trim(), Txt_Note.Text.Trim() });
            }
            Session.Add("dtOrderDetails5", dt);
            Grd_PrjAssetDetail.DataSource = dt;
            Grd_PrjAssetDetail.DataBind();
        }
        else
        {
            DataTable dt = new DataTable();

            dt.Columns.Add("PrjID", typeof(Decimal));
            dt.Columns.Add("PrjDes", typeof(String));
            dt.Columns.Add("OrderDetailID", typeof(Decimal));
            dt.Columns.Add("OrderDetailDescription", typeof(String));
            dt.Columns.Add("UserName",typeof(String));
            dt.Columns.Add("UseCase", typeof(String));
            dt.Columns.Add("Note",typeof(String));

            dt.Rows.Add(new Object[] {Decimal.Parse( Drp_PrjName.SelectedValue.ToString()),Drp_PrjName.SelectedItem.ToString(),Decimal.Parse( Drp_OrderDetail.SelectedValue.ToString()),Drp_OrderDetail.SelectedItem.ToString(),Drp_UserName.SelectedItem.ToString(), Txt_UseCase.Text.Trim(), Txt_Note.Text.Trim() });

            Session.Add("dtOrderDetails5", dt);

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

        }
        if (Session["dtOrderDetails5"] == null)
        {
            Btn_Save.Enabled = false;
        }
        else
        {
            Btn_Save.Enabled = true;
        }
        

    }


    protected void Btn_Save_Click(object sender, EventArgs e)
    {
        if (Session["dtOrderDetails5"] != null)
        {
            DataTable dt = new DataTable();
            dt = (DataTable)Session["dtOrderDetails5"];

            if (dt.Rows.Count > 0)
            {
                foreach (DataRow Row in dt.Rows)
                {
                   Class_tblProjectAssetDetail PrjAssetDtl = new Class_tblProjectAssetDetail(Decimal.Parse(Row[0].ToString()),Decimal.Parse(Row[2].ToString()),Drp_UserName.SelectedItem.ToString(),Txt_UseCase.Text.Trim(),Txt_Note.Text.Trim());
                   PrjAssetDtl.UDF_SaveProjectAssetDetail();
                }

                Session.Remove("dtOrderDetails5");
                Response.Redirect("ProjectAssetDetails.aspx?ID=" + DateTime.Now.Ticks.ToString());
            }
            else
            {
                Session.Remove("dtOrderDetails5");
                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_PrjAssetDetail_SelectedIndexChanged(object sender, EventArgs e)
    {
        DataTable dt = new DataTable();
        dt = (DataTable)Session["dtOrderDetails5"];

        foreach (DataRow Row in dt.Rows)
        {
            if (Decimal.Parse(Row[2].ToString()) == Decimal.Parse(Grd_PrjAssetDetail.SelectedDataKey.Value.ToString()))
            {
                Row.Delete();
                Row.EndEdit();
                break;
            }
        }
        Session.Add("dtOrderDetails5", dt);
        Grd_PrjAssetDetail.DataSource = dt;
        Grd_PrjAssetDetail.DataBind();

        if (Session["dtOrderDetails5"] == null)
        {
            Btn_Save.Enabled = false;
        }
        else
        {
            Btn_Save.Enabled = true;
        }
    }
}
   
       

