using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Xrm;
using Microsoft.Xrm.Client;

namespace ToolsCRM
{
    public partial class user : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            var xrm = new XrmServiceContext("Xrm");
                //.Where(c => c.EMailAddress1.EndsWith("@example.com"));
            var contacts =
                (from u in xrm.new_uzytkownikkontaktySet
                 join uzyt in xrm.new_uzytkownicySet on u.new_Uzytkownik.Id equals uzyt.new_uzytkownicyId
                 join kon in xrm.ContactSet on u.new_Kontakt.Id equals kon.ContactId
                 where uzyt.new_name == Session["user"].ToString()
                 select kon).Take(5);   
            GridView1.DataSource = contacts;    
            GridView1.DataBind();

            var activities = (from dzialania in xrm.ActivityPointerSet
                              select dzialania).Take(5);
            GridView2.DataSource = activities;
            GridView2.DataBind();

            

            if (Session["user"] == null)
                Response.Redirect("error.aspx");
            else
            {
                Label1.Text = Session["user"].ToString();
                
            }
        }

        protected void Wyloguj_Click(object sender, EventArgs e)
        {
            Session["admin"] = null;
            Session["user"] = null;
            Response.Redirect("Index.aspx");
        }

        protected void GridView1_DataBound(object sender, EventArgs e)
        {
            for (int i = 0; i < GridView1.Rows.Count; i++)
            {
                if (GridView1.Rows[i].Cells[6].Text == "0")
                    GridView1.Rows[i].Cells[6].Text = "aktywny";
                else
                    GridView1.Rows[i].Cells[6].Text = "nieaktywny";
            }
        }  

    }
}