Imports System
Imports System.ComponentModel
Imports System.Collections.Generic
Imports System.Diagnostics
Imports System.Text

Imports DevExpress.ExpressApp
Imports DevExpress.ExpressApp.Actions
Imports DevExpress.Persistent.Base
Imports DevExpress.Data.Filtering
Imports DevExpress.ExpressApp.Reports.Win
Imports DevExpress.XtraPrinting
Imports System.IO
Imports DevExpress.Persistent.BaseImpl
Imports DevExpress.Xpo
Imports Microsoft.Office.Interop
Imports DevExpress.ExpressApp.Win.SystemModule
Imports DevExpress.ExpressApp.Editors
Imports DevExpress.ExpressApp.Utils

Public Class SendMailVC
    Inherits DevExpress.ExpressApp.ViewController
    Private reportService As WinReportServiceController
    Private printingService As PrintingController
    Private _MemoryStream As New MemoryStream
    Private _StreamReader As New StreamReader(_MemoryStream)
    Private _RichEditControl As RichEditControl

    Public Sub New()
        MyBase.New()

        'This call is required by the Component Designer.
        InitializeComponent()
        RegisterActions(components)

    End Sub
    Protected Overrides Sub OnViewControlsCreated()
        MyBase.OnViewControlsCreated()
        If View.Id = "CRMEstate_Leads_DetailView" Then
            Dim _DetailView As DetailView = TryCast(View, DetailView)
            Dim _RichEditContolViewItem As ViewItem = _DetailView.FindItem("DocumentBody")
            If _RichEditContolViewItem IsNot Nothing Then
                AddHandler _RichEditContolViewItem.ControlCreated, AddressOf RichEditContolViewItem_ControlCreated
            End If

        End If
    End Sub
    Protected Overrides Sub OnActivated()
        MyBase.OnActivated()
        reportService = Frame.GetController(Of WinReportServiceController)()
        printingService = Frame.GetController(Of PrintingController)()
    End Sub

    Private Sub aSendMailReportCRMEstate_Leads_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs)

    End Sub
    Private Sub reportService_CustomShowPreview(ByVal sender As Object, ByVal e As CustomShowPreviewEventArgs)
        e.Report.CreateDocument()
        e.Report.ExportToHtml(_MemoryStream)
        e.Handled = True
    End Sub
    Private Sub aSendMailCRMEstate_Leads_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aSendMailCRMEstate_Leads.Execute
        Try
            Dim _OutlookEMail As OutlookEMail
            Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
            Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace

            Dim _CRMEstate_Leads As CRMEstate_Leads = TryCast(View.SelectedObjects(0), CRMEstate_Leads)

            Dim _FileName As String = Path.GetTempPath & "Ajánlat.pdf"
            Dim _StreamFile As New FileStream(_FileName, FileMode.Create)

            _RichEditControl.ExportToPdf(_StreamFile)
            _StreamFile.Close()

            _StreamFile = New FileStream(_FileName, FileMode.Open, FileAccess.Read)
            Dim _StreamReader As New StreamReader(_StreamFile)
            _StreamFile.Seek(0, SeekOrigin.Begin)

            Dim _OutlookEMailAttachments As OutlookEMailAttachments = _onew.CreateObject(Of OutlookEMailAttachments)()
            Dim _DocumentFileData As DocumentFileData = _onew.CreateObject(Of DocumentFileData)()
            _DocumentFileData.File = New FileData(_onew.Session)
            _DocumentFileData.File.LoadFromStream("Ajánlat.pdf", _StreamFile) '_FileData
            _DocumentFileData.File.FileName = "Ajánlat.pdf"
            _OutlookEMailAttachments.DocumentFileData = _onew.GetObject(_DocumentFileData)

            _StreamReader.Close()
            _StreamFile.Close()

            '-- Most jön az új outlook  !
            _MemoryStream.Position = 0
            _OutlookEMail = _onew.CreateObject(Of OutlookEMail)()
            _OutlookEMail.HTMLBody = ""
            _OutlookEMail.eCRMEstate_Leads = eCRMEstate_Leads.eCRMEstate
            _OutlookEMail.CRMEstate_Leads = _onew.GetObjectByKey(Of CRMEstate_Leads)(_CRMEstate_Leads.Oid)
            If _CRMEstate_Leads.EMail = "" Or _CRMEstate_Leads.EMail Is Nothing Then
                _OutlookEMail.Recipients = "< = Nincs beállítva e-mail cím az ügyfélhez! = >"
            Else
                _OutlookEMail.Recipients = _CRMEstate_Leads.EMail
            End If
            _OutlookEMail.OutlookEMailAttachments.Add(_OutlookEMailAttachments)
            e.ShowViewParameters.CreatedView = Application.CreateDetailView(_onew, "OutlookEMail_DetailView", True, _OutlookEMail)
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, String.Format(CaptionHelper.GetLocalizedText("Exceptions\SISBusinessExceptions", "MsgBoxCheckItAgain")))
        End Try
    End Sub
    Private Sub RichEditContolViewItem_ControlCreated(sender As Object, e As EventArgs)
        Dim _ItemControl As Object = TryCast(sender, ViewItem).Control
        _RichEditControl = TryCast(_ItemControl, RichEditUserControl).RichEditControl
    End Sub
    Private Sub aSendEMailLawyerDocument_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aSendEMailLawyerDocument.Execute
        Try
            Dim _OutlookEMail As OutlookEMail
            Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
            Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace

            Dim _CRMEstate_Opportunity As CRMEstate_Opportunity = TryCast(View.SelectedObjects(0), CRMEstate_Opportunity)
            If _CRMEstate_Opportunity Is Nothing Then Throw New Exception("*Váratlan hiba történt, a folyamat megszakadt!")
            Dim _ImmovablesGroupsParameters As ImmovablesGroupsParameters = _onew.FindObject(Of ImmovablesGroupsParameters)(CriteriaOperator.Parse("ImmovablesGroups.Oid=?", _
                                                                        _CRMEstate_Opportunity.CRMEstate_Leads.ImmovablesPriceListHeader.ImmovablesGroups.Oid))

            If _ImmovablesGroupsParameters Is Nothing Then Throw New Exception("*Nincsenek beállítva a(z) " + _
                _CRMEstate_Opportunity.CRMEstate_Leads.ImmovablesPriceListHeader.ImmovablesGroups.ShortName + " paraméterei!")
            Dim _ReportData As ReportData = _ImmovablesGroupsParameters.ReportData_Contract_Temp
            If _ReportData Is Nothing Then Throw New Exception("*Nincs beállítva az ügyvédi segédanyag nyomtatvány!" + vbNewLine + "Kérem, ellenőrizze!")

            Dim _XafReport As Reports.XafReport = _ReportData.LoadReport(_onew)
            Dim _FilterString As String = "[CRMEstate_Opportunity.Oid]='" & _CRMEstate_Opportunity.Oid.ToString & "'"
            _XafReport.FilterString = _FilterString
            Dim _FileName As String = Path.GetTempPath & "Ügyvédi segédlet.pdf"
            Dim _StreamFile As New FileStream(_FileName, FileMode.Create)

            _XafReport.ExportToPdf(_StreamFile)
            _StreamFile.Close()

            _StreamFile = New FileStream(_FileName, FileMode.Open, FileAccess.Read)
            Dim _StreamReader As New StreamReader(_StreamFile)
            _StreamFile.Seek(0, SeekOrigin.Begin)

            Dim _OutlookEMailAttachments As OutlookEMailAttachments = _onew.CreateObject(Of OutlookEMailAttachments)()
            Dim _DocumentFileData As DocumentFileData = _onew.CreateObject(Of DocumentFileData)()
            _DocumentFileData.File = New FileData(_ocur.Session)
            _DocumentFileData.File.LoadFromStream("Ügyvédi segédlet.pdf", _StreamFile) '_FileData
            _DocumentFileData.File.FileName = "Ügyvédi segédlet.pdf"
            _OutlookEMailAttachments.DocumentFileData = _DocumentFileData

            _StreamReader.Close()
            _StreamFile.Close()

            '-- Most jön az új outlook  !
            _MemoryStream.Position = 0
            _OutlookEMail = _onew.CreateObject(Of OutlookEMail)()
            _OutlookEMail.Subject = _CRMEstate_Opportunity.CRMEstate_Leads.DisplayName + " - Ügyvédi segédlet"
            _OutlookEMail.HTMLBody = "Tisztelt Németh és Herrmann Ügyvédi Iroda!"
            _OutlookEMail.eCRMEstate_Leads = eCRMEstate_Leads.eCRMEstate
            _OutlookEMail.Recipients = "< = Nincs beállítva e-mail = >"

            _OutlookEMail.OutlookEMailAttachments.Add(_OutlookEMailAttachments)
            e.ShowViewParameters.CreatedView = Application.CreateDetailView(_onew, "OutlookEMail_DetailView", True, _OutlookEMail)
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly)
        End Try
    End Sub
End Class
