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 SefareshGheyreMashmol : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        if (Session["dtOrderDetails6"] == 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["dtOrderDetails6"] != null)
        {
            int int_Allow = 0;
            DataTable dt = new DataTable();
            dt = (DataTable)Session["dtOrderDetails6"];


            foreach (DataRow Row in dt.Rows)
            {
                if (Decimal.Parse(Row[0].ToString()) == Decimal.Parse(Drp_Assets.SelectedValue.ToString()))
                {
                    int_Allow = 1;
                }
            }
            if (int_Allow == 0)
            {
                Dec_Total = Decimal.Parse(Txt_CountAssets.Text.Trim()) * Decimal.Parse(Txt_UnitPrice.Text.Trim());
                dt.Rows.Add(new Object[] { Decimal.Parse(Drp_Assets.SelectedValue.ToString()), Drp_Assets.SelectedItem.ToString(), Decimal.Parse(Drp_PlaceTahvil.SelectedValue.ToString()), Drp_PlaceTahvil.SelectedItem.ToString(), float.Parse(Txt_CountAssets.Text.Trim()), Decimal.Parse(Txt_UnitPrice.Text.Trim()), Dec_Total });
                Txt_CountAssets.Text = String.Empty;
                Txt_UnitPrice.Text = String.Empty;
            }

            Session.Add("dtOrderDetails6", dt);
            Grd_OrderDetailsM.DataSource = dt;
            Grd_OrderDetailsM.DataBind();
        }
        else
        {
            DataTable dt = new DataTable();

            dt.Columns.Add("AssetsID",typeof(Decimal));
            dt.Columns.Add("AssetsName",typeof(String));
            dt.Columns.Add("WarwHouseID",typeof(int));
            dt.Columns.Add("WarwHouseName",typeof(String));
            dt.Columns.Add("Quantity",typeof(float));
            dt.Columns.Add("UnitPrice",typeof(Decimal));
            dt.Columns.Add("TotalPrice",typeof(Decimal));

            Dec_Total = Decimal.Parse(Txt_CountAssets.Text.Trim()) * Decimal.Parse(Txt_UnitPrice.Text.Trim());
            dt.Rows.Add(new Object[] {Decimal.Parse(Drp_Assets.SelectedValue.ToString()),Drp_Assets.SelectedItem.ToString(),Decimal.Parse(Drp_PlaceTahvil.SelectedValue.ToString()),Drp_PlaceTahvil.SelectedItem.ToString(),float.Parse(Txt_CountAssets.Text.Trim()),Decimal.Parse(Txt_UnitPrice.Text.Trim()),Dec_Total  });
            Txt_UnitPrice.Text = String.Empty;
            Txt_CountAssets.Text = String.Empty;
           
            Session.Add("dtOrderDetails6", dt);
            Grd_OrderDetailsM.DataSource = dt;
            Grd_OrderDetailsM.DataBind();
        }


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

            if (dt.Rows.Count > 0)
            {
             
                foreach (DataRow Row in dt.Rows)
                {
                    
                    Class_WhAssetsGe AssetGe = new Class_WhAssetsGe(Decimal.Parse(Row[0].ToString()), int.Parse(Row[2].ToString()), float.Parse(Row[4].ToString()), Decimal.Parse(Row[5].ToString()), Decimal.Parse(Row[6].ToString()));
                    AssetGe.UDF_SaveWhAssetGe();

                }
                Session.Remove("dtOrderDetails6");
                Response.Redirect("SefareshGheyreMashmol.aspx?ID=" + DateTime.Now.Ticks.ToString());
            }
            else
            {
                Session.Remove("dtOrderDetails6");
                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_OrderDetailsM_SelectedIndexChanged(object sender, EventArgs e)
    {
        DataTable dt = new DataTable();
        dt = (DataTable)Session["dtOrderDetails6"];
        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["dtOrderDetails6"] == null)
        {
            Btn_Save.Enabled = false;
        }
        else
        {
            Btn_Save.Enabled = true;
        }
    }
}
