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 TahvileSefaresh : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["dtOrderDetails3"] == null)
        {
            Btn_Save.Enabled = false;
        }
        else
        {
            Btn_Save.Enabled = true;
        }
    }


    protected void Grd_AssetUnits_SelectedIndexChanged(object sender, EventArgs e)
    {
        DataTable dt = new DataTable();
        dt = (DataTable)Session["dtOrderDetails3"];
        foreach (DataRow Row in dt.Rows)
        {
            if (Decimal.Parse(Row[0].ToString()) == Decimal.Parse(Grd_OrderDetails.SelectedDataKey.Value.ToString()))
            {
                Row.Delete();
                Row.EndEdit();
                break;
            }
        }
        Session.Add("dtOrderDetails3", dt);
        Grd_OrderDetails.DataSource = dt;
        Grd_OrderDetails.DataBind();
        if (Session["dtOrderDetails3"] == null)
        {
            Btn_Save.Enabled = false;
        }
        else
        {
            Btn_Save.Enabled = true;
        }
    }
    protected void Btn_ViewUserOrders_Click(object sender, EventArgs e)
    {
        if (Session["dtOrderDetails3"] != null)
        {
            int int_Allow = 0;
            DataTable dt = new DataTable();
            dt = (DataTable)Session["dtOrderDetails3"];
            foreach (DataRow Row in dt.Rows)
            {
                if (Decimal.Parse(Row[0].ToString()) == Decimal.Parse(Drp_OrdersDetail.SelectedValue.ToString()))
                {
                    int_Allow = 1;
                }
            }
            if (int_Allow == 0)
            {
                dt.Rows.Add(new Object[] { Decimal.Parse(Drp_OrdersDetail.SelectedValue.ToString()), Drp_OrdersDetail.SelectedItem.ToString() });
            }
            Session.Add("dtOrderDetails3", dt);
            Grd_OrderDetails.DataSource = dt;
            Grd_OrderDetails.DataBind();
        }
        else
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("OrderDetailID", typeof(Decimal));
            dt.Columns.Add("OrderDescription", typeof(String));
            dt.Rows.Add(new Object[] { Decimal.Parse(Drp_OrdersDetail.SelectedValue.ToString()), Drp_OrdersDetail.SelectedItem.ToString() });

            Session.Add("dtOrderDetails3", dt);

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

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

    protected void Btn_Save_Click(object sender, EventArgs e)
    {
        if (Session["dtOrderDetails3"] != null)
        {
            DataTable dt = new DataTable();
            dt = (DataTable)Session["dtOrderDetails3"];
            if (dt.Rows.Count > 0)
            {
                foreach (DataRow Row in dt.Rows)
                {
                    Class_tblDeliverdAssets DeliverAsset = new Class_tblDeliverdAssets(Drp_Deli_User.SelectedItem.ToString(), Decimal.Parse(Row[0].ToString()));// اشتباه در این محل اتفاق افتاده بود
                    DeliverAsset.UDF_SaveDeliveredOrders();
                }

                Session.Remove("dtOrderDetails3");
                Response.Redirect("TahvileSefaresh.aspx?ID=" + DateTime.Now.Ticks.ToString());
            }
            else
            {
                Session.Remove("dtOrderDetails3");
                Lbl_Error.Visible = true;
                Lbl_Error.Text = "رکوردی برای ثبت موجود نمی باشد";
                Btn_Save.Enabled = false;
            }
        }
        else
        {
            Lbl_Error.Visible = true;
            Lbl_Error.Text = "رکوردی برای ثبت موجود نمی باشد";
            Btn_Save.Enabled = false;
        }
    }
}
