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 ETOZ_frmAdminReporVacunaAntirrabica2026
    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 RepVacunacion

#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
        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, "AdminReporVacunaAntirrabica2026") = 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"
                    grdRepVacAnti.MasterTableView.SortExpressions.Clear()
                    grdRepVacAnti.MasterTableView.GroupByExpressions.Clear()
                    grdRepVacAnti.Rebind()
                Case "RebindAndNavigate"
                    grdRepVacAnti.MasterTableView.SortExpressions.Clear()
                    grdRepVacAnti.MasterTableView.GroupByExpressions.Clear()
                    grdRepVacAnti.MasterTableView.CurrentPageIndex = grdRepVacAnti.MasterTableView.PageCount - 1
                    grdRepVacAnti.Rebind()
            End Select
        End If
    End Sub

#End Region

#Region "Carga de Datos y Mapeo"

    Private Sub CargarRepJorVac(ByVal id As String)

        Dim Ds As New DataSet
        Dim objparametros As New Collection
        Dim idUsuario As Integer = Convert.ToInt32(Session("IdUsuario"))
        Dim idRol As Integer? = oRutinas.GetRoli(Session("UserRoles"), Session)
        objparametros.Add(New CmdParametro("@IdUsuario", idUsuario, SqlDbType.Int, False))
        If idRol.HasValue Then
            objparametros.Add(New CmdParametro("@IdRol", idRol.Value, SqlDbType.Int, False))
        Else
            objparametros.Add(New CmdParametro("@IdRol", 0, SqlDbType.Int, False))
        End If
        Ds = clsBitacora.GetSelect("[app].[SpIVCSeleccionarRepJornadaVacuna]", objparametros, Session, 96, True)
        If Ds IsNot Nothing AndAlso Ds.Tables.Count > 0 Then
            grdRepVacAnti.DataSource = Ds.Tables(0)
        Else
            grdRepVacAnti.DataSource = New DataTable()
        End If
    End Sub

#End Region

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

    Protected Sub grdRepVacAnti_NeedDataSource(ByVal source As Object, ByVal e As Telerik.Web.UI.GridNeedDataSourceEventArgs) Handles grdRepVacAnti.NeedDataSource
        CargarRepJorVac(ID)
    End Sub

    Protected Sub grdRepVacAnti_ItemCreated(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridItemEventArgs) Handles grdRepVacAnti.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("IdtblRepVacAnt").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 ShowEditFormRepVac('{0}');", idVacuna)
            End If

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

    Protected Sub grdRepVacAnti_ItemCommand(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridCommandEventArgs) Handles grdRepVacAnti.ItemCommand
        If e.CommandName = "Inactivar" Then
            Try
                ' 1. Validar que el CommandArgument no sea nulo o vacío
                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

                    ' CORRECCIÓN: Pasar idRepVac en lugar de "e"
                    Dim Parametro As New CmdParametro("@IdtblRepVacAnt", idRepVac, SqlDbType.Int, False)
                    ObjParametros.Add(Parametro)

                    ' 2. Ejecutar el procedimiento
                    clsBitacora.GetSelect("[app].[SpRepVacAnt_Inactivar]", ObjParametros, Session, 96, True)

                    ' 3. Refrescar la grilla
                    grdRepVacAnti.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 grdRepVacAnti_ItemDataBound(sender As Object, e As Telerik.Web.UI.GridItemEventArgs) Handles grdRepVacAnti.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, "AdminReporVacunaAntirrabica2026")
            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, "AdminReporVacunaAntirrabica2026")
            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
