using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Globalization;
using System.Threading;
using System.Reflection;
using System.Resources;
using System.IO;

public partial class Organization_Settings_Organization_LeaveSettingsCompany : Base.BasePage
{
    ResourceManager rm;

    LeaveSettingsEventData_Angler objLeaveSettingsEventData = new LeaveSettingsEventData_Angler();
    ShiftEvenData objShift = new ShiftEvenData();
    cls_Common objClassCommon = new cls_Common();
    string gstrUserGroupName = string.Empty;

    protected override void Page_Load(object sender, EventArgs e)
    {
        // Checking Session
        base.Page_Load(sender, e);

        // Get the culture info to set the language
        CultureInfo ci;

        string source_Language;
        source_Language = Session["Session_ActiveLanguage"].ToString();
        rm = new ResourceManager(source_Language, System.Reflection.Assembly.Load("App_GlobalResources"));
        ci = Thread.CurrentThread.CurrentCulture;
        site.SiteMapProvider = Convert.ToString(Session["SiteMapProvider"]);
        LoadStrings(ci);

        gstrUserGroupName = Session["UserGroupName"].ToString();

        btnApprovalCC.Attributes.Add("onclick", "document.getElementById('ctl00_BodyContentPH_iframePopup').src='GetEmailIdsPopup.aspx';return false;");

        if (!IsPostBack)
        {
            ddlCompany.Focus();
            BindCompany();
            objClassCommon.menu("LeaveSettings", "Company ");
        }
    }

