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;

namespace NikaiDSR.Manager
{
    public partial class ChangeReportees : PageBase
    {
        #region declarartion
        UserManager UM = new UserManager();
        CommonManager CM = new CommonManager();
        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
               PageTitle = "NIKAI- CHANGE REPORTEES";
                BindCheckBox();
            }
        }

        void BindCheckBox()
        {
            IList<ReporteesForUserCreationDTO> UserList = UM.GetReporteesForUserCreation(RoleID, LocationId);

            ChkReportees.DataTextField = "Name";
            ChkReportees.DataValueField = "Site_User_Id";
            ChkReportees.DataSource = UserList.Where(c => c.Site_User_Id != UserID);
            ChkReportees.DataBind();

            IList<ManagerExecutivesDTO> LinkedUserList = CM.GetLinkedExecutives(UserID);
            foreach (ListItem item in ChkReportees.Items)
            {
                if (LinkedUserList.Select(c=>c.Site_User_Id).Contains(Convert.ToInt32(item.Value)))
                    item.Selected =true;
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            string Error = "";
            if (!Validate(ref Error))
                ShowErrorMessage(Error, errMsg, Common.ERROR_MESSAGE_TYPE.ERROR);
            else
            {
                IList<int> LinkedExecutiveIds = new List<int>();
                foreach (ListItem item in ChkReportees.Items)
                {
                    if (item.Selected == true)
                        LinkedExecutiveIds.Add(Convert.ToInt32(item.Value));
                }

                UM.InsertUpdateLinkedExecutives(LinkedExecutiveIds, UserID);

                Error = "Updated Successfully.";
                ShowErrorMessage(Error, errMsg, Common.ERROR_MESSAGE_TYPE.SUCCESS);
            }
        }

        bool Validate(ref string Error)
        {
            bool validateFlag = false;
            Error = "Select at least one Reportee.";

            foreach (ListItem item in ChkReportees.Items)
            {
                if (item.Selected == true)
                {
                    validateFlag = true;
                    break;
                }
            }

            return validateFlag;
        }
    }
}