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

#End Region

#Region "Carga de Datos y Mapeo"

    Private Sub CargarRepAutoAsisTec(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].[SpIVCSeleccionarRepAutoAsisTecPrincipal]", objparametros, Session, 96, True)
        If Ds IsNot Nothing AndAlso Ds.Tables.Count > 0 Then
            grdAutoAsisTec.DataSource = Ds.Tables(0)
        Else
            grdAutoAsisTec.DataSource = New DataTable()
        End If
    End Sub

#End Region

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

    Protected Sub grdAutoAsisTec_NeedDataSource(ByVal source As Object, ByVal e As Telerik.Web.UI.GridNeedDataSourceEventArgs) Handles grdAutoAsisTec.NeedDataSource
        CargarRepAutoAsisTec(ID)
    End Sub

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

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

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

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

    Protected Sub grdAutoAsisTec_ItemCommand(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridCommandEventArgs) Handles grdAutoAsisTec.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 idRepAuto As Integer = Convert.ToInt32(e.CommandArgument)
                    Dim ObjParametros As New Collection

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

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

                    ' 3. Refrescar la grilla
                    grdAutoAsisTec.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 grdAutoAsisTec_ItemDataBound(sender As Object, e As Telerik.Web.UI.GridItemEventArgs) Handles grdAutoAsisTec.ItemDataBound
        If TypeOf e.Item Is GridDataItem Then
            Dim item As GridDataItem = CType(e.Item, GridDataItem)
            Dim btnVerificar As LinkButton = CType(item.FindControl("btnVerificar"), LinkButton)
            Dim btnInactivar As LinkButton = CType(item.FindControl("BtnInactivar"), LinkButton)
            Dim drPermisos As DataRow = oRutinas.GetPermisos(Session, "frmAdminAutAsisTec")
            If drPermisos IsNot Nothing Then
                If btnVerificar IsNot Nothing Then btnVerificar.Visible = CBool(drPermisos("IndActualizar"))
                If btnInactivar IsNot Nothing Then btnInactivar.Visible = CBool(drPermisos("IndEliminar"))
            Else
                If btnVerificar IsNot Nothing Then btnVerificar.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, "frmAdminAutAsisTec")
            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
