Imports com.esendex.sdk.sent

Public Class usrCntlSMS

    Dim FahrerAbholAuftrag = False
    Dim MONAT As Date = CDate("01." & Now.Month & "." & Now.Year) '.AddMonths(-1)
    Dim srcTAG As Date = CDate(Now.ToShortDateString)
    Dim SQL As New VERAG_PROG_ALLGEMEIN.SQL
    Dim JAHR As Date = CDate("01.01." & Now.Year)


    Sub New()

        ' Dieser Aufruf ist für den Designer erforderlich.
        InitializeComponent()

        ' Fügen Sie Initialisierungen nach dem InitializeComponent()-Aufruf hinzu.

    End Sub


    Dim fullyPainted = False



    Protected Overrides Sub WndProc(ByRef m As Message)
        MyBase.WndProc(m)
        If m.Msg = 15 AndAlso Not fullyPainted Then
            fullyPainted = True

            setDGVOptions()

            '  initAllg()
            '  initFilter()
        End If
    End Sub



    Private Sub usrCntlSDLAbholung_Load(sender As Object, e As EventArgs) Handles Me.Load


        cboAbsedner.Items.Clear()
        Select Case VERAG_PROG_ALLGEMEIN.cAllgemein.FIRMA
            Case "IMEX"
                cboAbsedner.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("IMEX Spedition", "EX0272082"))
            Case "ATILLA"
                cboAbsedner.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("ATILLA Spedition", "EX0252746"))
            Case "UNISPED"
                cboAbsedner.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("UNISPED Spedition", "EX0300174"))
            Case "FRONTOFFICE"
                cboAbsedner.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("FRONT-OFFICE", "EX0300173"))
            Case "AMBAR"
                cboAbsedner.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("AMBAR", "EX0315223"))
            Case Else
                cboAbsedner.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("VERAG Spedition AG", "EX0252513"))
                cboAbsedner.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("VERAG 360", "EX0252747"))
                If VERAG_PROG_ALLGEMEIN.cBerechtignunen.CHECK_BERECHTIGUNG_bool("ADMINFUNCTIONS", "SDL") Then
                    cboAbsedner.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("ATILLA Spedition", "EX0252746"))
                    cboAbsedner.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("IMEX Spedition", "EX0272082"))
                End If
                cboAbsedner.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("UNISPED Spedition", "EX0300174"))
                cboAbsedner.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("FRONT-OFFICE", "EX0300173"))
                cboAbsedner.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("AMBAR", "EX0315223"))
        End Select

        cboAbsedner.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("WHATSAPP", "EX0341154"))

        Select Case VERAG_PROG_ALLGEMEIN.cAllgemein.FIRMA
            Case "ATILLA" : cboAbsedner.changeItem("EX0252746")
            Case "IMEX" : cboAbsedner.changeItem("EX0272082")
            Case "UNISPED" : cboAbsedner.changeItem("EX0300174")
            Case "FRONTOFFICE" : cboAbsedner.changeItem("EX0341154")
            Case "AMBAR" : cboAbsedner.changeItem("EX0315223")
            Case Else
                Select Case VERAG_PROG_ALLGEMEIN.cAllgemein.MITARBEITER.mit_abteilung
                    Case "MDM" : cboAbsedner.changeItem("EX0252747")
                    Case Else : cboAbsedner.changeItem("EX0252513") 'VERAG
                End Select
        End Select

        If VERAG_PROG_ALLGEMEIN.cBerechtignunen.CHECK_BERECHTIGUNG_bool("ADMINFUNCTIONS", "SDL") Then
            cboAbsedner.Enabled = True
        Else
            cboAbsedner.Enabled = False
        End If

        txtAbfertDat.Text = Now.AddMonths(-6).ToShortDateString 'CDate("01." & Now.ToString("MM.yyyy")).AddMonths(-1).ToShortDateString
        txtAbfertDatBis.Text = Now.ToShortDateString

        btnMonat.Text = MONAT.ToString("MMMM")
        btnJahr.Text = JAHR.Year

        cboSachbearbeiter.fillWithSQL("SELECT [mit_id],[mit_vname] + ' '+ [mit_nname] as Name " &
                                       " FROM [ADMIN].[dbo].[tblMitarbeiter] WHERE mit_niederlassung IN ('SUB','Suben')  AND mit_abteilung='ZOLL' AND [mit_gekuendigt]=0   order by name", False, "ADMIN", True)
        '    btnMonat.PerformClick()
        '   If VERAG_PROG_ALLGEMEIN.cAllgemein.MITARBEITER.mit_abteilung <> "MDM" And VERAG_PROG_ALLGEMEIN.cAllgemein.MITARBEITER.mit_abteilung <> "EDV" Then
        'cbxFahrerAbholungsAuftrag. 
        '   End If

        btnSuche.PerformClick()

        ' If VERAG_PROG_ALLGEMEIN.cBerechtignunen.CHECK_BERECHTIGUNG_bool("BH_Vorauskasse_buchen", "SDL") Then
        'pnlBH.Visible = True
        ' End If

        txtHandyNr.Focus()

    End Sub


    Private Sub DateTimePicker1_ValueChanged(sender As Object, e As EventArgs) Handles DateTimePicker1.ValueChanged
        txtAbfertDat._value = CDate(sender.value).ToShortDateString
    End Sub

    Private Sub DateTimePicker2_ValueChanged(sender As Object, e As EventArgs) Handles DateTimePicker2.ValueChanged
        txtAbfertDatBis._value = CDate(sender.value).ToShortDateString
    End Sub

    Sub initBtns(MonatJahr)
        btnMonat.Text = MONAT.ToString("MMMM")
        btnJahr.Text = JAHR.Year
        Select Case MonatJahr
            Case "Monat"
                DateTimePicker1.Value = MONAT
                DateTimePicker2.Value = MONAT.AddMonths(1).AddDays(-1)
            Case "Jahr"
                '  If cbxGeschaftsjahr.Checked Then
                'DateTimePicker1.Value = JAHR.AddMonths(1)
                ' DateTimePicker2.Value = JAHR.AddYears(1).AddMonths(1).AddDays(-1)
                ' Else
                DateTimePicker1.Value = JAHR
                DateTimePicker2.Value = JAHR.AddYears(1).AddDays(-1)
                'End If
            Case "Tag"
                '  If cbxGeschaftsjahr.Checked Then
                'DateTimePicker1.Value = JAHR.AddMonths(1)
                ' DateTimePicker2.Value = JAHR.AddYears(1).AddMonths(1).AddDays(-1)
                ' Else
                DateTimePicker1.Value = srcTAG
                DateTimePicker2.Value = srcTAG
                If srcTAG = Now.ToShortDateString Then
                    Button5.Text = "Heute"
                ElseIf srcTAG = Now.AddDays(-1).ToShortDateString Then
                    Button5.Text = "Gestern"
                ElseIf srcTAG = Now.AddDays(-2).ToShortDateString Then
                    Button5.Text = "Vorgestern"
                Else
                    Button5.Text = srcTAG.ToString("dd.MM")
                End If

                'End If
            Case ""
                DateTimePicker1.Value = ("01." & Now.Month & "." & Now.Year)
                DateTimePicker2.Value = Now
        End Select
        btnSuche.PerformClick()
    End Sub


    Private Sub btnSuche_Click(sender As Object, e As EventArgs) Handles btnSuche.Click
        Me.Cursor = Cursors.WaitCursor

        Dim TopMax = If(IsNumeric(txtMaxSrch.Text), " TOP " & txtMaxSrch.Text, "")
        Dim sqlstr As String = ""


        sqlstr &= " SELECT " & TopMax & " [sms_id],[sms_Datum],[sms_Account], sms_Vorwahl + '' + [sms_Handynummer] as Nummer,[sms_Nachricht],[sms_Sachbearbeiter],CASE WHEN sms_Status IN (2,10) then 'OK' when sms_Status IN (3,4,5,6,8,11) THEN 'FEHLER' when sms_Status IN (-1,0,1,7,9) THEN 'WARTEND' ELSE '' END as Status,sms_BatchId,sms_MessageIds FROM [tblSMS]  WHERE 1=1 "

        With MyDatagridview1
            '  .DataSource = myTable
            .SET_SQL(sqlstr & getWhere() & "  order by sms_Datum desc, sms_id", "FMZOLL", getList)
            .LOAD()
            setDGVOptions()
        End With
        showDGV()

        Me.Cursor = Cursors.Default

    End Sub


    Function getWhere() As String
        Dim sqlstr = ""

        If txtAbfertDat.Text <> "" Then
            If txtAbfertDatBis.Text <> "" Then
                sqlstr &= " AND ([sms_Datum] BETWEEN @DatVon AND @DatBis ) "
            Else
                sqlstr &= " AND ([sms_Datum] BETWEEN @DatVon AND @DatVon ) "
            End If
        End If


        If txtHandyNr.Text <> "" Then sqlstr &= " AND (sms_Vorwahl + '' + [sms_Handynummer]) LIKE @HandyNr "
        If cboSachbearbeiter._value <> "" Then sqlstr &= " AND sms_SachbearbeiterId LIKE @Personalnummer  "
        If cboAbsedner._value <> "" Then sqlstr &= " AND [sms_Account] LIKE @Absender  "



        Return sqlstr
    End Function

    Function getList() As List(Of VERAG_PROG_ALLGEMEIN.SQLVariable)

        Dim list As New List(Of VERAG_PROG_ALLGEMEIN.SQLVariable)

        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("DatVon", txtAbfertDat.Text & " 00:00:00"))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("DatBis", txtAbfertDatBis.Text & " 23:59:59"))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Absender", cboAbsedner._value))

        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Personalnummer", cboSachbearbeiter._value))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("HandyNr", ("%" & txtHandyNr.Text & "%").Replace("*", "%").Replace("/", "").Replace(" ", "")))

        Return list

    End Function

    Sub showDGV()

        With MyDatagridview1

            If .ColumnCount = 0 Then Me.Cursor = Cursors.Default : Exit Sub
            .RowHeadersVisible = False
            .ReadOnly = True
            .SelectionMode = DataGridViewSelectionMode.FullRowSelect
            .MultiSelect = False

            .Columns("sms_id").Visible = False
            .Columns("sms_BatchId").Visible = False
            .Columns("sms_MessageIds").Visible = False

            .Columns("sms_Datum").Width = 100
            .Columns("sms_Datum").HeaderText = "Datum"
            .Columns("sms_Datum").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter


            .Columns("sms_Sachbearbeiter").Width = 100
            .Columns("sms_Sachbearbeiter").HeaderText = "Erfassung SB"

            .Columns("sms_Account").Visible = False

            .Columns("Nummer").Width = 150
            .Columns("Nummer").HeaderText = "Handy Nummer"


            .Columns("sms_Nachricht").MinimumWidth = 100
            .Columns("sms_Nachricht").AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
            .Columns("sms_Nachricht").HeaderText = "Nachricht"




            '  lblErgebnis.Text = "Ergebnise: " & .RowCount
        End With
    End Sub


    Private Sub FlatButton7_Click(sender As Object, e As EventArgs) Handles FlatButton7.Click
        MyDatagridview1.RELOAD()
        setDGVOptions()
    End Sub

    Sub setDGVOptions()
        Try
            Exit Sub
            With MyDatagridview1
                For Each r As DataGridViewRow In .Rows
                    If r.Cells("sdlAb_Storniert").Value IsNot DBNull.Value AndAlso r.Cells("sdlAb_Storniert").Value Then
                        r.DefaultCellStyle.ForeColor = Color.Red
                        r.DefaultCellStyle.SelectionForeColor = Color.Red
                        '    r.DefaultCellStyle.SelectionBackColor = Color.White
                    ElseIf r.Cells("sdlAb_Abgeholt").Value IsNot DBNull.Value AndAlso r.Cells("sdlAb_Abgeholt").Value Then
                        r.DefaultCellStyle.ForeColor = Color.DarkGreen
                        r.DefaultCellStyle.SelectionForeColor = Color.DarkGreen
                        '  r.DefaultCellStyle.SelectionBackColor = Color.White
                    End If
                Next
            End With
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
    Private Sub btnExcel_Click(sender As Object, e As EventArgs) Handles btnExcel.Click
        Me.Cursor = Cursors.WaitCursor
        cProgramFunctions.genExcelFromDGV_NEW(MyDatagridview1, False)
        Me.Cursor = Cursors.Default
    End Sub



    Private Sub MyDatagridview1_CellMouseClick(sender As Object, e As DataGridViewCellMouseEventArgs) Handles MyDatagridview1.CellMouseClick
        Exit Sub
        If e.Button = Windows.Forms.MouseButtons.Right Then
            MyDatagridview1.ClearSelection()
            MyDatagridview1.Rows(e.RowIndex).Selected = True
            ' MyDatagridview1.CurrentCell = MyDatagridview1.Rows(e.RowIndex).Cells("sms_Handynummer")
            MyDatagridview1.Rows(e.RowIndex).Selected = True
            cntxtOptionen.Show(System.Windows.Forms.Cursor.Position)
        End If
    End Sub

    Private Sub MyDatagridview1_SelectionChanged(sender As Object, e As EventArgs) Handles MyDatagridview1.SelectionChanged

        If MyDatagridview1.SelectedRows.Count > 0 Then

        End If
    End Sub



    Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
        srcTAG = srcTAG.AddDays(-1)
        initBtns("Tag")
    End Sub

    Private Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.Click
        srcTAG = srcTAG.AddDays(1)
        initBtns("Tag")
    End Sub

    Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
        initBtns("Tag")
    End Sub

    Private Sub btnMonat_Click(sender As Object, e As EventArgs) Handles btnMonat.Click
        initBtns("Monat")
    End Sub


    Private Sub btnJahr_Click(sender As Object, e As EventArgs) Handles btnJahr.Click
        initBtns("Jahr")
    End Sub

    Private Sub btnBisMonat_Click(sender As Object, e As EventArgs) Handles btnBisMonat.Click
        MONAT = MONAT.AddMonths(1)
        initBtns("Monat")
    End Sub

    Private Sub btnVonMonat_Click(sender As Object, e As EventArgs) Handles btnVonMonat.Click
        MONAT = MONAT.AddMonths(-1)
        initBtns("Monat")
    End Sub

    Private Sub btnBisJahr_Click(sender As Object, e As EventArgs) Handles btnBisJahr.Click
        JAHR = JAHR.AddYears(1)
        initBtns("Jahr")
    End Sub

    Private Sub btnVonJahr_Click(sender As Object, e As EventArgs) Handles btnVonJahr.Click
        JAHR = JAHR.AddYears(-1)
        initBtns("Jahr")
    End Sub '



    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Dim f As New frmSendSMS
        If f.ShowDialog(Me) = DialogResult.OK Then
            MyDatagridview1.RELOAD()
        End If
    End Sub


    Private Sub txtAuftragsnr_KeyDown(sender As Object, e As KeyEventArgs) Handles txtHandyNr.KeyDown
        If e.KeyCode = Keys.Return Then
            btnSuche.PerformClick()
        End If
    End Sub

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

        If MyDatagridview1.SelectedRows.Count > 0 Then
            Dim SMS = New VERAG_PROG_ALLGEMEIN.cSMS(MyDatagridview1.SelectedRows(0).Cells("sms_id").Value)
            Select Case SMS.sms_art
                Case "WHATSAPP" : MsgBox("WhatsApp-Status: " & SMS.checkWhatsAppStatus)
                Case "SMS" : MsgBox("SMS-Status: " & SMS.checkSMSStatus)
            End Select

            'Exit Sub

            'Select Case SMS.checkSMSStatus
            '    Case <= 0 : MsgBox("SMS-Status: Wartend...")
            '    Case 1 : MsgBox("SMS-Status: Wartend...")
            '    Case 2 : MsgBox("SMS-Status: Gesendet")
            '    Case 3 : MsgBox("SMS-Status: Empfangen")
            '    Case 4 : MsgBox("SMS-Status: Fehler")
            'End Select
        End If
    End Sub

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click

        If MyDatagridview1.SelectedRows.Count > 0 Then
            Dim SMS = New VERAG_PROG_ALLGEMEIN.cSMS(MyDatagridview1.SelectedRows(0).Cells("sms_id").Value)
            If If(SMS.sms_Uris, "") <> "" Then
                Process.Start(SMS.sms_Uris)
            End If

        End If
    End Sub
End Class

