Imports System.Data.SqlClient
Imports System.ComponentModel

Public Class frmMitarbBerechtigungen
    Private UID As New cProgramme
    Private bindingSource1 As New BindingSource
    Dim sqlConn As New sqlConn
    Private dataAdapter As New SqlDataAdapter
    Private cma As CurrencyManager
    Public codename As String
    Private Grid_aktiv As Boolean = False
    Public progId As Integer = -1
    Public maId As Integer = -1

    Private Sub frmProgBerechtigungen_Load(sender As Object, e As EventArgs) Handles Me.Load
        If codename = "ADMIN" Then picIcon.Image = My.Resources.admin
        If codename = "UID" Then picIcon.Image = My.Resources.uid_ico
        If codename = "DISPO" Then picIcon.Image = My.Resources.dispo_ico
        If codename = "PARKZEIT" Then picIcon.Image = My.Resources.parkzeit
        If codename = "AVISO" Then picIcon.Image = My.Resources.Aviso

        lblTitel.Text = codename

        'dgvBer.DataSource = Me.bindingSource1
       
        Dim sql As String = " SELECT [ber_id] " &
                                  " ,[ber_name]" &
                                  " ,[ber_progId]," &
                                  " isnull((select top 1 ber_allowed from [ADMIN].[dbo].[tblBerProg] where [ber_id]=[tblBerProg].bepr_berId and (SELECT [prog_codename] FROM [ADMIN].[dbo].[tblProgramme] WHERE prog_id= [ber_progId])=[tblBerProg].bepr_programm AND [tblBerProg].bepr_userId=" & maId & "),0) as allowed" &
                                  " ,[ber_sicherheitsstufe]" &
                            " FROM [ADMIN].[dbo].[tblBerechtigungen]" &
                            " WHERE ber_progId = " & progId & ""
        ' initBinding("select * from tblBerechtigungen where ber_progId=" & progId)
        initBinding(sql)


    End Sub

    Private Sub initBinding(sql)
        ' dgvInitWait = True
        Dim sqlstr As String = sql
        ' dgvLKW.DataSource = bindingSDL
        Dim d As New SqlDataAdapter()
        Try
            Dim connectionString As String = My.Resources.connStringAdmin

            Me.dataAdapter = New SqlDataAdapter(sqlstr, connectionString)
        Catch ex As SqlException
            MessageBox.Show("Der Connection-String kann nicht verarbeitet werden. Wenden Sie sich an den Programmbetreuer.")
        End Try
        Dim commandBuilder As New SqlCommandBuilder(Me.dataAdapter)
        Dim table As New DataTable()
        Me.dataAdapter.Fill(table)
        '   table.Columns("KundenNr").DefaultValue = kdNr
        table.Locale = System.Globalization.CultureInfo.InvariantCulture
        'bindingSDL.Clear()
        dgvBer.DataBindings.Clear()
        dgvBer.DataSource = table
        'dgvInitWait = False
        Spalten_festlegen()
    End Sub
    Private Sub initDgv(ByVal sqlCommand As String)
        Grid_aktiv = False
        dgvBer.DataSource = Me.bindingSource1
        Dim d As New SqlDataAdapter()
        Try
            Dim connectionString As String = My.Resources.connStringAdmin
            dataAdapter = New SqlDataAdapter(sqlCommand, connectionString)
        Catch ex As SqlException
            MessageBox.Show("Der Connection-String kann nicht verarbeitet werden. Wenden Sie sich an den Programmbetreuer.")
        End Try
        Dim commandBuilder As New SqlCommandBuilder(dataAdapter)
        Dim table As New DataTable()
        table.Locale = System.Globalization.CultureInfo.InvariantCulture
        dataAdapter.Fill(table)
        bindingSource1.DataSource = table
        Spalten_festlegen()
        Grid_aktiv = True
    End Sub
    Private Sub CurrentChanged(ByVal sender As Object, ByVal e As EventArgs)
        MsgBox(DirectCast(sender, CurrencyManager).Position.ToString())
    End Sub

    Private Sub btnReload_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnReload.Click
        initDgv(dataAdapter.SelectCommand.CommandText)
    End Sub
    Private Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
        '   Me.dataAdapter.Update(CType(Me.bindingSource1.DataSource, DataTable))

        For Each r As DataGridViewRow In dgvBer.Rows
            sqlConn.setBerechtigungenNEU(maId, r.Cells(0).Value, codename, r.Cells(3).Value)
        Next
    End Sub

    Private Sub Spalten_festlegen()
        With dgvBer
            .RowTemplate.Height = 20
            .RowTemplate.DefaultCellStyle.Alignment = DataGridViewContentAlignment.TopLeft

            .AllowUserToOrderColumns = True

            .Columns(0).Visible = False     'ID nicht anzeigen

            .Columns(1).Width = 250
            .Columns(1).HeaderText = "Bezeichnung"

            .Columns(4).Width = 150
            .Columns(4).HeaderText = "Wert (alt)"

            .Columns(2).Visible = False
        End With
    End Sub


    ' Private listChangedHandler As ListChangedEventHandler
    ' Private positionChangedHandler As EventHandler
    ' Private dataManager As CurrencyManager

    'Overridable Sub bindingSource1_AddingNew(ByVal sender As Object, ByVal e As AddingNewEventArgs) Handles dgvBer.BindingContextChanged
    '   sender.tryDataBinding()
    'MyBase.OnBindingContextChanged(e)

    'MsgBox("test")
    'Dim dtaSet As DataSet = bindingSource1.DataSource
    'Dim dataTable As DataTable = dtaSet.Tables("tblBerechtigungen")
    'Dim DataRow As DataRow = dataTable.NewRow()
    'DataRow("ber_progId") = "1"

    'e.NewObject = DataRow

    ' End Sub

    Private Sub dgvBer_RowsAdded(sender As Object, e As DataGridViewRowsAddedEventArgs) Handles dgvBer.RowsAdded
        If Grid_aktiv Then dgvBer.CurrentRow.Cells(3).Value = progId
    End Sub
End Class
