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_ESIEntryEdit : 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;
    DataSet dsPFESIModify = new DataSet();
    Int32 getPFESIEntryId;
    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"]);
        getPFESIEntryId = Convert.ToInt32(Request.QueryString["Policy_Id"]);
        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();
            LoadDataPFESIEntry();
           
        }
        btn_save.OnClientClick = "if(Page_ClientValidate()) return confirm('" + rm.GetString("Alert_ConfirmSave", ci) + "');return false;";

    }

    private void LoadDataPFESIEntry()
    {
        DataTable dtLoanEntry = new DataTable();
        Hashtable htable = new Hashtable();
        htable.Add("@Policy_Id", getPFESIEntryId);
        htable.Add("@type", "P");
        htable.Add("@UserGroupName", gstrUserGroupName);

        dsPFESIModify = objPayrollEventData.SelectPFESIPolicyDetails(htable);

        if (dsPFESIModify.Tables[0].Rows.Count > 0)
        {
            if (dsPFESIModify != null && dsPFESIModify.Tables[0].Rows.Count > 0)
            {
                if (Convert.ToString(dsPFESIModify.Tables[0].Rows[0]["companyID"]) != null)
                {
                  ddlCompany.SelectedValue = Convert.ToString(dsPFESIModify.Tables[0].Rows[0]["companyID"]);
                }
                if(Convert.ToString(dsPFESIModify.Tables[0].Rows[0]["PFESI_PolicyName"]) != null)
                {
                    txt_PolicyName.Text = Convert.ToString(dsPFESIModify.Tables[0].Rows[0]["PFESI_PolicyName"]);
                }
                if (Convert.ToString(dsPFESIModify.Tables[0].Rows[0]["Gender"]) != null)
                {
                    ddlGender.SelectedValue = Convert.ToString(dsPFESIModify.Tables[0].Rows[0]["Gender"]);
                }
                if (Convert.ToString(dsPFESIModify.Tables[0].Rows[0]["EmployeeType"]) != null)
                {
                    ddlEmpType.SelectedValue = Convert.ToString(dsPFESIModify.Tables[0].Rows[0]["EmployeeType"]);
                }
                if(Convert.ToString(dsPFESIModify.Tables[0].Rows[0]["PF_Employee"])!=null)
                {
                    txt_PFEmployee.Text = Convert.ToString(dsPFESIModify.Tables[0].Rows[0]["PF_Employee"]);
                }
                if(Convert.ToString(dsPFESIModify.Tables[0].Rows[0]["PF_Employer"])!=null)
                {
                    txt_PFEmployer.Text = Convert.ToString(dsPFESIModify.Tables[0].Rows[0]["PF_Employer"]);
                }
                if (Convert.ToString(dsPFESIModify.Tables[0].Rows[0]["ESI_Employee"]) != null)
                {
                    txt_ESIEmployee.Text = Convert.ToString(dsPFESIModify.Tables[0].Rows[0]["ESI_Employee"]);
                }
                if (Convert.ToString(dsPFESIModify.Tables[0].Rows[0]["ESI_Employer"]) != null)
                {
                    txt_ESIEmployer.Text = Convert.ToString(dsPFESIModify.Tables[0].Rows[0]["ESI_Employer"]);
                }
            }
        }
       

    }

    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("PFESI_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()
    {
          try
            {
                Hashtable htable = new Hashtable();
                htable.Add("@PFESIPolicyName", txt_PolicyName.Text);
                htable.Add("@PFPolicyid", getPFESIEntryId);
                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", Convert.ToDouble(txt_PFEmployer.Text));
                htable.Add("@PFEmployee", Convert.ToDouble(txt_PFEmployee.Text));
                htable.Add("@ESIEmployer", Convert.ToDouble(txt_ESIEmployer.Text));
                htable.Add("@ESIEmployee", Convert.ToDouble(txt_ESIEmployee.Text));
                htable.Add("@tag", "U");
                objPayrollEventData.InsertPFESIPolicyDetails(htable);

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


            //base.MessageWithRedirect("Policy Modified successfully", "TA_UI_Master_PF_ESIEntryList.aspx");
        }
            catch (Exception ex)
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", "alert(' ');", true);
               
            }



    }

    protected void btn_cancel_Click(object sender, EventArgs e)
    {

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