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.XtraGrid
Imports DevExpress.XtraGrid.Views.Grid
Imports DevExpress.ExpressApp.Win.Editors
Imports DevExpress.XtraPivotGrid
Imports DevExpress.ExpressApp.PivotGrid.Win
Imports DevExpress.XtraGrid.Columns

Public Class GLCardVC
    Inherits SISBusiness.Module.GLCardVC
    Dim _WaitFormClass As New WaitFormClass
    Private _GridListEditor As GridListEditor
    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

        If View.Id = "GLCardsHeader_GLCardsRows_ListView" Then
            If _GridListEditor IsNot Nothing Then
                RemoveHandler _GridListEditor.GridView.CellValueChanged, AddressOf GLCardsHeader_GLCardsRows_ListView_CellValueChanged
            End If
        End If
    End Sub
    Private Sub GLCardVC_ViewControlsCreated(sender As Object, e As System.EventArgs) Handles Me.ViewControlsCreated
        If View.Id = "GLCardsHeader_GLCardsRows_ListView" Then
            Dim gridControl As GridControl = CType(((CType(View, ListView)).Editor).Control, GridControl)
            Dim gridView As GridView = CType(gridControl.FocusedView, GridView)
            gridView.OptionsCustomization.AllowSort = False
            gridView.OptionsCustomization.AllowGroup = False

            Dim _ListView As ListView = CType(View, ListView)
            Dim _PropertyCollectionSource As PropertyCollectionSource = CType(_ListView.CollectionSource, PropertyCollectionSource)
            _GridListEditor = TryCast(TryCast(View, ListView).Editor, GridListEditor)
            If _GridListEditor IsNot Nothing Then
                AddHandler _GridListEditor.GridView.CellValueChanged, AddressOf GLCardsHeader_GLCardsRows_ListView_CellValueChanged
                '    _GridListEditor.AllowEdit = True

                '    For i As Integer = 0 To _GridListEditor.Columns.Count - 1

                '        If _GridListEditor.GridView.Columns(i).FieldName <> "Note_Rows" Then
                '            _GridListEditor.GridView.Columns(i).OptionsColumn.ReadOnly = True
                '            _GridListEditor.GridView.Columns(i).OptionsColumn.AllowEdit = False
                '            _GridListEditor.GridView.Columns(i).OptionsColumn.TabStop = False
                '        End If

                '    Next
            End If
        End If
        If View.Id = "GLCardsHeader_GLCardsRowsPivot_ListView" Then
            Dim _PivotGridListEditor = TryCast(TryCast(View, ListView).Editor, PivotGridListEditor)
            If _PivotGridListEditor.PivotGridControl IsNot Nothing Then
                AddHandler _PivotGridListEditor.PivotGridControl.CustomFieldValueCells, AddressOf PivotGridControl_CustomFieldValueCells
            End If
        End If
    End Sub


    Private Sub GLCardsHeader_GLCardsRows_ListView_CellValueChanged(sender As Object, e As Views.Base.CellValueChangedEventArgs)
        Dim nestedFrame As NestedFrame = TryCast(Frame, NestedFrame)
        Select Case TryCast(e.Column, GridColumn).FieldName
            Case "Note_Rows"
                If nestedFrame IsNot Nothing Then
                    Frame.GetController(Of DevExpress.ExpressApp.Validation.PersistenceValidationController)().Active.SetItemValue("", False)
                    Dim parentView As View = nestedFrame.View
                    TryCast(parentView.ObjectSpace, Xpo.XPObjectSpace).CommitChanges()
                    Frame.GetController(Of DevExpress.ExpressApp.Validation.PersistenceValidationController)().Active.SetItemValue("", True)
                    parentView.ObjectSpace.Refresh()
                End If
        End Select
    End Sub

    Private Sub PivotGridControl_CustomFieldValueCells(sender As Object, e As PivotCustomFieldValueCellsEventArgs)
        Dim pivot As PivotGridControl = CType(sender, PivotGridControl)
        Dim rowFieldsCount As Integer = pivot.GetFieldsByArea(PivotArea.RowArea).Count
        Dim BallanceHUFAreaIndex As Integer = pivot.Fields("BallanceHUF").AreaIndex
        For i As Integer = 0 To e.GetCellCount(False) - 2
            Dim cell As FieldValueCell = e.GetCell(False, i)
            If cell IsNot Nothing Then
                If cell.Field IsNot Nothing Then
                    If cell.Field.AreaIndex = rowFieldsCount - 1 AndAlso Math.Round(CDec(e.GetCellValue(BallanceHUFAreaIndex, cell.MaxIndex)), 0, MidpointRounding.AwayFromZero) = 0 Then
                        e.Remove(cell)
                    End If
                End If
            End If
        Next i
    End Sub

End Class
