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

Public Class Subcontractor_VC
    Inherits DevExpress.ExpressApp.ViewController

    Public Sub New()
        MyBase.New()
        InitializeComponent()
        RegisterActions(components)
    End Sub

    Protected Overrides Sub OnActivated()
        MyBase.OnActivated()

        Frame.GetController(Of Subcontractor_VC).aSelectCustomersSubcontractor.Active.SetItemValue("", False)

        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, XPObjectSpace)

        If View.Id = "Subcontractor_ListView" Then
            Frame.GetController(Of CloneObjectViewController).Active.SetItemValue("", False)
        End If
        If View.Id = "Subcontractor_SubcontractorCustomers_ListView" Then
            Frame.GetController(Of NewObjectViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of LinkUnlinkController)().Active.SetItemValue("", False)
            Frame.GetController(Of ListViewProcessCurrentObjectController).Active.SetItemValue("", False)
            Frame.GetController(Of AuditTrailVC).aGetAuditTrail.Active.SetItemValue("", False)

            Frame.GetController(Of Subcontractor_VC).aSelectCustomersSubcontractor.Active.SetItemValue("", True)
        End If
        If View.Id = "Subcontractor_SubcontractorWorksheetType_ListView" Then
            Frame.GetController(Of LinkUnlinkController)().Active.SetItemValue("", False)
            Frame.GetController(Of ListViewProcessCurrentObjectController).Active.SetItemValue("", False)
            Frame.GetController(Of CloneObjectViewController).Active.SetItemValue("", False)
            Frame.GetController(Of AuditTrailVC).aGetAuditTrail.Active.SetItemValue("", False)
        End If
        If View.Id = "PCIGroup_ListView_Subcontractor" Then

            Frame.GetController(Of NewObjectViewController).Active.SetItemValue("", False)
            Frame.GetController(Of DeleteObjectsViewController).Active.SetItemValue("", False)
            Frame.GetController(Of CloneObjectViewController).Active.SetItemValue("", False)
            Frame.GetController(Of LinkUnlinkController)().Active.SetItemValue("", False)
            Frame.GetController(Of ListViewProcessCurrentObjectController).Active.SetItemValue("", False)
            Frame.GetController(Of CloneObjectViewController).Active.SetItemValue("", False)

            Dim _Contacts As Contacts = _ocur.FindObject(Of Contacts)(CriteriaOperator.Parse("User.Oid = ?", SecuritySystem.CurrentUserId))

            If _Contacts IsNot Nothing Then
                If _Contacts.Customers IsNot Nothing Then
                    TryCast(View, ListView).CollectionSource.Criteria("BySubcontractorPCI") = CriteriaOperator.Parse("Customers.Oid = ?", _Contacts.Customers.Oid)
                Else
                    TryCast(View, ListView).CollectionSource.Criteria("BySubcontractorPCI") = CriteriaOperator.Parse("1=2")
                End If
            Else
                TryCast(View, ListView).CollectionSource.Criteria("BySubcontractorPCI") = CriteriaOperator.Parse("1=2")
            End If
        End If
        '------------------DeatailViews----------------------
        If View.Id = "Subcontractor_DetailView" Then
            Frame.GetController(Of NewObjectViewController).Active.SetItemValue("", False)
            Frame.GetController(Of CloneObjectViewController).Active.SetItemValue("", False)
        End If
    End Sub
    Protected Overrides Sub OnDeactivated()
        MyBase.OnDeactivated()
        If View.Id = "Subcontractor_ListView" Then
            Frame.GetController(Of CloneObjectViewController).Active.SetItemValue("", True)
        End If
        If View.Id = "Subcontractor_SubcontractorCustomers_ListView" Then
            Frame.GetController(Of NewObjectViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of LinkUnlinkController)().Active.SetItemValue("", True)
            Frame.GetController(Of ListViewProcessCurrentObjectController).Active.SetItemValue("", True)
            Frame.GetController(Of AuditTrailVC).aGetAuditTrail.Active.SetItemValue("", True)
        End If
        If View.Id = "Subcontractor_SubcontractorWorksheetType_ListView" Then
            Frame.GetController(Of LinkUnlinkController)().Active.SetItemValue("", True)
            Frame.GetController(Of ListViewProcessCurrentObjectController).Active.SetItemValue("", True)
            Frame.GetController(Of CloneObjectViewController).Active.SetItemValue("", True)
            Frame.GetController(Of AuditTrailVC).aGetAuditTrail.Active.SetItemValue("", True)
        End If
        If View.Id = "PCIGroup_ListView_Subcontractor" Then

            Frame.GetController(Of NewObjectViewController).Active.SetItemValue("", True)
            Frame.GetController(Of DeleteObjectsViewController).Active.SetItemValue("", True)
            Frame.GetController(Of CloneObjectViewController).Active.SetItemValue("", True)
            Frame.GetController(Of LinkUnlinkController)().Active.SetItemValue("", True)
            Frame.GetController(Of ListViewProcessCurrentObjectController).Active.SetItemValue("", True)
            Frame.GetController(Of CloneObjectViewController).Active.SetItemValue("", True)
        End If
        '------------------DeatailViews----------------------
        If View.Id = "Subcontractor_DetailView" Then
            Frame.GetController(Of NewObjectViewController).Active.SetItemValue("", True)
            Frame.GetController(Of CloneObjectViewController).Active.SetItemValue("", True)
        End If
    End Sub
    Private Sub aSelectCustomersSubcontractor_CustomizePopupWindowParams(sender As Object, e As DevExpress.ExpressApp.Actions.CustomizePopupWindowParamsEventArgs) Handles aSelectCustomersSubcontractor.CustomizePopupWindowParams
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace


        Dim _CS As New CollectionSource(_onew, GetType(Customers))

        '_CS.BeginUpdateCriteria()
        '_CS.Criteria.Clear()
        '_CS.Criteria("First") = ""
        '_CS.EndUpdateCriteria()

        e.View = Application.CreateListView("Customers_ListView_Select", _CS, False)
    End Sub
    Private Sub aSelectCustomersSubcontractor_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.PopupWindowShowActionExecuteEventArgs) Handles aSelectCustomersSubcontractor.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _Customers As Customers
        Dim _Subcontractor As Subcontractor = TryCast(TryCast(View, ListView).CollectionSource, PropertyCollectionSource).MasterObject
        Dim _SubcontractorCustomers As SubcontractorCustomers
        Dim _Exists As Boolean = False
        If _Subcontractor Is Nothing Then Exit Sub

        If e.PopupWindow.View.SelectedObjects.Count > 0 Then

            For Each _Customers In e.PopupWindow.View.SelectedObjects
                _Exists = False
                For Each _SubcontractorCustomers In _Subcontractor.SubcontractorCustomers
                    If _SubcontractorCustomers.Oid = _Customers.Oid Then
                        _Exists = True
                    End If
                Next
                If _Exists = False Then
                    _SubcontractorCustomers = _ocur.CreateObject(Of SubcontractorCustomers)()
                    _SubcontractorCustomers.Customers = _ocur.GetObject(_Customers)
                    _SubcontractorCustomers.Subcontractor = _Subcontractor
                End If
            Next
            _ocur.CommitChanges()
        End If

        Dim nestedFrame As NestedFrame = TryCast(Frame, NestedFrame)
        If nestedFrame IsNot Nothing Then
            Dim parentView As View = nestedFrame.View
            parentView.ObjectSpace.Refresh()
        End If
    End Sub
    Private Sub aCreateSubcontractorUserRoles_CustomizePopupWindowParams(sender As Object, e As DevExpress.ExpressApp.Actions.CustomizePopupWindowParamsEventArgs) Handles aCreateSubcontractorUserRoles.CustomizePopupWindowParams
        Try
            Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
            Dim _CS As New CollectionSource(_onew, GetType(SubcontractorRoles))

            e.View = Application.CreateListView("SubcontractorRoles_ListView", _CS, False)
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly)
        End Try
    End Sub
    Private Sub aCreateSubcontractorUserRoles_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.PopupWindowShowActionExecuteEventArgs) Handles aCreateSubcontractorUserRoles.Execute
        Try
            Dim _ocur As Xpo.XPObjectSpace = View.ObjectSpace
            Dim _SubcontractorRoles As SubcontractorRoles = TryCast(e.PopupWindow.View.SelectedObjects(0), SubcontractorRoles)
            Dim _Subcontractor As Subcontractor = TryCast(TryCast(TryCast(View, ListView).CollectionSource, PropertyCollectionSource).MasterObject, Subcontractor)
            For Each _User As DevExpress.Persistent.BaseImpl.User In View.SelectedObjects
                Dim _SubcontractorUserRoles As SubcontractorUserRoles = _ocur.CreateObject(Of SubcontractorUserRoles)()
                _SubcontractorUserRoles.User = _User
                _SubcontractorUserRoles.SubcontractorRoles = _ocur.GetObjectByKey(Of SubcontractorRoles)(_SubcontractorRoles.Oid)
                _SubcontractorUserRoles.Subcontractor = _ocur.GetObjectByKey(Of Subcontractor)(_Subcontractor.Oid)
            Next
            _ocur.CommitChanges()
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly)
        End Try
    End Sub
End Class
