using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AlfredoLotar.Livro.Aspnet.SchoolModel;
using System.Data;
using System.Drawing;
using System.Data.Objects;
using System.Data.Common;

public partial class UpdatePerson : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void UpdateAdd_Click(object sender, EventArgs e)
    {
        using (SchoolEntities contexto = new SchoolEntities())
        { 
            try
            {
                if (!string.IsNullOrWhiteSpace(txtNome.Text.Trim()) &&
                    !string.IsNullOrWhiteSpace(txtSobrenome.Text.Trim()))
                {
                    int id = Convert.ToInt32(GridView1.SelectedDataKey.Value);
                    Person p = (from e1 in contexto.Person where e1.PersonID == id
                             select e1).First();

                    p.FirstName = txtNome.Text.Trim();
                    p.LastName = txtSobrenome.Text.Trim();
                    p.HireDate = DateTime.Now;
                    p.EnrollmentDate = DateTime.Now.AddYears(1);                    
                    contexto.SaveChanges();
                    Preencher();
                    UpdateAdd.Enabled = true;
                }
                else
                {
                    Label1.Text = "Preencha todos os campos.";
                    Label1.ForeColor = Color.Red;
                }

            }
            catch (EntitySqlException ex)
            {
                Response.Write("Erro EntitySqlException");
            }
            catch (EntityCommandExecutionException ex)
            {
                Response.Write("Erro EntityCommandExecutionException");
            }
        }
    }

    void Preencher()
    {
        using (SchoolEntities contexto = new SchoolEntities())
        {
            GridView1.DataSource = contexto.ExecuteStoreQuery<Person>
                    ("Select top 20 * from Person order by PersonID DESC");
            GridView1.DataBind();
        }
    }
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        using (SchoolEntities contexto = new SchoolEntities())
        {
            try{
                int i=Convert.ToInt32(GridView1.SelectedDataKey.Value);
                IQueryable<Person> p = contexto.Person.Where(t => t.PersonID ==i);

                foreach (Person reg in p)
                {  
                    txtID.Text = GridView1.SelectedDataKey.Value.ToString();
                    txtNome.Text = reg.FirstName.ToString();
                    txtSobrenome.Text = reg.LastName.ToString();
                }
                UpdateAdd.Enabled = true;
                btnPreencher.Enabled = false;
            }
            catch (EntitySqlException ex)
            {
               Response.Write(ex.ToString());
            }
        }
    }
    protected void btnPreencher_Click(object sender, EventArgs e)
    {
        Preencher();
    }
}