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.Editors
Imports DevExpress.XtraRichEdit.API.Native
Imports DevExpress.XtraRichEdit
Imports DevExpress.Data.Filtering
Imports DevExpress.Persistent.BaseImpl

Public Class ContractTemplate_VC
    Inherits SISBusiness.Module.ContractTemplate_VC

    Private _RichEditControl As RichEditControl

    Public Sub New()
        MyBase.New()
        InitializeComponent()
        RegisterActions(components)
    End Sub
    Protected Overrides Sub OnActivated()
        MyBase.OnActivated()
        If View.Id = "ContractTemplate_DetailView" Then
            Dim _RTFDocumentsHandler As RTFDocumentsHandler = TryCast(View.SelectedObjects(0), RTFDocumentsHandler)
            Dim _DetailView As DetailView = TryCast(View, DetailView)
            Dim _RichEditContolViewItem As ViewItem = _DetailView.FindItem("DocumentBody")
            If _RichEditContolViewItem IsNot Nothing Then
                AddHandler _RichEditContolViewItem.ControlCreated, AddressOf RichEditContolViewItem_ControlCreated
            End If
        End If
    End Sub
    Private Sub aContractTemplate_InsertDocVariable_Execute(sender As Object, e As SimpleActionExecuteEventArgs) Handles aContractTemplate_InsertDocVariable.Execute
        Try
            Dim _ContractTemplate As ContractTemplate = TryCast(View.SelectedObjects(0), ContractTemplate)
            If _ContractTemplate Is Nothing Then Throw New Exception("*Váratlan hiba történt, a folyamat megszakadt!")
            If String.IsNullOrEmpty(_ContractTemplate.ContractsProperties) Then Throw New Exception("*Nem lett kiválasztva a kapcsolódó objektum!")
            _RichEditControl.Document.Fields.Add(_RichEditControl.Document.CaretPosition, "DOCVARIABLE Contracts." + _ContractTemplate.ContractsProperties)
            _ContractTemplate.ContractsProperties = ""
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, "Hiba!")
        End Try
    End Sub

    Private Sub RichEditContolViewItem_ControlCreated(sender As Object, e As EventArgs)
        Dim _ItemControl As Object = TryCast(sender, ViewItem).Control
        Dim _RichEditUserControl As RichEditUserControl = TryCast(_ItemControl, RichEditUserControl)
        _RichEditControl = TryCast(_ItemControl, RichEditUserControl).RichEditControl
    End Sub

    Private Sub aContractTemplate_InsertAPCSheetDocVariable_Execute(sender As Object, e As SimpleActionExecuteEventArgs) Handles aContractTemplate_InsertAPCSheetDocVariable.Execute
        Try
            Dim _ContractTemplate As ContractTemplate = TryCast(View.SelectedObjects(0), ContractTemplate)
            If _ContractTemplate Is Nothing Then Throw New Exception("*Váratlan hiba történt, a folyamat megszakadt!")
            If String.IsNullOrEmpty(_ContractTemplate.APCSheetByProductsProperties) Then Throw New Exception("*Nem lett kiválasztva a kapcsolódó objektum!")
            _RichEditControl.Document.Fields.Add(_RichEditControl.Document.CaretPosition, "DOCVARIABLE APCSheetByProducts." + _ContractTemplate.APCSheetByProductsProperties)
            _ContractTemplate.APCSheetByProductsProperties = ""
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, "Hiba!")
        End Try
    End Sub
    Private Sub aInsertRTFText_Execute(sender As Object, e As SimpleActionExecuteEventArgs) Handles aInsertRTFText.Execute
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _User As User = _onew.GetObjectByKey(Of User)(_onew.GetKeyValue(SecuritySystem.CurrentUser))
        Dim _RTFCopy As RTFCopy = _onew.FindObject(Of RTFCopy)(CriteriaOperator.Parse("UserCreated=?", _User))
        Dim _ContractTemplate As ContractTemplate = TryCast(View.SelectedObjects(0), ContractTemplate)

        Dim _DocPos As DocumentPosition = _RichEditControl.Document.CaretPosition

        _RichEditControl.Document.InsertHtmlText(_DocPos, _RTFCopy.RTFString)
    End Sub
End Class
