using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using digioz;

public partial class controls_admin_Statistics : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            multiView.ActiveViewIndex = 0;
            FillGridViews();
        }
    }

    protected void Menu1_MenuItemClick(object sender, MenuEventArgs e)
    {
        multiView.ActiveViewIndex = Int32.Parse(e.Item.Value);
        FillGridViews();
    }

    protected void GridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        FillGridViews();
        
        GridView gridView = sender as GridView;

        if (gridView != null)
        {
            gridView.PageIndex = e.NewPageIndex;
            gridView.DataBind();
        }
    }

    protected void ShowStatisticsButton_Click(object sender, EventArgs e)
    {
        FillGridViews();
    }
    
    private void FillGridViews()
    {
        if (!IsPostBack)
        {
            // Show statistics for the last ten days on the first load.
            startDateTextBox.Text = DateTime.Today.AddDays(-9).ToString("MM/dd/yyyy");
            endDateTextBox.Text = DateTime.Today.ToString("MM/dd/yyyy");
        }

        DateTime startDate = DateTime.Today;
        DateTime endDate = DateTime.Today;
        DateTime tmpDate;

        if (DateTime.TryParse(startDateTextBox.Text, out tmpDate))
        {
            startDate = tmpDate;
        }

        if (DateTime.TryParse(endDateTextBox.Text, out tmpDate))
        {
            endDate = tmpDate;
        }

        dailyVisitorsGridView.DataSource = VisitorManagement.GetDailyVisitorCount(startDate, endDate);
        visitorIPsGridView.DataSource = VisitorManagement.GetVisitorIps(startDate, endDate);
        countriesGridView.DataSource = VisitorManagement.GetVisitorCountries(startDate, endDate);
        pageHitsGridView.DataSource = PageHitsManagement.GetHitStatistics();
        browsersGridView.DataSource = VisitorManagement.GetVisitorBrowsers(startDate, endDate);
        osGridView.DataSource = VisitorManagement.GetVisitorOSs(startDate, endDate);

        DataBind();
    }
}
