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 VEtterem.Kosar;
using System.Data;
using Oracle.DataAccess.Types;

namespace VEtterem.Pultos
{
    public partial class Sales : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            (Master.FindControl("LabelContentCim") as Label).Text = "Akciók";
        }

        protected void UjAkcioButton_Click(object sender, EventArgs e)
        {
            PanelUjButton.Visible = false;
            PanelMilyen.Visible = true;
            PanelAkciok.Visible = true;
            PanelGrids.Visible = false;
        }

        protected void ButtonMegsem_Click(object sender, EventArgs e)
        {
            PanelUjButton.Visible = true;
            PanelAkciok.Visible = false;
            PanelMilyen.Visible = false;
            PanelGrids.Visible = true;
        }

        protected void MilyenRadioButtonList_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (MilyenRadioButtonList.SelectedValue)
            {
                case "Tipus_akcio":
                    {
                        ETDropDownList.Visible = true;

                        DropDownListPillA.Visible = false;
                        Labelszoveg.Visible = false;
                        DDLKOra.Visible = false;
                        DDLVOra.Visible = false;
                        CompareValidator1.Enabled = false;
                        LabelKezdoOra.Visible = false;
                        LabelVegOra.Visible = false;
                        break;
                    }
                case "Idoszakos_akcio":
                    {
                        ETDropDownList.Visible = false;
                        DropDownListPillA.Visible = false;

                        Labelszoveg.Visible = true;
                        DDLKOra.Visible = true;
                        DDLVOra.Visible = true;
                        CompareValidator1.Enabled = true;
                        LabelKezdoOra.Visible = true;
                        LabelVegOra.Visible = true;
                        break;
                    }
                case "Pillanatnyi_akcio":
                    {
                        DropDownListPillA.Visible = true;

                        ETDropDownList.Visible = false;
                        Labelszoveg.Visible = false;
                        DDLKOra.Visible = false;
                        DDLVOra.Visible = false;
                        CompareValidator1.Enabled = false;
                        LabelKezdoOra.Visible = false;
                        LabelVegOra.Visible = false;
                        break;
                    }
                default:
                    {
                        break;
                    }
            }
        }

        protected void ButtonRogzites_Click(object sender, EventArgs e)
        {
            if (CalendarEttol.SelectedDate.DayOfYear <= CalendarEddig.SelectedDate.DayOfYear)
            {
                int counter = 0;
                using (OracleConnection sqlc = new OracleConnection())
                {

                    sqlc.ConnectionString = ConfigurationManager.ConnectionStrings["VCS"].ConnectionString;
                    OracleCommand cmd = new OracleCommand();
                    cmd.Connection = sqlc;
                    sqlc.Open();
                    cmd.CommandType = CommandType.StoredProcedure;

                    switch (MilyenRadioButtonList.SelectedValue)
                    {
                        case "Tipus_akcio":
                            {
                                OracleTimeStamp ots = new OracleTimeStamp(CalendarEddig.SelectedDate.Year, CalendarEddig.SelectedDate.Month,
                                    CalendarEddig.SelectedDate.Day, 23, 59, 59);
                                cmd.CommandText = "INSERTTIPUSAKCIO";
                                cmd.Parameters.Clear();
                                cmd.Parameters.Add("p_leiras", OracleDbType.Varchar2).Value = TextBoxLeiras.Text;
                                cmd.Parameters.Add("p_kezdet", OracleDbType.TimeStamp).Value = CalendarEttol.SelectedDate;
                                cmd.Parameters.Add("p_veg", OracleDbType.TimeStamp).Value = ots;
                                cmd.Parameters.Add("p_kedvezmeny", OracleDbType.Int32).Value = Int32.Parse(TextBoxSzazalek.Text);
                                cmd.Parameters.Add("p_etipus", OracleDbType.Int32).Value = ETDropDownList.SelectedValue;
                                break;                                
                            }
                        case "Idoszakos_akcio":
                            {
                                OracleTimeStamp ots = new OracleTimeStamp(CalendarEddig.SelectedDate.Year, CalendarEddig.SelectedDate.Month,
                                    CalendarEddig.SelectedDate.Day, 23, 59, 59);
                                cmd.CommandText = "INSERTIDOSZAKOSAKCIO";
                                cmd.Parameters.Clear();
                                cmd.Parameters.Add("p_leiras", OracleDbType.Varchar2).Value = TextBoxLeiras.Text;
                                cmd.Parameters.Add("p_kezdet", OracleDbType.TimeStamp).Value = CalendarEttol.SelectedDate;
                                cmd.Parameters.Add("p_veg", OracleDbType.TimeStamp).Value = ots;
                                cmd.Parameters.Add("p_kezdoora", OracleDbType.Int32).Value = Int32.Parse(DDLKOra.SelectedValue);
                                cmd.Parameters.Add("p_vegora", OracleDbType.Int32).Value = Int32.Parse(DDLVOra.SelectedValue);
                                cmd.Parameters.Add("p_kedvezmeny", OracleDbType.Int32).Value = Int32.Parse(TextBoxSzazalek.Text);
                                break;
                            }
                        case "Pillanatnyi_akcio":
                            {
                                OracleTimeStamp ots = new OracleTimeStamp(CalendarEddig.SelectedDate.Year, CalendarEddig.SelectedDate.Month,
                                    CalendarEddig.SelectedDate.Day, 23, 59, 59);
                                cmd.CommandText = "INSERTPILLAKCIO";
                                cmd.Parameters.Clear();
                                cmd.Parameters.Add("p_leiras", OracleDbType.Varchar2).Value = TextBoxLeiras.Text;
                                cmd.Parameters.Add("p_kezdet", OracleDbType.TimeStamp).Value = CalendarEttol.SelectedDate;
                                cmd.Parameters.Add("p_veg", OracleDbType.TimeStamp).Value = ots;
                                cmd.Parameters.Add("p_kedvezmeny", OracleDbType.Int32).Value = Int32.Parse(TextBoxSzazalek.Text);
                                cmd.Parameters.Add("p_cikk", OracleDbType.Int32).Value = DropDownListPillA.SelectedValue;
                                break;
                            }
                        default:
                            {

                                break;
                            }

                    }
                    counter = cmd.ExecuteNonQuery();
                }

                PanelUjButton.Visible = true;
                UjAkcioButton.Visible = true;
                PanelAkciok.Visible = false;
                PanelMilyen.Visible = false;
                PanelGrids.Visible = true;
                CalendarEttol.SelectedDate = DateTime.Now;
                CalendarEddig.SelectedDate = DateTime.Now;
                TextBoxLeiras.Text = "";
                DDLKOra.SelectedIndex = 0;
                DDLVOra.SelectedIndex = 0;
                TextBoxSzazalek.Text = "";
                ETDropDownList.SelectedIndex = 0;
                LabelInfo.Text = "";
                AkciokGridViewETA.DataBind();
                AkciokGridViewIA.DataBind();
                GridViewPA.DataBind();
                Naplo.NaplobaIr(User.Identity.Name, Request.UserHostAddress, Esemeny.NewSale);
            }
            else
            {
                LabelInfo.Text = "A kezdődátumnak kisebb-egyenlőnek kell lennie a végdátumnál!";
            }
        }

        protected void ButtonTorles_Click(object sender, CommandEventArgs e)
        {
            using (OracleConnection sqlc = new OracleConnection())
            {
                sqlc.ConnectionString = ConfigurationManager.ConnectionStrings["VCS"].ConnectionString;
                OracleCommand cmd = new OracleCommand();
                cmd.Connection = sqlc;
                sqlc.Open();
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = "DeleteAkcio";
                GridView gv = null;
                string checkboxId = null;
                switch (e.CommandName)
                {
                    case "TorlesIA": { gv = AkciokGridViewIA; checkboxId = "CheckBoxIATorles"; break; }
                    case "TorlesPA": { gv = GridViewPA; checkboxId = "CheckBoxPATorles"; break; }
                    case "TorlesETA": { gv = AkciokGridViewETA; checkboxId = "CheckBoxETATorles"; break; }
                }
                foreach (GridViewRow gvr in gv.Rows)
                {
                    //ha be volt pipálva

                    if (((CheckBox)gvr.FindControl(checkboxId)).Checked)
                    {
                        string torlendoId = ((Label)gvr.FindControl("AkcioID")).Text.ToString();
                        //törlés
                        cmd.Parameters.Clear();
                        cmd.Parameters.Add("@p_AkcioID", OracleDbType.Int32).Value = torlendoId;
                        cmd.ExecuteNonQuery();
                        Naplo.NaplobaIr(User.Identity.Name, Request.UserHostAddress, Esemeny.DelSale);
                    }
                }
                gv.DataBind();
            }


        }






    }
}
