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

    protected void Btn_Insert_Click(object sender, EventArgs e)
    {
        if (Session["dtOrderDetails"] != null)
        {
            int int_Allow = 0;
            DataTable dt = new DataTable();
            dt = (DataTable)Session["dtOrderDetails"];
            foreach (DataRow Row in dt.Rows)
            {
                if (Decimal.Parse(Row[0].ToString()) == Decimal.Parse(Drp_WhAsset.SelectedValue.ToString()))
                {
                    float Flt_AssetQ = 0;
                    Flt_AssetQ = float.Parse(Row[2].ToString());
                    Flt_AssetQ += float.Parse(Txt_AsseQuantity.Text.Trim());
                    Row[2] = Flt_AssetQ.ToString();
                    int_Allow = 1;
                }
            }
            if (int_Allow == 0)
            {
                dt.Rows.Add(new Object[] { Decimal.Parse(Drp_WhAsset.SelectedValue.ToString()), Drp_WhAsset.SelectedItem.ToString(), float.Parse(Txt_AsseQuantity.Text.Trim()), Txt_OrderNotes.Text.Trim() });
            }
            
            Session.Add("dtOrderDetails", dt);
            Grd_OrderDetails.DataSource = dt;
            Grd_OrderDetails.DataBind();
        }
        else
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("AssetID", typeof(Decimal));
            dt.Columns.Add("AssetName", typeof(String));
            dt.Columns.Add("AssetQuantity", typeof(float));
            dt.Columns.Add("OrderDetailNotes", typeof(String));

            dt.Rows.Add(new Object[]{Decimal.Parse(Drp_WhAsset.SelectedValue.ToString()) ,  Drp_WhAsset.SelectedItem.ToString() ,float.Parse(Txt_AsseQuantity.Text.Trim()) , Txt_OrderNotes.Text.Trim()});

            Session.Add("dtOrderDetails" , dt);
            Grd_OrderDetails.DataSource = dt;
            Grd_OrderDetails.DataBind();
        }
        Txt_AsseQuantity.Text = String.Empty;
        Txt_OrderNotes.Text = String.Empty;

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

    }

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

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

    protected void Btn_AddOrder_Click(object sender, EventArgs e)
    {
        UDF_AddOrderToDB();
        Grd_OrderDetails.Visible = false;
        Btn_Insert.Enabled = false;
        
    }

    // از این متد برای درج سفارش کالا از انبار و جزئیات سفارش کالا در پایگاه داده استفاده می شود
    private void UDF_AddOrderToDB()
    {
        Decimal Dec_OrderID = 0;
        Class_ShamsiTools DtShamsi = new Class_ShamsiTools();
        DtShamsi.UDF_GetCurrentShamsiDate();
        Class_tblOrder SaveOrder = new Class_tblOrder(Session["UserName"].ToString(), DtShamsi.CurrentShmasiDate, false);
        Dec_OrderID = SaveOrder.UDF_SaveOrder();
        Class_tblOrderDetails SaveOrderDetails;
        Lbl_Error.Text = "جزئیات سفارش درج شد. کدسفارش جهت پیگیری: " + Dec_OrderID.ToString() + " میباشد.";
        DataTable dt = new DataTable();
        dt = (DataTable)Session["dtOrderDetails"];
        foreach (DataRow Row in dt.Rows)
        {
            SaveOrderDetails = new Class_tblOrderDetails(Dec_OrderID, Decimal.Parse(Row[0].ToString()), float.Parse(Row[2].ToString()), Row[3].ToString());
            SaveOrderDetails.UDF_SaveOrderDetails();
        }
        Session["dtOrderDetails"] = "";
        try
        {
            dt = (DataTable)Session["dtOrderDetails"];
        }
        catch
        { }
        Grd_OrderDetails.DataSource = dt;
        Grd_OrderDetails.DataBind();
    }
}
