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.PivotGrid.Win
Imports DevExpress.XtraPivotGrid
Imports DevExpress.Data.Filtering

Public Class GLReportCashflowVC
    Inherits Nuvolar.Module.GLReportCashflowVC
    Dim _WaitFormClass As New WaitFormClass
    Private _PivotGridListEditor As PivotGridListEditor
    Private _PivotGrdidControl As PivotGridControl
    Private _Criteria As String = ""
    Public Sub New()
        MyBase.New()

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

    End Sub
    Protected Overrides Sub OnActivated()
        MyBase.OnActivated()
        AddHandler DoWork, AddressOf _WaitFormClass.DoWork
        AddHandler InitWork, AddressOf _WaitFormClass.Initwork
        AddHandler FinalWork, AddressOf _WaitFormClass.Finalwork
    End Sub
    Protected Overrides Sub OnDeactivated()
        MyBase.OnDeactivated()
        RemoveHandler DoWork, AddressOf _WaitFormClass.DoWork
        RemoveHandler InitWork, AddressOf _WaitFormClass.Initwork
        RemoveHandler FinalWork, AddressOf _WaitFormClass.Finalwork
    End Sub
    Protected Overrides Sub OnViewControlsCreated()
        MyBase.OnViewControlsCreated()
        If View.Id = "GLReportCashflowHeader_GLReportCashflowRows_ListView_Pivot" Then
            _PivotGridListEditor = TryCast(TryCast(View, ListView).Editor, PivotGridListEditor)

            If _PivotGridListEditor IsNot Nothing Then
                _PivotGrdidControl = _PivotGridListEditor.PivotGridControl
                AddHandler _PivotGrdidControl.CellClick, AddressOf PivotGrdidControl_CellClick
            End If
        End If
    End Sub

    Private Sub aViewPivotDetailCashflow_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aViewPivotDetailCashflow.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _CS As CollectionSource = New CollectionSource(_onew, GetType(GLReportCashflowRows))
        Dim _GLReportCashflowHeader As GLReportCashflowHeader = Nothing

        If TryCast(TryCast(View, ListView).CollectionSource, PropertyCollectionSource) IsNot Nothing Then
            _GLReportCashflowHeader = TryCast(TryCast(TryCast(View, ListView).CollectionSource, PropertyCollectionSource).MasterObject, GLReportCashflowHeader)
        Else
            If TryCast(View, ListView).CollectionSource.List.Count > 0 Then
                _GLReportCashflowHeader = TryCast(TryCast(View, ListView).CollectionSource.List(0), GLReportCashflowRows).GLReportCashflowHeader
            End If
        End If


        If LTrim(RTrim(_Criteria <> "")) Then
            _CS.BeginUpdateCriteria()
            _CS.Criteria("MasterFilter") = CriteriaOperator.Parse("GLReportCashflowHeader.Oid=?", _GLReportCashflowHeader.Oid)
            _CS.Criteria("DetailFilter") = CriteriaOperator.Parse(_Criteria)
            _CS.EndUpdateCriteria()

            e.ShowViewParameters.CreatedView = Application.CreateListView("GLReportCashflowRows_ListView_PivotDetail", _CS, True)

        End If
    End Sub

    Private Sub PivotGrdidControl_CellClick(sender As Object, e As PivotCellEventArgs)
        Dim _RowPivotGridField As PivotGridField
        Dim _ColumnPivotGridField As PivotGridField
        '_PivotGrdidControl.CreateDrillDownDataSource(e.ColumnIndex, e.RowIndex)

        Dim s As String
        If e.RowValueType = PivotGridValueType.Value Then
            If e.ColumnValueType = PivotGridValueType.Value Then
                _Criteria = ""
                For Each _RowPivotGridField In e.GetRowFields
                    s = CStr(_PivotGrdidControl.GetFieldValue(_RowPivotGridField, e.RowIndex))
                    If LTrim(RTrim(s)) <> "" Then
                        If _Criteria = "" Then
                            _Criteria = _RowPivotGridField.ExpressionFieldName.ToString & "='"
                            _Criteria = _Criteria & s & "'" & Chr(13) & Chr(10)
                        Else
                            _Criteria = _Criteria & " and " & _RowPivotGridField.ExpressionFieldName.ToString & "='"
                            _Criteria = _Criteria & s & "'" & Chr(13) & Chr(10)
                        End If
                    End If
                Next

                For Each _ColumnPivotGridField In e.GetColumnFields
                    s = CStr(_PivotGrdidControl.GetFieldValue(_ColumnPivotGridField, e.ColumnIndex))
                    If LTrim(RTrim(s)) <> "" Then
                        If _Criteria = "" Then
                            _Criteria = _ColumnPivotGridField.ExpressionFieldName.ToString & "='"
                            _Criteria = _Criteria & s & "'" & Chr(13) & Chr(10)
                        Else
                            _Criteria = _Criteria & " and " & _ColumnPivotGridField.ExpressionFieldName.ToString & "='"
                            _Criteria = _Criteria & s & "'" & Chr(13) & Chr(10)
                        End If
                    End If
                Next

            End If
        End If
    End Sub

End Class
