Imports DevExpress.Web
Imports System.Data.SqlClient

Public Class ucGrado
    Inherits ucBase

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        RegisterCustomMessage(gridGradosSAPT)
    End Sub

    Protected Sub gridExperiencia_InitNewRow(sender As Object, e As Data.ASPxDataInitNewRowEventArgs) Handles gridExperiencia.InitNewRow
        e.NewValues("EXP_CODIGO") = "000"
    End Sub

    Protected Shared Sub FillEspecialidadCombo(ByVal cmb As ASPxComboBox, ByVal grado As String)
        If String.IsNullOrEmpty(grado) Then
            Return
        End If

        Dim especialidades As List(Of clEspecialidad) = GetEspecialidades(grado)


        cmb.Items.Clear()
        For Each especialidad As clEspecialidad In especialidades
            cmb.Items.Add(especialidad.Nombre, especialidad.Value)
        Next especialidad
        If cmb.Items.Count = 1 Then
            cmb.SelectedIndex = 0
        End If
    End Sub

    Private Shared Function GetEspecialidades(ByVal grado As String) As List(Of clEspecialidad)
        Dim ListaEspecialidades As New List(Of clEspecialidad)
        Using cnn As New SqlConnection(ConfigurationManager.ConnectionStrings("cnn").ConnectionString)
            cnn.Open()
            Dim cmd As New SqlCommand("select e.ESP_CODIGO as codigo, e.ESP_NOMBRE as nombre from GOES.dbo.RHM_ORG_GRADOESPECIALIDAD as ge, GOES.dbo.RHM_ORG_ESPECIALIDAD as e where gra_codigo=@varGradoCodigo AND ge.ESP_CODIGO = e.ESP_CODIGO order by ASCII(lower(ESP_NOMBRE)), ESP_NOMBRE", cnn)
            cmd.Parameters.Add("@varGradoCodigo", SqlDbType.VarChar).Value = grado

            Dim dr As SqlDataReader = cmd.ExecuteReader
            Dim oEspecialidad As clEspecialidad
            While dr.Read
                oEspecialidad = New clEspecialidad(dr("codigo"), dr("nombre"))
                ListaEspecialidades.Add(oEspecialidad)

            End While
            dr.Close()
            cnn.Close()
        End Using

        Return ListaEspecialidades
    End Function

    Private Sub cmbEspecialidad_OnCallback(ByVal source As Object, ByVal e As CallbackEventArgsBase)
        FillEspecialidadCombo(TryCast(source, ASPxComboBox), e.Parameter)
    End Sub

    Protected Sub gridGradosSAPT_CellEditorInitialize(sender As Object, e As ASPxGridViewEditorEventArgs) Handles gridGradosSAPT.CellEditorInitialize
        If e.Column.FieldName = "GRA_CODIGO" Then
            e.Editor.Focus()
        End If

        If (e.Column.FieldName = "GRA_CODIGO") And Not TryCast(sender, ASPxGridView).IsNewRowEditing Then
            e.Editor.ClientEnabled = False
        End If
    End Sub

    Protected Sub gridGradosSAPT_DataBound(sender As Object, e As EventArgs) Handles gridGradosSAPT.DataBound
        gridGradosSAPT.JSProperties("cpSummary") = gridGradosSAPT.GetTotalSummaryValue(gridGradosSAPT.TotalSummary("ContEsp"))
    End Sub

    Protected Sub gridGradosSAPT_RowDeleting(sender As Object, e As Data.ASPxDataDeletingEventArgs) Handles gridGradosSAPT.RowDeleting
        If dSAPT_GradoBusinessObject.CountEspByGrado(e.Values("GRA_CODIGO"), Session("puesto").ToString) > 0 Then
            CustomMessage("Este grado tiene especialidades asociadas al puesto.<br><p>Elimine primero las especialidades asociadas y luego el grado.</p>", "Eliminación de grado", gridGradosSAPT)
            e.Cancel = True
        End If
    End Sub

    Protected Sub gridEspecialidad_CellEditorInitialize(sender As Object, e As ASPxGridViewEditorEventArgs) Handles gridEspecialidad.CellEditorInitialize
        If (e.Column.FieldName = "GRA_CODIGO" Or e.Column.FieldName = "ESP_CODIGO") And Not TryCast(sender, ASPxGridView).IsNewRowEditing Then
            e.Editor.ClientEnabled = False
        End If
        If e.Column.FieldName = "GRA_CODIGO" Then
            e.Editor.Focus()
        End If
        If (Not gridEspecialidad.IsEditing) OrElse e.Column.FieldName <> "ESP_CODIGO" Then
            Return
        End If
        Dim combo As ASPxComboBox = TryCast(e.Editor, ASPxComboBox)
        If Not gridEspecialidad.IsNewRowEditing Then
            Dim val As Object = gridEspecialidad.GetRowValuesByKeyValue(e.KeyValue, "GRA_CODIGO")
            If val Is DBNull.Value Then
                Return
            End If
            Dim grado As String = CStr(val)
            FillEspecialidadCombo(combo, grado)
        End If
        AddHandler combo.Callback, AddressOf cmbEspecialidad_OnCallback
    End Sub

    Protected Sub gridEspecialidad_RowDeleting(sender As Object, e As Data.ASPxDataDeletingEventArgs) Handles gridEspecialidad.RowDeleting
        gridEspecialidad.JSProperties("cpUpdate") = True
    End Sub

    Protected Sub gridEspecialidad_RowUpdating(sender As Object, e As Data.ASPxDataUpdatingEventArgs) Handles gridEspecialidad.RowUpdating
        gridEspecialidad.JSProperties("cpUpdate") = True
    End Sub

    Protected Sub gridEspecialidad_RowInserting(sender As Object, e As Data.ASPxDataInsertingEventArgs) Handles gridEspecialidad.RowInserting
        'e.NewValues("COD_GRAESP") = e.NewValues("GRA_CODIGO") & "_" & e.NewValues("ESP_CODIGO")
        gridEspecialidad.JSProperties("cpUpdate") = True
    End Sub

    Protected Sub gridGradosSAPT_RowInserting(sender As Object, e As Data.ASPxDataInsertingEventArgs) Handles gridGradosSAPT.RowInserting
        gridGradosSAPT.JSProperties("cpInserted") = True
    End Sub

    Protected Sub gridIdioma_RowInserting(sender As Object, e As Data.ASPxDataInsertingEventArgs) Handles gridIdioma.RowInserting
        gridIdioma.JSProperties("cpInserted") = True
    End Sub
End Class