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 Microsoft.Office.Interop
Imports DevExpress.ExpressApp.Win.Editors
Imports DevExpress.ExpressApp.SystemModule
Imports DevExpress.ExpressApp.CloneObject
Imports DevExpress.ExpressApp.Editors

Public Class CRMEstateVC
    Inherits Nuvolar.Module.CRMEstateVC
    Private _GridListEditor As GridListEditor
    Public Sub New()
        MyBase.New()
        InitializeComponent()
        RegisterActions(components)
    End Sub
    Protected Overrides Sub OnViewControlsCreated()
        MyBase.OnViewControlsCreated()


    End Sub
    Protected Overrides Sub OnActivated()
        MyBase.OnActivated()
        AddHandler CreateReservationReminder, AddressOf _CreateReservationReminder
        If View.Id = "CRMEstate_Leads_CRMEstate_Leads_Immovables_ListView" Or View.Id = "CRMEstate_Opportunity_CRMEstate_Opportunity_Immovables_ListView" Then
            Frame.GetController(Of DevExpress.ExpressApp.Win.SystemModule.WinModificationsController).ModificationsHandlingMode = ModificationsHandlingMode.AutoCommit
        End If
        If View.Id = "CRMEstate_Leads_CRMEstate_Leads_Activity_ListView" Then
            Frame.GetController(Of DeleteObjectsViewController).Active.SetItemValue("", False)
            Frame.GetController(Of ModificationsController).Active.SetItemValue("", False)
            Frame.GetController(Of LinkUnlinkController).Active.SetItemValue("", False)
            Frame.GetController(Of CloneObjectViewController).Active.SetItemValue("", False)
        End If
        If View.Id = "OutlookEMail_DetailView_View" Then
            Frame.GetController(Of ModificationsController).Active.SetItemValue("", False)
        End If
        If View.Id = "CRMEstate_Leads_DetailView" Then
            For Each editor As PropertyEditor In (CType(View, DetailView)).GetItems(Of PropertyEditor)()

                If editor.PropertyName = "FirstName" Then
                    AddHandler editor.ControlCreated, AddressOf editor_FirstName
                End If
                If editor.PropertyName = "LastName" Then
                    AddHandler editor.ControlCreated, AddressOf editor_LastName
                End If
            Next editor
        End If
        'If View.Id = "CRMEstate_Opportunity_Payment_Forecast_DetailView" Then
        '    Dim _CRMEstate_Opportunity_Payment_Forecast As CRMEstate_Opportunity_Payment_Forecast = TryCast(View.SelectedObjects(0), CRMEstate_Opportunity_Payment_Forecast)
        '    If _CRMEstate_Opportunity_Payment_Forecast.CurrencyName.ShortName = "HUF" Then
        '        Dim _SumPriceNettoHUF_ViewItem As DevExpress.ExpressApp.Editors.ViewItem = TryCast(View, DetailView).FindItem("SumPriceNettoHUF")
        '        Dim _DoublePropertyEditor_HUF As DevExpress.ExpressApp.Win.Editors.DoublePropertyEditor = TryCast(_SumPriceNettoHUF_ViewItem, DevExpress.ExpressApp.Win.Editors.DoublePropertyEditor)
        '        AddHandler _DoublePropertyEditor_HUF.ControlValueChanged, AddressOf DoublePropertyEditor_HUF_ControlValueChanged
        '    End If

        'End If
    End Sub
    Protected Overrides Sub OnDeactivated()
        MyBase.OnDeactivated()
        If View.Id = "CRMEstate_Leads_CRMEstate_Leads_Immovables_ListView" Or View.Id = "CRMEstate_Opportunity_CRMEstate_Opportunity_Immovables_ListView" Then
            Frame.GetController(Of DevExpress.ExpressApp.Win.SystemModule.WinModificationsController).ModificationsHandlingMode = ModificationsHandlingMode.Confirmation
        End If
        If View.Id = "CRMEstate_Leads_CRMEstate_Leads_Activity_ListView" Then
            Frame.GetController(Of DeleteObjectsViewController).Active.SetItemValue("", True)
            Frame.GetController(Of ModificationsController).Active.SetItemValue("", True)
            Frame.GetController(Of LinkUnlinkController).Active.SetItemValue("", True)
            Frame.GetController(Of CloneObjectViewController).Active.SetItemValue("", True)
        End If
        If View.Id = "OutlookEMail_DetailView_View" Then
            Frame.GetController(Of ModificationsController).Active.SetItemValue("", True)
        End If

    End Sub
    Public Sub _CreateReservationReminder(ByVal _CRMEstate_Leads_Immovables_Reservation As CRMEstate_Leads_Immovables_Reservation)
        Dim _OutlookApp As New Outlook.Application()
        Dim _OutlookNameSpace As Outlook.NameSpace = _OutlookApp.GetNamespace("MAPI")
        Dim _NewTask As Outlook.TaskItem = _OutlookApp.CreateItem(Outlook.OlItemType.olTaskItem)
        Dim _Date As New DateTime(_CRMEstate_Leads_Immovables_Reservation.DateTo.Year, _CRMEstate_Leads_Immovables_Reservation.DateTo.Month, _CRMEstate_Leads_Immovables_Reservation.DateTo.Day, 9, 0, 0)
        Dim _DueDate As New DateTime(_CRMEstate_Leads_Immovables_Reservation.DateTo.Year, _CRMEstate_Leads_Immovables_Reservation.DateTo.Month, _CRMEstate_Leads_Immovables_Reservation.DateTo.Day, 12, 0, 0)

        _OutlookNameSpace.Logon(, , True, True)
        _NewTask.ReminderSet = True
        _NewTask.Body = "Lejáró opcionális foglalás!"
        _NewTask.DueDate = _DueDate
        _NewTask.ReminderTime = _Date
        _NewTask.Subject = _CRMEstate_Leads_Immovables_Reservation.CRMEstate_Leads_Immovables.Immovables.DisplayName
        _NewTask.UnRead = True
        _NewTask.Save()

    End Sub
    Private Sub aViewEMail_CRMEstate_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aViewEMail_CRMEstate.Execute
        Try
            Dim _ocur As Xpo.XPObjectSpace = View.ObjectSpace
            Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
            Dim _CRMEstate_Leads_Activity As CRMEstate_Leads_Activity = TryCast(View.SelectedObjects(0), CRMEstate_Leads_Activity)
            If _CRMEstate_Leads_Activity Is Nothing Then Throw New Exception("*Nincs kijelölve egyetlen sor sem!")

            Dim _OutlookEMail As OutlookEMail = _onew.FindObject(Of OutlookEMail)(DevExpress.Data.Filtering.CriteriaOperator.Parse("Oid=?", _CRMEstate_Leads_Activity.Email_Oid))
            Dim _OutlookEMailIn As OutlookEMailIn = _onew.FindObject(Of OutlookEMailIn)(DevExpress.Data.Filtering.CriteriaOperator.Parse("Oid=?", _CRMEstate_Leads_Activity.Email_Oid))

            If _OutlookEMail Is Nothing And _OutlookEMailIn Is Nothing Then Throw New Exception("*Váratlan hiba történt, a folyamat megszakadt!")

            If _OutlookEMail Is Nothing Then
                _OutlookEMail = _onew.CreateObject(Of OutlookEMail)()
                _OutlookEMail.eCRMEstate_Leads = eCRMEstate_Leads.eCRMEstate
                _OutlookEMail.HTMLBody = _OutlookEMailIn.EmailBody
                _OutlookEMail.Subject = _OutlookEMailIn.EmailSubject

                For Each _OutlookEMailAttachments As OutlookEMailAttachments In _OutlookEMailIn.OutlookEMailAttachments
                    _OutlookEMail.OutlookEMailAttachments.Add(_OutlookEMailAttachments)
                Next
                _OutlookEMail.Recipients = _OutlookEMailIn.ReceiverEmailAdress
                _OutlookEMail.OutlookEMailIn_Oid = _OutlookEMailIn.Oid.ToString
                _OutlookEMail.eCRMEstate_Leads = eCRMEstate_Leads.eCRMEstate

                If TryCast(TryCast(TryCast(View, ListView).CollectionSource, PropertyCollectionSource).MasterObject, CRMEstate_Leads) IsNot Nothing Then
                    _OutlookEMail.CRMEstate_Leads = _onew.GetObjectByKey(Of CRMEstate_Leads)(TryCast(TryCast(TryCast(View, ListView).CollectionSource, PropertyCollectionSource).MasterObject, CRMEstate_Leads).Oid)
                Else
                    _OutlookEMail.CRMEstate_Leads = _onew.GetObjectByKey(Of CRMEstate_Leads)(TryCast(TryCast(TryCast(View, ListView).CollectionSource, PropertyCollectionSource).MasterObject, CRMEstate_Opportunity).CRMEstate_Leads.Oid)
                End If

            End If

            e.ShowViewParameters.CreatedView = Application.CreateDetailView(_onew, "OutlookEMail_DetailView_View", False, _OutlookEMail)

        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly)
        End Try
    End Sub
    Private Sub aSendEMail_CRMEstate_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aSendEMail_CRMEstate.Execute
        Try
            Dim _ocur As Xpo.XPObjectSpace = View.ObjectSpace
            Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
            Dim _PropertyCollectionSource As PropertyCollectionSource = TryCast(TryCast(View, ListView).CollectionSource, PropertyCollectionSource)
            If _PropertyCollectionSource Is Nothing Then Throw New Exception("*Váratlan hiba történt, a folyamat megszakadt!")

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

            If TryCast(_PropertyCollectionSource.MasterObject, CRMEstate_Leads) IsNot Nothing Then
                Dim _CRMEstate_Leads As CRMEstate_Leads = TryCast(_PropertyCollectionSource.MasterObject, CRMEstate_Leads)

                _OutlookEMail.Subject = "< Nincs beállítva >"
                If _CRMEstate_Leads.EMail = "" Or _CRMEstate_Leads.EMail Is Nothing Then
                    _OutlookEMail.Recipients = "< Nincs beállítva >"
                Else
                    _OutlookEMail.Recipients = _CRMEstate_Leads.EMail
                End If
                _OutlookEMail.eCRMEstate_Leads = eCRMEstate_Leads.eCRMEstate
                _OutlookEMail.HTMLBody = "Tisztelt " + _CRMEstate_Leads.LastName + " " + _CRMEstate_Leads.FirstName + _
                                        "!<P>&nbsp;</P><P>&nbsp;</P><P>&nbsp;</P>Üdvözlettel: " + _CRMEstate_Leads.HRPerson.FullName
                _OutlookEMail.CRMEstate_Leads = _onew.GetObjectByKey(Of CRMEstate_Leads)(_CRMEstate_Leads.Oid)
            End If

            If TryCast(_PropertyCollectionSource.MasterObject, CRMEstate_Opportunity) IsNot Nothing Then
                Dim _CRMEstate_Opportunity As CRMEstate_Opportunity = TryCast(_PropertyCollectionSource.MasterObject, CRMEstate_Opportunity)

                _OutlookEMail.Subject = "< Nincs beállítva >"
                If _CRMEstate_Opportunity.CRMEstate_Leads.EMail = "" Or _CRMEstate_Opportunity.CRMEstate_Leads.EMail Is Nothing Then
                    _OutlookEMail.Recipients = "< Nincs beállítva >"
                Else
                    _OutlookEMail.Recipients = _CRMEstate_Opportunity.CRMEstate_Leads.EMail
                End If
                _OutlookEMail.eCRMEstate_Leads = eCRMEstate_Leads.eCRMEstate
                _OutlookEMail.HTMLBody = "Tisztelt " + _CRMEstate_Opportunity.CRMEstate_Leads.LastName + " " + _CRMEstate_Opportunity.CRMEstate_Leads.FirstName + _
                                        "!<P>&nbsp;</P><P>&nbsp;</P><P>&nbsp;</P>Üdvözlettel: " + _CRMEstate_Opportunity.CRMEstate_Leads.HRPerson.FullName
                _OutlookEMail.CRMEstate_Leads = _onew.GetObjectByKey(Of CRMEstate_Leads)(_CRMEstate_Opportunity.CRMEstate_Leads.Oid)
            End If

            e.ShowViewParameters.CreatedView = Application.CreateDetailView(_onew, "OutlookEMail_DetailView", False, _OutlookEMail)
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly)
        End Try
    End Sub
    Private Sub aReplyEmail_CRMEstate_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aReplyEmail_CRMEstate.Execute
        Try
            Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
            Dim _OutlookEMail As OutlookEMail = TryCast(View.SelectedObjects(0), OutlookEMail)
            If _OutlookEMail Is Nothing Then Throw New Exception("*Váratlan hiba történt, a folyamat megszakadt!")

            Dim _OutlookEMailIn_old As OutlookEMailIn = _onew.FindObject(Of OutlookEMailIn)(DevExpress.Data.Filtering.CriteriaOperator.Parse("Oid=?", _OutlookEMail.OutlookEMailIn_Oid))
            If _OutlookEMailIn_old Is Nothing Then Throw New Exception("*Kimenő e-mail-re nem lehet válaszolni!")

            Dim _OutlookEMail_new As OutlookEMail = _onew.CreateObject(Of OutlookEMail)()

            _OutlookEMail_new.Recipients = _OutlookEMailIn_old.SenderEmailAddress
            _OutlookEMail_new.Subject = "RE: " + _OutlookEMailIn_old.EmailSubject
            Dim _HTML_Body As String = "<br><br><p class=""MsoNormal""><span style=""COLOR: #1f497d""><o:p>&nbsp;</o:p></span></p><div><div style=""BORDER-BOTTOM: medium none; BORDER-LEFT:" + _
                "medium none; PADDING-BOTTOM: 0cm; PADDING-LEFT: 0cm; PADDING-RIGHT: 0cm; BORDER-TOP: #b5c4df 1pt solid; BORDER-RIGHT: medium none; PADDING-TOP: 3pt""><p class=""MsoNormal"" > " + _
                "<br><b><span style=""FONT-FAMILY: 'Tahoma','sans-serif'; FONT-SIZE: 10pt"" lang=""HU"">From:</span></b>" + _OutlookEMailIn_old.SenderName + _
                "<span style=""FONT-FAMILY: 'Tahoma','sans-serif'; FONT-SIZE: 10pt"" lang=""HU""> " + _OutlookEMailIn_old.SenderEmailAddress + "<br>" + _
                "<b>Sent:</b>" + _OutlookEMailIn_old.ReceivedTime.ToString + "<br><b>To:</b>" + _OutlookEMailIn_old.ReceiverEmailAdress + "<br><b>Subject:</b>" + _OutlookEMailIn_old.EmailSubject + "<br><br>"
            _HTML_Body = _HTML_Body + _OutlookEMailIn_old.EmailBody

            _OutlookEMail_new.HTMLBody = _HTML_Body
            _OutlookEMail_new.CRMEstate_Leads = _onew.GetObjectByKey(Of CRMEstate_Leads)(_OutlookEMail.CRMEstate_Leads.Oid)
            _OutlookEMail_new.eCRMEstate_Leads = eCRMEstate_Leads.eCRMEstate

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

    Private Sub DoublePropertyEditor_HUF_ControlValueChanged(sender As Object, e As EventArgs)
        Throw New NotImplementedException
    End Sub

    Private Sub editor_LastName(sender As Object, e As EventArgs)
        Dim _LastNameStringPropertyEditor As StringPropertyEditor = TryCast(sender, StringPropertyEditor)
        AddHandler _LastNameStringPropertyEditor.Control.Click, AddressOf StringPropertyEditor_Click
    End Sub
    Private Sub editor_FirstName(sender As Object, e As EventArgs)
        Dim _FirstNameStringPropertyEditor As StringPropertyEditor = TryCast(sender, StringPropertyEditor)
        AddHandler _FirstNameStringPropertyEditor.Control.Click, AddressOf StringPropertyEditor_Click
    End Sub
    Private Sub StringPropertyEditor_Click(sender As Object, e As EventArgs)
        sender.SelectAll()
    End Sub

End Class
