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_Delete : System.Web.UI.Page
{
    static String PAGINA_LOGIN = "../auth/Login.aspx";
    static String PAGINA_AGENDA = "Default.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
            {
                //Ficheiro Log
                Application.Lock();
                (new ManageLog()).addregisto(Server.MapPath("../system_monitor/log.txt"), Convert.ToInt32(Session["ID_USER"]), ManageLog.ACCAO_ELIMINAR_CONTACTO_AGENDA);
                Application.UnLock();

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


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

                    dc.CONTACTOs.DeleteOnSubmit(contacto);
                    dc.SubmitChanges();

                    //Ficheiro Log
                    Application.Lock();
                    (new ManageLog()).addregisto(Server.MapPath("../system_monitor/log.txt"), Convert.ToInt32(Session["ID_USER"]), ManageLog.ACCAO_ELIMINAR_CONTACTO_AGENDA);
                    Application.UnLock();

                    mv.SetActiveView(v_sucesso);
                }
                catch (Exception ex)
                {
                    mv.SetActiveView(v_erro);
                }    
            }
            catch (Exception ex)
            {
                mv.SetActiveView(v_erro);
            }
        }
       
    }
     
}
