using Helper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DTO;
using Telerik.Web.UI;
using BusinessLayer;

namespace NikaiDSR.Manager
{

    public partial class TeamReport : PageBase
    {
        #region declaration
        CommonManager CM = new CommonManager();
        ReportManager RM = new ReportManager();
        UserManager UM = new UserManager();
        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            Grid.GridExporting += new OnGridExportingEventHandler(RadGrid1_GridExporting);

            if (!IsPostBack)
            {
                PageTitle = "NIKAI- REPORT A: SUMMARY BY TEAM";
                FillCountryComboBox();
                FillRegionComboBox();
                FillLocationComboBox();
               
                dpFrom.SelectedDate = DateTime.Now.AddDays(-30);
                dpTo.SelectedDate = DateTime.Now;

                UserRoleLocationRegionCountryDTO user = UM.GetUserRoleLocationRegionCountry(UserID, null, null, null, null, null, null, null, null, null, null, null).FirstOrDefault();

                if (RoleID == 4)
                {
                    cmbLocation.SelectedValue = LocationId.ToString();
                }

               else if (RoleID == 3)
                {
                    cmbLocation.Enabled = true;
                    cmbRegion.SelectedValue = user.Region_Id.ToString();
                }

               else if (RoleID == 2)
                {
                    cmbLocation.Enabled = true;
                    cmbRegion.Enabled = true;
                    cmbCountry.SelectedValue = user.Country_Id.ToString();
                }
                else
                {
                    cmbLocation.Enabled = true;
                    cmbRegion.Enabled = true;
                    cmbCountry.Enabled = true;
                }
            }
        }

        void FillCountryComboBox()
        {
            IList<CountryDTO> CountryList = CM.GetCountry(null, null);
            Helper.Validation.FillTelerikDropdown(cmbCountry, CountryList, "Country_Name", "Country_Id", "Select Country", "0");
        }

        void FillRegionComboBox()
        {
            IList<RegionDTO> RegionList = CM.GetRegion(null, null, null);
            Helper.Validation.FillTelerikDropdown(cmbRegion, RegionList, "Region_Name", "Region_Id", "Select Region", "0");
        }

        void FillLocationComboBox()
        {
            IList<LocationDTO> LocationList = CM.GetLocation(null, null, null);
            Helper.Validation.FillTelerikDropdown(cmbLocation, LocationList, "Location_Name", "Location_Id", "Select Location", "0");
        }


        protected void Grid_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
        {
            Grid.DataSource = RM.GetManagerReport(dpFrom.SelectedDate, dpTo.SelectedDate, Convert.ToInt32(cmbCountry.SelectedValue),
                                                  Convert.ToInt32(cmbRegion.SelectedValue), Convert.ToInt32(cmbLocation.SelectedValue),
                                                  rblAccounts.SelectedValue, UserID, null, false);
        }

        protected void Grid_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
        {

        }
        protected void btnGo_Click(object sender, EventArgs e)
        {
            string Error = "";
            if (!Validate(ref Error))
                ShowErrorMessage(Error, errMsg, Common.ERROR_MESSAGE_TYPE.ERROR);
            else
            {
                Grid.Rebind();
            }
        }
        bool Validate(ref string Error)
        {
            bool validateFlag = false;

            if (!(Helper.Validation.DatePickerValidate(dpFrom))
                && Validation.DatePickerValidate(dpTo))
            //&& Validation.ComboBoxValidate(cmbCountry)
            //&& Validation.ComboBoxValidate(cmbRegion)
            //&& Validation.ComboBoxValidate(cmbLocation))
            {
                Error = (string)GetGlobalResourceObject("Res", "Fill_all_mandatory_fields");
            }
            else if (dpFrom.SelectedDate > dpTo.SelectedDate)
            {
                Error = (string)GetGlobalResourceObject("Res", "To_FromDate");
            }
            else
            {
                validateFlag = true;
            }

            return validateFlag;
        }

        #region Export Report

        protected void Radgrid1_ItemCommand(object sender, GridCommandEventArgs e)
        {
            if (e.CommandName == RadGrid.ExportToExcelCommandName)
            {

                Grid.ExportSettings.FileName = "SUMMARY_BY_TEAM";
            }
        }

        protected void RadGrid1_GridExporting(object source, GridExportingArgs e)
        {
            if (e.ExportType == ExportType.Excel)
            {

                string customText = "SUMMARY BY TEAM <br/>";
                ExcelHeader(e, customText);
            }
        }

        #endregion

    }
}