using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using QuintoCaminoV2.Data_objects;

namespace QuintoCaminoV2.Admin
{
    public partial class AdminCharlas : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            getCharlas();
        }

        public void getCharlas()
        {
            DataLayer oDataLayer = new DataLayer();
            Data_objects.Charlas oCharlas = new Data_objects.Charlas();
            grdCharlas.DataSource = oDataLayer.get_Charlas(oCharlas);
            grdCharlas.DataBind();

        }


        protected void grdCharlas_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            List<Data_objects.Charlas> lstCharlas = new List<Data_objects.Charlas>();
            Data_objects.Charlas oCharla = new Data_objects.Charlas();
            DataLayer oDataLayer = new DataLayer();
            Errores oError = new Errores();
            oCharla.intCharlaId = Convert.ToInt32(e.Keys[0]);
            lstCharlas.Add(oCharla);

            oError = oDataLayer.DeleteCharla(lstCharlas);

            if (!(oError.bitError))
            {
                getCharlas();
            }
        }


        [System.Web.Services.WebMethod]
        public static void SaveCharla(string txtTitCharla, string txtFechaCharla, string txtDesCharCorta, string vchURLCharla)
        {
            List<Data_objects.Charlas> lstCharlas = new List<Data_objects.Charlas>();
            Data_objects.Charlas oCharla = new Data_objects.Charlas();
            DataLayer oDataLayer = new DataLayer();
            Errores oError = new Errores();

            oCharla.vchTitulo = txtTitCharla;
            oCharla.vchUrl = vchURLCharla;
            oCharla.vchFecha = txtFechaCharla;
            oCharla.vchDescripcionCorta = txtDesCharCorta;
            oCharla.bitActivo = true;
            lstCharlas.Add(oCharla);

            oError = oDataLayer.insertCharla(lstCharlas);

            if (!(oError.bitError))
            {
               
            }

        }
      

        protected void grdCharlas_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            int index = Convert.ToInt32(e.CommandArgument);

            if (e.CommandName.Equals("detail"))
            {

                int ID = Convert.ToInt32(grdCharlas.DataKeys[index].Value.ToString());
                Response.Redirect("CharlasDetalle.aspx?idCharla=" + ID);

            }

            if (e.CommandName.Equals("detail"))
            {

                int ID = Convert.ToInt32(grdCharlas.DataKeys[index].Value.ToString());
                Response.Redirect("CharlasDetalle.aspx?idCharla=" + ID);

            }
        }

        protected void grdCharlas_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                string item = e.Row.Cells[1].Text;
                foreach (Button button in e.Row.Cells[6].Controls.OfType<Button>())
                {
                    if (button.CommandName == "delete")
                    {
                        button.Attributes["onclick"] = "if(!confirm('Desear eliminar la charla: " + item + "?')){ return false; };";
                    }
                }
            }
        }
    }
}