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 Telerik.Web.UI;

namespace NikaiDSR.Executive
{
    public partial class ChangeAttandence : PageBase
    {
        #region Declaration
        AttendanceManager AM = new AttendanceManager();
        CommonManager CM = new CommonManager();
        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                PageTitle = "NIKAI- CHANGE ATTENDANCE";
                lblDate.Text = Request.QueryString["Dt"].ToString();
                FillExecutiveComboBox();

                
                AbsentDTO absent = AM.GetAbsent(Convert.ToInt32(Request.QueryString["UID"]), Convert.ToDateTime(lblDate.Text));

                if (absent != null)
                {
                    Session["Absent"] = absent;
                    rblPresentAbsent.SelectedValue = "2";
                }
                else
                {
                    Session["Absent"] = null;
                    rblPresentAbsent.SelectedValue = "1";
                }

                cmbExecutive.SelectedValue = Request.QueryString["UID"].ToString();
            }
        }

        void FillExecutiveComboBox()
        {
            if (RoleID != 5)
            {
                IList<ManagerExecutivesDTO> LinkedExecutives = CM.GetLinkedExecutives(UserID);
                Helper.Validation.FillTelerikDropdown(cmbExecutive, LinkedExecutives, "Site_User_Name", "Site_User_Id", UserName, UserID.ToString());
            }
            else
            {
                cmbExecutive.Items.Add(new RadComboBoxItem(UserName, UserID.ToString()));
                cmbExecutive.Items[0].Selected = true;
            }
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            if (Session["Absent"] != null && rblPresentAbsent.SelectedValue == "1")
            {
                AM.DeleteAbsent(Convert.ToDateTime(lblDate.Text), Convert.ToInt32(cmbExecutive.SelectedValue));
            }
            else if (Session["Absent"] == null && rblPresentAbsent.SelectedValue == "2")
            {
                AM.InsertAbsent(Convert.ToDateTime(lblDate.Text), Convert.ToInt32(cmbExecutive.SelectedValue));
            }

            if (RoleID == 5)
            {
                Response.Redirect("LandingPageExecutive.aspx");
            }
            else
            {
                Response.Redirect("~/Admin/LandingPageAdmin.aspx");
            }
        }
    }
}