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.Win.Editors

Public Class Worksheet_VC_ListView
    Inherits DevExpress.ExpressApp.ViewController(Of ListView)
    Private _GridListEditor As GridListEditor

    Public Sub New()
        MyBase.New()
        InitializeComponent()
        RegisterActions(components)
    End Sub

    Protected Overrides Sub OnDeactivated()
        MyBase.OnDeactivated()
        If View.Id = "WorkSheet_Header_WorkSheet_Rows_ListView" Then
            If _GridListEditor IsNot Nothing Then
                RemoveHandler _GridListEditor.GridView.CellValueChanged, AddressOf WorkSheet_Header_WorkSheet_Rows_ListView_CellValueChanged
            End If
        End If
        If View.Id = "WorkSheet_Header_Worksheet_CostRows_ListView" Then
            If _GridListEditor IsNot Nothing Then
                RemoveHandler _GridListEditor.GridView.CellValueChanged, AddressOf WorkSheet_Header_Worksheet_CostRows_ListView_CellValueChanged
            End If
        End If
    End Sub
    Protected Overrides Sub OnViewControlsCreated()
        MyBase.OnViewControlsCreated()

        If View.Id = "WorkSheet_Header_WorkSheet_Rows_ListView" Then
            _GridListEditor = TryCast(View.Editor, GridListEditor)

            If _GridListEditor IsNot Nothing Then
                AddHandler _GridListEditor.GridView.CellValueChanged, AddressOf WorkSheet_Header_WorkSheet_Rows_ListView_CellValueChanged
            End If

            'If _GridListEditor IsNot Nothing Then
            '    _GridListEditor.AllowEdit = True
            '    For i As Integer = 0 To _GridListEditor.Columns.Count - 1
            '        If _GridListEditor.GridView.Columns(i).FieldName <> "RowGroup1" And _
            '            _GridListEditor.GridView.Columns(i).FieldName <> "RowGroup2" And _
            '            _GridListEditor.GridView.Columns(i).FieldName <> "ShortName" And _
            '            _GridListEditor.GridView.Columns(i).FieldName <> "Description" And _
            '            _GridListEditor.GridView.Columns(i).FieldName <> "Quantity" 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 i
            'End If
        End If
        If View.Id = "WorkSheet_Header_Worksheet_CostRows_ListView" Then
            _GridListEditor = TryCast(View.Editor, GridListEditor)

            If _GridListEditor IsNot Nothing Then
                AddHandler _GridListEditor.GridView.CellValueChanged, AddressOf WorkSheet_Header_Worksheet_CostRows_ListView_CellValueChanged
            End If

            'If _GridListEditor IsNot Nothing Then
            '    _GridListEditor.AllowEdit = False
            '    For i As Integer = 0 To _GridListEditor.Columns.Count - 1
            '        If _GridListEditor.GridView.Columns(i).FieldName = "FinalPriceHUF_Out" Or _
            '            _GridListEditor.GridView.Columns(i).FieldName = "FinalPriceDEV_Out" Or _
            '            _GridListEditor.GridView.Columns(i).FieldName = "Controlling!" Then

            '            _GridListEditor.GridView.Columns(i).OptionsColumn.ReadOnly = False
            '            _GridListEditor.GridView.Columns(i).OptionsColumn.AllowEdit = True
            '            _GridListEditor.GridView.Columns(i).OptionsColumn.TabStop = True
            '        End If
            '    Next i
            'End If
        End If
        'If View.Id = "CreateInvoiceFromWorksheetCost_PopUp_Worksheet_CostRows_ListView" Then
        '    _GridListEditor = TryCast(View.Editor, GridListEditor)
        '    If _GridListEditor IsNot Nothing Then
        '        _GridListEditor.AllowEdit = True
        '        For i As Integer = 0 To _GridListEditor.Columns.Count - 1
        '            If _GridListEditor.GridView.Columns(i).FieldName <> "Controlling!" 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 i
        '    End If

        'End If
    End Sub
    Private Sub WorkSheet_Header_WorkSheet_Rows_ListView_CellValueChanged(sender As Object, e As DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs)
        Dim nestedFrame As NestedFrame = TryCast(Frame, NestedFrame)
        If nestedFrame IsNot Nothing Then
            Dim parentView As View = nestedFrame.View
            parentView.ObjectSpace.CommitChanges()
            parentView.ObjectSpace.Refresh()
        End If
    End Sub
    Private Sub WorkSheet_Header_Worksheet_CostRows_ListView_CellValueChanged(sender As Object, e As DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs)
        Try
            Dim nestedFrame As NestedFrame = TryCast(Frame, NestedFrame)
            If nestedFrame IsNot Nothing Then
                Dim parentView As View = nestedFrame.View
                Dim _Worksheet_CostRows As Worksheet_CostRows = TryCast(parentView.SelectedObjects(0), Worksheet_CostRows)

                If e.Column.FieldName = "FinalPriceHUF_Out" Then
                    _Worksheet_CostRows.SumPriceHUF_Out = _Worksheet_CostRows.FinalPriceHUF_Out * _Worksheet_CostRows.QTT
                End If
                If e.Column.FieldName = "FinalPriceDEV_Out" Then
                    _Worksheet_CostRows.SumPriceDEV_Out = _Worksheet_CostRows.FinalPriceDEV_Out * _Worksheet_CostRows.QTT
                End If
                If _Worksheet_CostRows.Worksheet_Header.CurrencyName.ShortName <> "HUF" Then
                    _Worksheet_CostRows.DifferentPercent = Math.Round((1 - (_Worksheet_CostRows.FinalPriceDEV_In / _Worksheet_CostRows.FinalPriceDEV_Out)) * 100, 2, MidpointRounding.AwayFromZero)
                    _Worksheet_CostRows.DifferentDEV = (_Worksheet_CostRows.FinalPriceDEV_Out - _Worksheet_CostRows.FinalPriceDEV_In) * _Worksheet_CostRows.QTT
                Else
                    _Worksheet_CostRows.DifferentPercent = Math.Round((1 - (_Worksheet_CostRows.FinalPriceHUF_In / _Worksheet_CostRows.FinalPriceHUF_Out)) * 100, 2, MidpointRounding.AwayFromZero)
                    _Worksheet_CostRows.DifferentHUF = (_Worksheet_CostRows.FinalPriceHUF_Out - _Worksheet_CostRows.FinalPriceHUF_In) * _Worksheet_CostRows.QTT
                End If

                parentView.ObjectSpace.CommitChanges()
                parentView.ObjectSpace.Refresh()
            End If
        Catch ext As Exception
            MsgBox(ext.Message)
        End Try
    End Sub
End Class
