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.Utils

Public Class CRM_OpportunitiesVC
    Inherits DevExpress.ExpressApp.ViewController

    Public Sub New()
        MyBase.New()
        InitializeComponent()
        RegisterActions(components)
    End Sub
    Protected Overrides Sub OnActivated()
        MyBase.OnActivated()
        Frame.GetController(Of CRM_OpportunitiesVC).aCRM_Opportunities_Contact_Role_CreateMail.Active.SetItemValue("", False)
        If View.Id = "CRM_Opportunities_OfferOutHeader_ListView" Then
            Frame.GetController(Of OffersOutPrintVC)().aOfferOutSendMailDeatil.Active.SetItemValue("", True)
        End If
        If View.Id = "CRM_Opportunities_CRM_Opportunities_Contacts_Roles_ListView" Then
            Frame.GetController(Of CRM_OpportunitiesVC).aCRM_Opportunities_Contact_Role_CreateMail.Active.SetItemValue("", True)
        End If
    End Sub
    Protected Overrides Sub OnDeactivated()
        MyBase.OnDeactivated()
        If View.Id = "CRM_Opportunities_OfferOutHeader_ListView" Then
            Frame.GetController(Of OffersOutPrintVC)().aOfferOutSendMailDeatil.Active.SetItemValue("", False)
        End If
        If View.Id = "CRM_Opportunities_CRM_Opportunities_Contacts_Roles_ListView" Then
            Frame.GetController(Of CRM_OpportunitiesVC).aCRM_Opportunities_Contact_Role_CreateMail.Active.SetItemValue("", False)
        End If
    End Sub
    Private Sub aCRM_Opportunities_CreateMail_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aCRM_Opportunities_CreateMail.Execute
        Try
            Dim _CRM_Opportunities As CRM_Opportunities = TryCast(e.CurrentObject, CRM_Opportunities)
            Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)

            If _CRM_Opportunities.Contacts Is Nothing Then Throw New Exception("*Nincs kapcsolat ehhez az ügyfélhez!")
            If _CRM_Opportunities.Contacts.Email Is Nothing Then Throw New UserFriendlyException("Nincs beállítva e-mail cím!")
            If _CRM_Opportunities.Contacts.Email = "" Then Throw New UserFriendlyException("Nincs beállítva e-mail cím!")

            Dim _OutlookEMail As OutlookEMail = _ocur.CreateObject(Of OutlookEMail)()

            _OutlookEMail.CRM_Leads = _CRM_Opportunities.CRM_Leads
            _OutlookEMail.CRM_Opportunities = _CRM_Opportunities

            _OutlookEMail.Recipients = _CRM_Opportunities.Contacts.Email
            _OutlookEMail.OwnerUser = _CRM_Opportunities.UserCreated
            _OutlookEMail.eCRMEstate_Leads = eCRMEstate_Leads.eCRM
            _OutlookEMail.Subject = _CRM_Opportunities.ShortName

            _OutlookEMail.Save()

            e.ShowViewParameters.CreatedView = Application.CreateDetailView(_ocur, "OutlookEMail_DetailView", False, _OutlookEMail)
            View.Refresh()
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly)
        End Try

    End Sub
    Private Sub aCRM_Opportunities_ReadMail_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aCRM_Opportunities_ReadMail.Execute
        Dim _CRM_Leads_Activity As CRM_Leads_Activity = TryCast(e.SelectedObjects(0), CRM_Leads_Activity)
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)

        If _CRM_Leads_Activity IsNot Nothing Then
            '--Email megkeresése és új benyitása
            If _CRM_Leads_Activity.Email_Oid = "" Then
                Throw New UserFriendlyException("Nincs kapcsolódó e-mail üzenet!")
            Else
                Dim _OutlookEMail As OutlookEMail = _ocur.FindObject(Of OutlookEMail)(CriteriaOperator.Parse("Oid=?", _CRM_Leads_Activity.Email_Oid))
                Dim _OutlookEMailIn As OutlookEMailIn = _ocur.FindObject(Of OutlookEMailIn)(CriteriaOperator.Parse("Oid=?", _CRM_Leads_Activity.Email_Oid))
                If _OutlookEMail IsNot Nothing Then
                    e.ShowViewParameters.CreatedView = Application.CreateDetailView(_ocur, "OutlookEMail_DetailView", False, _OutlookEMail)
                Else
                    e.ShowViewParameters.CreatedView = Application.CreateDetailView(_ocur, "OutlookEMailIn_DetailView", False, _OutlookEMailIn)
                End If
                View.Refresh()
            End If
        End If
    End Sub
    Private Sub aCRM_Opportunities_Contact_Role_CreateMail_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aCRM_Opportunities_Contact_Role_CreateMail.Execute
        Try
            Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
            Dim _OutlookEMail As OutlookEMail = _ocur.CreateObject(Of OutlookEMail)()
            Dim _Contacts As Contacts = TryCast(View.SelectedObjects(0), CRM_Opportunities_Contacts_Roles).Contacts
            Dim _CRM_Opportunities As CRM_Opportunities = TryCast(TryCast(View, ListView).CollectionSource, PropertyCollectionSource).MasterObject

            Dim _Contacts_Collection As New ArrayList

            For Each _CRM_Opportunities_Contacts_Roles As CRM_Opportunities_Contacts_Roles In View.SelectedObjects
                If String.IsNullOrEmpty(_CRM_Opportunities_Contacts_Roles.Contacts.Email) Then _
                    _Contacts_Collection.Add(_CRM_Opportunities_Contacts_Roles.Contacts)
            Next

            If _Contacts_Collection.Count > 0 Then
                Dim _ErrorMessage As String = "A következő kapcsolatoknak nincs e-mail cím beállítva:" + vbNewLine

                For Each _ContactsError As Contacts In _Contacts_Collection
                    _ErrorMessage += _ContactsError.FirstName + " " + _ContactsError.LastName + vbNewLine
                Next
                _ErrorMessage += "Kérem, ellenőrizze!"
                Throw New Exception(_ErrorMessage)
            End If


            _OutlookEMail.CRM_Leads = _CRM_Opportunities.CRM_Leads
            _OutlookEMail.CRM_Opportunities = _CRM_Opportunities

            _OutlookEMail.Recipients = ""
            For Each _CRM_Opportunities_Contacts_Roles As CRM_Opportunities_Contacts_Roles In View.SelectedObjects
                _OutlookEMail.Recipients += _CRM_Opportunities_Contacts_Roles.Contacts.Email + "; "
            Next

            _OutlookEMail.OwnerUser = _CRM_Opportunities.UserCreated
            _OutlookEMail.eCRMEstate_Leads = eCRMEstate_Leads.eCRM

            e.ShowViewParameters.CreatedView = Application.CreateDetailView(_ocur, "OutlookEMail_DetailView", False, _OutlookEMail)
            View.Refresh()
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, String.Format(CaptionHelper.GetLocalizedText("Exceptions\NuvolarExceptions", "MsgBoxCheckItAgain")))
        End Try
    End Sub
End Class
