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 Marjoee : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["dtOrderDetails4"] == null)
        {
            Btn_Save.Enabled = false;
        }
        else
        {
            Btn_Save.Enabled = true;
        }
    }
    protected void Btn_ViewUserOrders_Click(object sender, EventArgs e)
    {
        if (Session["dtOrderDetails4"] != null)
        {
            DataTable dt = new DataTable();
            int int_Allow = 0;
            dt = (DataTable)Session["dtOrderDetails4"];

            foreach (DataRow Row in dt.Rows)
            {
                if (Decimal.Parse(Row[0].ToString()) == Decimal.Parse(Drp_OrderDetailDes.SelectedValue.ToString()))
                {
                    int_Allow = 1;
                }
            }
            if (int_Allow == 0)
            {
                dt.Rows.Add(new Object[] { Decimal.Parse(Drp_OrderDetailDes.SelectedValue.ToString()), Drp_OrderDetailDes.SelectedItem.ToString(), Drp_ElatMarjoee.SelectedItem.ToString(),Txt_Note.Text.Trim()  });
                Session.Add("dtOrderDetails4", dt);
                Grd_AssetUnits.DataSource = dt;
                Grd_AssetUnits.DataBind();

            }
        }
        else
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("OrderDetailID", typeof(Decimal));
            dt.Columns.Add("OrderDescription", typeof(String));
            dt.Columns.Add("VazyiatMarjoee", typeof(String));
            dt.Columns.Add("ElatMarjoeeNot", typeof(String));
            
            dt.Rows.Add(new Object[] { Decimal.Parse(Drp_OrderDetailDes.SelectedValue.ToString()), Drp_OrderDetailDes.SelectedItem.ToString(),Drp_ElatMarjoee.SelectedItem.ToString(),Txt_Note.Text.Trim()  });

            Session.Add("dtOrderDetails4", dt);
            Grd_AssetUnits.DataSource = dt;
            Grd_AssetUnits.DataBind();

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

        if (Session["dtOrderDetails4"] != null)
        {
            DataTable dt = new DataTable();
            dt = (DataTable)Session["dtOrderDetails4"];
            if (dt.Rows.Count > 0)
            {
                foreach (DataRow Row in dt.Rows)
                {
                    Class_tblMarjoee mrj = new Class_tblMarjoee(Decimal.Parse(Row[0].ToString()), int.Parse(Drp_ElatMarjoee.SelectedValue.ToString()), Txt_Note.Text.Trim() );
                    mrj.UDF_SaveMarjoee();
                }
            }
            Session.Remove("dtOrderDetails4");
            Response.Redirect("Marjoee.aspx?ID=" + DateTime.Now.Ticks.ToString());

        }
        else
        {
            Session.Remove("dtOrderDetails4");
            Lbl_Error.Visible = true;
            Lbl_Error.Text = "رکوردی برای مرجوعی موجود نمی باشد";
            Btn_Save.Enabled=false;

        }
       

    }
    protected void Grd_AssetUnits_SelectedIndexChanged(object sender, EventArgs e)
    {
        DataTable dt = new DataTable();
        dt = (DataTable)Session["dtOrderDetails4"];
        foreach (DataRow Row in dt.Rows)
        {
            if (Decimal.Parse(Row[0].ToString()) == Decimal.Parse(Grd_AssetUnits.SelectedDataKey.Value.ToString()))
            {
                Row.Delete();
                Row.EndEdit();
                break;
            }
        }
        Session.Add("dtOrderDetails4", dt);
        Grd_AssetUnits.DataSource = dt;
        Grd_AssetUnits.DataBind();

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