using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
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.Xml.Linq;

public partial class address_book_Update : System.Web.UI.Page
{
    static String PAGINA_AGENDA = "Default.aspx";
    static String PAGINA_ERRO = "../Default.aspx";
    static String PAGINA_LOGIN = "../auth/Login.aspx";

    protected void Page_Load(object sender, EventArgs e)
    {
        tag_voltar.HRef = PAGINA_AGENDA;
        
        if (Session["ID_USER"] == null || Session["IS_ADMIN"] == null)
            Response.Redirect(PAGINA_LOGIN);

        else if (Session["IS_ADMIN"].ToString().CompareTo("False") == 0)
            mv.SetActiveView(v_permissao);

        else
        {
            try
            {
                if (!Page.IsPostBack)
                {

                    decimal id_contacto = Convert.ToDecimal(Request.QueryString["id"]);

                    DataClassesDataContext dc = new DataClassesDataContext();
                    CONTACTO contacto = dc.CONTACTOs.First(p => p.ID_CONTACTO == id_contacto);

                    text_box_nome.Text = contacto.NOME;
                    text_box_tipo.Text = contacto.TIPO;
                    text_box_morada.Text = contacto.MORADA;
                    text_box_telefone.Text = contacto.NUMERO_TELEFONE_PRI;
                    text_box_fax.Text = contacto.NUMERO_FAX;
                    text_box_email.Text = contacto.EMAIL;
                    text_box_observacoes.Text = contacto.OBSERVACOES;
                }

            }
            catch (Exception ex)
            {
                mv.SetActiveView(v_error);
            }
        }
    }
   


    protected void Button_Guardar_Click(object sender, EventArgs e)
    {

        try
        {
            decimal id_contacto = Convert.ToDecimal(Request.QueryString["id"]);
            DataClassesDataContext dc = new DataClassesDataContext();

            CONTACTO contacto = dc.CONTACTOs.First(p => p.ID_CONTACTO == id_contacto);

            contacto.NOME = text_box_nome.Text;
            contacto.TIPO = text_box_tipo.Text;
            contacto.MORADA = text_box_morada.Text;
            contacto.NUMERO_TELEFONE_PRI = text_box_telefone.Text;
            contacto.NUMERO_FAX = text_box_fax.Text;
            contacto.EMAIL = text_box_email.Text;
            contacto.OBSERVACOES = text_box_observacoes.Text;

            dc.SubmitChanges();

            mv.SetActiveView(v_success);

        }
        catch (Exception ex)
        {
            mv.SetActiveView(v_error);
        }   
    }
  


    protected void Button_Cancelar_Click(object sender, EventArgs e)
    {
        Response.Redirect(PAGINA_AGENDA);
    }

}
