Imports DigitalData.GUIs.Common
Imports DigitalData.Modules.EDMI.API.DatabaseWithFallback

Public Class frmAdmin_notResponsibleConfig
    Dim oProfilID As Integer = 0
    Dim oCaption As String = ""
    Dim oQuestion As String = ""
    Dim oNotResponsible_SQL As String
    Public Sub New(pProfilID As Integer)
        MyBase.New()
        oProfilID = pProfilID

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

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

    Private Sub BarButtonItem1_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItem1.ItemClick
        Dim oUpd = $"UPDATE TBPM_PROFILE SET NOT_RESP_COMMENT = '{checkCommentMuss.Checked}', NOT_RESP_SQL = '{oNotResponsible_SQL.Replace("'", "''")}',NOT_RESP_COMMENT_ATTRIBUTE = '{cmbAttrComment.Text}', CHANGED_WHO = '{User.UserName}' WHERE GUID = {oProfilID}"
        If DatabaseECM.ExecuteNonQuery(oUpd) = True Then
            oUpd = $"EXEC PRDD_GUI_LANGUAGE_PHRASE 'PM','PROFILE_NOT_RESPONSIBLE_QUEST{oProfilID}','{USER_LANGUAGE}','PROFILE_NOT_RESPONSIBLE_QUEST','{txtPROFILE_NOT_RESPONSIBLE_QUEST.Text}','','{USER_USERNAME}','{oProfilID}',0"
            LOGGER.Debug($"oUpd: {oUpd}")
            If DatabaseECM.ExecuteNonQuery(oUpd) Then

                oUpd = $"EXEC PRDD_GUI_LANGUAGE_PHRASE 'PM','PROFILE_NOT_RESPONSIBLE_CAPT{oProfilID}','{USER_LANGUAGE}','PROFILE_NOT_RESPONSIBLE_CAPT','{txtCaption_NotResponsible.Text}','','{USER_USERNAME}','{oProfilID}',0"
                If DatabaseECM.ExecuteNonQuery(oUpd) Then
                    bstitmInfo.Caption = "Data saved successfully - " + Now.ToString
                    LOGGER.Debug($"oUpd: {oUpd}")
                End If



            End If
        Else
            bstitmInfo.Caption = "Attention: Error storing data - Check Your log"
        End If
    End Sub

    Private Sub frmAdmin_notResponsibleConfig_Load(sender As Object, e As EventArgs) Handles Me.Load
        Try
            Dim oNR_Act_Attr As String
            Dim oSQL = $"SELECT NOT_RESP_SQL, NOT_RESP_COMMENT_ATTRIBUTE,NOT_RESP_COMMENT FROM TBPM_PROFILE WHERE GUID = {oProfilID}"
            Dim oDT = DatabaseECM.GetDatatable(oSQL)
            oNotResponsible_SQL = oDT.Rows(0).Item("NOT_RESP_SQL")
            oNR_Act_Attr = oDT.Rows(0).Item("NOT_RESP_COMMENT_ATTRIBUTE")
            checkCommentMuss.Checked = oDT.Rows(0).Item("NOT_RESP_COMMENT")
            LOGGER.Debug($"oNR_Act_Attr: {oNR_Act_Attr}")


            For Each oRow As DataRow In CURRENT_DT_PROFILE_LANGUAGE.Rows
                If oRow.Item("TITLE") = $"PROFILE_NOT_RESPONSIBLE_CAPT{oProfilID}" And oRow.Item("LANGUAGE") = USER_LANGUAGE Then
                    oCaption = oRow.Item("STRING1")
                ElseIf oRow.Item("TITLE") = $"PROFILE_NOT_RESPONSIBLE_QUEST{oProfilID}" And oRow.Item("LANGUAGE") = USER_LANGUAGE Then
                    oQuestion = oRow.Item("STRING1")
                End If
            Next


            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.cmbAttrComment.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
                    End Select
                    If oOk Then
                        Me.cmbAttrComment.Items.Add(oRow.Item("ATTR_TITLE"))
                    End If
                Next
            End If
            If oNR_Act_Attr <> "" Then
                cmbAttrComment.SelectedIndex = cmbAttrComment.FindStringExact(oNR_Act_Attr)
            Else
                cmbAttrComment.SelectedIndex = -1
            End If

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

    End Sub

    Private Sub btnNotResponsibleSQL_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnNotResponsibleSQL.ItemClick
        Try
            CURRENT_ProfilGUID = oProfilID
            CURRENT_DESIGN_TYPE = "SQL_NOT_RESPONSIBLE"
            Dim oldSQL = oNotResponsible_SQL
            Dim oForm As New frmSQLEditor(LOGCONFIG, DatabaseECM) With {
                .SQLCommand = oNotResponsible_SQL,
                .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 NotResponsible-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}', NOT_RESP_SQL = '{oForm.SQLCommand.Replace("'", "''")}' WHERE GUID = {CURRENT_ProfilGUID}"
                        If DatabaseFallback.ExecuteNonQueryECM(oUpdate) = True Then
                            oNotResponsible_SQL = oForm.SQLCommand
                            bstitmInfo.Caption = $"SQL saved - {Now.ToLongTimeString}"
                            CHANGES_FORM_DESIGN = True
                        End If
                    End If
                End If
            End If
        Catch ex As Exception
            MsgBox("Error in Saving Profile SQL: " & vbNewLine & vbNewLine & ex.Message)
        End Try
    End Sub

    Private Sub frmAdmin_notResponsibleConfig_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
        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