using System;
using System.Data;
using System.Globalization;
using System.Resources;
using System.Threading;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Organization_Settings_Organization_EmployeeFlexiEligible : Base.BasePage
{
    #region Global Variable Declaration
    Cls_CheckUser objCheckUser = new Cls_CheckUser();
    DataSet ds_Checkuser = new DataSet();
    HeaderInformation hdrTable = new HeaderInformation();
    GetReports rptEmp = new GetReports();
    Rpt_Common rptcommon = new Rpt_Common();
    ResourceManager rm;
    cls_Common objCommon = new cls_Common();
    EmployeeEventData objEmployee = new EmployeeEventData();
    string strFromdate, gstrDateFormat, strTodate;
    CultureInfo ci;
    #endregion

    #region Page Load Event
    protected override void Page_Load(object sender, EventArgs e)
    {
         //this.ucOrganizationLevel.ehClick += new EventHandler(this.btnPageButton_Click);
        
        base.Page_Load(sender, e);
        if (!Page.IsPostBack)
        {
            Hidden1.Value = Convert.ToString(Session["IDateFormat"]);
            base.CheckPages(0);
            BindingEmployeeCategoryandType();
            txtEmployeeName.Focus();
            objCommon.menu("EmployeeMaster", "103");
        }
        string source_Language;
        source_Language = Convert.ToString(Session["Session_ActiveLanguage"]);
        rm = new ResourceManager(source_Language, System.Reflection.Assembly.Load("App_GlobalResources"));
        ci = Thread.CurrentThread.CurrentCulture;
        site.SiteMapProvider = Convert.ToString(Session["SiteMapProvider"]);
        LoadStrings(ci);
        string strSetAlert = "";
        strSetAlert = strSetAlert + "<script language='javascript'>";
        strSetAlert = strSetAlert + "SetAlertMessages('" + rm.GetString("Alert_EnterToDate", ci) + "');";
        strSetAlert = strSetAlert + "SetAlertCheckDate('" + rm.GetString("Alert_Validmonthdatefourdigit", ci) + "','" + rm.GetString("Alert_Validdatemonthfourdigit", ci) + "','" + rm.GetString("Alert_Invalidyearentry", ci) + "','" + rm.GetString("Alert_Invalidmonthentry", ci) + "','" + rm.GetString("Alert_Invaliddateentry", ci) + "','" + rm.GetString("Alert_IncorrectdateformatDDMONYYYY", ci) + "','" + rm.GetString("Alert_Entertime", ci) + "','" + rm.GetString("Alert_Invalidvalueforhours", ci) + "','" + rm.GetString("Alert_Invalidvalueforminites", ci) + "','" + rm.GetString("Alert_Invalidtimeformat1", ci) + "','" + rm.GetString("Alert_PleaseentervalidDateandTime", ci) + "','" + rm.GetString("Alert_ValiddatemonthMMDDYYHHMM", ci) + "','" + rm.GetString("Alert_ValiddatemonthMMDDYYHHMMTT", ci) + "','" + rm.GetString("Alert_PleaseentervalidDateandTime", ci) + "','" + rm.GetString("Alert_Invalidvalueforhours", ci) + "','" + rm.GetString("Alert_Invalidvalueforminites", ci) + "');";
        strSetAlert = strSetAlert + "</script>";
        Page.RegisterStartupScript("SetAlertMessages", strSetAlert);
        UserRights();
    }
    #endregion

    #region User Defined Functions
    private void UserRights()
    {
        ds_Checkuser = objCheckUser.checkRights(103, Convert.ToInt32(Session["userGroup_Id"]), "addPrv");
        if (ds_Checkuser.Tables[0].Rows.Count == 0)
        {
            Response.Redirect("OrganizationLandingPage.aspx");
        }
        else
        {
        }
    }
    private void LoadStrings(CultureInfo ci)
    {
        btn_View.Text = "&nbsp;&nbsp;" + rm.GetString("btn_View", ci);
        lblEmployeeDetailsReport.Text = rm.GetString("Flexi_Heading", ci);
        lblEmployeeName.Text = rm.GetString("email_EmployeeName", ci);
        lblEmployeeId.Text = rm.GetString("email_EmployeeId", ci);
        lblEmpCategory.Text = rm.GetString("Master_Category", ci);
        lblEmpType.Text = rm.GetString("Master_Type", ci);
        lblTimePeriod.Text = rm.GetString("Reports_TimePeriod", ci);
        lblFrom.Text = rm.GetString("email_FromDate", ci);
        lblTo.Text = rm.GetString("email_ToDate", ci);
        cus_FromDate.ErrorMessage = rm.GetString("", ci);
        cus_ToDate.ErrorMessage = rm.GetString("", ci);
        cus_ComToDate.ErrorMessage = rm.GetString("", ci);
        ddlEmpCatagory.Items[0].Text = rm.GetString("Select", ci);
        ddlEmpType.Items[0].Text = rm.GetString("Select", ci);
        lblFilter.Text = rm.GetString("lblFilter", ci);
        lblSelect.Text = rm.GetString("FlexibleTimings", ci);
        rdby2n.Text= rm.GetString("ShiftSetting_Yes", ci);
        rdbn2y.Text = rm.GetString("ShiftSetting_No", ci);
        rdbAll.Text = rm.GetString("ExportData_All", ci);
        lbl_FlexibleHours.Text = rm.GetString("Caption_FlexibleHoursApplicability", ci);
    }
    private void BindingEmployeeCategoryandType()
    {
        DataSet dsCategory = new DataSet();
        dsCategory = objEmployee.SelectCategoryDetailsEmployee("");
        if (dsCategory != null)
        {
            if (dsCategory.Tables[0].Rows.Count > 0)
            {
                ddlEmpCatagory.DataSource = dsCategory;
                ddlEmpCatagory.DataTextField = "CategoryName";
                ddlEmpCatagory.DataValueField = "CategoryId";
                ddlEmpCatagory.DataBind();
            }
            dsCategory.Dispose();
            foreach (ListItem item in ddlEmpCatagory.Items)
            {
                item.Attributes.Add("title", item.Text);
            }
        }
        DataSet dsType = new DataSet();
        dsType = objEmployee.SelectTypeDetailsEmployee("");
        if (dsType != null)
        {
            if (dsType.Tables[0].Rows.Count > 0)
            {
                ddlEmpType.DataSource = dsType;
                ddlEmpType.DataTextField = "TypeName";
                ddlEmpType.DataValueField = "TypeId";
                ddlEmpType.DataBind();
            }
            dsType.Dispose();
            foreach (ListItem item in ddlEmpType.Items)
            {
                item.Attributes.Add("title", item.Text);
            }
        }
    }
    public void ValidateFromDate(object s, ServerValidateEventArgs e)
    {
        try
        {
            strFromdate = txtFromDate.Text;
            gstrDateFormat = Convert.ToString(Session["MDateFormat"]);
            strFromdate = objCommon.DateConvertion(gstrDateFormat, strFromdate);
            DateTime dtCurrentDate = Convert.ToDateTime(DateTime.Today.Date);
            DateTime dtFromDate = DateTime.Parse(strFromdate);
            TimeSpan diff = dtFromDate.Subtract(dtCurrentDate);
            int day = diff.Days;
            if (day <= 0)
                e.IsValid = true;
            else
                e.IsValid = false;
        }
        catch
        {
        }
    }
    public void ValidateToDate(object s, ServerValidateEventArgs e)
    {
        try
        {
            gstrDateFormat = Convert.ToString(Session["MDateFormat"]);
            strTodate = txtToDate.Text;
            strTodate = objCommon.DateConvertion(gstrDateFormat, strTodate);
            DateTime dtCurrentDate = Convert.ToDateTime(DateTime.Today.Date);
            DateTime dtFromDate = DateTime.Parse(strTodate);
            TimeSpan diff = dtFromDate.Subtract(dtCurrentDate);
            int day = diff.Days;
            if (day <= 0)
                e.IsValid = true;
            else
                e.IsValid = false;
        }
        catch
        {
        }
    }
    public void CompareDate(object s, ServerValidateEventArgs e)
    {
        try
        {
            strFromdate = txtFromDate.Text;
            strTodate = txtToDate.Text;
            gstrDateFormat = Convert.ToString(Session["MDateFormat"]);
            strTodate = objCommon.DateConvertion(gstrDateFormat, strTodate);
            strFromdate = objCommon.DateConvertion(gstrDateFormat, strFromdate);
            DateTime dtToDate = DateTime.Parse(strTodate);
            DateTime dtFromDate = DateTime.Parse(strFromdate);
            TimeSpan diff = dtToDate.Subtract(dtFromDate);
            int day = diff.Days;
            if (day >= 0)
                e.IsValid = true;
            else
                e.IsValid = false;
        }
        catch
        {
        }
    }
    #endregion

    #region Button Click Events
    protected void btn_View_Click(object sender, EventArgs e)
    {
        bool blnParentExists = true;
        int isFlexible = 0;
        if (Session["parentLevelId"] == null)
        {
            blnParentExists = false;
        }
        else if (Convert.ToInt32(Session["parentLevelId"]) == 0)
        {
            blnParentExists = false;
        }
        if (blnParentExists == false)
        {
            string strMsg;
            strMsg = "<script language='javascript'>";
            strMsg = strMsg + "alert('" + rm.GetString("Alert_SelectCompany", ci) + "');";
            strMsg = strMsg + "</script>";
            Page.RegisterStartupScript("SelectCompany", strMsg);
            return;
        }
        else
        {
            if (rdby2n.Checked == true)
            {
                isFlexible = 1;
            }
            if (rdbn2y.Checked == true)
            {
                isFlexible = 2;
            }
            string Level = "";
            Rpt_Common rptcommon = new Rpt_Common();
            Level = rptcommon.getNodes(Convert.ToInt16(Session["parentLevelId"]), Convert.ToString(Session["LevelRights"]));
            org_levelsEventData getOrg = new org_levelsEventData();
            getOrg.GetOrgParentLevels_FromID(Convert.ToInt16(Session["parentLevelId"]));
            DataSet ds_getOrg = new DataSet();
            ds_getOrg = getOrg.GetOrgParentLevels();
            int iCompanyId = 0;
            if (ds_getOrg != null)
            {
                if (ds_getOrg.Tables[0].Rows.Count > 0)
                {
                    iCompanyId = Convert.ToInt32(ds_getOrg.Tables[0].Rows[0]["LevelID"].ToString());
                }
            }
            int iFlagFlexi = 0;
            int iFlagFlexiPattern = 0; // 1 - Organization, 2 - Individual Employee , 0 - no flexi
            ShiftSettingsEventData getShift = new ShiftSettingsEventData();
            DataSet dsShift = new DataSet();
            dsShift = getShift.GetShiftSettingsByComId(iCompanyId);
            if (dsShift != null && dsShift.Tables.Count > 0 && dsShift.Tables[0].Rows.Count > 0)
            {
                if (dsShift.Tables[0].Rows[0]["IsFlexible"] != DBNull.Value)
                {
                    if (Convert.ToString(dsShift.Tables[0].Rows[0]["IsFlexible"]) == "True")
                    {
                        iFlagFlexi = 1;
                    }
                }
            }
            if (iFlagFlexi == 0 && isFlexible == 1)
            {
                string strScript = "<script language='javascript' type='text/javascript'>";
                strScript += "alert('" + rm.GetString("Alert_Flexible_hours_not_set_to_the_Company", ci) + "');";
                strScript += " </script>";
                this.RegisterStartupScript("closeScript", strScript);
                return;
            }
            string strMsg;
            strMsg = "<script language='javascript'>";
           // strMsg = strMsg + "popupwindow('Organization_EmployeeFlexiPopup.aspx?isFlexible=" + isFlexible + "&EmpId=" + txtEmployeeId.Text + "&EmpName=" + txtEmployeeName.Text + "&EmpCategory=" + ddlEmpCatagory.SelectedValue + "&EmpType=" + ddlEmpType.SelectedValue + "&FromDate=" + txtFromDate.Text + "&ToDate=" + txtToDate.Text + "&parentLevelId=" + Session["parentLevelId"] + "','',900,500);";
            strMsg = strMsg + "document.getElementById('ctl00_BodyContentPH_iframePopup').src = 'Organization_EmployeeFlexiPopup.aspx?isFlexible=" + isFlexible + "&EmpId=" + txtEmployeeId.Text + "&EmpName=" + txtEmployeeName.Text + "&EmpCategory=" + ddlEmpCatagory.SelectedValue + "&EmpType=" + ddlEmpType.SelectedValue + "&FromDate=" + txtFromDate.Text + "&ToDate=" + txtToDate.Text + "&parentLevelId=" + Session["parentLevelId"] + "';";
            strMsg = strMsg + "$(document).ready(function(){$('#myModal').modal('show');});";
            strMsg = strMsg + "</script>";
            Page.RegisterStartupScript("SelectCompany", strMsg);
        }
    }

    protected void btnPageButton_Click(object sender, EventArgs e)
    {
        ScriptManager.RegisterStartupScript(this, GetType(), "showalert", "Loaddropdown();", true);

    }

    #endregion
}
