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_ApprovalSettings : Base.BasePage
{
    #region Blobal Variable Declaration
    ResourceManager rm;
    CultureInfo ci;
    ShiftEvenData objShift = new ShiftEvenData();
    cls_Common objClassCommon = new cls_Common();
    Cls_CheckUser objCheckUser = new Cls_CheckUser();
    DataSet ds_Checkuser = new DataSet();
    string gstrUserGroupName = string.Empty;
    int shiftPatternExists = 0;
    int shiftRosterExists = 0;
    int otApprovalExists = 0;
    int leaveSanctionExists = 0;
    int permissionSanctionExists = 0;
    int manualEntryExists = 0;
    int OnDutyEntryExists = 0;
    int ApprovalExists = 0;
    #endregion
    #region Page Load Event
    protected override void Page_Load(object sender, EventArgs e)
    {
        base.Page_Load(sender, e);
        if (!Page.IsPostBack)
        {
            base.CheckPages(110);
        }
        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);
        gstrUserGroupName = Convert.ToString(Session["UserGroupName"]);
        shiftPatternExists = objClassCommon.CheckPageExistsInLicense(20);
        shiftRosterExists = objClassCommon.CheckPageExistsInLicense(69);
        otApprovalExists = objClassCommon.CheckPageExistsInLicense(97);
        leaveSanctionExists = objClassCommon.CheckPageExistsInLicense(260);
        permissionSanctionExists = objClassCommon.CheckPageExistsInLicense(261);
        manualEntryExists = objClassCommon.CheckPageExistsInLicense(98);
        OnDutyEntryExists = objClassCommon.CheckPageExistsInLicense(174);
        ApprovalExists = objClassCommon.CheckPageExistsInLicense(248);
        if (!IsPostBack)
        {
            objClassCommon.menu("Configuration", "110");
            ddlCompany.Focus();
            CompanyBinding();
            if (shiftPatternExists == 0)
            {
                trPattern.Style.Add("display", "none");
            }
            if (shiftRosterExists == 0)
            {
                trRoster.Style.Add("display", "none");
            }
            if (otApprovalExists == 0)
            {
                trOTApproval.Style.Add("display", "none");
            }
            if (leaveSanctionExists == 0)
            {
                trLeaveSanction.Style.Add("display", "none");
            }
            if (permissionSanctionExists == 0)
            {
                trPermissionSanction.Style.Add("display", "none");
            }
            if (manualEntryExists == 0)
            {
                trManualEntry.Style.Add("display", "none");
            }
            if (OnDutyEntryExists == 0)
            {
                trOnDutyEntry.Style.Add("display", "none");
            }
            if (ApprovalExists == 0)
            {
                trApproval.Style.Add("display", "none");
            }
            btn_Add.OnClientClick = "return confirm('" + rm.GetString("Alert_AreyousuretosaveApprovalSettings", ci) + "');";
        }
        UserRights();
    }
    #endregion
    #region User Defined Functions
    private void CompanyBinding()
    {
        DataSet dsCompany = new DataSet();
        dsCompany = objShift.SelectCompany(gstrUserGroupName, Convert.ToString(Session["LevelRights"]), 0);
        if (dsCompany != null)
        {
            if (dsCompany.Tables[0].Rows.Count > 0)
            {
                ddlCompany.DataSource = dsCompany;
                ddlCompany.DataTextField = "CompanyName";
                ddlCompany.DataValueField = "CompanyId";
                ddlCompany.DataBind();
            }
        }
        foreach (ListItem item in ddlCompany.Items)
        {
            item.Attributes.Add("title", item.Text);
        }
    }
    private void LoadStrings(CultureInfo ci)
    {
        lbl_Header.Text = rm.GetString("OrganisationSettings_ApprovalSettings", ci);
        lblCompany.Text = rm.GetString("EntranceMaster_Company", ci);
        btn_Add.Text = "&nbsp;&nbsp;" + rm.GetString("btn_Save", ci);
        lbl_Pattern.Text = rm.GetString("OrganisationSettings_ApprovalSettings_Pattern", ci);
        lbl_Roster.Text = rm.GetString("OrganisationSettings_ApprovalSettings_Roster", ci);
        lbl_OTApproval.Text = rm.GetString("lblOVerTimeApproval", ci);
        lbl_LeaveSanction.Text = rm.GetString("OrganisationSettings_ApprovalSettings_LeaveSanction", ci);
        lbl_PermissionSanction.Text = rm.GetString("OrganisationSettings_ApprovalSettings_PermissionSanction", ci);
        lbl_ManualEntry.Text = rm.GetString("OrganisationSettings_ApprovalSettings_ManualEntry", ci);
        lbl_OnDutyEntry.Text = rm.GetString("OnDutyApproval_Header", ci);
        rbtnNoLeaveSanction.Text = rm.GetString("ShiftSetting_No", ci);
        rbtnNoManualEntry.Text = rm.GetString("ShiftSetting_No", ci);
        rbtnNoOnDutyEntry.Text = rm.GetString("ShiftSetting_No", ci);
        rbtnNoOTApproval.Text = rm.GetString("ShiftSetting_No", ci);
        rbtnNoPattern.Text = rm.GetString("ShiftSetting_No", ci);
        rbtnNoRoster.Text = rm.GetString("ShiftSetting_No", ci);
        rbtnNoPermissionSanction.Text = rm.GetString("ShiftSetting_No", ci);
        rbtnYesLeaveSanction.Text = rm.GetString("ShiftSetting_Yes", ci);
        rbtnYesManualEntry.Text = rm.GetString("ShiftSetting_Yes", ci);
        rbtnYesOnDutyEntry.Text = rm.GetString("ShiftSetting_Yes", ci);
        rbtnYesOTApproval.Text = rm.GetString("ShiftSetting_Yes", ci);
        rbtnYesPattern.Text = rm.GetString("ShiftSetting_Yes", ci);
        rbtnYesRoster.Text = rm.GetString("ShiftSetting_Yes", ci);
        rbtnYesPermissionSanction.Text = rm.GetString("ShiftSetting_Yes", ci);
        ddlCompany.Items[0].Text = rm.GetString("Select", ci);
    }
    public void resetOptions()
    {
        rbtnNoPattern.Checked = true;
        rbtnNoRoster.Checked = true;
        rbtnNoOTApproval.Checked = true;
        rbtnNoLeaveSanction.Checked = true;
        rbtnNoPermissionSanction.Checked = true;
        rbtnNoManualEntry.Checked = true;
        rbtnNoOnDutyEntry.Checked = true;
        rbtnYesPattern.Checked = false;
        rbtnYesRoster.Checked = false;
        rbtnYesOTApproval.Checked = false;
        rbtnYesLeaveSanction.Checked = false;
        rbtnYesPermissionSanction.Checked = false;
        rbtnYesManualEntry.Checked = false;
        rbtnYesOnDutyEntry.Checked = false;
    }
    public void UserRights()
    {
        try
        {
            ds_Checkuser = objCheckUser.checkRights(110, Convert.ToInt32(Session["userGroup_Id"]), "");
            if (ds_Checkuser.Tables[0].Rows.Count > 0)
            {
                if (ds_Checkuser.Tables[0].Rows[0]["viewPrv"] != DBNull.Value && Convert.ToInt32(ds_Checkuser.Tables[0].Rows[0]["viewPrv"]) == 0)
                {
                    Response.Redirect("OrganizationLandingPage.aspx");
                }
                if (ds_Checkuser.Tables[0].Rows[0]["addPrv"] != DBNull.Value && Convert.ToInt32(ds_Checkuser.Tables[0].Rows[0]["addPrv"]) == 1)
                {
                    this.btn_Add.Visible = true;
                }
                else
                {
                    this.btn_Add.Visible = false;
                }
            }
            else
            {
                Response.Redirect("OrganizationLandingPage.aspx");
            }
        }
        catch
        {
        }
    }
    #endregion
    #region DropDown list Select Index changed Events
    protected void ddlCompany_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddlCompany.SelectedIndex == 0)
        {
            resetOptions();
            base.Message(rm.GetString("Alert_Select_a_Company_to_view_Approval_Settings", ci));
        }
        else
        {
            int intCompanyId = 0;
            intCompanyId = Convert.ToInt32(ddlCompany.SelectedValue);
            DataSet dsApprovalSettings = new DataSet();
            dsApprovalSettings = objShift.SelectApprovalSettings(intCompanyId);
            if (dsApprovalSettings != null)
            {
                if (dsApprovalSettings.Tables.Count > 0)
                {
                    if (dsApprovalSettings.Tables[0].Rows.Count > 0)
                    {
                        if (Convert.ToBoolean(dsApprovalSettings.Tables[0].Rows[0]["Pattern"]) == true)
                        {
                            rbtnYesPattern.Checked = true;
                            rbtnNoPattern.Checked = false;
                        }
                        else
                        {
                            rbtnNoPattern.Checked = true;
                            rbtnYesPattern.Checked = false;
                        }
                        if (Convert.ToBoolean(dsApprovalSettings.Tables[0].Rows[0]["Roster"]) == true)
                        {
                            rbtnYesRoster.Checked = true;
                            rbtnNoRoster.Checked = false;
                        }
                        else
                        {
                            rbtnNoRoster.Checked = true;
                            rbtnYesRoster.Checked = false;
                        }
                        if (Convert.ToBoolean(dsApprovalSettings.Tables[0].Rows[0]["OTApproval"]) == true)
                        {
                            rbtnYesOTApproval.Checked = true;
                            rbtnNoOTApproval.Checked = false;
                        }
                        else
                        {
                            rbtnNoOTApproval.Checked = true;
                            rbtnYesOTApproval.Checked = false;
                        }
                        if (Convert.ToBoolean(dsApprovalSettings.Tables[0].Rows[0]["LeaveSanction"]) == true)
                        {
                            rbtnYesLeaveSanction.Checked = true;
                            rbtnNoLeaveSanction.Checked = false;
                        }
                        else
                        {
                            rbtnNoLeaveSanction.Checked = true;
                            rbtnYesLeaveSanction.Checked = false;
                        }
                        if (Convert.ToBoolean(dsApprovalSettings.Tables[0].Rows[0]["PermissionSanction"]) == true)
                        {
                            rbtnYesPermissionSanction.Checked = true;
                            rbtnNoPermissionSanction.Checked = false;
                        }
                        else
                        {
                            rbtnNoPermissionSanction.Checked = true;
                            rbtnYesPermissionSanction.Checked = false;
                        }
                        if (Convert.ToBoolean(dsApprovalSettings.Tables[0].Rows[0]["ManualEntry"]) == true)
                        {
                            rbtnYesManualEntry.Checked = true;
                            rbtnNoManualEntry.Checked = false;
                        }
                        else
                        {
                            rbtnNoManualEntry.Checked = true;
                            rbtnYesManualEntry.Checked = false;
                        }
                        if (Convert.ToBoolean(dsApprovalSettings.Tables[0].Rows[0]["ODEntry"]) == true)
                        {
                            rbtnYesOnDutyEntry.Checked = true;
                            rbtnNoOnDutyEntry.Checked = false;
                        }
                        else
                        {
                            rbtnNoOnDutyEntry.Checked = true;
                            rbtnYesOnDutyEntry.Checked = false;
                        }
                    }
                    else
                    {
                        resetOptions();
                        base.Message(rm.GetString("Msg_ApprovalSettings", ci));
                    }
                }
            }
        }
    }
    #endregion
    #region Button Click Events
    protected void btn_Add_Click(object sender, EventArgs e)
    {
        int intCompanyId = 0;
        int intPattern = 0;
        int intRoster = 0;
        int intOTApproval = 0;
        int intLeaveSanction = 0;
        int intPermissionSanction = 0;
        int intManualEntry = 0;
        int intOnDutyEntry = 0;
        int intApproval = 0;
        int intShiftApproval = 0;
        if (ddlCompany.SelectedIndex == 0)
        {
            base.Message(rm.GetString("Alert_Select_a_Company_to_Save_Approval_Settings", ci));
        }
        else
        {
            intCompanyId = Convert.ToInt32(ddlCompany.SelectedValue);
            if (rbtnYesPattern.Checked == true)
            {
                intPattern = 1;
            }
            if (rbtnYesRoster.Checked == true)
            {
                intRoster = 1;
            }
            if (rbtnYesOTApproval.Checked == true)
            {
                intOTApproval = 1;
            }
            if (rbtnYesLeaveSanction.Checked == true)
            {
                intLeaveSanction = 1;
            }
            if (rbtnYesPermissionSanction.Checked == true)
            {
                intPermissionSanction = 1;
            }
            if (rbtnYesManualEntry.Checked == true)
            {
                intManualEntry = 1;
            }
            if (rbtnYesOnDutyEntry.Checked == true)
            {
                intOnDutyEntry = 1;
            }
            if (rbtnYesApproval.Checked == true)
            {
                intApproval = 1;
            }
            if (rbtnYesShiftApproval.Checked == true)
            {
                intShiftApproval = 1;
            }
            int Emp_Id = Convert.ToInt32(Session["EmpAutoID"]);
            objShift.InsertUpdateApprovalSettings(intCompanyId, intPattern, intRoster, intOTApproval, intLeaveSanction, 
                intPermissionSanction, intManualEntry, intOnDutyEntry, intApproval, Emp_Id, true, intShiftApproval);
            base.MessageWithRedirect(rm.GetString("Alert_Approval_Settings_Saved_Successfully", ci), "Organization_ApprovalSettings.aspx");
        }
    }
    #endregion
}
