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

namespace NikaiDSR.Manager
{
    public partial class SalesPersonReport : PageBase
    {
        #region declaration
        CommonManager CM = new CommonManager();
        ReportManager RM = new ReportManager();
        #endregion

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

            if (!IsPostBack)
            {
                PageTitle = "NIKAI- REPORT B: SUMMARY BY SALESPERSON";
                FillExecutiveComboBox();

                dpFrom.SelectedDate = DateTime.Now.AddDays(-30);
                dpTo.SelectedDate = DateTime.Now;
            }
        }

        void FillExecutiveComboBox()
        {
            IList<ManagerExecutivesDTO> LinkedExecutives = CM.GetLinkedExecutives(UserID);
            Helper.Validation.FillTelerikDropdown(cmbExecutive, LinkedExecutives, "Site_User_Name", "Site_User_Id", "Select", "0");
        }
        protected void Grid_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
        {
            int? ExecutiveId = null;
            if (cmbExecutive.SelectedValue != "0")
                ExecutiveId = Convert.ToInt32(cmbExecutive.SelectedValue);

            Grid.DataSource = RM.GetManagerReport(dpFrom.SelectedDate, dpTo.SelectedDate, null, null, null,
                                                  rblAccounts.SelectedValue, UserID, ExecutiveId, true);
        }

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

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

            return validateFlag;
        }

        protected void Grid_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
        {
            if (e.Item is GridDataItem)
            {
                GridDataItem item = (GridDataItem)e.Item;
                item["Present"].Text = (item["Present"].Text == "0") ? "On Leave" : "Present";
            }
        }

        #region Export Report

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

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

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

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

        #endregion
    }
}