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.ExpressApp.SystemModule
Imports DevExpress.ExpressApp.CloneObject
Imports DevExpress.Xpo
Imports DevExpress.ExpressApp.Editors
Imports DevExpress.Data.Filtering

Public Class ProductFinderVC
    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()

        Frame.GetController(Of ProductFinderVC).aToTableProductFinder.Active.SetItemValue("", False)

        If View.Id = "ProductFinder_DetailView" Then
            Frame.GetController(Of ProductFinderVC).aToTableProductFinder.Active.SetItemValue("", True)
            Frame.GetController(Of NewObjectViewController).Active.SetItemValue("", False)
            Frame.GetController(Of DeleteObjectsViewController).Active.SetItemValue("", False)
            Frame.GetController(Of ListViewProcessCurrentObjectController).Active.SetItemValue("", False)
            Frame.GetController(Of LinkUnlinkController).Active.SetItemValue("", False)
            Frame.GetController(Of CloneObjectViewController).Active.SetItemValue("", False)
            Frame.GetController(Of ModificationsController).Active.SetItemValue("", False)

        End If
        If View.Id = "ProductFinder_ProductSelect_ListView" Then
            Frame.GetController(Of ListViewProcessCurrentObjectController).Active.SetItemValue("", False)
            Frame.GetController(Of LinkUnlinkController).Active.SetItemValue("", False)
        End If
        If View.Id = "ProductFinder_ProductSelected_ListView" Then
            Frame.GetController(Of ListViewProcessCurrentObjectController).Active.SetItemValue("", False)
            Frame.GetController(Of LinkUnlinkController).Active.SetItemValue("", False)
        End If
        If View.Id = "ProductFinder_StorageOutCirculation_ListView" Then
            Frame.GetController(Of NewObjectViewController).Active.SetItemValue("", False)
            Frame.GetController(Of DeleteObjectsViewController).Active.SetItemValue("", False)
            Frame.GetController(Of ListViewProcessCurrentObjectController).Active.SetItemValue("", False)
            Frame.GetController(Of LinkUnlinkController).Active.SetItemValue("", False)
            Frame.GetController(Of CloneObjectViewController).Active.SetItemValue("", False)

        End If
        If View.Id = "ProductFinder_ProductFinderInfo_ListView" Then
            Frame.GetController(Of NewObjectViewController).Active.SetItemValue("", False)
            Frame.GetController(Of DeleteObjectsViewController).Active.SetItemValue("", False)
            Frame.GetController(Of ListViewProcessCurrentObjectController).Active.SetItemValue("", False)
            Frame.GetController(Of LinkUnlinkController).Active.SetItemValue("", False)
            Frame.GetController(Of CloneObjectViewController).Active.SetItemValue("", False)

        End If
    End Sub
    Protected Overrides Sub OnDeactivated()
        MyBase.OnDeactivated()

    End Sub

    Private Sub aToTableProductFinder_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aToTableProductFinder.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)

        Dim _ProductSelect_ListPropertyEditor As ListPropertyEditor = TryCast(View, DetailView).FindItem("ProductSelect")
        Dim _ProductSelected_ListPropertyEditor As ListPropertyEditor = TryCast(View, DetailView).FindItem("ProductSelected")

        Dim _ProductFinder As ProductFinder = TryCast(View.SelectedObjects(0), ProductFinder)
        Dim _ProductSelect As ProductSelect
        Dim _ProductSelected As ProductSelected

        Dim _IsExists As Boolean = False

        If _ProductSelect_ListPropertyEditor IsNot Nothing Then
            For Each _ProductSelect In _ProductSelect_ListPropertyEditor.ListView.SelectedObjects
                If _ProductSelected_ListPropertyEditor IsNot Nothing Then
                    _IsExists = False
                    For Each _ProductSelected In _ProductSelected_ListPropertyEditor.ListView.CollectionSource.List
                        If _ProductSelected.Products Is _ProductSelect.Products Then
                            _IsExists = True
                            Exit For
                        End If
                    Next

                    If _IsExists Then
                        _ProductSelected.QTT += 1
                    Else
                        _ProductSelected = _ocur.CreateObject(Of ProductSelected)()
                        _ProductSelected.MasterKey = _ProductFinder.MasterKey
                        _ProductSelected.Products = _ProductSelect.Products
                        _ProductSelected.QTT = 1
                        _ProductSelected_ListPropertyEditor.ListView.CollectionSource.Add(_ProductSelected)
                    End If
                    _ocur.CommitChanges()
                End If
            Next
        End If

    End Sub
    Private Sub aToTableD_ProductFinder_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aToTableD_ProductFinder.Execute
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _ProductSelected_ListPropertyEditor As ListPropertyEditor = TryCast(View, DetailView).FindItem("ProductSelected")
        Dim _ProductFinder As ProductFinder = TryCast(View.SelectedObjects(0), ProductFinder)
        Dim _ProductSelected_Collection As New XPCollection(Of ProductSelected)(_onew.Session, CriteriaOperator.Parse("1=2"))
        Dim _ProductSelected As ProductSelected
        If _ProductSelected_ListPropertyEditor IsNot Nothing Then
            For Each _ProductSelected In _ProductSelected_ListPropertyEditor.ListView.SelectedObjects
                _ProductSelected_Collection.Add(_onew.GetObject(_ProductSelected))
            Next
            _onew.Delete(_ProductSelected_Collection)
            _onew.CommitChanges()
            _ProductSelected_ListPropertyEditor.ListView.CollectionSource.Reload()
        End If
    End Sub
End Class
