using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
using System.Linq;
using System.Resources;
using System.Threading;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Masters_TA_UI_Master_PF_ESIEntryAdd : Base.BasePage
{
    cls_Common objCommon = new cls_Common();
    Cls_CheckUser objCheckUser = new Cls_CheckUser();
    DataSet ds_Checkuser = new DataSet();
    ResourceManager rm;
    CultureInfo ci;
    PayrollConfigurationEventData objPayrollEventData = new PayrollConfigurationEventData();
    ShiftEvenData objShift = new ShiftEvenData();
    EmployeeEventData objEmployeeEventData = new EmployeeEventData();
    string gstrUserGroupName = string.Empty;
    protected override void Page_Load(object sender, EventArgs e)
    {
        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;
        site.SiteMapProvider = Convert.ToString(Session["SiteMapProvider"]);
        // LoadStrings Function fills the Label from the Resource File
        LoadStrings(ci);
        gstrUserGroupName = Convert.ToString(Session["UserGroupName"]);

        string strSetAlert = "";
        //strSetAlert = strSetAlert + "chkTDS('" + rm.GetString("Alert_Enterinhhmmformat", ci) + "');";
        Session["ddlEmployeeCompany"] = Convert.ToInt16(ddlCompany.SelectedValue);
        Session["ddlEmployeeCategory"] = Convert.ToInt16(ddlEmpCatagory.SelectedValue);
        Session["ddlEmployeeType"] = Convert.ToInt16(ddlEmpType.SelectedValue);
        Session["ddlGender"] = Convert.ToInt16(ddlGender.SelectedValue);
        if (!IsPostBack)
        {
            Session["ddlEmployeeCategory"] = Convert.ToInt16(ddlEmpCatagory.SelectedValue);
            Session["ddlEmployeeType"] = Convert.ToInt16(ddlEmpType.SelectedValue);
            Session["ddlGender"] = Convert.ToInt16(ddlGender.SelectedValue);
            BindingEmployeeCategoryandType();
            objCommon.menu("PayrollMaster", "318");
            CompanyBinding();
        }


    }
    private void LoadStrings(CultureInfo ci)
    {
        lbl_HeaderPF_ESIPolicy.Text = rm.GetString("HeaderPF_ESIPolicy", ci);
        lblCompany.Text = rm.GetString("EntranceMaster_Company", ci);
        lbl_PolicyName.Text = rm.GetString("LeavePermissionPolicy_PolicyName", ci);
        lblGender.Text = rm.GetString("EmployeeMaster_Gender", ci);
        lbl_IndicatesMandatoryFields.Text = rm.GetString("IndicatesMandatoryFields", ci);
        lblEmpCategory.Text = rm.GetString("Master_Category", ci);
        lblEmpType.Text = rm.GetString("Master_Type", ci);
        chkPF_ESIPolicy.Text = rm.GetString("PF_ESIPolicyConfig", ci);
       // btn_Add.Text = "&nbsp;&nbsp;" + rm.GetString("btn_Add", ci);
        //chkProfessionalTax.Text = rm.GetString("UserGroup_Professional", ci);
        //btn_Add_Professional.Text = "&nbsp;&nbsp;" + rm.GetString("btn_Add", ci);
        btn_save.Text = "&nbsp;&nbsp;" + rm.GetString("btn_Save", ci);
        btn_cancel.Text = "&nbsp;&nbsp;" + rm.GetString("btn_Back", ci);


    }

    private void UserRights()
    {
        ds_Checkuser = objCheckUser.checkRights(318, Convert.ToInt32(Session["userGroup_Id"]), "addPrv");

        if (ds_Checkuser.Tables[0].Rows.Count == 0)
        {
            Response.Redirect("MasterLandingPage.aspx");
        }
    }

    protected void ddlEmpCatagory_DataBound(object sender, EventArgs e)
    {
        DropDownList ddl = sender as DropDownList;
        DropdownTooltip(ddl);
    }
    protected void ddlEmpType_DataBound(object sender, EventArgs e)
    {
        DropDownList ddl = sender as DropDownList;
        DropdownTooltip(ddl);
    }

    private void DropdownTooltip(DropDownList ddl)
    {
        if (ddl != null)
        {
            foreach (ListItem li in ddl.Items)
            {
                li.Attributes["title"] = li.Text;
            }
        }
    }
    private void BindingEmployeeCategoryandType()
    {
        // employee category binding
        DataSet dsCategory = new DataSet();
        dsCategory = objEmployeeEventData.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);
            }
        }
        // employee type binding
        DataSet dsType = new DataSet();
        dsType = objEmployeeEventData.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);
            }
        }

    }
    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);
        }
    }

    protected void btn_save_Click(object sender, EventArgs e)
    {
        InsertPFESIPolicy();
        
    }

    private void InsertPFESIPolicy()
    {
        double pfEmployer = 0;
        double esiEmployer = 0;
        pfEmployer = Convert.ToString(txt_PFEmployer.Text)==""?0:Convert.ToDouble(txt_PFEmployer.Text);
        esiEmployer= Convert.ToString(txt_ESIEmployer.Text) == "" ? 0 : Convert.ToDouble(txt_ESIEmployer.Text);


        Hashtable phtable = new Hashtable();
        phtable.Add("@tag", "policyName");
        phtable.Add("@PFESIPolicyName", txt_PolicyName.Text);
        DataSet dsPolicyName = objPayrollEventData.InsertPFESIPolicyDetails(phtable);
        if(dsPolicyName.Tables[0].Rows.Count==0)
        {
            try
            {
                Hashtable htable = new Hashtable();
                htable.Add("@PFESIPolicyName", txt_PolicyName.Text);
                htable.Add("@companyID", Convert.ToInt32(ddlCompany.SelectedItem.Value));
                htable.Add("@EmployeeCategory", Convert.ToInt16(Session["ddlEmployeeCategory"]));
                htable.Add("@EmployeeType", Convert.ToInt16(Session["ddlEmployeeType"]));
                htable.Add("@Gender", Convert.ToString(Session["ddlGender"]));
                htable.Add("@PFEmployer", pfEmployer);
                htable.Add("@PFEmployee", Convert.ToDouble(txt_PFEmployee.Text));
                htable.Add("@ESIEmployer", esiEmployer);
                htable.Add("@ESIEmployee", Convert.ToDouble(txt_ESIEmployee.Text));
                htable.Add("@tag", "I");
                objPayrollEventData.InsertPFESIPolicyDetails(htable);

                string strRedirectPage = "TA_UI_Master_PF_ESIEntryList.aspx";
                string strScript = "";
                strScript = strScript + "<script language='javascript'>";
                strScript = strScript + "alert('" + rm.GetString("Alert_PFESIPolicysavedsuccessfully", ci) + "');";
                strScript = strScript + "location.replace('" + strRedirectPage + "');";
                strScript = strScript + "</script>";
                ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", strScript, false);

            }
            catch (Exception ex)
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", "alert('Policy Cannot Added ');", true);
                // ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", "alert('" + rm.GetString("Alert_ApplyLeaveForLeavePolicy", ci) + "');", true);
            }

        }
        else
        {
            ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", "alert('" + rm.GetString("Alert_PolicyNameAlreadyExists", ci) + "');", true);
        }


    }

    protected void btn_cancel_Click(object sender, EventArgs e)
    {

        Response.Redirect("../Masters/TA_UI_Master_PF_ESIEntryList.aspx");
    }
}