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_ConveyanceAllowanceAdd : 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", "319");
            CompanyBinding();
        }


    }
    private void LoadStrings(CultureInfo ci)
    {
        lbl_HeaderConveyance.Text = rm.GetString("Conveyance_policy", ci);
        lblCompany.Text = rm.GetString("EntranceMaster_Company", ci);
        lbl_PolicyName.Text = rm.GetString("ConveyanceAllowance_Name", 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("ConveyanceAllowanceHeader", 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(319, 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)
    {
        //string str = txt_MaximumAmount.Text;
        //if(str=="")
        //{
        //    Page.ClientScript.RegisterStartupScript(this.GetType(), "myScript", "ValidateAll();", true);
        //}
        //else
        //{
            InsertPFESIPolicy();
        //}
       
       // Response.Redirect("../Masters/TA_UI_Master_ConveyanceAllowanceList.aspx");
    }

    private void InsertPFESIPolicy()
    {
        Hashtable phtable = new Hashtable();
        phtable.Add("@tag", "PolicyName");
        phtable.Add("@CAPolicyName", txt_PolicyName.Text);
        DataSet dsPolicyName = objPayrollEventData.InsertandupdateConveyancePolicyDetails(phtable);

        Int64 hdnTxt = Convert.ToInt64(hdnTxtconAmount.Value);

        if (dsPolicyName.Tables[0].Rows.Count == 0)
        {
            try
            {
                Hashtable htable = new Hashtable();
                htable.Add("@CAPolicyName", 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("@CAAmount", Convert.ToInt64(hdnTxt));
                htable.Add("@CAPercentage", Convert.ToDouble("0"));

                //if (rbtn_Amount.Checked)
                //{
                   
                //    htable.Add("@CAType", rbtn_Amount.Text);
                //}
                //else
                //{
                //    htable.Add("@CAPercentage", Convert.ToDouble(Convert.ToString(txt_ConPercentage.Text)==""?0: Convert.ToDouble(txt_ConPercentage.Text)));
                //    htable.Add("@CAType", rbtn_Percentage.Text);
                //}

                htable.Add("@CAFixAmount", Convert.ToInt32(txt_MaximumAmount.Text));
                
                htable.Add("@tag", "I");
                objPayrollEventData.InsertandupdateConveyancePolicyDetails(htable);
               base.MessageWithRedirect("Conveyance Allowance saved successfully ", "../Masters/TA_UI_Master_ConveyanceAllowanceList.aspx");

            }
            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_ConveyanceAllowanceList.aspx");
    }
}