using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Oracle.DataAccess.Client;
using System.Configuration;
using System.Data;
using Oracle.DataAccess.Types;
using VEtterem.Kosar;

namespace VEtterem
{
    public partial class Order : System.Web.UI.Page
    {        
        protected void Page_Load(object sender, EventArgs e)
        {
            (Master.FindControl("LabelContentCim") as Label).Text = "Rendelés";
            if (!IsPostBack)
                BindData();
        }
        protected void BindData()
        {
            Kosar.Kosar kosar = (Kosar.Kosar)Session["Kosar"];

            OrderGW.DataSource = kosar.Tetelek;
            OrderGW.DataBind();
            if (((Kosar.Kosar)Session["Kosar"]).Tetelek.Count == 0)
                RendelesButton.Enabled = false;
            else
                RendelesButton.Enabled = true;
        }

        protected void OrderGW_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Footer)
            {
                Kosar.Kosar kosar = (Kosar.Kosar)Session["Kosar"];
                e.Row.Cells[3].Text = "Végösszeg: " + kosar.OsszesenAkc().ToString() + " Ft";
            }
        }

        protected void VisszaButton_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Cart.aspx");
        }

        protected void RendelesButton_Click(object sender, EventArgs e)
        {
            int insCounter = 0;
            using (OracleConnection oc = new OracleConnection())
            {
                oc.ConnectionString = ConfigurationManager.ConnectionStrings["VCS"].ConnectionString;
                OracleCommand ocmd = new OracleCommand("SELECT ugyfelid from UGYFEL WHERE bejelentkezesinev = :pnev", oc);
                ocmd.Parameters.Add("pnev", OracleDbType.Varchar2).Value = User.Identity.Name;
                oc.Open();
                int uid;
                using (OracleDataReader odr = ocmd.ExecuteReader())
                {
                    odr.Read();
                    uid = int.Parse(odr.GetValue(0).ToString());
                }

                Kosar.Kosar kosar = (Kosar.Kosar)Session["Kosar"];                
                ocmd = new OracleCommand("INSERT INTO Rendeles(Rendelesid, ugyfel, feladva, akcios_vegosszeg)" 
                    + "VALUES(Rendeles_seq.NEXTVAL, :puid, SYSDATE, "+ kosar.OsszesenAkc() + ")", oc);
                ocmd.Parameters.Add("puid", OracleDbType.Int32).Value = uid;
                insCounter = ocmd.ExecuteNonQuery();
                                
                foreach (KosarTetel kt in kosar.Tetelek)
                {
                    ocmd = new OracleCommand("INSERT INTO Rendeles_tetel(Rendeles, RT_Sorszam, Cikk, CikkEgysegar, DB)" 
                        + "VALUES(Rendeles_seq.CURRVAL, Rendeles_tetel_seq.NEXTVAL,"
                        + kt.EID.ToString() + ", " + kt.Ar.ToString() + ", " + kt.Mennyiseg.ToString() + ")", oc);
                    ocmd.ExecuteNonQuery();
                }
            }
            if(insCounter > 0)            
            {
                Kosar.Kosar kosar = (Kosar.Kosar)Session["Kosar"];
                kosar.Kiszed();
                OrderGW.Enabled = OrderGW.Visible = false;
                VisszaButton.Visible = false;
                RendelesButton.Visible = false;
                SikeresLabel.Text = "A rendelést sikeresen leadta!";
                SikeresLabel.Visible = true;
                EtlaphozLinkButton.Visible = true;

                Naplo.NaplobaIr(User.Identity.Name, Request.UserHostAddress, Esemeny.Ord);
            }
                
          
        }

        protected void EtlaphozLinkButton_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Menu.aspx");
        }        

    }
}