    /// <summary>
    /// BindCompany() Function is used to load all the available companies which is
    /// defined in the Level Details Master in the Company Drop down.
    /// </summary>
    /// 
    private void BindCompany()
    {
        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();
            }
        }
    }

    protected void ddlCompany_SelectedIndexChanged(object sender, EventArgs e)
    {
        ClearValues();

        if (ddlCompany.SelectedValue != "0")
        {
            getValue(int.Parse(ddlCompany.SelectedValue));
        }
    }

    protected void getValue(int CompanyId)
    {
        DataSet dsLeaveSettings = new DataSet();
        dsLeaveSettings = objLeaveSettingsEventData.GetLeaveSettingsByComId(CompanyId);

        if (dsLeaveSettings.Tables[0].Rows.Count > 0)
        {
            if (dsLeaveSettings.Tables[0].Rows[0]["Approval"] != DBNull.Value && dsLeaveSettings.Tables[0].Rows[0]["Approval"].ToString() != string.Empty)
            {
                if (dsLeaveSettings.Tables[0].Rows[0]["Approval"].ToString() == "False")
                {
                    rbtBoth.Checked = true;
                }
                else if (dsLeaveSettings.Tables[0].Rows[0]["Approval"].ToString() == "True")
                {
                    rbtEitherOne.Checked = true;
                }
            }
            if (dsLeaveSettings.Tables[0].Rows[0]["FirstLevelIntimation"] != DBNull.Value && dsLeaveSettings.Tables[0].Rows[0]["FirstLevelIntimation"].ToString() != string.Empty)
            {
                if (dsLeaveSettings.Tables[0].Rows[0]["FirstLevelIntimation"].ToString() == "False")
                {
                    rbtFirstLevelYes.Checked = true;
                }
                else if (dsLeaveSettings.Tables[0].Rows[0]["FirstLevelIntimation"].ToString() == "True")
                {
                    rbtFirstLevelNo.Checked = true;
                }
            }
            if (dsLeaveSettings.Tables[0].Rows[0]["SecondLevelIntimation"] != DBNull.Value && dsLeaveSettings.Tables[0].Rows[0]["SecondLevelIntimation"].ToString() != string.Empty)
            {
                if (dsLeaveSettings.Tables[0].Rows[0]["SecondLevelIntimation"].ToString() == "False")
                {
                    rbtSecondLevelYes.Checked = true;
                }
                else if (dsLeaveSettings.Tables[0].Rows[0]["SecondLevelIntimation"].ToString() == "True")
                {
                    rbtSecondLevelNo.Checked = true;
                }
            }
            if (dsLeaveSettings.Tables[0].Rows[0]["ApprovalCC"] != DBNull.Value && dsLeaveSettings.Tables[0].Rows[0]["ApprovalCC"].ToString() != string.Empty)
            {
                txtApprovalCC.Text = dsLeaveSettings.Tables[0].Rows[0]["ApprovalCC"].ToString();
            }
            if (dsLeaveSettings.Tables[0].Rows[0]["ToMail_DailyReport"] != DBNull.Value && dsLeaveSettings.Tables[0].Rows[0]["ToMail_DailyReport"].ToString() != string.Empty)
            {
                txtDailyReportTO.Text = dsLeaveSettings.Tables[0].Rows[0]["ToMail_DailyReport"].ToString();
            }
            if (dsLeaveSettings.Tables[0].Rows[0]["CCMail_DailyReport"] != DBNull.Value && dsLeaveSettings.Tables[0].Rows[0]["CCMail_DailyReport"].ToString() != string.Empty)
            {
                txtDailyReportCC.Text = dsLeaveSettings.Tables[0].Rows[0]["CCMail_DailyReport"].ToString();
            }
        }

        dsLeaveSettings.Clear();
        dsLeaveSettings.Dispose();
    }

    protected void ClearValues()
    {
        rbtBoth.Checked = false;
        rbtEitherOne.Checked = false;
        rbtFirstLevelYes.Checked = false;
        rbtFirstLevelNo.Checked = false;
        rbtSecondLevelYes.Checked = false;
        rbtSecondLevelNo.Checked = false;
        txtApprovalCC.Text = string.Empty;
        txtDailyReportCC.Text = string.Empty;
        txtDailyReportTO.Text = string.Empty;
    }

    protected void btnApprovalCC_Click(object sender, EventArgs e)
    {
        btnApprovalCC.Attributes.Add("onclick", "window.open('GetEmailIdsPopup.aspx','Test','height=500,top=200,left=250,width=600');return false;");
    }

    protected void butAdd_Click(object sender, EventArgs e)
    {
        try
        {
            int intCompanyId = 0, intLeaveApproval = 0, intFirstLevelIntimation = 0, intSecondLevelIntimation = 0;

            if (ddlCompany.SelectedValue != "0")
            {
                intCompanyId = Convert.ToInt32(ddlCompany.SelectedValue);
            }
            else
            {
                lbl_Error.Text = "Select Company";
                return;
            }

            if (rbtBoth.Checked == true)
            {
                intLeaveApproval = 0;
            }
            else if (rbtEitherOne.Checked == true)
            {
                intLeaveApproval = 1;
            }
            else
            {
                // Nothing
            }

            if (rbtFirstLevelYes.Checked == true)
            {
                intFirstLevelIntimation = 0;
            }
            else if (rbtFirstLevelNo.Checked == true)
            {
                intFirstLevelIntimation = 1;
            }
            else
            {
                // Nothing
            }

            if (rbtSecondLevelYes.Checked == true)
            {
                intSecondLevelIntimation = 0;
            }
            else if (rbtSecondLevelNo.Checked == true)
            {
                intSecondLevelIntimation = 1;
            }
            else
            {
                // Nothing
            }

            objLeaveSettingsEventData.InsertUpdateLeaveSettings(intCompanyId, intLeaveApproval, intFirstLevelIntimation, intSecondLevelIntimation, txtApprovalCC.Text, txtDailyReportTO.Text, txtDailyReportCC.Text);

            string strAlertMessage = "alert('Leave Settings saved successfully');location.replace('Organization_LeaveSettingsCompany.aspx')";

            Page.ClientScript.RegisterStartupScript(this.GetType(), "AlertBox", strAlertMessage, true);

        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    // Getting Information from resource file and display it in label

    private void LoadStrings(CultureInfo ci)
    {
        //lbl_Header.Text = rm.GetString("Org_LeaveSettings", ci);
        lbl_IndicatesMandatoryFields.Text = rm.GetString("IndicatesMandatoryFields", ci);
        lblCompany.Text = rm.GetString("EntranceMaster_Company", ci);
        butAdd.Text = "&nbsp;&nbsp;" + rm.GetString("btn_Save", ci);
        btn_Cancel.Text = "&nbsp;&nbsp;" + rm.GetString("btn_Back", ci);
    }

    protected void btn_Cancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("Organization_LeaveSettings.aspx");
    }
}
