Public Class frmModaltransStammreferenz

    Property STAMMREF = Nothing
    Property STAMMREF_DYID As Integer = -1
    Dim verfahren As String
    Dim land As String
    Public nl As String
    Dim Kdnr As Object = Nothing
    Property sentToDakosy As Boolean = False
    Dim freetoselect = False
    Dim SQL As New VERAG_PROG_ALLGEMEIN.SQL

    Dim SelectNL = False
    Dim FIRMA_DY = VERAG_PROG_ALLGEMEIN.cAllgemein.FIRMA

    Sub New(land, verfahren, freetoselect, Optional Kdnr = Nothing, Optional nl = Nothing, Optional FIRMA_DY = "")

        ' Dieser Aufruf ist für den Designer erforderlich.
        InitializeComponent()
        Me.verfahren = verfahren
        Me.land = land
        Me.freetoselect = freetoselect
        Me.Kdnr = Kdnr
        Me.nl = nl

        If FIRMA_DY <> "" Then Me.FIRMA_DY = FIRMA_DY
        ' Fügen Sie Initialisierungen nach dem InitializeComponent()-Aufruf hinzu.

    End Sub

    Private Sub frmDakosyStammreferenz_Load(sender As Object, e As EventArgs) Handles Me.Load
        SelectNL = False
        Select Case FIRMA_DY
            Case "VERAG" : txtDyMandant.Text = "VERG"
                txtDyNiederlassung.Text = VERAG_PROG_ALLGEMEIN.cAllgemein.NIEDERLASSUNG
            Case "ATILLA" : txtDyMandant.Text = "ATIL" : txtDyNiederlassung.Text = "SUW"
            Case "IMEX" : txtDyMandant.Text = "IMEX" : txtDyNiederlassung.Text = If(nl <> "", nl, "IME")
            Case "UNISPED" : txtDyMandant.Text = "UNAT" : txtDyNiederlassung.Text = "ATSP"
            Case "AMBAR" : txtDyMandant.Text = "ATIL" : txtDyNiederlassung.Text = "AMBWO" : SelectNL = True
        End Select

        cboVerfahren.Items.Clear()
        cboVerfahren.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("NCTS - Declaration", "ncts"))
        cboVerfahren.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("EXPORT - Declaration", "export"))
        cboVerfahren.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("IMPORT - Declaration", "import"))
        cboVerfahren.changeItem(verfahren)

        cboLand.Items.Clear()
        cboLand.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("UK", "UK"))
        cboLand.changeItem(land)


        'If Not VERAG_PROG_ALLGEMEIN.cBerechtignunen.CHECK_BERECHTIGUNG_bool("ZOLL_ModaltransZA_Senden", "AVISO") Then
        '    Button1.Enabled = False
        'End If
        'If Not VERAG_PROG_ALLGEMEIN.cBerechtignunen.CHECK_BERECHTIGUNG_bool("ZOLL_Modaltrans_AVISO_Edit", "AVISO") Then
        '    btnOK.Enabled = False
        'End If

        If Not freetoselect Then
            cboVerfahren.Enabled = False
            cboLand.Enabled = False
        End If

        If Kdnr IsNot Nothing AndAlso IsNumeric(Kdnr) Then
            txtSearch.Text = Kdnr
        End If
    End Sub

    Private Sub cboVerfahren_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cboVerfahren.SelectedIndexChanged, cboLand.SelectedIndexChanged
        initDGV()
    End Sub

    Sub initDGV()
        With dgvStammref
            .Columns.Clear()
            If cboLand._value <> "" And cboVerfahren._value <> "" Then

                Dim where = " AND kdNr is null"

                .RowTemplate.Height = 30
                .RowTemplate.DefaultCellStyle.Font = New Font(Me.Font.FontFamily, 10)
                .DataSource = SQL.loadDgvBySql("SELECT * FROM [tblOregonNCTS_ReferenceTemplate]  LEFT JOIN Adressen ON AdressenNr=kdNr  WHERE country='" & cboLand._value & "' AND declaration='" & cboVerfahren._value & "' and mandant='" & txtDyMandant.Text & "' " & If(SelectNL, " AND niederlassung='" & txtDyNiederlassung.Text & "' ", "") & "  and visible=1 " & where & " order by descr ", "FMZOLL")
                .Columns("Id").Visible = False
                .Columns("reference").Visible = False
                .Columns("standard").Visible = False
                .Columns("niederlassung").Visible = False
                .Columns("mandant").Visible = False
                .Columns("reference").HeaderText = "Stammreferenz"
                .Columns("reference").Width = 200
                .Columns("descr").HeaderText = "Bezeichnung"
                .Columns("descr").AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
                .ClearSelection()


            End If
        End With
        With MyDatagridview1
            .Columns.Clear()
            If cboLand._value <> "" And cboVerfahren._value <> "" Then

                Dim where = " AND  kdNr is not null "
                If txtSearch.Text <> "" Then
                    where &= " and (kdNr LIKE '" & txtSearch.Text & "%' OR Ordnungsbegriff LIKE '" & txtSearch.Text & "%') "
                End If

                .RowTemplate.Height = 30
                .RowTemplate.DefaultCellStyle.Font = New Font(Me.Font.FontFamily, 10)
                .DataSource = SQL.loadDgvBySql("SELECT * FROM [tblOregonNCTS_ReferenceTemplate]  LEFT JOIN Adressen ON AdressenNr=KdNr  WHERE country='" & cboLand._value & "' AND declaration='" & cboVerfahren._value & "' and mandant='" & txtDyMandant.Text & "'  " & If(SelectNL, " AND niederlassung='" & txtDyNiederlassung.Text & "' ", "") & " and visible=1 " & where & " order by KdNr,descr ", "FMZOLL")
                .Columns("Id").Visible = False
                .Columns("reference").Visible = False
                .Columns("standard").Visible = False
                .Columns("reference").HeaderText = "Stammreferenz"
                .Columns("reference").Width = 200
                .Columns("descr").HeaderText = "Bezeichnung"
                .Columns("descr").AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill

                .Columns("kdNr").HeaderText = "KundenNr"
                .Columns("kdNr").Width = 60
                .Columns("Ordnungsbegriff").HeaderText = "Firma"
                .Columns("Ordnungsbegriff").Width = 200
                .Columns("niederlassung").HeaderText = "NL"
                .Columns("niederlassung").Width = 50
                .ClearSelection()


            End If
        End With
    End Sub



    Sub openDY(sentToModaltrans As Boolean)

        If dgvStammref.SelectedRows.Count > 0 Or MyDatagridview1.SelectedRows.Count > 0 Then
            Dim dgv As DataGridView = Nothing
            If dgvStammref.SelectedRows.Count > 0 Then dgv = dgvStammref
            If MyDatagridview1.SelectedRows.Count > 0 Then dgv = MyDatagridview1

            If dgv.SelectedRows(0).Cells("reference").Value <> "" Then
                STAMMREF = dgv.SelectedRows(0).Cells("reference").Value
                STAMMREF_DYID = dgv.SelectedRows(0).Cells("Id").Value
                Me.sentToDakosy = sentToDakosy
                Me.nl = dgv.SelectedRows(0).Cells("niederlassung").Value
                '  Me.txtDyMandant = dgv.SelectedRows(0).Cells("dySr_dy_Mandant").Value
                'STAMMREF_DYID = (New VERAG_PROG_ALLGEMEIN.SQL).getValueTxtBySql("SELECT isnull([dySr_dyId],-1)  FROM [tblDakosy_Stammreferenzen] WHERE dySr_Stammreferenz='" & MyDatagridview1.SelectedRows(0).Cells("dySr_Stammreferenz").Value & "' ", "FMZOLL")
                Exit Sub
            End If
        Else
            For Each r As DataGridViewRow In dgvStammref.Rows
                If r.Cells("standard").Value = True Then
                    STAMMREF = r.Cells("reference").Value
                    STAMMREF_DYID = r.Cells("Id").Value
                    Me.sentToDakosy = sentToDakosy
                    Exit Sub
                End If
            Next
        End If
        Me.sentToDakosy = sentToDakosy 'Falls keine STAMMREF
    End Sub

    Private Sub btnOK_Click(sender As Object, e As EventArgs) Handles btnOK.Click
        openDY(False)
    End Sub

    Private Sub btnOK_Click2(sender As Object, e As EventArgs) Handles Button1.Click

        If vbYes = MsgBox("Sollen die Daten abgeschickt werden?", vbYesNoCancel) Then
            openDY(True)
        End If
    End Sub



    Private Sub dgvStammref_SelectionChanged(sender As Object, e As EventArgs) Handles dgvStammref.SelectionChanged
        If dgvStammref.SelectedRows.Count > 0 Then
            Label3.Text = dgvStammref.SelectedRows(0).Cells("reference").Value
        Else
            Label3.Text = ""
        End If
        MyDatagridview1.ClearSelection()
    End Sub



    Private Sub MyDatagridview1_SelectionChanged(sender As Object, e As EventArgs) Handles MyDatagridview1.SelectionChanged
        If MyDatagridview1.SelectedRows.Count > 0 Then
            Label3.Text = MyDatagridview1.SelectedRows(0).Cells("reference").Value
        Else
            Label3.Text = ""
        End If
        dgvStammref.ClearSelection()
    End Sub


    Private Sub dgvStammref_DoubleClick(sender As Object, e As Object) Handles dgvStammref.CellDoubleClick, dgvStammref.CellContentDoubleClick
        If btnOK.Enabled Then
            btnOK.PerformClick()
        Else
            Button1.PerformClick()
        End If
    End Sub

    Private Sub MyTextBox1_TextChanged(sender As Object, e As EventArgs) Handles txtSearch.TextChanged
        initDGV()
    End Sub

    Private Sub MyDatagridview1_DoubleClick(sender As Object, e As EventArgs) Handles MyDatagridview1.CellDoubleClick, MyDatagridview1.CellContentDoubleClick
        If btnOK.Enabled Then
            btnOK.PerformClick()
        Else
            Button1.PerformClick()
        End If
    End Sub


    Private Sub frmDakosyStammreferenz_Shown(sender As Object, e As EventArgs) Handles Me.Shown
        txtSearch.Focus()
    End Sub

    Private Sub MyDatagridview1_DoubleClick(sender As Object, e As DataGridViewCellEventArgs) Handles MyDatagridview1.CellDoubleClick, MyDatagridview1.CellContentDoubleClick

    End Sub
End Class