using System;
using System.Data;
using System.Globalization;
using System.Resources;
using System.Threading;
using System.Web.UI;
using System.Web.UI.WebControls;
using DevExpress.Web;
using System.Web.UI.HtmlControls;
using System.Collections;

public partial class Masters_TA_UI_Master_TaxPolicy : Base.BasePage
{
         ResourceManager rm;
        CultureInfo ci;
        Cls_CheckUser objCheckUser = new Cls_CheckUser();
        cls_Common objCommon = new cls_Common();
        DataSet ds_Checkuser = new DataSet();
        PayrollConfigurationEventData objPayrollEvent = new PayrollConfigurationEventData();
        string gstrUserGroupName = string.Empty;
        int EmployeeID = 0;
        string source_Language;
        string strScript = string.Empty;

    protected override void Page_Load(object sender, EventArgs e)
    {
        base.Page_Load(sender, e);
        grv_TaxPolicy.Styles.CssFilePath = Convert.ToString(Session["GridCSS"]);
        //get the culture info to set the language
        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);
        string strSetAlert = "";
        strSetAlert = strSetAlert + "<script language='javascript'>";
        strSetAlert = strSetAlert + "RestoreAlert('" + rm.GetString("Alert_Norecordsfound", ci) + "','" + rm.GetString("Alert_Areyousuretorestore", ci) + "','" + rm.GetString("Alert_Selecteddataisnotindeletedstatus", ci) + "','" + rm.GetString("Alert_Selectdatatorestore", ci) + "','" + rm.GetString("Alert_Nodeletedrecordsavailable", ci) + "');";
        strSetAlert = strSetAlert + "</script>";
        Page.RegisterStartupScript("RestoreAlert", strSetAlert);
        EmployeeID = Convert.ToInt32(Session["EmpAutoID"].ToString());
        //Set UserRights
        UserRights();
        LoadTaxPolicyDetails();
        gstrUserGroupName = Convert.ToString(Session["UserGroupName"]);
        if (gstrUserGroupName != "SuperAdmin")
        {
            //grv_LeavePolicy.Columns[3].Visible = false;
            btn_Restore.Visible = false;
            btn_PermanentDelete.Visible = false;
        }
        btn_Add.Focus();
        if (!IsPostBack)
        {
            objCommon.menu("PayrollMaster", "317");
        }
    }

    private void LoadTaxPolicyDetails()
    {
        Hashtable htable = new Hashtable();
        htable.Add("@tag", "SL");
        DataSet ds = new DataSet();
        ds = objPayrollEvent.InsertTaxPolicy(htable);
        grv_TaxPolicy.DataSource = ds;
        grv_TaxPolicy.DataBind();
    }

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

    protected void btn_Cancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("TA_UI_MasterLeaveMain.aspx");
    }
    protected void EditLinkButton_Click(object sender, EventArgs e)
    {
        LinkButton btn = sender as LinkButton;
        int policy_ID = Convert.ToInt32(btn.CommandName);
        Response.Redirect("TA_UI_Master_TaxPolicyModify.aspx?Policy_Id=" + policy_ID);
    }
    protected void btn_delete_Click(object sender, EventArgs e)
    {
        LinkButton btn = sender as LinkButton;
        int intPolicyID = Convert.ToInt32(btn.CommandName);
        Hashtable htable = new Hashtable();
        htable.Add("@tag", "D");
        htable.Add("@TaxPolicyID", intPolicyID);
        DataSet dSet = new DataSet();
        dSet = objPayrollEvent.InsertTaxPolicy(htable);

        if (Convert.ToInt16(dSet.Tables[0].Rows[0]["D_count"]) != 0)
        {
            base.MessageWithRedirect("Tax Policy deleted successfully", "TA_UI_Master_TaxPolicy.aspx");
        }
        else
        {
            base.Message(rm.GetString("Master_Tax_Policy_Cannot_Delete", ci));
        }
        //int count = objPayrollEvent.PermanentDeletePolicy(intPolicyID);
        //if (count == 0)
        //{
        //    //objLeave.Delete_LeavePolicies(Convert.ToString(intPolicyID), EmployeeID);
        //     objPayrollEvent.PermanentDeleteLeavePolicies(Convert.ToString(intPolicyID), EmployeeID);
        //    base.MessageWithRedirect(rm.GetString("Alert_LeavePolicydeletedsuccessfully", ci), "TA_UI_MasterLeavePolicyList.aspx");
        //}
        //else
        //{
        //    base.Message(rm.GetString("Master_Leave_Policy_Cannot_Delete", ci));
        //    base.Message(rm.GetString("Alert_Cannot_Delete_this_Record_It_is_Already_mapped", ci));
        //}

    }

    protected void btn_Restore_Click(object sender, EventArgs e)
    {
        if (hdnSelectRow.Value != string.Empty && hdnSelectRow.Value != null)
        {
            //string[] LeavePolicyIDs;
            //LeavePolicyIDs = objLeave.commaStringToArray(hdnSelectRow.Value);
            //for (int i = 0; i < LeavePolicyIDs.Length; i++)
            //{
            //    DataSet ds = objLeave.CheckLeavePolicyDuplicate(Convert.ToInt32(LeavePolicyIDs[i]));
            //    if (Convert.ToInt32(ds.Tables[0].Rows[0]["Count"]) > 0 || Convert.ToInt32(ds.Tables[0].Rows[0]["Count1"]) > 0)
            //    {
            //        if (Convert.ToInt32(ds.Tables[0].Rows[0]["Count"]) > 0)
            //        {
            //            base.Message(rm.GetString("Message_Cannot_Restore_LeavePolicy", ci));
            //            // grv_LeavePolicy.DataBind();
            //            hdnSelectRow.Value = "";
            //            return;
            //        }
            //        if (Convert.ToInt32(ds.Tables[0].Rows[0]["Count1"]) > 0)
            //        {
            //            base.Message(rm.GetString("Message_Cannot_Restore", ci));
            //            // grv_LeavePolicy.DataBind();
            //            hdnSelectRow.Value = "";
            //            return;
            //        }
            //    }
            //}
            //  objLeave.RestoreLeavePolicies(hdnSelectRow.Value, EmployeeID);
            base.MessageWithRedirect(rm.GetString("Alert_LeavePolicyrestoredsuccessfully", ci), "TA_UI_MasterLeavePolicyList.aspx");
        }
        else
        {
            strScript = "";
            strScript = strScript + "<script language='javascript'>";
            strScript = strScript + "alert('" + rm.GetString("DeletionRecordAlert", ci) + "')";
            strScript = strScript + "</script>";
            Page.RegisterStartupScript("DelAlert1", strScript);
            return;
        }
    }
    protected void btn_PermanentDelete_Click(object sender, EventArgs e)
    {
        if (hdnSelectRow.Value != string.Empty && hdnSelectRow.Value != null)
        {
            Hashtable dHashTable = new Hashtable();
            dHashTable.Add("@Type", "PermanentDeleteTaxPolicy");
            dHashTable.Add("@PolicyId", hdnSelectRow.Value);
            DataSet ds = objPayrollEvent.PermanentDeletePolicy(dHashTable);
            base.MessageWithRedirect("Record Sucessfully Deleted", "TA_UI_Master_TaxPolicy.aspx");
        }
        else
        {
            strScript = "";
            strScript = strScript + "<script language='javascript'>";
            strScript = strScript + "alert('" + rm.GetString("DeletionRecordAlert", ci) + "')";
            strScript = strScript + "</script>";
            Page.RegisterStartupScript("DelAlert1", strScript);
            return;
        }
    }
    private void LoadStrings(CultureInfo ci)
    {
        lbl_HeaderTaxPolicy.Text = "Tax Policy";
        btn_Add.Text = "&nbsp;&nbsp;" + rm.GetString("btn_Add", ci);
        btn_Delete.Text = "&nbsp;&nbsp;" + rm.GetString("btn_Delete", ci);
        btn_Cancel.Text = "&nbsp;&nbsp;" + rm.GetString("btn_Back", ci);
        btn_Restore.Text = "&nbsp;&nbsp;" + rm.GetString("btn_Restore", ci);
        btn_PermanentDelete.Text = "&nbsp;&nbsp;" + rm.GetString("btn_Delete", ci);
        grv_TaxPolicy.Columns[2].Caption = rm.GetString("LeavePermissionPolicy_PolicyName", ci);
        lblDataView.Text = rm.GetString("lblDataView", ci);
    }

    private void UserRights()
    {
        ds_Checkuser = objCheckUser.checkRights(315, Convert.ToInt32(Session["userGroup_Id"]), "");
        if (ds_Checkuser.Tables[0].Rows.Count > 0)
        {
            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;
            }
            if (ds_Checkuser.Tables[0].Rows[0]["deletePrv"] != DBNull.Value && Convert.ToInt32(ds_Checkuser.Tables[0].Rows[0]["deletePrv"]) == 1)
            {
                this.btn_Delete.Visible = true;
                this.btn_PermanentDelete.Visible = true;
                this.btn_Restore.Visible = false;
                grv_TaxPolicy.Columns[5].Visible = true;
                grv_TaxPolicy.Columns[0].Visible = true;
            }
            else
            {
                this.btn_Delete.Visible = false;
                this.btn_PermanentDelete.Visible = false;
                this.btn_Restore.Visible = false;
                grv_TaxPolicy.Columns[0].Visible = false;
                grv_TaxPolicy.Columns[5].Visible = false;
            }
            if (ds_Checkuser.Tables[0].Rows[0]["modifyPrv"] != DBNull.Value && Convert.ToInt32(ds_Checkuser.Tables[0].Rows[0]["modifyPrv"]) == 1)
            {
                grv_TaxPolicy.Columns[4].Visible = true;
            }
            else
            {
                grv_TaxPolicy.Columns[4].Visible = false;
            }
            if (ds_Checkuser.Tables[0].Rows[0]["viewPrv"] != DBNull.Value && Convert.ToInt32(ds_Checkuser.Tables[0].Rows[0]["viewPrv"]) == 1)
            {
                this.grv_TaxPolicy.Columns[3].Visible = true;
            }
            else
            {
                this.grv_TaxPolicy.Columns[3].Visible = false;
            }
        }
        else
        {
            Response.Redirect("MasterLandingPage.aspx");
        }
    }
    protected void grv_TaxPolicy_HtmlRowCreated(object sender, ASPxGridViewTableRowEventArgs e)
    {
        if (e.RowType == DevExpress.Web.GridViewRowType.Data)
        {
            source_Language = Convert.ToString(Session["Session_ActiveLanguage"]);
            rm = new ResourceManager(source_Language, System.Reflection.Assembly.Load("App_GlobalResources"));
            ci = Thread.CurrentThread.CurrentCulture;
            Label DeletedRecord = (Label)grv_TaxPolicy.FindRowCellTemplateControl(e.VisibleIndex, null, "lblIsDeleted");
            HtmlInputCheckBox deleteChkBxItem = (HtmlInputCheckBox)grv_TaxPolicy.FindRowCellTemplateControl(e.VisibleIndex, null, "chkSelect");
            LinkButton LnkbtnDelete = (LinkButton)grv_TaxPolicy.FindRowCellTemplateControl(e.VisibleIndex, null, "btnDelete");
            if (deleteChkBxItem != null)
            {
                if (deleteChkBxItem.Disabled == false)
                {
                    LnkbtnDelete.Enabled = true;
                }
                else
                {
                    LnkbtnDelete.Enabled = false;
                    LnkbtnDelete.OnClientClick = null;
                }
            }
            if (DeletedRecord != null)
            {
                if (DeletedRecord.Text == "True")
                {
                    DeletedRecord.Text = rm.GetString("Grid_True", ci);
                    if (hdnDeletedData.Value == string.Empty)
                    {
                        hdnDeletedData.Value = e.GetValue("TaxPolicy_Id").ToString();
                    }
                    else
                    {
                        if (hdnDeletedData.Value.Contains(e.GetValue("TaxPolicy_Id").ToString()) == false)
                        {
                            hdnDeletedData.Value = hdnDeletedData.Value + "," + e.GetValue("TaxPolicy_Id").ToString();
                        }
                    }
                }
                else if (DeletedRecord.Text == "False")
                {
                    DeletedRecord.Text = rm.GetString("Grid_False", ci);
                }
            }
        }
    }
}