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.Scheduler.Web
Imports DevExpress.Xpo
Imports DevExpress.XtraScheduler
Imports DevExpress.ExpressApp.Web.SystemModule
Imports DevExpress.Data.Filtering
Imports DevExpress.Persistent.BaseImpl
Imports DevExpress.ExpressApp.Web.Editors
Imports DevExpress.Web.ASPxScheduler.Drawing
Imports DevExpress.Web


Public Class HRPersonEventVC
    Inherits DevExpress.ExpressApp.ViewController
    Private _SchedulerListEditor As ASPxSchedulerListEditor
    Public Sub New()
        MyBase.New()

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

    End Sub
    Protected Overrides Sub OnViewControlsCreated()
        MyBase.OnViewControlsCreated()

        If View.Id = "HRPersonEvent_ListView_Subcontractor" Then
            Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
            _SchedulerListEditor = TryCast(TryCast(View, ListView).Editor, ASPxSchedulerListEditor)

            InitASPxSchedulerControl()
            AddHandler _SchedulerListEditor.ResourceDataSourceCreated, AddressOf _SchedulerListEditor_ResourceDataSourceCreated
            AddHandler _SchedulerListEditor.SchedulerControl.InitAppointmentImages, AddressOf _SchedulerListEditor_SchedulerControl_InitAppointmentImages
        End If
    End Sub
    Private Sub InitASPxSchedulerControl()
        _SchedulerListEditor.SchedulerControl.ActiveViewType = DevExpress.XtraScheduler.SchedulerViewType.WorkWeek
        _SchedulerListEditor.SchedulerControl.Views.WorkWeekView.VisibleTime = New TimeOfDayInterval(New TimeSpan(7, 0, 0), New TimeSpan(18, 0, 0))

        Frame.GetController(Of SchedulerActionsController).EditSeriesAction.Active.SetItemValue("", False)
        Frame.GetController(Of SchedulerActionsController).OpenSeriesAction.Active.SetItemValue("", False)
        Frame.GetController(Of SchedulerActionsController).RestoreOccurrenceAction.Active.SetItemValue("", False)
        Frame.GetController(Of ListViewController).EditAction.Active.SetItemValue("", False)

    End Sub

    Private Sub _SchedulerListEditor_ResourceDataSourceCreated(sender As Object, e As Scheduler.ResourceDataSourceCreatedEventArgs)
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _Resources_Collection As XpCollection = TryCast(TryCast(e.DataSource, WebDataSource).Collection, XPCollection)
        Dim _User As User = _ocur.GetObjectByKey(Of User)(_ocur.GetKeyValue(SecuritySystem.CurrentUser))
        Dim _Contacts As Contacts = _ocur.FindObject(Of Contacts)(CriteriaOperator.Parse("User=?", _User))
        Dim _Customers As Customers = _Contacts.Customers
        Dim _CriteriaString As String = ""
        Dim _HRPerson As HRPerson
        For Each _Contacts In _Customers.Contacts
            _HRPerson = _ocur.FindObject(Of HRPerson)(CriteriaOperator.Parse("User.Oid=?", _Contacts.User.Oid.ToString))
            If _HRPerson IsNot Nothing Then
                If _CriteriaString = "" Then
                    _CriteriaString = "Oid in ('" & _HRPerson.Oid.ToString & "'"
                Else
                    _CriteriaString += ",'" & HRPerson.OidInitializationMode.ToString & "'"
                End If
            End If
        Next
        If _CriteriaString = "" Then
            _CriteriaString = "1=2"
        Else
            _CriteriaString += ")"
        End If
        If _Resources_Collection IsNot Nothing Then
            _Resources_Collection.CriteriaString = _CriteriaString
        End If
    End Sub

    Private Sub _SchedulerListEditor_SchedulerControl_InitAppointmentImages(sender As Object, e As DevExpress.Web.ASPxScheduler.AppointmentImagesEventArgs)
        Dim info As AppointmentImageInfo = New AppointmentImageInfo
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _HRPersonTasks As HRPersonTasks = _ocur.FindObject(Of HRPersonTasks)(CriteriaOperator.Parse("HRPersonEvent=?", e.Appointment.Id.ToString))

        If _HRPersonTasks IsNot Nothing Then
            info.ImageProperties = New ImageProperties("~/Images/BO_Task.png")
            e.ImageInfoList.Add(info)
        End If
    End Sub

End Class
