using System;
using System.Collections.Generic;
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.Admin
{
    public partial class Target : PageBase
    {
        #region Declare
        TargetManager TM = new TargetManager();
        CommonManager CM = new CommonManager();
        UserManager UM = new UserManager();
        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                PageTitle = "NIKAI- ADMIN - TARGET";
                FillLocationComboBox();
                FillExecutiveComboBox();
                FillYearComboBox();
            }
        }

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

        void FillExecutiveComboBox()
        {
            IList<UserRoleLocationRegionCountryDTO> Executives = UM.GetUserRoleLocationRegionCountry(null, null, null, 1, null, null, Convert.ToInt32(cmbLocation.SelectedValue), null, null, null, null, null);
            Helper.Validation.FillTelerikDropdown(cmbExecutive, Executives, "Name", "Site_User_Id", "Select", "0");
        }

        protected void cmbLocation_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            if (Validation.ComboBoxValidate(cmbLocation))
                FillExecutiveComboBox();
            else
            {
                cmbExecutive.Items.Add(new RadComboBoxItem("Select Year", "0"));
            }
        }

        void FillYearComboBox()
        {
            cmbYear.Items.Add(new RadComboBoxItem("Select Year", "0"));

            for (int i = -5; i <= 5; i++)
                cmbYear.Items.Add(new RadComboBoxItem(DateTime.Now.AddYears(i).Year.ToString(), DateTime.Now.AddYears(i).Year.ToString()));
        }

        protected void btnGo_Click(object sender, EventArgs e)
        {
            Grid.Rebind();
        }

        protected void Grid_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
        {
            IList<TargetDTO> TargetList = TM.GetTarget(Convert.ToInt32(cmbExecutive.SelectedValue), Convert.ToInt32(cmbYear.SelectedValue));

            if (TargetList.Count <= 0)
            {
                for (int i = 1; i <= 12; i++)
                    TargetList.Add(new TargetDTO() { Month = i });
            }

            Grid.DataSource = TargetList;
        }

        protected void Grid_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
        {
            if (e.Item is GridDataItem)
            {
                GridDataItem item = (GridDataItem)e.Item;

                switch (item["Month"].Text)
                {
                    case "1":
                        item["Month_Value"].Text = "Jan";
                        break;
                    case "2":
                        item["Month_Value"].Text = "Feb";
                        break;
                    case "3":
                        item["Month_Value"].Text = "Mar";
                        break;
                    case "4":
                        item["Month_Value"].Text = "Apr";
                        break;
                    case "5":
                        item["Month_Value"].Text = "May";
                        break;
                    case "6":
                        item["Month_Value"].Text = "Jun";
                        break;
                    case "7":
                        item["Month_Value"].Text = "Jul";
                        break;
                    case "8":
                        item["Month_Value"].Text = "Aug";
                        break;
                    case "9":
                        item["Month_Value"].Text = "Sep";
                        break;
                    case "10":
                        item["Month_Value"].Text = "Oct";
                        break;
                    case "11":
                        item["Month_Value"].Text = "Nov";
                        break;
                    case "12":
                        item["Month_Value"].Text = "Dec";
                        break;
                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            IList<TargetDTO> TargetList = new List<TargetDTO>();
            TargetDTO target;

            string Error = "";
            if (!Validate(ref Error))
            {
                ShowErrorMessage(Error, errMsg, Common.ERROR_MESSAGE_TYPE.ERROR);
            }
            else
            {
                foreach (var GridItem in Grid.Items)
                {
                    target = new TargetDTO();

                    GridDataItem item = (GridDataItem)GridItem;
                    if (item["Target_Id"].Text != "0")
                        target.Target_Id = Convert.ToInt32(item["Target_Id"].Text);
                    target.Month = Convert.ToInt32(item["Month"].Text);
                    target.Site_User_Id = Convert.ToInt32(cmbExecutive.SelectedValue);
                    target.Year = Convert.ToInt32(cmbYear.SelectedValue);
                    if (((RadNumericTextBox)item.FindControl("txtSales")).Text != "")
                        target.Sales = Convert.ToInt32(((RadNumericTextBox)item.FindControl("txtSales")).Text);
                    if (((RadNumericTextBox)item.FindControl("txtSecondarySales")).Text != "")
                        target.Secondary_Sales = Convert.ToInt32(((RadNumericTextBox)item.FindControl("txtSecondarySales")).Text);
                    if (((RadNumericTextBox)item.FindControl("txtCollection")).Text != "")
                        target.Collection = Convert.ToInt32(((RadNumericTextBox)item.FindControl("txtCollection")).Text);
                    TargetList.Add(target);
                }

                TM.InsertUpdateTargets(TargetList);
                Grid.Rebind();
                Error = "Updated Successfully";
                ShowErrorMessage(Error, errMsg, Common.ERROR_MESSAGE_TYPE.SUCCESS);
            }
        }

        bool Validate(ref string Error)
        {
            if (Helper.Validation.ComboBoxValidate(cmbExecutive)
                && Validation.ComboBoxValidate(cmbYear))
                return true;
            else
            {
                Error = "Fill the Mandatory fields.";
                return false;
            }
        }
    }
}