Imports System.ComponentModel
Imports DevExpress.XtraEditors
Imports DigitalData.GUIs.Common

Public Class frmAdmin_rejectionConfig
    Dim oProfilID As Integer = 0
    Dim oRej_SQL As String
    Dim oCaption As String = ""
    Dim oRej_SQL_Reasons As String
    Dim oRej_Act_Attr As String
    Dim oRej_Comment_Attr As String
    Public Sub New(pProfilID As Integer)
        MyBase.New()
        oProfilID = pProfilID

        InitializeComponent()
        ' Add any initialization after the InitializeComponent() call.

    End Sub

    Private Sub BarButtonItem2_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItem2.ItemClick
        Dim oUpd = $"UPDATE TBPM_PROFILE SET BTN_REJECT_CAPTION = '{txtCaption_Rejection.Text}', REJECT_SQL_REASONS = '{oRej_SQL_Reasons.Replace("'", "''")}', REJECT_ACTIONS_ATTRIBUTE = '{cmbAttrRejReas.Text}', REJECT_COMMENT_ATTRIBUTE = '{cmbAttrRejComment.Text}', CHANGED_WHO = '{User.UserName}' WHERE GUID = {oProfilID}"
        If DatabaseECM.ExecuteNonQuery(oUpd) = True Then
            oUpd = $"EXEC PRDD_GUI_LANGUAGE_PHRASE 'PM','PROFILE_REJECTION_CAPT{oProfilID}','{USER_LANGUAGE}','PROFILE_REJECTION_CAPT','{txtCaption_Rejection.Text}','','{USER_USERNAME}','{oProfilID}',0"
            If DatabaseECM.ExecuteNonQuery(oUpd) Then
                bstitmInfo.Caption = "Data saved successfully - " + Now.ToString
            End If
        Else
            bstitmInfo.Caption = "Attention: Error storing data - Check Your log"
        End If

    End Sub

    Private Sub BarButtonItem1_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItem1.ItemClick
        Try
            CURRENT_ProfilGUID = oProfilID
            CURRENT_DESIGN_TYPE = "SQL_BTNFINISH"
            Dim oldSQL = oRej_SQL
            Dim oForm As New frmSQLEditor(LOGCONFIG, DatabaseECM) With {
                .SQLCommand = oldSQL,
                .SQLConnection = 0,
                .PlaceholdersManualPrefix = "CTRL",
                .PlaceholdersManualTitle = "Controls",
                .PlaceholdersManual = CURRENT_CONTROL_NAME_LIST.ToDictionary(Function(name) name, Function(name) name)
            }
            oForm.ShowDialog()

            If oForm.DialogResult = DialogResult.OK Then
                If oldSQL <> oForm.SQLCommand Then
                    Dim oResult As MsgBoxResult = MsgBox("Do You want to save the changed SQL?", MsgBoxStyle.YesNo, ADDITIONAL_TITLE)
                    'wenn Speichern ja
                    If oResult = DialogResult.Yes Then
                        Dim oUpdate As String = $"Update TBPM_PROFILE SET CHANGED_WHO = '{USER_USERNAME}', SQL_BTN_REJECT = '{oForm.SQLCommand.Replace("'", "''")}' WHERE GUID = {CURRENT_ProfilGUID}"
                        If DatabaseFallback.ExecuteNonQueryECM(oUpdate) = True Then
                            oRej_SQL = oForm.SQLCommand
                            bstitmInfo.Caption = $"Profile SQL Rejection saved - {Now.ToLongTimeString}"
                            CHANGES_FORM_DESIGN = True
                        End If
                    End If
                End If
            End If
        Catch ex As Exception
            MsgBox("Error in Saving Profile SQLRejection: " & vbNewLine & vbNewLine & ex.Message)
        End Try
    End Sub

    Private Sub BarButtonItem4_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItem4.ItemClick
        Try
            CURRENT_ProfilGUID = oProfilID
            CURRENT_DESIGN_TYPE = "SQL_REJ_REASONS"
            Dim oldSQL = oRej_SQL_Reasons
            Dim oForm As New frmSQLEditor(LOGCONFIG, DatabaseECM) With {
                .SQLCommand = oRej_SQL_Reasons,
                .SQLConnection = 0,
                .PlaceholdersManualPrefix = "CTRL",
                .PlaceholdersManualTitle = "Controls",
                .PlaceholdersManual = CURRENT_CONTROL_NAME_LIST.ToDictionary(Function(name) name, Function(name) name)
            }
            oForm.ShowDialog()

            If oForm.DialogResult = DialogResult.OK Then
                If oldSQL <> oForm.SQLCommand Then
                    Dim oResult As MsgBoxResult = MsgBox("Do You want to save the changed Reason-SQL?", MsgBoxStyle.YesNo, ADDITIONAL_TITLE)
                    'wenn Speichern ja
                    If oResult = DialogResult.Yes Then
                        Dim oUpdate As String = $"Update TBPM_PROFILE SET CHANGED_WHO = '{USER_USERNAME}', REJECT_SQL_REASONS = '{oForm.SQLCommand.Replace("'", "''")}' WHERE GUID = {CURRENT_ProfilGUID}"
                        If DatabaseFallback.ExecuteNonQueryECM(oUpdate) = True Then
                            oRej_SQL_Reasons = oForm.SQLCommand
                            bstitmInfo.Caption = $"SQL Reasons saved - {Now.ToLongTimeString}"
                            CHANGES_FORM_DESIGN = True
                        End If
                    End If
                End If
            End If
        Catch ex As Exception
            MsgBox("Error in Saving Profile SQLRejection: " & vbNewLine & vbNewLine & ex.Message)
        End Try
    End Sub

    Private Sub frmAdmin_rejectionConfig_Load(sender As Object, e As EventArgs) Handles Me.Load
        Try
            If IDB_ACTIVE = False Then
                If MyIndicies IsNot Nothing Then
                    Dim i As Integer = 0
                    For Each index As String In MyIndicies
                        Dim _vektorString = False
                        Dim oIndexType = WINDREAM.GetTypeOfIndex(index)
                        i += 1
                        MyIndicies_Types.Add(oIndexType)
                        Select Case oIndexType
                            Case 4097
                                _vektorString = True
                            Case 36865
                                _vektorString = True
                            Case Else
                                _vektorString = False
                        End Select
                        If _vektorString = False Then
                            Me.cmbAttrRejReas.Items.Add(index)
                            Me.cmbAttrRejComment.Items.Add(index)
                        End If

                    Next
                End If
            Else
                For Each oRow As DataRow In IDBData.DTVWIDB_BE_ATTRIBUTE.Rows
                    Dim oOk As Boolean = False
                    Select Case CInt(oRow.Item("TYP_ID"))
                        Case 1
                            oOk = True
                        Case 8
                            oOk = True
                    End Select
                    If oOk Then
                        Me.cmbAttrRejReas.Items.Add(oRow.Item("ATTR_TITLE"))
                        Me.cmbAttrRejComment.Items.Add(oRow.Item("ATTR_TITLE"))
                    End If
                Next
            End If



            Dim oSQL = $"SELECT SQL_BTN_REJECT,BTN_REJECT_CAPTION,REJECT_SQL_REASONS,REJECT_ACTIONS_ATTRIBUTE,REJECT_COMMENT_ATTRIBUTE FROM TBPM_PROFILE WHERE GUID = {oProfilID}"
            Dim oDT = DatabaseECM.GetDatatable(oSQL)
            oCaption = oDT.Rows(0).Item(1)
            oRej_SQL_Reasons = oDT.Rows(0).Item(0)
            oRej_SQL_Reasons = oDT.Rows(0).Item(2)
            oRej_Act_Attr = oDT.Rows(0).Item(3)
            oRej_Comment_Attr = oDT.Rows(0).Item(4)
            For Each oRow As DataRow In CURRENT_DT_PROFILE_LANGUAGE.Rows
                If oRow.Item("TITLE") = $"BTN_REJECTION_CAPT{oProfilID}" And oRow.Item("LANGUAGE") = USER_LANGUAGE Then
                    oCaption = oRow.Item("STRING1")
                End If
            Next
            If oRej_Act_Attr <> "" Then
                cmbAttrRejReas.SelectedIndex = cmbAttrRejReas.FindStringExact(oRej_Act_Attr)
            Else
                cmbAttrRejReas.SelectedIndex = -1
            End If
            If oRej_Comment_Attr <> "" Then
                cmbAttrRejComment.SelectedIndex = cmbAttrRejComment.FindStringExact(oRej_Comment_Attr)
            Else
                cmbAttrRejComment.SelectedIndex = -1
            End If

            txtCaption_Rejection.Text = oCaption
        Catch ex As Exception
            MsgBox("Error in Loading Form: " & vbNewLine & vbNewLine & ex.Message)
        End Try


    End Sub

    Private Sub txtCaption_Rejection_TextChanged(sender As Object, e As EventArgs) Handles txtCaption_Rejection.TextChanged
        If txtCaption_Rejection.Text <> String.Empty Then
            SimpleButton1.Text = txtCaption_Rejection.Text
        End If

    End Sub

    Private Sub Label1_Click(sender As Object, e As EventArgs) Handles Label1.Click

    End Sub

    Private Sub Label3_Click(sender As Object, e As EventArgs) Handles Label3.Click

    End Sub

    Private Sub SimpleButton1_Click(sender As Object, e As EventArgs) Handles SimpleButton1.Click

    End Sub

    Private Sub frmAdmin_rejectionConfig_Closing(sender As Object, e As CancelEventArgs) Handles Me.Closing
        Dim oSQL = $"select * from TBDD_GUI_LANGUAGE_PHRASE WHERE MODULE = 'PM' AND TITLE LIKE 'PROFILE_%'"
        CURRENT_DT_PROFILE_LANGUAGE = DatabaseECM.GetDatatable(oSQL)
    End Sub
End Class