using System;
using System.Data;
using System.Globalization;
using System.Resources;
using System.Threading;
using System.Web.UI;
public partial class Masters_Policy_Config : Base.BasePage
{
    #region "Variable Declaration"
    SMSNotification objSMS = new SMSNotification();
    ResourceManager rm;
    CultureInfo ci;
    int empAutoID = 0;
    Cls_CheckUser objCheckUser = new Cls_CheckUser();
    DataSet ds_Checkuser = new DataSet();
    cls_Common objcommon = new cls_Common();
    EmployeeEventData objEmployee = new EmployeeEventData();
    #endregion
    #region PageLod
    protected override void Page_Load(object sender, EventArgs e)
    {
        //session checking
        base.Page_Load(sender, e);
        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;
        //Set UserRights
        if (!IsPostBack)
        {
            objcommon.menu("Configuration", "307");
            UserRights();
            LoadStrings(ci);
            hdnProcessDate.Value = Convert.ToString(Session["IDateFormat"]);
            BindPolicyDetails();
        }
        site.SiteMapProvider = Convert.ToString(Session["SiteMapProvider"]);
        empAutoID = Convert.ToInt32(Convert.ToString(Session["EmpAutoID"]));
    }
    #endregion
    #region UserDefinedFunctions
    private void LoadStrings(CultureInfo ci)
    {
        btn_save.Text = "&nbsp;&nbsp;" + rm.GetString("btn_Save", ci);
    }
    private void UserRights()
    {
        //Todo: need to update the value as 307 - Policy Configuration page value
        ds_Checkuser = objCheckUser.checkRights(307, Convert.ToInt32(Session["userGroup_Id"]), "");
        if (ds_Checkuser.Tables[0].Rows.Count > 0)
        {
            if (ds_Checkuser.Tables[0].Rows[0]["addPrv"] != DBNull.Value && Convert.ToInt32(ds_Checkuser.Tables[0].Rows[0]["addPrv"]) == 1)
            {
                this.btn_save.Visible = true;
            }
            else
            {
                this.btn_save.Visible = false;
            }
        }
        else
        {
            Response.Redirect("MasterLandingPage.aspx");
        }
    }
    #region ConvertHoursMinsToMinutes
    // Function to Convert hh:mm format to Total Minutes
    public int ConvertHoursMinsToMinutes(string inHoursMins)
    {
        int intTotalMinutes = 0, intHours = 0, intMinutes = 0;
        string[] HoursMins = inHoursMins.Split(':');
        intHours = Int16.Parse(HoursMins[0]);
        intMinutes = Int16.Parse(HoursMins[1]);
        intTotalMinutes = (intHours * 60) + intMinutes;
        return (intTotalMinutes);
    }
    #endregion
    private void BindPolicyDetails()
    {
        DataSet dspolicyConfiguration = new DataSet();
        if (Convert.ToString(ViewState["PolicyConfig"]) == "")
        {
            dspolicyConfiguration = objSMS.GetPolicyConfigurationDetails();
            ViewState["PolicyConfig"] = dspolicyConfiguration;
        }
        else
        {
            dspolicyConfiguration = (DataSet)ViewState["PolicyConfig"];
        }
        if (dspolicyConfiguration.Tables.Count > 0)
        {
            if (dspolicyConfiguration.Tables[0].Rows.Count > 0)
            {
                string strSetYearSelect = "";
                strSetYearSelect = strSetYearSelect + "<script language='javascript'>";
                if (Convert.ToString(dspolicyConfiguration.Tables[0].Rows[0]["Daily_Rrd"]) == "True")
                {
                    chkboxDailyRqrd.Checked = true;
                }
                else
                {
                    chkboxDailyRqrd.Checked = false;
                }
                strSetYearSelect = "";
                strSetYearSelect = strSetYearSelect + "<script language='javascript'>";
                strSetYearSelect = strSetYearSelect + "EnableDisableControls(1);";
                strSetYearSelect = strSetYearSelect + "</script>";
                ClientScript.RegisterStartupScript(Page.GetType(), "SetCounts1", strSetYearSelect);
                if (Convert.ToString(dspolicyConfiguration.Tables[0].Rows[0]["Day_Rqrd"]) == "True")
                {
                    chkboxDayDeviation.Checked = true;
                }
                else
                {
                    chkboxDayDeviation.Checked = false;
                }
                strSetYearSelect = "";
                strSetYearSelect = strSetYearSelect + "<script language='javascript'>";
                strSetYearSelect = strSetYearSelect + "EnableDisableControls(2);";
                strSetYearSelect = strSetYearSelect + "</script>";
                ClientScript.RegisterStartupScript(Page.GetType(), "SetCounts2", strSetYearSelect);
                if (Convert.ToString(dspolicyConfiguration.Tables[0].Rows[0]["Weekly_Rqrd"]) == "True")
                {
                    chkboxWeeklyDeviation.Checked = true;
                }
                else
                {
                    chkboxWeeklyDeviation.Checked = false;
                }
                strSetYearSelect = "";
                strSetYearSelect = strSetYearSelect + "<script language='javascript'>";
                strSetYearSelect = strSetYearSelect + "EnableDisableControls(3);";
                strSetYearSelect = strSetYearSelect + "</script>";
                ClientScript.RegisterStartupScript(Page.GetType(), "SetCounts3", strSetYearSelect);
                if (Convert.ToString(dspolicyConfiguration.Tables[0].Rows[0]["Quarterly_Rqrd"]) == "True")
                {
                    chkboxQuarterlyDeviation.Checked = true;
                }
                else
                {
                    chkboxQuarterlyDeviation.Checked = false;
                }
                strSetYearSelect = "";
                strSetYearSelect = strSetYearSelect + "<script language='javascript'>";
                strSetYearSelect = strSetYearSelect + "EnableDisableControls(4);";
                strSetYearSelect = strSetYearSelect + "</script>";
                ClientScript.RegisterStartupScript(Page.GetType(), "SetCounts4", strSetYearSelect);
                if (Convert.ToString(dspolicyConfiguration.Tables[0].Rows[0]["Cont_Absent"]) == "True")
                {
                    chkboxContinuousAbsent.Checked = true;
                }
                else
                {
                    chkboxContinuousAbsent.Checked = false;
                }
                strSetYearSelect = "";
                strSetYearSelect = strSetYearSelect + "<script language='javascript'>";
                strSetYearSelect = strSetYearSelect + "EnableDisableControls(5);";
                strSetYearSelect = strSetYearSelect + "</script>";
                ClientScript.RegisterStartupScript(Page.GetType(), "SetCounts5", strSetYearSelect);
                if (Convert.ToString(dspolicyConfiguration.Tables[0].Rows[0]["Daily_Hrs"]) != "")
                {
                    txtDailyHrs.Text = Convert.ToString(dspolicyConfiguration.Tables[0].Rows[0]["Daily_Hrs"]);
                }
                if (Convert.ToString(dspolicyConfiguration.Tables[0].Rows[0]["Day_Value"]) != "")
                {
                    txtdayDeviation.Text = Convert.ToString(dspolicyConfiguration.Tables[0].Rows[0]["Day_Value"]);
                }
                if (Convert.ToString(dspolicyConfiguration.Tables[0].Rows[0]["Weekly_Hrs"]) != "")
                {
                    txtWeeklyDeviationHrs.Text = Convert.ToString(dspolicyConfiguration.Tables[0].Rows[0]["Weekly_Hrs"]);
                }
                if (Convert.ToString(dspolicyConfiguration.Tables[0].Rows[0]["Quarterly_Hrs"]) != "")
                {
                    txtQuarterDeviationHrs.Text = Convert.ToString(dspolicyConfiguration.Tables[0].Rows[0]["Quarterly_Hrs"]);
                }
                if (Convert.ToString(dspolicyConfiguration.Tables[0].Rows[0]["Cont_Value"]) != "")
                {
                    txtContinuousAbsentdays.Text = Convert.ToString(dspolicyConfiguration.Tables[0].Rows[0]["Cont_Value"]);
                }
                if (Convert.ToString(dspolicyConfiguration.Tables[0].Rows[0]["ApproFolderPath"]) != "")
                {
                    txtApprovedAttendance.Text = Convert.ToString(dspolicyConfiguration.Tables[0].Rows[0]["ApproFolderPath"]);
                }
                if (Convert.ToString(dspolicyConfiguration.Tables[0].Rows[0]["EmpAPI1"]) != "")
                {
                    txtboxEMPServer1.Text = Convert.ToString(dspolicyConfiguration.Tables[0].Rows[0]["EmpAPI1"]);
                }
                if (Convert.ToString(dspolicyConfiguration.Tables[0].Rows[0]["EmpAPI2"]) != "")
                {
                    txtboxEmpServer2.Text = Convert.ToString(dspolicyConfiguration.Tables[0].Rows[0]["EmpAPI2"]);
                }
                if (Convert.ToString(dspolicyConfiguration.Tables[0].Rows[0]["EmpAPI3"]) != "")
                {
                    txtboxEmpServer3.Text = Convert.ToString(dspolicyConfiguration.Tables[0].Rows[0]["EmpAPI3"]);
                }
                if (Convert.ToString(dspolicyConfiguration.Tables[0].Rows[0]["BlockAPI"]) != "")
                {
                    txtblockingUnblocking.Text = Convert.ToString(dspolicyConfiguration.Tables[0].Rows[0]["BlockAPI"]);
                }
                if (Convert.ToString(dspolicyConfiguration.Tables[0].Rows[0]["LogDownloadAPI"]) != "")
                {
                    txtlogDownloading.Text = Convert.ToString(dspolicyConfiguration.Tables[0].Rows[0]["LogDownloadAPI"]);
                }
                if (Convert.ToString(dspolicyConfiguration.Tables[0].Rows[0]["userName"]) != "")
                {
                    txtuserName.Text = Convert.ToString(dspolicyConfiguration.Tables[0].Rows[0]["userName"]);
                }
                if (Convert.ToString(dspolicyConfiguration.Tables[0].Rows[0]["password"]) != "")
                {
                    txtpwd.Text = Convert.ToString(dspolicyConfiguration.Tables[0].Rows[0]["password"]);
                    txtpwd.Attributes["value"] = Convert.ToString(dspolicyConfiguration.Tables[0].Rows[0]["password"]);
                    txtpwd.Attributes.Add("value", Convert.ToString(dspolicyConfiguration.Tables[0].Rows[0]["password"]));
                }
                if (Convert.ToString(dspolicyConfiguration.Tables[0].Rows[0]["DataProcessDate"]) != "")
                {
                    DateTime date1 = new DateTime();
                    txtProcessDate.Text = Convert.ToString(dspolicyConfiguration.Tables[0].Rows[0]["DataProcessDate"]);
                    date1 = Convert.ToDateTime(dspolicyConfiguration.Tables[0].Rows[0]["DataProcessDate"]);
                    txtProcessDate.Text = date1.ToString(Convert.ToString(Session["MDateFormat"]));
                }
            }
        }
    }
    #endregion
    protected void btn_save_Click(object sender, EventArgs e)
    {
        string strScript = "";
        if (chkboxDailyRqrd.Checked == true && txtDailyHrs.Text == "")
        {
            strScript = strScript + "<script language='javascript'>";
            strScript = strScript + "alert('Enter Daily Hours Value before saving');";
            strScript = strScript + "</script>";
            ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", strScript, false);
        }
        else if (chkboxDayDeviation.Checked == true && txtdayDeviation.Text == "")
        {
            strScript = strScript + "<script language='javascript'>";
            strScript = strScript + "alert('Enter Day Value before saving');";
            strScript = strScript + "</script>";
            ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", strScript, false);
        }
        else if (chkboxWeeklyDeviation.Checked == true && txtWeeklyDeviationHrs.Text == "")
        {
            base.Message("Enter Weekly Hours value before saving");
        }
        else if (chkboxQuarterlyDeviation.Checked && txtQuarterDeviationHrs.Text == "")
        {
            strScript = strScript + "<script language='javascript'>";
            strScript = strScript + "alert('Enter Quarterly OT Hours value before saving');";
            strScript = strScript + "</script>";
            ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", strScript, false);
        }
        else if (chkboxContinuousAbsent.Checked && (txtContinuousAbsentdays.Text == "" || txtContinuousAbsentdays.Text == ""))
        {
            strScript = strScript + "<script language='javascript'>";
            strScript = strScript + "alert('Enter continuously Absent Day value before saving');";
            strScript = strScript + "</script>";
            ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", strScript, false);
        }
        else if (txtApprovedAttendance.Text != "" && System.IO.Directory.Exists(txtApprovedAttendance.Text) == false)
        {
            strScript = strScript + "<script language='javascript'>";
            strScript = strScript + "alert('Specified Folder is not available in the current System');";
            strScript = strScript + "</script>";
            ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", strScript, false);
        }
        else
        {
            try
            {
                int dailyDeviation = 0;
                string gstrDateFormat = "";
                gstrDateFormat = Convert.ToString(Session["MDateFormat"]);
                cls_Common objClassCommon = new cls_Common();
                dailyDeviation = ConvertHoursMinsToMinutes((chkboxDailyRqrd.Checked) ? Convert.ToString(txtDailyHrs.Text) : "00:00");
                string strprocesData = objClassCommon.DateConvertion(gstrDateFormat, Convert.ToString(txtProcessDate.Text));
                objSMS.InsertUpdatingPolicyConfigDetails
                    (
                        (chkboxDailyRqrd.Checked) ? 1 : 0,
                        Convert.ToInt32(dailyDeviation),
                        (chkboxDayDeviation.Checked) ? 1 : 0,
                        (chkboxDayDeviation.Checked) ? Convert.ToInt32(txtdayDeviation.Text) : 0,
                        (chkboxWeeklyDeviation.Checked) ? 1 : 0,
                        (chkboxWeeklyDeviation.Checked) ? Convert.ToInt32(txtWeeklyDeviationHrs.Text) : 0,
                        (chkboxQuarterlyDeviation.Checked) ? 1 : 0,
                        (chkboxQuarterlyDeviation.Checked) ? Convert.ToInt32(txtQuarterDeviationHrs.Text) : 0,
                        (chkboxContinuousAbsent.Checked) ? 1 : 0,
                        (chkboxContinuousAbsent.Checked) ? Convert.ToInt32(txtContinuousAbsentdays.Text) : 0,
                        empAutoID,
                        Convert.ToString(txtApprovedAttendance.Text),
                        Convert.ToString(txtboxEMPServer1.Text),
                        Convert.ToString(txtboxEmpServer2.Text),
                        Convert.ToString(txtboxEmpServer3.Text),
                        Convert.ToString(txtboxEMPServer1.Text),
                        Convert.ToString(txtlogDownloading.Text),
                        Convert.ToString(txtuserName.Text),
                        Convert.ToString(txtpwd.Text),
                        strprocesData
                        );
                ViewState["PolicyConfig"] = "";
                BindPolicyDetails();
                strScript = strScript + "<script language='javascript'>";
                strScript = strScript + "alert('Policy Configuration is saved successfully');";
                strScript = strScript + "</script>";
                ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", strScript, false);
            }
            catch (Exception ex)
            {
                base.Message(ex.ToString());
            }
        }
    }
}