using System;
using System.Collections;
using System.Configuration;
using System.Data;
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.Data.SqlClient;
using Microsoft.ApplicationBlocks.Data;
using System.Collections.Generic;


public partial class Admin_ManageEvents : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Utility.IsNull(Session["Admin"]))
        {
            Response.Redirect("~/Home.aspx");
        }

        pnlResults.Visible = false;

        if (!IsPostBack)
        {
            gvEvents.DataSource = MSEvent.AllEvents;

            gvEvents.DataBind();
        }
    }
    protected void gvEvents_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        GridView gv = (GridView)sender;
        GridViewRow row = gv.Rows[e.RowIndex];

        TextBox txtTitle = (TextBox)row.FindControl("txtTitle");
        TextBox txtDescription = (TextBox)row.FindControl("txtDescription");
        TextBox txtEventDate = (TextBox)row.FindControl("txtEventDate");

        int _id = Int32.Parse(gv.DataKeys[e.RowIndex].Value.ToString());

        MSEvent _event = MSEvent.GetEventByID(_id);
        _event.Title = txtTitle.Text;
        _event.Description = txtDescription.Text;
        _event.EventDate = DateTime.Parse(txtEventDate.Text);

        _event.Save();
        gvEvents.SelectedIndex = -1;
        gvEvents.EditIndex = -1;
        gvEvents.DataSource = MSEvent.AllEvents;
        gvEvents.DataBind();

    }
    protected void gvEvents_Command(object sender, GridViewCommandEventArgs e)
    {

    }
    protected void gvEvents_RowUpdated(object sender, GridViewUpdatedEventArgs e)
    {
        GridView gv = (GridView)sender;
        GridViewRow row = gv.Rows[gv.EditIndex];

        TextBox txtTitle = (TextBox)row.FindControl("txtTitle");
        TextBox txtDescription = (TextBox)row.FindControl("txtDescription");
        TextBox txtEventDate = (TextBox)row.FindControl("txtEventDate");

        int _id = Int32.Parse(gv.DataKeys[gv.EditIndex].Value.ToString());

        MSEvent _event = MSEvent.GetEventByID(_id);
        _event.Title = txtTitle.Text;
        _event.Description = txtDescription.Text;
        _event.EventDate = DateTime.Parse(txtEventDate.Text);

        _event.Save();
        gvEvents.SelectedIndex = -1;
        gvEvents.EditIndex = -1;
        gvEvents.DataBind();

    }

    protected void gvEvents_CancelEdit(object sender, GridViewCancelEditEventArgs e)
    {
        gvEvents.SelectedIndex = -1;
        gvEvents.EditIndex = -1;
        gvEvents.DataSource = MSEvent.AllEvents;
        gvEvents.DataBind();
    }
    protected void gvEvents_RowEditing(object sender, GridViewEditEventArgs e)
    {
        gvEvents.SelectedIndex = e.NewEditIndex;
        gvEvents.EditIndex = e.NewEditIndex;
        gvEvents.DataSource = MSEvent.AllEvents;
        gvEvents.DataBind();  
    }

    protected void gvEvents_Delete(object sender, GridViewDeleteEventArgs e)
    {
        GridView gv = (GridView)sender;
        gv.SelectedIndex = -1;
        int _id = Int32.Parse(gv.DataKeys[e.RowIndex].Value.ToString());

        string sql = "DELETE FROM Events WHERE ID =  "+_id;
        SqlHelper.ExecuteNonQuery(CommandType.Text, sql);
        pnlResults.CssClass = "SuccessPanel";
        pnlResults.Visible = true;
        lblResults.Text = "That Event was deleted";

        gv.DataSource = MSEvent.AllEvents;
        gv.DataBind();

    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        MSEvent _event = new MSEvent(0,DateTime.Parse(txtEventDate.Text), txtTitle.Text,txtDescription.Text);
        _event.Save();
        txtDescription.Text = "";
        txtEventDate.Text = "";
        txtTitle.Text = "";

        gvEvents.DataSource = MSEvent.AllEvents;
        gvEvents.DataBind();     
    
    }
}
