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

Public Class DateYearCriteriaVC
    Inherits DevExpress.ExpressApp.ViewController

    Public Sub New()
        MyBase.New()

        'This call is required by the Component Designer.
        InitializeComponent()
        RegisterActions(components)
        'Me.aDateYearFilter.Items.Clear()
        'Me.aDateYearFilter.Items.Add(
        For Each _item In Me.aDateYearFilter.Items
            Select Case _item.Caption
                Case "CurrentYear"
                    _item.Caption = Year(Now).ToString
                    _item.Id = "CurrentYear"
                    Me.aDateYearFilter.SelectedItem = _item
                Case "PrevYear"
                    _item.Caption = Year(DateAdd(DateInterval.Year, -1, Now)).ToString
                    _item.Id = "PrevYear"
                Case "Prev2Year"
                    _item.Caption = Year(DateAdd(DateInterval.Year, -2, Now)).ToString
                    _item.Id = "Prev2Year"
                Case "Prev3Year"
                    _item.Caption = Year(DateAdd(DateInterval.Year, -3, Now)).ToString
                    _item.Id = "Prev3Year"
                Case "Prev4Year"
                    _item.Caption = Year(DateAdd(DateInterval.Year, -4, Now)).ToString
                    _item.Id = "Prev4Year"
                Case "All"
                    _item.Id = "All"
            End Select
        Next

    End Sub

    Protected Overrides Sub OnActivated()
        MyBase.OnActivated()

        Frame.GetController(Of DateYearCriteriaVC).aDateYearFilter.Active.SetItemValue("", False)

        If TryCast(View, ListView) IsNot Nothing Then
            Dim _ListView As ListView = TryCast(View, ListView)
            Dim _IModelListView As IModelListView
            Dim _IModelListViewExtender As IModelListViewExtender

            _IModelListView = _ListView.Model
            _IModelListViewExtender = TryCast(_IModelListView, IModelListViewExtender)


            If _IModelListViewExtender.DefaultDateYearInFilter = True Then
                Frame.GetController(Of DateYearCriteriaVC).aDateYearFilter.Active.SetItemValue("", True)
                Dim _Item As DevExpress.ExpressApp.Actions.ChoiceActionItem = Me.aDateYearFilter.FindItemByIdPath("CurrentYear")
                If _Item IsNot Nothing Then
                    Me.aDateYearFilter.SelectedItem = _Item
                    If _ListView.Id Like "*LookupListView*" Then
                    Else
                        _ListView.CollectionSource.BeginUpdateCriteria()
                        _ListView.CollectionSource.Criteria("DateYearFilter") = CriteriaOperator.Parse("GetYear(" & _IModelListViewExtender.DefaultDateYearInFilterProperty & ")=?", Year(Now).ToString)
                        _ListView.CollectionSource.EndUpdateCriteria()
                    End If
                End If
            End If
        End If
    End Sub

    Private Sub aDateYearFilter_Execute(sender As Object, e As SingleChoiceActionExecuteEventArgs) Handles aDateYearFilter.Execute
        Dim _ListView As ListView = TryCast(View, ListView)
        Dim _IModelListView As IModelListView
        Dim _IModelListViewExtender As IModelListViewExtender

        


        If _ListView IsNot Nothing Then
            If _ListView.Id Like "*LookupListView*" Then Exit Sub
            _IModelListView = _ListView.Model
            _IModelListViewExtender = TryCast(_IModelListView, IModelListViewExtender)

         
            _ListView.CollectionSource.BeginUpdateCriteria()
            Select Case e.SelectedChoiceActionItem.Caption
                Case Year(Now).ToString
                    _ListView.CollectionSource.Criteria("DateYearFilter") = CriteriaOperator.Parse("GetYear(" & _IModelListViewExtender.DefaultDateYearInFilterProperty & ")=?", Year(Now).ToString)
                Case Year(DateAdd(DateInterval.Year, -1, Now)).ToString
                    _ListView.CollectionSource.Criteria("DateYearFilter") = CriteriaOperator.Parse("GetYear(" & _IModelListViewExtender.DefaultDateYearInFilterProperty & ")=?", Year(DateAdd(DateInterval.Year, -1, Now)).ToString)
                Case Year(DateAdd(DateInterval.Year, -2, Now)).ToString
                    _ListView.CollectionSource.Criteria("DateYearFilter") = CriteriaOperator.Parse("GetYear(" & _IModelListViewExtender.DefaultDateYearInFilterProperty & ")=?", Year(DateAdd(DateInterval.Year, -2, Now)).ToString)
                Case Year(DateAdd(DateInterval.Year, -3, Now)).ToString
                    _ListView.CollectionSource.Criteria("DateYearFilter") = CriteriaOperator.Parse("GetYear(" & _IModelListViewExtender.DefaultDateYearInFilterProperty & ")=?", Year(DateAdd(DateInterval.Year, -3, Now)).ToString)
                Case Year(DateAdd(DateInterval.Year, -4, Now)).ToString
                    _ListView.CollectionSource.Criteria("DateYearFilter") = CriteriaOperator.Parse("GetYear(" & _IModelListViewExtender.DefaultDateYearInFilterProperty & ")=?", Year(DateAdd(DateInterval.Year, -4, Now)).ToString)
                Case Else
                    _ListView.CollectionSource.BeginUpdateCriteria()
                    _ListView.CollectionSource.Criteria.Remove("DateYearFilter")
                    _ListView.CollectionSource.EndUpdateCriteria()
            End Select
            _ListView.CollectionSource.EndUpdateCriteria()
        End If
    End Sub
End Class
