Imports System.Windows.Forms

Public Class frmTask_Editor

    Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Button.Click
        Try
            Dim upd = String.Format("UPDATE TBPMO_WORKFLOW_TASK SET COMMENT = '{0}', DUE_DATE = '{1}', STATE_ID = {2}, ACTIVE = {3}, CHANGED_WHO = '{4}', TASK_DATE = '{5}' " & _
                                    "WHERE GUID = {6}", COMMENTTextBox.Text, DUE_DATEDateTimePicker.Value, STATE_IDComboBox.SelectedValue, 1, Environment.UserName, DateTimePicker1.Value, Me.GUIDTextBox.Text)
            If ClassDatabase.Execute_non_Query(upd, True) Then
                Me.Close()
            Else
                MsgBox("Update not successfull. Please check logfile.", MsgBoxStyle.Exclamation)
            End If
            '  Me.TBPMO_WORKFLOW_TASKTableAdapter.cmdUpdate(COMMENTTextBox.Text, DUE_DATEDateTimePicker.Value, STATE_IDComboBox.SelectedValue, 1, Environment.UserName, DateTimePicker1.Value, Me.GUIDTextBox.Text)
        Catch ex As Exception
            MsgBox("Error in Exit:" & vbNewLine & ex.Message, MsgBoxStyle.Critical)
        End Try
    End Sub

    Private Sub Cancel_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel_Button.Click
        Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
        Me.Close()
    End Sub


    Private Sub frmWF_TaskEditor_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Try
            Me.TBPMO_WORKFLOW_TASK_STATETableAdapter.Connection.ConnectionString = MyConnectionString
            Me.TBPMO_WORKFLOW_TASKTableAdapter.Connection.ConnectionString = MyConnectionString
            Me.VWPMO_WF_ACTIVETableAdapter.Connection.ConnectionString = MyConnectionString
            Load_Task()

            If DD_DMSDataSet.VWPMO_WF_ACTIVE.Rows.Count = 0 Then
                Dim result As MsgBoxResult = MsgBox("Aktuell ist der Workflow anscheinend noch nicht aktiv - Wollen Sie diesen nun starten?", MsgBoxStyle.YesNo, "Frage:")
                If result = MsgBoxResult.Yes Then
                    TBPMO_WORKFLOW_TASKTableAdapter.cmdsetActive(Environment.UserName, CURRENT_RECORD_ID)
                    Load_Task()
                    If DD_DMSDataSet.VWPMO_WF_ACTIVE.Rows.Count = 0 Then
                        MsgBox("Keine aktiven Tasks - Bitte informieren Sie Ihren Systembetreuer.", MsgBoxStyle.Exclamation)
                        Me.Close()
                    End If
                Else
                    Me.Close()
                End If
            End If
        Catch ex As Exception
            MsgBox("Error Loading Form:" & vbNewLine & ex.Message, MsgBoxStyle.Critical)
            Me.Close()
        End Try

    End Sub

    Sub Load_Task()
        Try
            Me.VWPMO_WF_ACTIVETableAdapter.FillByRecord(Me.DD_DMSDataSet.VWPMO_WF_ACTIVE, CURRENT_RECORD_ID, Environment.UserName)
        Catch ex As Exception
            MsgBox("Error in Load_Task:" & vbNewLine & ex.Message, MsgBoxStyle.Critical)
        End Try
    End Sub
    Sub Load_Following()
        Try
            Dim sql = "select WF_TASK_ID,TITLE from VWPMO_WF_ACTIVE where WF_TASK_ID <> " & Me.GUIDTextBox.Text
            Dim DT As DataTable = ClassDatabase.Return_Datatable(sql, "GET FOLLOWING TASKS")


        
            'Dim sql As String = "SELECT T.GUID as [Task-ID],T1.TITLE as Task, T2.STATE_DESC As Status ,T.DUE_DATE as Fälligkeit" & _
            '                    " FROM TBPMO_WORKFLOW_TASK T, TBPMO_WORKFLOW T1, TBPMO_WORKFLOW_TASK_STATE T2" & _
            '                    " WHERE T.WF_ID = T1.GUID AND T.STATE_ID = T2.GUID AND T2.GUID NOT IN (SELECT WORKFLOW_FINAL_STATE1 FROM TBPMO_KONFIGURATIOn WHERE GUID = 1) AND T.RECORD_ID = " & CURRENT_RECORD_ID & " and T.GUID <> " & Me.GUIDTextBox.Text
            'Dim DT As DataTable = ClassDatabase.Return_Datatable(sql)
            'If DT Is Nothing Then
            '    Me.DataGridView1.Visible = False
            '    Exit Sub
            'End If
            If DT.rows.count > 0 Then
                If DT.rows.count = 1 Then
                    Label1.Text = "1 nachfolgender Task:"
                Else
                    Label1.Text = DT.rows.count.ToString & " nachfolgende Tasks:"
                End If

                ListBoxFollowing.Visible = True
                btndelete_following.Visible = True
                ListBoxFollowing.DataSource = DT
                ListBoxFollowing.DisplayMember = DT.Columns(1).ColumnName
                ListBoxFollowing.ValueMember = DT.Columns(0).ColumnName
                'Me.DataGridView1.DataSource = DT
            Else
                Label1.Text = "Keine nachfolgenden Tasks"
                Me.ListBoxFollowing.Visible = False
                btndelete_following.Visible = False
            End If
        Catch ex As Exception
            MsgBox("Error in Load_Following:" & vbNewLine & ex.Message, MsgBoxStyle.Critical)
        End Try
    End Sub
    Sub Load_User()
        Try
            Dim Sql = "SELECT GUID as [User-ID], NAME + ', ' + PRENAME AS [User] FROM TBDD_USER WHERE MODULE_RECORD_ORG = 1"
            Dim DT As DataTable = ClassDatabase.Return_Datatable(Sql)
            If DT Is Nothing = False Then
                Me.ListBoxUser.Visible = True
                Me.ListBoxUser.DataSource = DT
                Me.ListBoxUser.DisplayMember = DT.Columns(1).ColumnName
                Me.ListBoxUser.ValueMember = DT.Columns(0).ColumnName
            Else
                Me.ListBoxUser.Visible = False
            End If

        Catch ex As Exception
            MsgBox("Error in Load_User:" & vbNewLine & ex.Message, MsgBoxStyle.Critical)
        End Try
    End Sub
    Sub Load_TASK_USER()
        Try
            'Dim sql = "SELECT * FROM VWPMO_WF_USER WHERE LOWER(USERNAME) = '" & Environment.UserName & "' AND WF_TASK_ID = " & Me.GUIDTextBox.Text
            Dim sql = "SELECT * FROM VWPMO_WF_USER WHERE WF_TASK_ID = " & Me.GUIDTextBox.Text
            Dim DT As DataTable = ClassDatabase.Return_Datatable(sql)
            Dim count As Integer = 0
            If DT Is Nothing = False Then
                ListViewExclusiveUser.Items.Clear()
                For Each row As DataRow In DT.Rows
                    ListViewExclusiveUser.Items.Add(row.Item("GUID").ToString)
                    ListViewExclusiveUser.Items(count).SubItems.Add(row.Item("TITLE").ToString)
                    ListViewExclusiveUser.Items(count).SubItems.Add(row.Item("USERNAME").ToString)
                    count += 1
                Next
            End If
        Catch ex As Exception
            MsgBox("Error in Load_TASK_USER:" & vbNewLine & ex.Message, MsgBoxStyle.Critical)
        End Try
    End Sub

    Private Sub frmWF_TaskEditor_Shown(sender As Object, e As EventArgs) Handles Me.Shown
        Try
            Me.TBPMO_WORKFLOW_TASK_STATETableAdapter.FillBy(Me.DD_DMSDataSet.TBPMO_WORKFLOW_TASK_STATE, USER_LANGUAGE, WF_IDTextBox.Text, CURRENT_FORM_ID)
            Dim state = DD_DMSDataSet.VWPMO_WF_ACTIVE.Rows(0).Item("STATE_DESC")
            Me.STATE_IDComboBox.SelectedIndex = STATE_IDComboBox.FindStringExact(state)
        Catch ex As Exception
            MsgBox("Error in Load_TaskStates:" & vbNewLine & ex.Message, MsgBoxStyle.Critical)
        End Try
        Load_Following()
        Load_User()
        Load_TASK_USER()
    End Sub

    'Private Sub DataGridView2_MouseDown(sender As Object, e As MouseEventArgs)
    '    If DataGridViewUser.Visible = True Then
    '        'If tsbtnworkUser.Text <> "Bearbeitung aktiv" Then
    '        Dim i As Integer
    '        i = DataGridViewUser.CurrentRow.Index
    '        If DataGridViewUser.Item(0, i).Value Is Nothing = False Then
    '            Me.DataGridViewUser.DoDragDrop(DataGridViewUser.Item(0, i).Value.ToString, DragDropEffects.Copy)
    '        End If
    '    End If

    'End Sub
    Private Sub ListView1_DragEnter(sender As Object, e As DragEventArgs) Handles ListViewExclusiveUser.DragEnter
        ' Check the format of the data being dropped. 
        If (e.Data.GetDataPresent(DataFormats.Text)) Then
            ' Display the copy cursor. 
            e.Effect = DragDropEffects.Copy
        Else
            ' Display the no-drop cursor. 
            e.Effect = DragDropEffects.None
        End If
    End Sub

    Private Sub ListView1_DragDrop(sender As Object, e As DragEventArgs) Handles ListViewExclusiveUser.DragDrop
        Try
            If e.Data.GetData(DataFormats.Text) Is Nothing = False Then
                Dim sql = "Insert INTO TBPMO_WORKFLOW_TASK_USER (WF_TASK_ID,[USER_ID]) VALUES (" & Me.GUIDTextBox.Text & ", " & e.Data.GetData(DataFormats.Text) & ")"
                If ClassDatabase.Execute_non_Query(sql) = True Then
                    Load_TASK_USER()
                End If
            End If
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical, "Fehler beim Hinzufügen eines Users:")
        End Try
    End Sub

    Private Sub btndeleteuser_Click(sender As Object, e As EventArgs) Handles btndeleteuser.Click
        Try
            If (Me.ListViewExclusiveUser.SelectedItems.Count > 0) Then
                Dim sql = "DELETE FROM TBPMO_WORKFLOW_TASK_USER WHERE GUID = " & Me.ListViewExclusiveUser.SelectedItems.Item(0).Text
                ClassDatabase.Execute_non_Query(sql)
                Load_TASK_USER()
            End If

        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical, "Error in btndeleteuser:")
        End Try
    End Sub

    Private Sub btndeleteTask_Click(sender As Object, e As EventArgs) Handles btndeleteTask.Click
        Try
            Dim result As MsgBoxResult = MsgBox("Sind Sie sicher das Sie diesen Task löschen wollen??", MsgBoxStyle.YesNo, "Bestätigung erforderlich:")
            If result = MsgBoxResult.Yes Then
                Dim sql = "DELETE FROM TBPMO_WORKFLOW_TASK_USER WHERE WF_TASK_ID = " & Me.GUIDTextBox.Text
                If ClassDatabase.Execute_non_Query(sql, True) Then
                    sql = "DELETE FROM TBPMO_WORKFLOW_TASK WHERE GUID = " & Me.GUIDTextBox.Text
                    If ClassDatabase.Execute_non_Query(sql, True) Then
                        Load_Task()
                        Load_Following()
                        Load_TASK_USER()
                    End If
                End If

            End If
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical, "Error in Delete Task:")
        End Try
    End Sub

    Private Sub btnAddAppointment_Click(sender As Object, e As EventArgs) Handles btnAddAppointment.Click
        Dim Subject As String = "WF-Task: " & TITLETextBox.Text
        Dim Desc As String = COMMENTTextBox.Text
        ' Dim ControlID As Integer = 10
        Dim DueDate = DUE_DATEDateTimePicker.Value

        ClassFunctionCommandsUI.NewEditTaskAppointment(CURRENT_RECORD_ID, 0, Subject, Desc, DueDate)
    End Sub

    Private Sub btndelete_following_Click(sender As Object, e As EventArgs) Handles btndelete_following.Click
        Try
            If ListBoxFollowing.SelectedIndex <> -1 Then
                Dim ID = ListBoxFollowing.SelectedValue
                If ID > 0 Then
                    Dim result As MsgBoxResult = MsgBox("Sind Sie sicher das Sie diesen nachfolgenden Task löschen wollen??", MsgBoxStyle.YesNo, "Bestätigung erforderlich:")
                    If result = MsgBoxResult.Yes Then
                        Dim sql = "DELETE FROM TBPMO_WORKFLOW_TASK WHERE GUID = " & ID
                        If ClassDatabase.Execute_non_Query(sql, True) = True Then
                            Load_Task()
                            Load_Following()
                            Load_TASK_USER()
                        End If
                    End If
                End If
            End If
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical, "Error in Delete Following:")
        End Try

    End Sub

    Private Sub ListBoxUser_MouseDown(sender As Object, e As MouseEventArgs) Handles ListBoxUser.MouseDown

    End Sub

    Private Sub btnAddUser_Click(sender As Object, e As EventArgs) Handles btnAddUser.Click
        Try
            If ListBoxUser.SelectedIndex <> -1 And ListBoxUser.SelectedValue > 0 Then
                Dim sql = "Insert INTO TBPMO_WORKFLOW_TASK_USER (WF_TASK_ID,[USER_ID]) VALUES (" & Me.GUIDTextBox.Text & ", " & ListBoxUser.SelectedValue & ")"
                If ClassDatabase.Execute_non_Query(sql) = True Then
                    Load_TASK_USER()
                End If
            End If
        Catch ex As Exception
            MsgBox("Error in Adding User exclusive Task:" & vbNewLine & ex.Message, MsgBoxStyle.Critical)
        End Try
    End Sub

    Private Sub DateTimePicker1_ValueChanged(sender As Object, e As EventArgs) Handles DateTimePicker1.ValueChanged
    End Sub
End Class
