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 PurchasedOrders : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["dtOrderDetails2"] == null)
        {
            Btn_Save.Enabled = false;
        }
        else
        {
            Btn_Save.Enabled = true;
        }
    }
    protected void Btn_Insert_Click(object sender, EventArgs e)
    {
        Decimal Dec_Total = 0;
        if (Session["dtOrderDetails2"] != null)
        {
            int int_Allow = 0;
            DataTable dt = new DataTable();
            dt = (DataTable)Session["dtOrderDetails2"];
            foreach (DataRow Row in dt.Rows)
            {
                if (Decimal.Parse(Row[0].ToString()) == Decimal.Parse(Drp_OrderDetails.SelectedValue.ToString()))
                {
                    int_Allow = 1;
                }
            }
            if (int_Allow == 0)
            {

                Dec_Total = Decimal.Parse(Txt_UnitPrice.Text.Trim()) * UDF_AssetQuantity(Decimal.Parse(Drp_OrderDetails.SelectedValue.ToString()));
                dt.Rows.Add(new Object[] { Decimal.Parse(Drp_OrderDetails.SelectedValue.ToString()), Drp_OrderDetails.SelectedItem.ToString(), Txt_UnitPrice.Text.Trim(), Dec_Total });
            }

            Session.Add("dtOrderDetails2", dt);
            Grd_OrderDetailsM.DataSource = dt;
            Grd_OrderDetailsM.DataBind();
        }
        else
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("OrderDetailID", typeof(Decimal));
            dt.Columns.Add("OrderDescription", typeof(String));
            dt.Columns.Add("UnitPrice", typeof(Decimal));
            dt.Columns.Add("TotalPrice", typeof(Decimal));

            Dec_Total = Decimal.Parse(Txt_UnitPrice.Text.Trim()) * UDF_AssetQuantity(Decimal.Parse(Drp_OrderDetails.SelectedValue.ToString()));
            dt.Rows.Add(new Object[] { Decimal.Parse(Drp_OrderDetails.SelectedValue.ToString()), Drp_OrderDetails.SelectedItem.ToString(), Txt_UnitPrice.Text.Trim(), Dec_Total });

            Session.Add("dtOrderDetails2", dt);
            Grd_OrderDetailsM.DataSource = dt;
            Grd_OrderDetailsM.DataBind();
        }


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

    private Decimal UDF_AssetQuantity(Decimal Dec_OrderDetailID)
    {
        LSC_WareHouseDataContext db = new LSC_WareHouseDataContext();
        tblOrderDetail ord = db.tblOrderDetails.Single(n => n.OrderDetailID == Dec_OrderDetailID);
        return (Decimal.Parse(ord.AssetQuantity.ToString()));
    }

    private void UDF_AddDetailsToDb()
    {
        if (Session["dtOrderDetails2"] != null)
        {
            DataTable dt = new DataTable();
            dt = (DataTable)Session["dtOrderDetails2"];
            if (dt.Rows.Count > 0)
            {
                foreach (DataRow Row in dt.Rows)
                {// در این قسمت جزئیات سفارش های تحویلی به انبار در دیتابیس درج می شوند
                    Class_tblPuchOrders ClsPurch = new Class_tblPuchOrders(Decimal.Parse(Row[0].ToString()), int.Parse(Drp_WareHouseName.SelectedValue.ToString()), Decimal.Parse(Txt_UnitPrice.Text.Trim()),Decimal.Parse(Row[3].ToString()));
                    ClsPurch.UDF_SavePurchasedOrders();
                   
                }
                Session.Remove("dtOrderDetails2");
                Response.Redirect("PurchasedOrders.aspx?ID=" + DateTime.Now.Ticks.ToString());
            }
            else
            {
                Session.Remove("dtOrderDetails2");
                Lbl_Error.Visible = true;
                Lbl_Error.Text = "رکوردی برای ثبت موجود نمی باشد";
                Btn_Save.Enabled = false;
            }
        }
        else
        {
            Session.Remove("dtOrderDetails2");
            Lbl_Error.Visible = true;
            Lbl_Error.Text = "رکوردی برای ثبت موجود نمی باشد";
            Btn_Save.Enabled = false;
        }

    }
    protected void Btn_Save_Click(object sender, EventArgs e)
    {
        UDF_AddDetailsToDb();

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

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

        if (Session["dtOrderDetails2"] == null)
        {
            Btn_Save.Enabled = false;
        }
        else
        {
            Btn_Save.Enabled = true;
        }
    }
    protected void Grd_ShomarehSefaresh_SelectedIndexChanged(object sender, EventArgs e)
    {
        Txt_Code_Sefaresh.Text = Grd_ShomarehSefaresh.SelectedDataKey.Value.ToString();
    }
    protected void Btn_CloseOrder_Click(object sender, EventArgs e)
    {
        Class_tblOrder OrderClose = new Class_tblOrder(Decimal.Parse(Txt_Code_Sefaresh.Text.Trim()));
        OrderClose.UDF_CloseOrder();
        Grd_ShomarehSefaresh.DataBind();
        Txt_Code_Sefaresh.Text = "0";
    }
}
