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.SystemModule
Imports System.IO
Imports FarPoint.Win.Spread
Imports DevExpress.Persistent.BaseImpl
Imports DevExpress.ExpressApp.Reports
Imports DevExpress.Data.Filtering
Imports DevExpress.ExpressApp.Editors

Public Class OfferOutHeaderVC
    Inherits DevExpress.ExpressApp.ViewController
    Private _FpSpread As FpSpread
    Public Sub New()
        MyBase.New()

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

    End Sub

    Protected Overrides Sub OnActivated()
        MyBase.OnActivated()
        If View.Id = "OfferOutHeader_DetailView" Then
            AddHandler Frame.GetController(Of ModificationsController)().SaveAction.Executing, AddressOf SaveAction_Executing
            AddHandler Frame.GetController(Of ModificationsController)().SaveAndCloseAction.Executing, AddressOf SaveAction_Executing
            AddHandler Frame.GetController(Of ModificationsController)().SaveAndNewAction.Executing, AddressOf SaveAction_Executing

        End If
    End Sub
    Protected Overrides Sub OnDeactivated()
        MyBase.OnDeactivated()
        If View.Id = "OffersOutHeader_DetailView" Then
            RemoveHandler Frame.GetController(Of ModificationsController)().SaveAction.Executing, AddressOf SaveAction_Executing
            RemoveHandler Frame.GetController(Of ModificationsController)().SaveAndCloseAction.Executing, AddressOf SaveAction_Executing
            RemoveHandler Frame.GetController(Of ModificationsController)().SaveAndNewAction.Executing, AddressOf SaveAction_Executing

        End If
    End Sub
    Private Sub ExcelControlViewItem_ControlCreated(ByVal sender As Object, ByVal e As EventArgs)
        Dim itemControl As Object = (CType(sender, ViewItem)).Control

        _FpSpread = TryCast(itemControl, ExcelUserControl).FpSpreadControl

        Dim _FileName As String = Path.GetTempPath & "Out.xls"
        Dim _StreamFile As New FileStream(_FileName, FileMode.Create)
        Dim _OfferOutHeader As OfferOutHeader = TryCast(View.SelectedObjects(0), OfferOutHeader)
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        If _ocur.Session.IsNewObject(_OfferOutHeader) = False Then
            If _OfferOutHeader IsNot Nothing Then
                If _OfferOutHeader.ExcelFile IsNot Nothing Then
                    _OfferOutHeader.ExcelFile.SaveToStream(_StreamFile)
                    _StreamFile.Close()
                    _FpSpread.OpenExcel(_FileName)
                End If
            End If
        End If
    End Sub
    Protected Overrides Sub OnViewControlsCreated()
        MyBase.OnViewControlsCreated()
        If View.Id = "OfferOutHeader_DetailView" Then
            Dim _DetailView As DetailView = TryCast(View, DetailView)
            Dim _ExcelControlViewItem As ViewItem
            If _DetailView IsNot Nothing Then
                If _DetailView.GetItems(Of ExcelPropertyEditor).Count > 0 Then
                    _ExcelControlViewItem = _DetailView.GetItems(Of ExcelPropertyEditor)(0)
                    If _ExcelControlViewItem IsNot Nothing Then
                        AddHandler _ExcelControlViewItem.ControlCreated, AddressOf ExcelControlViewItem_ControlCreated
                    End If
                End If
            End If
        End If
    End Sub
    Sub SaveAction_Executing(sender As Object, e As System.ComponentModel.CancelEventArgs)
        Dim _FileName As String = Path.GetTempPath & "In.xls"
        Dim _StreamFile As New FileStream(_FileName, FileMode.Create)
        Dim _OfferOutHeader As OfferOutHeader = TryCast(View.SelectedObjects(0), OfferOutHeader)
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        If _OfferOutHeader IsNot Nothing Then
            If _FpSpread IsNot Nothing Then
                _FpSpread.SaveExcel(_StreamFile)
                _StreamFile.Close()
                Dim _StreamFile2 As New FileStream(_FileName, FileMode.Open)
                _OfferOutHeader.ExcelFile = New FileData(_ocur.Session)
                _OfferOutHeader.ExcelFile.LoadFromStream("Excel worksheet", _StreamFile2)
                _OfferOutHeader.Save()
                _StreamFile2.Close()
            End If
        End If
    End Sub
End Class
