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;

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

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {

            Response.Redirect("~/Pultos/ActiveOrdersDetails.aspx?RendelesID=" + RendelesekGridView.SelectedDataKey.Values[0] +
                "&UgyfelID=" + RendelesekGridView.SelectedDataKey.Values[1]);
        }

        protected void Button_Click(object sender, CommandEventArgs e)
        {
            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;
                if (e.CommandName == "Kesz")
                    cmd.CommandText = "KeszitikUpdate";
                else if (e.CommandName == "Futarnal")
                    cmd.CommandText = "FutarnalUpdate";
                else if (e.CommandName == "Telj")
                    cmd.CommandText = "TeljesitveUpdate";
                else if (e.CommandName == "Del")
                    cmd.CommandText = "RENDELESDELETE";
                foreach (GridViewRow gvr in RendelesekGridView.Rows)
                {
                    //ha be volt pipálva
                    if (((CheckBox)gvr.FindControl("FolyamatbanCheckBox")).Checked)
                    {
                        string folyamatbanId = ((Label)gvr.FindControl("RID")).Text.ToString();
                        //állapotváltás 
                        cmd.Parameters.Clear();
                        cmd.Parameters.Add("rid", OracleDbType.Int32).Value = folyamatbanId;
                        counter += cmd.ExecuteNonQuery();                        
                    }
                }
                
            }
            
                switch (e.CommandName)
                {
                    case "Del":
                        { Naplo.NaplobaIr(User.Identity.Name, Request.UserHostAddress, Esemeny.DelOrd); break; }
                    case "Telj":
                        { Naplo.NaplobaIr(User.Identity.Name, Request.UserHostAddress, Esemeny.ServOrd); break; }

                }
                
            
            RendelesekGridView.DataBind();            
        }

        protected void BeKiPipal()
        {            
            using (OracleConnection sqlc = new OracleConnection())
            {
                sqlc.ConnectionString = ConfigurationManager.ConnectionStrings["VCS"].ConnectionString;
                OracleCommand cmd = new OracleCommand();
                cmd.Connection = sqlc;
                sqlc.Open();
                foreach (GridViewRow gvr in RendelesekGridView.Rows)
                {                    
                    cmd.CommandText = "Select Keszitik, Futarnal from Rendeles where RendelesID = :prendelesid";
                    string RendId = ((Label)gvr.FindControl("RID")).Text.ToString();
                    cmd.Parameters.Clear();
                    cmd.Parameters.Add("prendelesid", OracleDbType.Int32).Value = RendId;

                    bool keszitikE = false;
                    bool futarnalE = false;
                    using (OracleDataReader dr = cmd.ExecuteReader())
                    {
                        dr.Read();
                        keszitikE = (dr.GetInt32(0) == 1) ? true : false;
                        futarnalE = (dr.GetInt32(1) == 1) ? true : false;
                    }                    
                    if (keszitikE)
                    {
                        ((CheckBox)gvr.FindControl("CheckBoxKeszitik")).Checked = true;
                        ((CheckBox)gvr.FindControl("CheckBoxFutarnal")).Checked = false;
                    }
                    if (futarnalE)
                    {
                        ((CheckBox)gvr.FindControl("CheckBoxFutarnal")).Checked = true;
                        ((CheckBox)gvr.FindControl("CheckBoxKeszitik")).Checked = false;
                    }


                }
            }

        }
        
        protected void RendelesekGridView_DataBound(object sender, EventArgs e)
        {
            BeKiPipal();
        }
        
    }
}
