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.Data.Filtering
Imports DevExpress.Xpo
Imports DevExpress.ExpressApp.Reports
Imports System.Drawing.Imaging
Imports DevExpress.XtraPrinting
Imports DevExpress.XtraReports.UI
Imports DevExpress.XtraPrinting.Preview
Imports DevExpress.ExpressApp.CloneObject
Imports DevExpress.XtraReports.Web
Imports System.IO
Imports DevExpress.Web.ASPxClasses.Internal
Imports DevExpress.ExpressApp.SystemModule
Imports DevExpress.ExpressApp.Reports.Web
Imports DevExpress.Persistent.BaseImpl


Public Class OperationalLog_VC
    Inherits Nuvolar.Module.OperationalLog_VC

    Dim _XafReport As XafReport

    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 = "OperationLog_ViewLogs_PopUp_DetailView" Then
            Frame.GetController(Of CloneObjectViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of ModificationsController).SaveAction.Active.SetItemValue("", False)
            Frame.GetController(Of ModificationsController).SaveAndCloseAction.Active.SetItemValue("", False)
            Frame.GetController(Of ModificationsController).SaveAndNewAction.Active.SetItemValue("", False)
        End If
        If View.Id = "OperationalLog_ListView_Support" Then
            Frame.GetController(Of DevExpress.ExpressApp.Web.SystemModule.ListViewController).EditAction.Active.SetItemValue("", False)
        End If
        If View.Id = "ReportViewer_DetailView" Then
            Frame.GetController(Of CloneObjectViewController)().Active.SetItemValue("", False)
        End If
        If View.Id = "OperationalLog_DetailView_Support" Then
            Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
            Dim _OperationalLog As OperationalLog = TryCast(View.SelectedObjects(0), OperationalLog)
            Dim _User As User = _ocur.GetObjectByKey(Of User)(_ocur.GetKeyValue(SecuritySystem.CurrentUser))
            Dim _Contacts As Contacts = _ocur.FindObject(Of Contacts)(CriteriaOperator.Parse("User=?", _User))
            Dim _WorkPlace As WorkPlace = _ocur.FindObject(Of WorkPlace)(CriteriaOperator.Parse("Customers=?", _Contacts.Customers))
            Dim _WorkState As WorkState = _ocur.FindObject(Of WorkState)(CriteriaOperator.Parse("IsOperationalLog = True"))

            If _WorkPlace IsNot Nothing And _WorkState IsNot Nothing Then
                Dim _OperationalLogParameters As OperationalLogParameters = _ocur.FindObject(Of OperationalLogParameters) _
                                                                       (CriteriaOperator.Parse("CustomersFrom=? AND WorkPlace=?", _WorkPlace.CustomersFrom, _WorkPlace))
                If _OperationalLogParameters IsNot Nothing Then
                    With _OperationalLog
                        If .Customers Is Nothing Then .Customers = _Contacts.Customers
                        If .CustomersFrom Is Nothing Then .CustomersFrom = _WorkPlace.CustomersFrom
                        If .OperationalLogParameters Is Nothing Then .OperationalLogParameters = _OperationalLogParameters
                        If .WorkPlace Is Nothing Then .WorkPlace = _WorkPlace
                        If .WorkState Is Nothing Then .WorkState = _WorkState
                    End With
                End If
                'Else
                'MsgBox(String.Format("Nincs beállítva munkahely és / vagy munkafolyamat a {0} felhasználóhoz! A naplót nem lehet menteni!", _User.FullName), MsgBoxStyle.OkOnly, "Kérem, ellenőrizze!")
            End If
        End If
    End Sub
    Protected Overrides Sub OnDeactivated()
        MyBase.OnDeactivated()
        If View.Id = "OperationLog_ViewLogs_PopUp_DetailView" Then
            Frame.GetController(Of CloneObjectViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of ModificationsController).SaveAction.Active.SetItemValue("", True)
            Frame.GetController(Of ModificationsController).SaveAndCloseAction.Active.SetItemValue("", True)
            Frame.GetController(Of ModificationsController).SaveAndNewAction.Active.SetItemValue("", True)
        End If
        If View.Id = "OperationalLog_ListView_Support" Then
            'Frame.GetController(Of DevExpress.ExpressApp.Web.SystemModule.ListViewController).EditAction.Active.SetItemValue("", True)
        End If
        If View.Id = "ReportViewer_DetailView" Then
            Frame.GetController(Of CloneObjectViewController)().Active.SetItemValue("", True)
        End If
    End Sub

    Private Sub aOperationLog_PrintLog_Execute_Execute(sender As Object, e As SimpleActionExecuteEventArgs) Handles aOperationLog_PrintLog_Execute.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _OperationLog_ViewLogs_PopUp As OperationLog_ViewLogs_PopUp = TryCast(View.SelectedObjects(0), OperationLog_ViewLogs_PopUp)
        Dim _OperationalLog As OperationalLog = _ocur.FindObject(Of OperationalLog)(CriteriaOperator.Parse("WorkPlace.Oid=?", _OperationLog_ViewLogs_PopUp.WorkPlace.Oid))
        Dim _ReportData As ReportData

        If _OperationalLog IsNot Nothing Then
            _ReportData = _OperationalLog.OperationalLogParameters.ReportData

            Dim _ActFromDate As Date = New Date(_OperationLog_ViewLogs_PopUp.FromDate.Date.Year, _OperationLog_ViewLogs_PopUp.FromDate.Date.Month, _OperationLog_ViewLogs_PopUp.FromDate.Date.Day, _OperationLog_ViewLogs_PopUp.FromDateHour, _OperationLog_ViewLogs_PopUp.FromDateMin, 0)
            Dim _ActToDate As Date = New Date(_OperationLog_ViewLogs_PopUp.ToDate.Date.Year, _OperationLog_ViewLogs_PopUp.ToDate.Date.Month, _OperationLog_ViewLogs_PopUp.ToDate.Date.Day, _OperationLog_ViewLogs_PopUp.ToDateHour, _OperationLog_ViewLogs_PopUp.ToDateMin, 0)

            Dim _OperationalLog_Collection As New XPCollection(Of OperationalLog)(_ocur.Session, CriteriaOperator.Parse("WorkPlace.Oid=? AND  ObjectCreated<? AND ObjectCreated>?", _
                                                                                              _OperationLog_ViewLogs_PopUp.WorkPlace.Oid, _ActToDate, _ActFromDate))

            Dim _OperationalLog_OidArray As String = "Oid in('"

            If _OperationalLog_Collection.Count > 0 Then
                For Each _OperationalLog_Selected As OperationalLog In _OperationalLog_Collection
                    _OperationalLog_OidArray += _OperationalLog_Selected.Oid.ToString + "','"
                Next
            Else
                _OperationalLog_OidArray = "1=2"
            End If

            If _OperationalLog_OidArray.Length > 3 Then
                _OperationalLog_OidArray = _OperationalLog_OidArray.Remove(_OperationalLog_OidArray.Length - 2, 2)
                _OperationalLog_OidArray += ")"
            End If

            Frame.GetController(Of WebReportServiceController).ShowPreview(_ReportData, CriteriaOperator.Parse(_OperationalLog_OidArray))
        End If

    End Sub

    Private Sub aOperationLog_PrintLogs_Execute(sender As Object, e As SimpleActionExecuteEventArgs) Handles aOperationLog_PrintLogs.Execute
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _OperationalLog As OperationalLog = _onew.GetObject(View.SelectedObjects(0))
        Dim _OperationLog_ViewLogs_PopUp As OperationLog_ViewLogs_PopUp = _onew.CreateObject(Of OperationLog_ViewLogs_PopUp)()

        If View.Id = "OperationalLog_ListView_Support" Then
            Dim _User As User = _onew.GetObjectByKey(Of User)(_onew.GetKeyValue(SecuritySystem.CurrentUser))
            Dim _Contacts As Contacts = _onew.FindObject(Of Contacts)(CriteriaOperator.Parse("User=?", _User))
            Dim _Workplace As WorkPlace = _onew.FindObject(Of WorkPlace)(CriteriaOperator.Parse("Customers=? and OperationalLogType='General'", _Contacts.Customers))
            _OperationLog_ViewLogs_PopUp.WorkPlace = _Workplace

        End If

        If View.Id = "OperationalLog_ListView_Support_HFR" Then
            Dim _User As User = _onew.GetObjectByKey(Of User)(_onew.GetKeyValue(SecuritySystem.CurrentUser))
            Dim _Contacts As Contacts = _onew.FindObject(Of Contacts)(CriteriaOperator.Parse("User=?", _User))
            Dim _Workplace As WorkPlace = _onew.FindObject(Of WorkPlace)(CriteriaOperator.Parse("Customers=? and OperationalLogType='HFR'", _Contacts.Customers))

            _OperationLog_ViewLogs_PopUp.WorkPlace = _Workplace
        End If

        _OperationLog_ViewLogs_PopUp.CustomersFrom = _OperationalLog.CustomersFrom

        e.ShowViewParameters.CreatedView = Application.CreateDetailView(_onew, "OperationLog_ViewLogs_PopUp_DetailView", True, _OperationLog_ViewLogs_PopUp)
        e.ShowViewParameters.TargetWindow = TargetWindow.Current
        e.ShowViewParameters.NewWindowTarget = NewWindowTarget.Default
    End Sub
    Private Sub aOperationaLog_CreateNewSupportLog_Execute(sender As Object, e As SimpleActionExecuteEventArgs) Handles aOperationaLog_CreateNewSupportLog.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _User As User = _ocur.GetObjectByKey(Of User)(_ocur.GetKeyValue(SecuritySystem.CurrentUser))
        Dim _Contacts As Contacts = _ocur.FindObject(Of Contacts)(CriteriaOperator.Parse("User=?", _User))
        Dim _WorkPlace As WorkPlace = _ocur.FindObject(Of WorkPlace)(CriteriaOperator.Parse("Customers=?", _Contacts.Customers))
        Dim _WorkState As WorkState = _ocur.FindObject(Of WorkState)(CriteriaOperator.Parse("IsOperationalLog = True"))

        If _WorkPlace IsNot Nothing And _WorkState IsNot Nothing Then
            Dim _OperationalLogParameters As OperationalLogParameters = _ocur.FindObject(Of OperationalLogParameters) _
                                                                        (CriteriaOperator.Parse("CustomersFrom=? AND WorkPlace=?", _WorkPlace.CustomersFrom, _WorkPlace))
            If _OperationalLogParameters IsNot Nothing Then
                Dim _OperationalLog As New OperationalLog(_onew.Session)
                With _OperationalLog
                    .Customers = _onew.GetObject(_Contacts.Customers)
                    .CustomersFrom = _onew.GetObject(_WorkPlace.CustomersFrom)
                    .OperationalLogParameters = _onew.GetObject(_OperationalLogParameters)
                    .WorkPlace = _onew.GetObject(_WorkPlace)
                    .WorkState = _onew.GetObject(_WorkState)
                End With

                e.ShowViewParameters.CreatedView = Application.CreateDetailView(_onew, "OperationalLog_DetailView_Support", True, _OperationalLog)
                e.ShowViewParameters.TargetWindow = TargetWindow.Current
                e.ShowViewParameters.NewWindowTarget = NewWindowTarget.Default
            End If
            ' Else
            '    MsgBox(String.Format("Nincs beállítva munkahely és / vagy munkafolyamat a {0} felhasználóhoz!", _User.FullName), MsgBoxStyle.OkOnly, "Kérem, ellenőrizze!")
        End If
    End Sub
End Class
