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
Imports DevExpress.Xpo
Imports DevExpress.Data.Filtering
Imports DevExpress.ExpressApp.SystemModule
Imports DevExpress.XtraGrid.Columns

Public Class StorageIn_VC
    Inherits DevExpress.ExpressApp.ViewController

    Private _GridListEditor As GridListEditor

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

    Protected Overrides Sub OnActivated()
        MyBase.OnActivated()
        If View.Id = "StorageInHeader_DetailView" Then
            AddHandler View.ControlsCreated, AddressOf StorageInHeader_DetailView_ControlCreated
        End If
    End Sub
    Protected Overrides Sub OnDeactivated()
        MyBase.OnDeactivated()
        If View.Id = "StorageInHeader_StorageInRows_ListView" Then
            If _GridListEditor IsNot Nothing Then
                RemoveHandler _GridListEditor.GridView.CellValueChanged, AddressOf StorageInHeader_StorageInRows_ListView_CellValueChanged
            End If
        End If
    End Sub

    Protected Overrides Sub OnViewControlsCreated()
        MyBase.OnViewControlsCreated()
        If View.Id = "StorageInRows_ListView" Or _
            View.Id = "StorageInHeader_StorageInRows_ListView" Then
            _GridListEditor = TryCast(TryCast(View, ListView).Editor, GridListEditor)

            If _GridListEditor IsNot Nothing Then
                AddHandler _GridListEditor.GridView.CellValueChanged, AddressOf StorageInHeader_StorageInRows_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 <> "QTT_Will" 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 StorageInHeader_StorageInRows_ListView_CellValueChanged(sender As Object, e As DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs)
        If TryCast(e.Column, GridColumn).FieldName = "QTT_Will" Then
            Dim nestedFrame As NestedFrame = TryCast(Frame, NestedFrame)
            If nestedFrame IsNot Nothing Then
                Try
                    Dim parentView As View = nestedFrame.View
                    Dim _uow As New UnitOfWork(TryCast(parentView.ObjectSpace, Xpo.XPObjectSpace).Session.DataLayer)
                    Dim _StorageInRows As StorageInRows = _uow.GetObjectByKey(Of StorageInRows)(TryCast(View.SelectedObjects(0), StorageInRows).Oid)
                    If _StorageInRows.StorageInHeader.IsStorno = False Then
                        Dim _StorageComputed As StorageComputed = _uow.FindObject(Of StorageComputed)(CriteriaOperator.Parse("Storage=? AND Products=?", _StorageInRows.StorageInHeader.Storage, _StorageInRows.Products))
                        Dim _FullResource As Double = _StorageComputed.QTTFree + _StorageComputed.QTTReservedIn
                        Dim _StorageInRows_QTT_Will_Sum As Double = 0
                        _StorageInRows.QTT_Will = e.Value
                        _StorageInRows.Save()

                        Dim _StorageInRows_Collection As New XPCollection(Of StorageInRows)(PersistentCriteriaEvaluationBehavior.InTransaction, TryCast(_uow, Session), CriteriaOperator.Parse("Products=? AND StorageInHeader.Storage=?", _StorageInRows.Products, _StorageInRows.StorageInHeader.Storage))
                        For Each _StorageInRows_Collection_Item As StorageInRows In _StorageInRows_Collection
                            _StorageInRows_QTT_Will_Sum += _StorageInRows_Collection_Item.QTT_Will
                        Next
                        _StorageComputed.QTTReservedIn = _StorageInRows_QTT_Will_Sum

                        _uow.CommitChanges()
                        If Frame.GetController(Of RefreshController).RefreshAction.Active Then
                            Frame.GetController(Of RefreshController).RefreshAction.DoExecute()
                        End If

                    End If
                Catch exp As Exception
                    MsgBox(exp.Message)
                End Try
            End If
        End If
    End Sub

    Private Sub StorageInHeader_DetailView_ControlCreated(sender As Object, e As EventArgs)
        If View.Id = "StorageInHeader_DetailView" Then
            Dim _row_Products_Editor As DevExpress.ExpressApp.Win.Editors.LookupPropertyEditor
            _row_Products_Editor = TryCast(View, DetailView).FindItem("row_Products")

            AddHandler _row_Products_Editor.ControlValueChanged, AddressOf _row_Products_Editor_ControlValueChanged
        End If
    End Sub

    Private Sub _row_Products_Editor_ControlValueChanged(sender As Object, e As EventArgs)
        Dim _row_QTT As DevExpress.ExpressApp.Win.Editors.DoublePropertyEditor
        _row_QTT = TryCast(View, DetailView).FindItem("row_QTT_WILL")
        If _row_QTT IsNot Nothing Then
            Dim _row_Products_Editor As DevExpress.ExpressApp.Win.Editors.LookupPropertyEditor
            _row_Products_Editor = TryCast(View, DetailView).FindItem("row_Products")
            If _row_Products_Editor IsNot Nothing Then
                Dim _Products As Nuvolar.Module.Products = TryCast(_row_Products_Editor.ControlValue, Nuvolar.Module.Products)
                If _Products IsNot Nothing Then
                    Dim _SpinEdit As DevExpress.XtraEditors.SpinEdit
                    _SpinEdit = TryCast(_row_QTT.Control, DevExpress.XtraEditors.SpinEdit)
                    If _SpinEdit IsNot Nothing Then
                        Select Case _Products.DecimalIn
                            Case 0
                                _SpinEdit.Properties.Mask.EditMask = "#,##0"
                                _SpinEdit.Properties.Mask.UseMaskAsDisplayFormat = True
                            Case 1
                                _SpinEdit.Properties.Mask.EditMask = "#,##0.0"
                                _SpinEdit.Properties.Mask.UseMaskAsDisplayFormat = True
                            Case 2
                                _SpinEdit.Properties.Mask.EditMask = "#,##0.00"
                                _SpinEdit.Properties.Mask.UseMaskAsDisplayFormat = True
                            Case 3
                                _SpinEdit.Properties.Mask.EditMask = "#,##0.000"
                                _SpinEdit.Properties.Mask.UseMaskAsDisplayFormat = True
                            Case 4
                                _SpinEdit.Properties.Mask.EditMask = "#,##0.0000"
                                _SpinEdit.Properties.Mask.UseMaskAsDisplayFormat = True
                            Case Else
                                _SpinEdit.Properties.Mask.EditMask = ""
                                _SpinEdit.Properties.Mask.UseMaskAsDisplayFormat = False
                        End Select
                    End If
                End If
            End If
        End If
    End Sub

End Class
