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

Public Class EstateVC
    Inherits DevExpress.ExpressApp.ViewController

    Public Sub New()
        MyBase.New()

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

    End Sub
    Protected Overrides Sub OnActivated()
        MyBase.OnActivated()
        If View.Id = "Immovables_ListView" Then
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.NewObjectViewController).Active.SetItemValue("", False)
        End If
        If View.Id = "Immovables_ChangeGroup_PopUp_DetailView" Then
            AddHandler Frame.GetController(Of DevExpress.ExpressApp.SystemModule.DialogController)().AcceptAction.Executing, AddressOf Immovables_ChangeGroup_PopUp_DetailView_AcceptAction_Executing
        End If
    End Sub
    Protected Overrides Sub OnDeactivated()
        MyBase.OnDeactivated()
        If View.Id = "Immovables_ListView" Then
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.NewObjectViewController).Active.SetItemValue("", True)
        End If
        If View.Id = "Immovables_ChangeGroup_PopUp_DetailView" Then
            RemoveHandler Frame.GetController(Of DevExpress.ExpressApp.SystemModule.DialogController)().AcceptAction.Executing, AddressOf Immovables_ChangeGroup_PopUp_DetailView_AcceptAction_Executing
        End If
    End Sub

    Private Sub aImmovables_ContractRows_Info_CustomizePopupWindowParams(sender As Object, e As DevExpress.ExpressApp.Actions.CustomizePopupWindowParamsEventArgs) Handles aImmovables_ContractRows_Info.CustomizePopupWindowParams
        Try
            Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
            Dim _Immovables As Immovables = TryCast(View.SelectedObjects(0), Immovables)
            If _Immovables Is Nothing Then Throw New Exception("*Váratlan hiba történt, a folyamat megszakadt!")

            Dim _CollectionSource As CollectionSource = New CollectionSource(_onew, GetType(ContractRows))

            _CollectionSource.BeginUpdateCriteria()

            _CollectionSource.Criteria("1") = CriteriaOperator.Parse("UniqueObjects.Oid=?", _Immovables.UniqueObjects.Oid)

            _CollectionSource.EndUpdateCriteria()

            e.View = Application.CreateListView("ContractRows_ListView_Immovables", _CollectionSource, True)
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly)
        End Try
    End Sub
    Private Sub aImmovables_ContractRows_Info_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.PopupWindowShowActionExecuteEventArgs) Handles aImmovables_ContractRows_Info.Execute
        Try

        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly)
        End Try
    End Sub
    Private Sub aImmovables_InvoiceRows_Info_CustomizePopupWindowParams(sender As Object, e As DevExpress.ExpressApp.Actions.CustomizePopupWindowParamsEventArgs) Handles aImmovables_InvoiceRows_Info.CustomizePopupWindowParams
        Try
            Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
            Dim _Immovables As Immovables = TryCast(View.SelectedObjects(0), Immovables)
            If _Immovables Is Nothing Then Throw New Exception("*Váratlan hiba történt, a folyamat megszakadt!")

            Dim _CollectionSource As CollectionSource = New CollectionSource(_onew, GetType(InvoiceRows))

            _CollectionSource.BeginUpdateCriteria()
            _CollectionSource.Criteria("1") = CriteriaOperator.Parse("UniqueObjects.Oid=? OR ContractRows.UniqueObjects.Oid=?", _Immovables.UniqueObjects.Oid, _Immovables.UniqueObjects.Oid)
            _CollectionSource.EndUpdateCriteria()

            e.View = Application.CreateListView("InvoiceRows_ListView_Immovables", _CollectionSource, True)

        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly)
        End Try
    End Sub
    Private Sub aImmovables_InvoiceRows_Info_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.PopupWindowShowActionExecuteEventArgs) Handles aImmovables_InvoiceRows_Info.Execute
        Try

        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly)
        End Try
    End Sub
    Private Sub aImmovables_ChangeGroup_CustomizePopupWindowParams(sender As Object, e As DevExpress.ExpressApp.Actions.CustomizePopupWindowParamsEventArgs) Handles aImmovables_ChangeGroup.CustomizePopupWindowParams
        Try
            Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
            Dim _Immovables_ChangeGroup_PopUp As Immovables_ChangeGroup_PopUp = _onew.CreateObject(Of Immovables_ChangeGroup_PopUp)()

            e.View = Application.CreateDetailView(_onew, "Immovables_ChangeGroup_PopUp_DetailView", True, _Immovables_ChangeGroup_PopUp)
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly)
        End Try
    End Sub
    Private Sub aImmovables_ChangeGroup_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.PopupWindowShowActionExecuteEventArgs) Handles aImmovables_ChangeGroup.Execute
        Try
            Dim _ocur As Xpo.XPObjectSpace = View.ObjectSpace
            Dim _Immovables_ChangeGroup_PopUp As Immovables_ChangeGroup_PopUp = _ocur.GetObject(TryCast(e.PopupWindow.View.SelectedObjects(0), Immovables_ChangeGroup_PopUp))

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

            For Each _Immovables As Immovables In View.SelectedObjects
                If _Immovables_ChangeGroup_PopUp.IsChange_CanForRent Then
                    _Immovables.CanForRent = _Immovables_ChangeGroup_PopUp.CanForRent
                End If
                If _Immovables_ChangeGroup_PopUp.IsChange_CanforSale Then
                    _Immovables.CanForSale = _Immovables_ChangeGroup_PopUp.CanForSale
                End If
                If _Immovables_ChangeGroup_PopUp.IsChange_CustomersLessor Then
                    _Immovables.CustomersLessor = _ocur.GetObjectByKey(Of Customers)(_Immovables_ChangeGroup_PopUp.CustomersLessor.Oid)
                End If
                If _Immovables_ChangeGroup_PopUp.IsChange_CustomersOwner Then
                    _Immovables.CustomersOwner = _ocur.GetObjectByKey(Of Customers)(_Immovables_ChangeGroup_PopUp.CustomersOwner.Oid)
                End If
                If _Immovables_ChangeGroup_PopUp.IsChange_ImmovableSoldState Then
                    _Immovables.ImmovableSoldState = _Immovables_ChangeGroup_PopUp.ImmovableSoldState
                End If
                If _Immovables_ChangeGroup_PopUp.IsChange_ImmovablesRentState Then
                    _Immovables.ImmovablesRentState = _Immovables_ChangeGroup_PopUp.ImmovablesRentState
                End If
            Next
            _ocur.CommitChanges()
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly)
        End Try
    End Sub
    Private Sub Immovables_ChangeGroup_PopUp_DetailView_AcceptAction_Executing(sender As Object, e As CancelEventArgs)
        Try
            Dim _Immovables_ChangeGroup_PopUp As Immovables_ChangeGroup_PopUp = TryCast(View.SelectedObjects(0), Immovables_ChangeGroup_PopUp)

            If _Immovables_ChangeGroup_PopUp.IsChange_CanForRent Then Exit Sub
            If _Immovables_ChangeGroup_PopUp.IsChange_CanforSale Then Exit Sub
            If _Immovables_ChangeGroup_PopUp.IsChange_CustomersLessor Then Exit Sub
            If _Immovables_ChangeGroup_PopUp.IsChange_CustomersOwner Then Exit Sub
            If _Immovables_ChangeGroup_PopUp.IsChange_ImmovableSoldState Then Exit Sub
            If _Immovables_ChangeGroup_PopUp.IsChange_ImmovablesRentState Then Exit Sub

            Throw New Exception("*Nincs beállítva egyetlen változtatás sem!")
        Catch ex As Exception
            e.Cancel = True
            MsgBox(ex.Message, MsgBoxStyle.OkOnly)
        End Try
    End Sub
End Class
