Imports System
Imports System.Data
Imports System.String
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports Microsoft.VisualBasic
Imports Telerik.QuickStart
Imports Telerik.Web.UI

Partial Class Administrador_frmAdminRazas
    Inherits System.Web.UI.Page

#Region "Declaración de Variables y Propiedades"

    Public oRutinas As New ClsRutinas
    Private _FechaHoy As Date
    Public oPropiedad As New MarBiologico

#End Region

#Region "Ciclo de Vida de la Página"

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        'Verificar si la sessión se cerro
        If Session("IdUsuario") = 0 Then
            Response.Redirect("../Administrador/Seguridad/frmExpiroSession.aspx", True)
            Exit Sub
        End If

        If IsPostBack Then Exit Sub
        If clsSeguridad.ValidarPermisos(Session, "frmAdminRazas") = False Then ''formulario 169
            Response.Redirect("../Administrador/Seguridad/Paginasinprivilegios.htm", True)
            Exit Sub
        End If

    End Sub

    Protected Sub Page_LoadComplete(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.LoadComplete

    End Sub

    Protected Overrides Sub RaisePostBackEvent(ByVal sourceControl As IPostBackEventHandler, ByVal eventArgument As String)
        MyBase.RaisePostBackEvent(sourceControl, eventArgument)
        If TypeOf sourceControl Is RadGrid Then
            Select Case eventArgument
                Case "Rebind"
                    grdRazas.MasterTableView.SortExpressions.Clear()
                    grdRazas.MasterTableView.GroupByExpressions.Clear()
                    grdRazas.Rebind()
                Case "RebindAndNavigate"
                    grdRazas.MasterTableView.SortExpressions.Clear()
                    grdRazas.MasterTableView.GroupByExpressions.Clear()
                    grdRazas.MasterTableView.CurrentPageIndex = grdRazas.MasterTableView.PageCount - 1
                    grdRazas.Rebind()
            End Select
        End If
    End Sub

#End Region

#Region "Carga de Datos y Mapeo"

    Private Sub CargarRazas()
        Dim Ds As New DataSet
        Dim objparametros As New Collection

        Dim drPermisos As DataRow = oRutinas.GetPermisos(Session, "frmAdminRazas")
        Dim idUsuarioParam As Object = Convert.ToInt32(Session("IdUsuario"))
        Dim Parametro As New CmdParametro("@IdUsuario", idUsuarioParam, SqlDbType.Int, False)
        objparametros.Add(Parametro)
        Ds = clsBitacora.GetSelect("[app].[SpIVCSeleccionarRazasAnimales]", objparametros, Session, 96, True)
        If Ds IsNot Nothing AndAlso Ds.Tables.Count > 0 Then
            grdRazas.DataSource = Ds.Tables(0)
        Else
            grdRazas.DataSource = New DataTable()
        End If
    End Sub

#End Region

#Region "Lógica del Grid (RadGrid)"

    Protected Sub grdRazas_NeedDataSource(ByVal source As Object, ByVal e As Telerik.Web.UI.GridNeedDataSourceEventArgs) Handles grdRazas.NeedDataSource
        CargarRazas()
    End Sub

    Protected Sub grdRazas_ItemCreated(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridItemEventArgs) Handles grdRazas.ItemCreated

        If TypeOf e.Item Is GridDataItem Then
            Dim btnEdit As LinkButton = TryCast(e.Item.FindControl("BtnEdit"), LinkButton)
            Dim btnView As LinkButton = TryCast(e.Item.FindControl("BtnView"), LinkButton)
            Dim dataItem As GridDataItem = DirectCast(e.Item, GridDataItem)
            Dim idVacuna As String = dataItem.GetDataKeyValue("idTblOpciones").ToString()
            Dim idRol As Integer? = oRutinas.GetRoli(Session("UserRoles"), Session)
            If idRol.HasValue AndAlso (idRol.Value = 1 OrElse idRol.Value = 20) Then
                If btnEdit IsNot Nothing Then
                    btnEdit.Visible = True
                End If
            End If

            ' ===== BOTÓN EDITAR =====
            If btnEdit IsNot Nothing Then
                btnEdit.Attributes("onclick") = String.Format("return ShowEditForm('{0}');", idVacuna)
            End If

            ' ===== BOTÓN VISUALIZAR =====
            If btnView IsNot Nothing Then
                btnView.Attributes("onclick") = String.Format("return ShowViewForm('{0}');", idVacuna)
            End If
        End If
    End Sub

    Protected Sub grdRazas_ItemCommand(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridCommandEventArgs) Handles grdRazas.ItemCommand
        If e.CommandName = "Inactivar" Then
            Try
                If e.CommandArgument IsNot Nothing AndAlso Not String.IsNullOrEmpty(e.CommandArgument.ToString()) Then
                    Dim idRepVac As Integer = Convert.ToInt32(e.CommandArgument)
                    Dim ObjParametros As New Collection
                    Dim Parametro As New CmdParametro("@idTblOpciones", idRepVac, SqlDbType.Int, False)
                    ObjParametros.Add(Parametro)
                    clsBitacora.GetSelect("[app].[SpRazas_Inactivar]", ObjParametros, Session, 96, True)
                    grdRazas.Rebind()
                Else
                    ' Opcional: Manejar el caso donde no hay ID
                End If

            Catch ex As Exception
                Dim mensaje As String = "Error al inactivar: " & ex.Message.Replace("'", "")
                ScriptManager.RegisterStartupScript(Me, Me.GetType(), "error", "alert('" & mensaje & "');", True)
            End Try
        End If
    End Sub

    Protected Sub grdRazas_ItemDataBound(sender As Object, e As Telerik.Web.UI.GridItemEventArgs) Handles grdRazas.ItemDataBound
        If TypeOf e.Item Is GridDataItem Then
            Dim item As GridDataItem = CType(e.Item, GridDataItem)
            Dim btnEdit As LinkButton = CType(item.FindControl("btnEdit"), LinkButton)
            Dim btnInactivar As LinkButton = CType(item.FindControl("BtnInactivar"), LinkButton)
            Dim drPermisos As DataRow = oRutinas.GetPermisos(Session, "frmAdminRazas")
            If drPermisos IsNot Nothing Then
                If btnEdit IsNot Nothing Then btnEdit.Visible = CBool(drPermisos("IndActualizar"))
                If btnInactivar IsNot Nothing Then btnInactivar.Visible = CBool(drPermisos("IndEliminar"))
            Else
                If btnEdit IsNot Nothing Then btnEdit.Visible = False
                If btnInactivar IsNot Nothing Then btnInactivar.Visible = False
            End If

        ElseIf TypeOf e.Item Is GridCommandItem Then
            Dim cmdItem As GridCommandItem = CType(e.Item, GridCommandItem)
            Dim btnInsertar As LinkButton = CType(cmdItem.FindControl("btnInsertar"), LinkButton)
            Dim drPermisoIns As DataRow = oRutinas.GetPermisos(Session, "frmAdminRazas")
            If btnInsertar IsNot Nothing Then
                If drPermisoIns IsNot Nothing Then
                    btnInsertar.Visible = CBool(drPermisoIns("IndInsertar"))
                Else
                    btnInsertar.Visible = False
                End If
            End If
        End If
    End Sub
#End Region

End Class