Imports Microsoft.VisualBasic
Imports System
Imports DevExpress.Xpo
Imports DevExpress.ExpressApp
Imports DevExpress.Data.Filtering
Imports System.Collections.Generic
Imports DevExpress.ExpressApp.Actions
Imports DevExpress.ExpressApp.Scheduler
Imports DevExpress.ExpressApp.Scheduler.Win
Imports DevExpress.Data.Helpers
Imports DevExpress.Persistent.BaseImpl
Imports DevExpress.XtraScheduler


Public Class HRPersonEventVC
    Inherits DevExpress.ExpressApp.ViewController
    Private _SchedulerListEditor As SchedulerListEditor
    Public Sub New()
        InitializeComponent()
        RegisterActions(components)
    End Sub
    Protected Overloads Overrides Sub OnActivated()
        MyBase.OnActivated()

    End Sub
    Protected Overloads Overrides Sub OnViewControlsCreated()
        MyBase.OnViewControlsCreated()
        If View.Id = "HRPersonEvent_ListView_CurrentUser" Then
            Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
            _SchedulerListEditor = TryCast((CType(View, ListView)).Editor, SchedulerListEditor)
            If _SchedulerListEditor IsNot Nothing Then
                InitSchedulerControl(_SchedulerListEditor.SchedulerControl)
                Dim _Resources_Collection As XPCollection = TryCast(_SchedulerListEditor.ResourcesDataSource, XPCollection)
                Dim _HRPerson As HRPerson = _ocur.FindObject(Of HRPerson)(CriteriaOperator.Parse("User=?", _ocur.GetObject(SecuritySystem.CurrentUser)))
                If _HRPerson Is Nothing Then
                    _Resources_Collection.Criteria = CriteriaOperator.Parse("1=2")
                Else
                    _Resources_Collection.Criteria = New BinaryOperator("Oid", _HRPerson.Oid)
                End If
            End If
        End If
        If View.Id Like "HRPersonEvent_ListView*" Then
            _SchedulerListEditor = TryCast((CType(View, ListView)).Editor, SchedulerListEditor)
            If _SchedulerListEditor IsNot Nothing Then
                AddHandler _SchedulerListEditor.SchedulerControl.AppointmentViewInfoCustomizing, AddressOf SchedulerListEditor_SchedulerControl_AppointmentViewInfoCustomizing
            End If
        End If
        If View.Id = "HRPersonEvent_ListView_All" Then
            Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
            _SchedulerListEditor = TryCast((CType(View, ListView)).Editor, SchedulerListEditor)
            If _SchedulerListEditor IsNot Nothing Then
                InitSchedulerControl(_SchedulerListEditor.SchedulerControl)
                Dim _Resources_Collection As XPCollection = TryCast(_SchedulerListEditor.ResourcesDataSource, XPCollection)
                Dim _HRPersonEventCriteria As HRPersonEventCriteria = _ocur.FindObject(Of HRPersonEventCriteria)(CriteriaOperator.Parse("ViewID=? and User.Oid=?", View.Id, SecuritySystem.CurrentUser.oid))
                If _HRPersonEventCriteria IsNot Nothing Then
                    _Resources_Collection.CriteriaString = _HRPersonEventCriteria.Criteria
                    _SchedulerListEditor.SchedulerControl.ActiveViewType = _HRPersonEventCriteria.ActiveViewType
                End If
            End If
        End If
    End Sub
    Protected Overrides Sub OnDeactivated()
        MyBase.OnDeactivated()
        If View.Id = "HRPersonEvent_ListView_All" Then
            Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
            _SchedulerListEditor = TryCast((CType(View, ListView)).Editor, SchedulerListEditor)
            If _SchedulerListEditor IsNot Nothing Then
                Dim _HRPersonEventCriteria As HRPersonEventCriteria = _ocur.FindObject(Of HRPersonEventCriteria)(CriteriaOperator.Parse("ViewID=? and User.Oid=?", View.Id, SecuritySystem.CurrentUser.oid))
                If _HRPersonEventCriteria IsNot Nothing Then
                    _HRPersonEventCriteria.ActiveViewType = _SchedulerListEditor.SchedulerControl.ActiveViewType
                    _ocur.CommitChanges()
                End If
            End If
        End If
    End Sub
    Private Sub InitSchedulerControl(_SchedulerControl As DevExpress.XtraScheduler.SchedulerControl)
        _SchedulerControl.Views.DayView.VisibleTime = New TimeOfDayInterval(New TimeSpan(7, 0, 0), New TimeSpan(19, 0, 0))
        _SchedulerControl.Views.WorkWeekView.VisibleTime = New TimeOfDayInterval(New TimeSpan(7, 0, 0), New TimeSpan(19, 0, 0))
        _SchedulerControl.OptionsCustomization.AllowAppointmentDragBetweenResources = UsedAppointmentType.None
    End Sub
    Private Sub aSelectHRPersonToScheduler_CustomizePopupWindowParams(sender As Object, e As DevExpress.ExpressApp.Actions.CustomizePopupWindowParamsEventArgs) Handles aSelectHRPersonToScheduler.CustomizePopupWindowParams
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _CS As CollectionSource = New CollectionSource(_onew, GetType(HRPerson))

        e.View = Application.CreateListView("HRPerson_ListView_Select", _CS, False)

    End Sub
    Private Sub aSelectHRPersonToScheduler_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.PopupWindowShowActionExecuteEventArgs) Handles aSelectHRPersonToScheduler.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _SchedulerListEditor As SchedulerListEditor = TryCast((CType(View, ListView)).Editor, SchedulerListEditor)
        Dim _HRPerson As HRPerson
        If _SchedulerListEditor IsNot Nothing Then
            Dim _Resources_Collection As XPCollection = TryCast(_SchedulerListEditor.ResourcesDataSource, XPCollection)
            If _Resources_Collection IsNot Nothing Then
                Dim Keys As New Collection
                Keys.Clear()
                For Each _HRPerson In e.PopupWindow.View.SelectedObjects
                    Keys.Add(_HRPerson.Oid)
                Next
                If Keys.Count <= 0 Then
                    _Resources_Collection.Criteria = Nothing
                Else
                    _Resources_Collection.Criteria = New InOperator("Oid", Keys)
                End If

                Dim _HRPersonEventCriteria As HRPersonEventCriteria = _ocur.FindObject(Of HRPersonEventCriteria)(CriteriaOperator.Parse("ViewID=? and User.Oid=?", View.Id, SecuritySystem.CurrentUser.oid))
                If _HRPersonEventCriteria Is Nothing Then
                    _HRPersonEventCriteria = _ocur.CreateObject(Of HRPersonEventCriteria)()
                End If
                _HRPersonEventCriteria.User = _ocur.FindObject(Of User)(CriteriaOperator.Parse("Oid=?", SecuritySystem.CurrentUser.oid))
                _HRPersonEventCriteria.ViewID = View.Id
                _HRPersonEventCriteria.Criteria = _Resources_Collection.CriteriaString

                _ocur.CommitChanges()
            End If
        End If
    End Sub

    Private Sub SchedulerListEditor_SchedulerControl_AppointmentViewInfoCustomizing(sender As Object, e As DevExpress.XtraScheduler.AppointmentViewInfoCustomizingEventArgs)

    End Sub

End Class
