Imports System.Reflection
Imports System.IO.File
Imports Microsoft.Office.Interop.Outlook

Public Class frmMenu

    Public menu As cMenu = Nothing


    Private Sub frmMenu_Load(sender As Object, e As EventArgs) Handles Me.Load
        '    If menu IsNot Nothing Then FillBoxes()
        panDebug.Visible = cListe.Debug
        FillDesigns()
        FillBoxes()
        Dim usrcntrl As New usrcntlSpeisen : usrcntrl.Dock = DockStyle.Fill : PanSpeise.Controls.Add(usrcntrl)

    End Sub

    Function FillDesigns()
        coboMenuArt.Items.Clear()
        coboMenuArt.Items.Add("Standard")
        For Each meh As String() In cListe.Hintergrund
            'MsgBox(meh(0))
            coboMenuArt.Items.Add(meh(0))
        Next
    End Function

    Function FillBoxes(Optional ByVal FillDate As Boolean = True)
        txtMenuTitel.Text = menu.MenuTitel
        If FillDate = False Then txtMenuTitel.Text &= cTools.DateToWeek(dtpbis.Value)
        chkPrint.Checked = menu.MenuPrint

        If FillDate = True Then dtpVon.Value = menu.MenuDateVon : dtpbis.Value = menu.MenuDateBis

        txtDetail1.Text = menu.MenuDetail1
        txtDetail2.Text = menu.MenuDetail2
        txtDetail3.Text = menu.MenuDetail3

        txtSuppe.Text = menu.MenuSuppe
        chkPrintSuppe.Checked = menu.MenuPrintSuppe

        txtMontag.Text = menu.MenuEssenMo
        txtDienstag.Text = menu.MenuEssenDi
        txtMittwoch.Text = menu.MenuEssenMi
        txtDonnerstag.Text = menu.MenuEssenDo
        txtFreitag.Text = menu.MenuEssenFr

        coboMenuArt.SelectedItem = menu.MenuArt
    End Function

    Function FillMenu()
        menu.MenuTitel = SQLTools.hochkomma(txtMenuTitel.Text)
        menu.MenuPrint = chkPrint.Checked
        menu.MenuDateVon = dtpVon.Value
        menu.MenuDateBis = dtpbis.Value
        menu.MenuDetail1 = SQLTools.hochkomma(txtDetail1.Text)
        menu.MenuDetail2 = SQLTools.hochkomma(txtDetail2.Text)
        menu.MenuDetail3 = SQLTools.hochkomma(txtDetail3.Text)
        menu.MenuSuppe = SQLTools.hochkomma(txtSuppe.Text)
        menu.MenuPrintSuppe = chkPrintSuppe.Checked
        menu.MenuEssenMo = SQLTools.hochkomma(txtMontag.Text)
        menu.MenuEssenDi = SQLTools.hochkomma(txtDienstag.Text)
        menu.MenuEssenMi = SQLTools.hochkomma(txtMittwoch.Text)
        menu.MenuEssenDo = SQLTools.hochkomma(txtDonnerstag.Text)
        menu.MenuEssenFr = SQLTools.hochkomma(txtFreitag.Text)
        menu.MenuArt = coboMenuArt.SelectedItem
    End Function

    Private Sub btnSpeichern_Click(sender As Object, e As EventArgs) Handles btnSpeichern.Click
        save()

    End Sub

    Private Function save()
        FillMenu()

        If menu.CheckExist Then
            menu.update()
        Else
            menu.saveAsNew()
        End If

        UpdateLastUsage(menu.MenuEssenMo, menu.MenuEssenMoDate)
        UpdateLastUsage(menu.MenuEssenDi, menu.MenuEssenDiDate)
        UpdateLastUsage(menu.MenuEssenMi, menu.MenuEssenMiDate)
        UpdateLastUsage(menu.MenuEssenDo, menu.MenuEssenDoDate)
        UpdateLastUsage(menu.MenuEssenFr, menu.MenuEssenFrDate)
        UpdateLastUsage(menu.MenuSuppe, menu.MenuSuppeDate)
    End Function


    Private Sub txtSuppe_klickeliklick(sender As Object, e As EventArgs) Handles txtSuppe.MouseClick, txtMontag.MouseClick, txtDienstag.MouseClick, txtMittwoch.MouseClick, txtDonnerstag.MouseClick, txtFreitag.MouseClick
        PanSpeise.Controls.Clear()
        deselectAllControls()
        'PanSuppe.BackColor = Color.Red
        sender.parent.backcolor = Color.Red
        Dim test As TextBox = sender
        Dim usrcntrl As New usrcntlSpeisen
        usrcntrl.senderbox.Text = sender.Text

        If sender.name = "txtSuppe" Then
            usrcntrl.SqlSpeiseArt &= " AND EssenType = 'Suppe'"
            usrcntrl.comSpeiseart.SelectedItem = "Suppe"
        Else
            usrcntrl.SqlSpeiseArt &= " AND EssenType = 'Hauptspeise'"
            usrcntrl.comSpeiseart.SelectedItem = "Hauptspeise"
        End If

        usrcntrl.Dock = DockStyle.Fill : PanSpeise.Controls.Add(usrcntrl)
        AddHandler usrcntrl.btnUebernehmen.Click, Function()
                                                      sender.text = usrcntrl.senderbox.Text
                                                  End Function
        AddHandler usrcntrl.dgvSpeise.CellDoubleClick, Function()
                                                               sender.text = usrcntrl.senderbox.Text
                                                           End Function
    End Sub


    Function deselectAllControls()

        Dim contr As Control
        For Each contr In PanLeft.Controls 'Me.Controls
            If contr.GetType() Is GetType(Panel) Then
                contr.BackColor = Color.White
            End If

        Next

    End Function


    Private Sub dtpVon_ValueChanged_1(sender As Object, e As EventArgs) Handles dtpVon.ValueChanged
        'MsgBox(dtpVon.Value.DayOfWeek)
        For i = 0 To 10
            If dtpVon.Value.DayOfWeek = 1 Then
                Exit For
            Else
                dtpVon.Value = dtpVon.Value.AddDays(-1)
            End If
        Next
        dtpbis.Value = dtpVon.Value.AddDays(4)

        lblMontag.Text = "Montag, den " & dtpVon.Value
        lblDienstag.Text = "Dienstag, den " & dtpVon.Value.AddDays(1)
        lblMittwoch.Text = "Mittwoch, den " & dtpVon.Value.AddDays(2)
        lblDonnerstag.Text = "Donnerstag, den " & dtpVon.Value.AddDays(3)
        lblFreitag.Text = "Freitag, den " & dtpVon.Value.AddDays(4)

        NewOrLoad()
    End Sub

    Private Sub btnLaufendeWoche_Click(sender As Object, e As EventArgs) Handles btnLaufendeWoche.Click
        dtpVon.Value = Now.Date
        NewOrLoad()
    End Sub

    Private Sub btnNaechsteWoche_Click(sender As Object, e As EventArgs) Handles btnNaechsteWoche.Click
        dtpVon.Value = Now.Date.AddDays(7)
        NewOrLoad()
    End Sub

    Private Sub btnWochePlus_Click(sender As Object, e As EventArgs) Handles btnWochePlus.Click
        dtpVon.Value = dtpVon.Value.AddDays(7)
        NewOrLoad()
    End Sub

    Private Sub btnWocheMinus_Click(sender As Object, e As EventArgs) Handles btnWocheMinus.Click
        dtpVon.Value = dtpVon.Value.AddDays(-7)
        NewOrLoad()
    End Sub

    Private Sub btnCancel_Click(sender As Object, e As EventArgs) Handles btnCancel.Click
        Close()
    End Sub

    Function NewOrLoad()
        menu = New cMenu
        menu.MenuID = dtpVon.Value & dtpbis.Value
        If menu.CheckExist Then
            menu.loadMenu(menu.MenuID)
            FillBoxes()
        Else
            FillBoxes(False)
        End If

        Dim usrcntrl As New usrcntlSpeisen : usrcntrl.Dock = DockStyle.Fill : PanSpeise.Controls.Clear() : PanSpeise.Controls.Add(usrcntrl)

        menu.MenuSuppeDate = dtpVon.Value
        menu.MenuEssenMoDate = dtpVon.Value
        menu.MenuEssenDiDate = dtpVon.Value.AddDays(1)
        menu.MenuEssenMiDate = dtpVon.Value.AddDays(2)
        menu.MenuEssenDoDate = dtpVon.Value.AddDays(3)
        menu.MenuEssenFrDate = dtpVon.Value.AddDays(4)
    End Function

    Private Sub btnPrint_Click(sender As Object, e As EventArgs) Handles btnMake.Click
        save()
        deselectAllControls()
        Dim dt As DataTable = menu.loadMenuToDT(menu.MenuID)
        PanSpeise.Controls.Clear()
        Dim usrcntrl As New usrcntlPrint
        usrcntrl.Printvorlage = "Mittagsmenue"
        usrcntrl.dt = dt
        usrcntrl.Dock = DockStyle.Fill
        PanSpeise.Controls.Add(usrcntrl)
    End Sub

    Private Sub btnTest_Click(sender As Object, e As EventArgs) Handles btnTest.Click

    End Sub

    Private Sub setEmailSend(sSubject As String, sBody As String,
                             sTo As String, sCC As String,
                             sFilename As String, sDisplayname As String,
                             Optional sBCC As String = "")

        Try
            Dim oApp As Microsoft.Office.Interop.Outlook.Application 'Interop.Outlook._Application
            oApp = New Microsoft.Office.Interop.Outlook.Application 'Interop.Outlook.Application

            Dim oMsg As Microsoft.Office.Interop.Outlook.MailItem 'Interop.Outlook._MailItem
            oMsg = oApp.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem)


            oMsg.Subject = sSubject
            'oMsg.Body = sBody

            oMsg.To = sTo
            oMsg.CC = sCC
            oMsg.BCC = sBCC

            Dim strS As String = sFilename
            Dim strN As String = sDisplayname
            If sFilename <> "" Then
                Dim sBodyLen As Integer = Int(sBody.Length)
                Dim oAttachs As Microsoft.Office.Interop.Outlook.Attachments = oMsg.Attachments
                Dim oAttach As Microsoft.Office.Interop.Outlook.Attachment

                oAttach = oAttachs.Add(strS, , sBodyLen, strN)

            End If

            '''
            Clipboard.SetText(IO.File.ReadAllText(frmMain.sAppPath & "\Mail\Highway.htm", System.Text.Encoding.Default))
            Dim myData As IDataObject = Clipboard.GetDataObject()
            oMsg.HTMLBody = CType(myData.GetData(GetType(String)), String)
            '''

            oMsg.Display()

        Catch exc As system.Exception
            MsgBox(exc.message)
        End Try

        'oMsg.Send()
        'oMsg.Save()
        ' oMsg.Move(objFolder)
        '  MessageBox.Show("Email Send", Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)

    End Sub

    Private Sub btnMail_Click(sender As Object, e As EventArgs) Handles btnMail.Click
        Dim subject As String = ""
        Dim body As String = "1" ' wird nicht verwendet, da hmtl Vorlage
        Dim empfaenger As String = ""
        Dim Anhang As String = ""
        Dim Anhangdateiname As String = ""
        Dim cc As String = ""
        Dim bcc As String = ""
        'setEmailSend("Testsubject", "TestBody", "ms@verag.ag", "", "C:\tmp\27.05.2019.pdf", "Mittagsmenue", "veragsped@gmail.com")

        '''Subject
        subject = "Mittagsmenü " & menu.MenuDateVon & " bis " & menu.MenuDateBis

        '''Anhang
        If Exists("C:\Gastro\Menues\" & menu.MenuDateVon.ToString.Substring(0, 10) & ".pdf") Then
            Anhang = "C:\Gastro\Menues\" & menu.MenuDateVon.ToString.Substring(0, 10) & ".pdf"
            Anhangdateiname = menu.MenuDateVon.ToString.Substring(0, 10) & ".pdf"
        Else
            MsgBox("Datei nicht gefunden. Bitte erst erstellen.")
            Exit Sub
        End If

        '''BCC
        Dim ds As New DataSet
        cSQL.SQL2DS("SELECT * FROM GVerteilerListe", ds)
        For Each row As DataRow In ds.Tables(0).Rows
            bcc &= row.Item("EmpfEMail").ToString & "; "
        Next
        bcc = bcc.Substring(0, bcc.Length - 2)

        ' MsgBox(bcc)

        setEmailSend(subject, body, empfaenger, cc, Anhang, Anhangdateiname, bcc)
    End Sub



    Function UpdateLastUsage(Speise As String, Datum As Date)
        If SQLTools.CheckIfExist("select essenname from gessen where essenname = '" & Speise & "'") Then
            Dim _essen As New cEssen
            _essen.getEssenByName(Speise)

            If Datum > _essen.EssenLastUsage Then
                _essen.EssenLastUsage = Datum
            End If
            _essen.update()
        End If
    End Function

    Private Sub btnFTP_Click(sender As Object, e As EventArgs) Handles btnFTP.Click
        If MsgBox("Achtung! Mit Bestätigen dieser Meldung wird das aktuelle Mittagsmenü auf der Homepage ÜBERSCHRIEBEN!", MsgBoxStyle.OkCancel, "Upload") = MsgBoxResult.Ok Then
            Dim anhang, anhangdateiname As String

            If Exists("C:\Gastro\Menues\" & menu.MenuDateVon.ToString.Substring(0, 10) & ".pdf") Then
                anhang = "C:\Gastro\Menues\" & menu.MenuDateVon.ToString.Substring(0, 10) & ".pdf"
                anhangdateiname = menu.MenuDateVon.ToString.Substring(0, 10) & ".pdf"
            Else
                MsgBox("Datei nicht gefunden. Bitte erst erstellen.")
                Exit Sub
            End If

            cFTP.Upload2FTP(cListe.HighwayFTPSrv & "/speisekarte/Mittagsmenue.pdf", cListe.HighwayFTPUser, cListe.HighwayFTPPwd, anhang)
        Else
            Exit Sub
        End If
    End Sub


End Class