
Imports VERAG_PROG_ALLGEMEIN

Public Class frmMitarbTimas

    Private avisoEmp As VERAG_PROG_ALLGEMEIN.cMitarbeiter
    Public mid As Integer = -1
    Public isUpdate As Boolean = False
    Dim emp As cTimasEmployee
    Dim timas As cTimasAPI
    Dim dtTimas As New DataTable
    Dim dtcustomFields As New DataTable

    Sub New()

        ' Dieser Aufruf ist für den Designer erforderlich.
        InitializeComponent()

        ' Fügen Sie Initialisierungen nach dem InitializeComponent()-Aufruf hinzu.

    End Sub


    Private Sub frmMitarbDetails_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        cbxEinsteigsDat.Checked = False
        cbxGebDat.Checked = False
        txtGebDat.Format = DateTimePickerFormat.Custom
        txtGebDat.CustomFormat = "dd.MM.yyyy"
        datGekuendigtAm.Format = DateTimePickerFormat.Custom
        datGekuendigtAm.CustomFormat = "dd.MM.yyyy"
        datEinstiegsdatum.Format = DateTimePickerFormat.Custom
        datEinstiegsdatum.CustomFormat = "dd.MM.yyyy"


        If mid > 0 Then
            initMitarbeiter(mid)
        Else
            MsgBox("Fehler beim Laden der Daten!")
        End If

    End Sub
    Private Sub initMitarbeiter(mid As Integer, Optional _isUpdate As Boolean = False)
        Try


            timas = New cTimasAPI("timas")
            emp = timas.getEmployee(mid)
            avisoEmp = New VERAG_PROG_ALLGEMEIN.cMitarbeiter(emp.externid)


            timas.getAllGroups(dtTimas)

            initDGV(dtTimas)

            isUpdate = _isUpdate
            Label48.Text = emp.externid
            txtUsername.Text = emp.login
            txtVname.Text = emp.firstname
            txtNname.Text = emp.lastname
            'txtPwd.Text = emp.password
            If emp.gender = "male" Then rbtnGeschlechtM.Checked = True
            If emp.gender = "female" Then rbtnGeschlechtW.Checked = True

            If emp.birthdayDate = "null" Then
                cbxGebDat.Checked = False
            Else
                cbxGebDat.Checked = True : txtGebDat.Text = emp.birthdayDate
            End If
            txtStrasse.Text = emp.street
            txtLohn.Text = emp.pnr1
            txtPlz.Text = emp.zipcode
            txtCard.Text = emp.card
            txtOrt.Text = emp.city
            txtInfo.Text = emp.info
            'cboNiederlassung.changeItem(mitarbeiter.mit_niederlassung)
            'cboAbteilung.changeItem(mitarbeiter.mit_abteilung)
            txtTelNr.Text = emp.phone1
            txtEmail.Text = emp.email
            txtMobiltel.Text = emp.phone2
            txtMandantenNr.Text = emp.clientNumber
            txtTimasID.Text = emp.ID

            txtTimasID.ReadOnly = True

            If emp.ID = avisoEmp.mit_timasId Then
                txtTimasID.BackColor = Color.LightGreen
            Else
                txtTimasID.BackColor = Color.IndianRed
            End If



            'txtPersonalNr.Text = emp.pnr1

            cbxAccount.Checked = emp.loginActive

            If emp.entryDate Is Nothing Then
                cbxEinsteigsDat.Checked = False
            Else
                cbxEinsteigsDat.Checked = True : datEinstiegsdatum.Text = emp.entryDate
            End If


            If emp.exitDate IsNot Nothing Then
                cbxGekuendigt.Checked = True
                datGekuendigtAm.Text = emp.exitDate
            Else
                cbxGekuendigt.Checked = False
            End If

            If emp.birthdayDate Is Nothing Then
                cbxGebDat.Checked = False
            Else
                cbxGebDat.Checked = True : txtGebDat.Text = emp.birthdayDate
            End If

            initcheckedlistbox(timas.getEmployeecustomFields(mid))
        Catch ex As Exception
            VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR(ex.Message, ex.StackTrace, System.Reflection.MethodInfo.GetCurrentMethod.Name)
        End Try

    End Sub


    Private Sub btnCancel_Click(sender As Object, e As EventArgs) Handles btnCancel.Click
        Me.Close()
    End Sub

    Private Sub btnOK_Click(sender As Object, e As EventArgs) Handles btnOK.Click
        Dim newMid As Integer = 0
        If txtVname.Text = "" And txtNname.Text = "" Then MsgBox("Bitte geben Sie einen Namen ein!") : Exit Sub
        getMitarbeiter()
        Me.Refresh()
        Me.Close()

    End Sub


    Private Sub getMitarbeiter()

        emp.login = txtUsername.Text
        'emp.password = txtPwd.Text
        If rbtnGeschlechtM.Checked Then emp.gender = "male"
        If rbtnGeschlechtW.Checked Then emp.gender = "female"
        emp.firstname = txtVname.Text
        emp.lastname = txtNname.Text
        If cbxGebDat.Checked Then emp.birthdayDate = txtGebDat.Text Else emp.birthdayDate = "null"
        emp.street = txtStrasse.Text
        emp.zipcode = txtPlz.Text
        emp.street = txtOrt.Text
        emp.pnr1 = txtLohn.Text
        emp.phone1 = txtTelNr.Text & " " & txtDurchwahl.Text
        emp.email = txtEmail.Text
        emp.phone2 = txtMobiltel.Text
        emp.info = txtInfo.Text
        emp.card = txtCard.Text
        emp.clientNumber = txtMandantenNr.Text


        If cbxEinsteigsDat.Checked Then emp.entryDate = datEinstiegsdatum.Text Else emp.entryDate = "null"
        If cbxGekuendigt.Checked Then emp.exitDate = datGekuendigtAm.Text Else emp.exitDate = "null"
        If cbxGebDat.Checked Then emp.birthdayDate = txtGebDat.Text Else emp.birthdayDate = "null"

        'mitarbeiter.mit_TESTonly = cbxTestaccount.Checked
        If checkMandatoryFields() Then
            If timas.updateEmployee(emp, dgvData) Then
                Dim UID As New cMitarbeiter()
                Dim mitarbeiter
                mitarbeiter = UID.getMitarbeiterByTimasId(emp.ID)
                If mitarbeiter IsNot Nothing Then
                    mitarbeiter.mit_PersonalNr = emp.pnr1
                    UID.updateMitarbeiter(mitarbeiter)
                End If

            End If
        Else
            MsgBox("Mitarbeiter konnte nicht upgedeated werden!")

        End If




    End Sub



    Private Sub btnSave_Click(sender As Object, e As EventArgs)
        If txtVname.Text = "" And txtNname.Text = "" Then MsgBox("Bitte geben Sie einen Namen ein!") : Exit Sub
        getMitarbeiter()
        If isUpdate Then
            emp.ID = mid
            'If UID.updateMitarbeiter(mitarbeiter) Then Me.Close()
        End If
    End Sub

    Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs)
        datEinstiegsdatum.Enabled = cbxEinsteigsDat.Checked
    End Sub

    Private Sub CheckBox2_CheckedChanged(sender As Object, e As EventArgs)
        txtGebDat.Enabled = cbxGebDat.Checked
    End Sub


    Private Sub cbxGekuendigt_CheckedChanged(sender As Object, e As EventArgs)
        datGekuendigtAm.Enabled = cbxGekuendigt.Checked
    End Sub



    Private Sub cboNiederlassung_SelectedIndexChanged(sender As Object, e As EventArgs)

    End Sub

    Private Sub Button5_Click(sender As Object, e As EventArgs)
        Dim frmBenutzerBerGruppen As New frmBenutzerBerGruppen(mid, 3, "DISPO")
        frmBenutzerBerGruppen.Show(Me)
    End Sub

    Private Sub lkl_LinkClicked(sender As Object, e As LinkLabelLinkClickedEventArgs) Handles lkl.LinkClicked
        If vbYes = MsgBox("Möchten Sie das Kennwort neu setzen?", vbYesNoCancel) Then
            If vbYes = MsgBox("Möchten Sie den Kennwort aus AVISO/SQL übernehmen?", vbYesNoCancel) Then
                If avisoEmp IsNot Nothing AndAlso avisoEmp.mit_pwd <> "" Then
                    emp.password = avisoEmp.mit_pwd
                    If checkMandatoryFields() Then
                        timas.updateEmployee(emp, Nothing)
                        Exit Sub
                    End If
                Else
                    MsgBox("Mitarbeiter-PW konnte konnten nicht aus AVISO geladen werden!")
                End If
            End If


            Dim input = InputBox("Neues Passwort:", "Passwort anlegen")
            If input <> "" Then
                getMitarbeiter()
                emp.password = input
                If checkMandatoryFields() Then
                    timas.updateEmployee(emp, Nothing)
                End If
            Else
                MsgBox("Passwort wurde nicht neu vergeben!")
            End If

        End If
    End Sub



    Private Function checkMandatoryFields() As Boolean


        If Not cbxEinsteigsDat.Checked Or emp.entryDate = "" Then
            MsgBox("Einstiegsdatum prüfen!")
            Return False
        End If

        If CDate(emp.entryDate).ToShortDateString <= DateTime.Parse("01/01/2000") Then
            MsgBox("Einstiegsdatum darf nicht vor 01.01.2000 liegen!")
            Return False
        End If



        If emp.firstname = "" Or emp.lastname = "" Then
            MsgBox("Vor- und Nachnamen angeben!")
            Return False
        End If

        If emp.info = "" Then
            MsgBox("Firma/Info angeben!")
            Return False
        End If

        Return True

    End Function

    Sub initDGV(dt As DataTable)


        If dtTimas.Columns.Count > 0 Then

            If TabControl1.TabCount = 0 Then
                For Each row As DataRow In dt.DefaultView.ToTable(True, "groupType").Rows
                    Dim TabIndex As Integer                                         'Anzahl der erstellten Tabs, bei keinem Tab ist der Index 0
                    TabIndex = TabControl1.TabCount
                    TabControl1.TabPages.Add(row("groupType"))
                    If TabIndex = 0 Then
                        Dim tab As TabPage = Me.TabControl1.SelectedTab
                        Dim dv = New DataView(dt)
                        dv.RowFilter = "groupType = '" & tab.Text & "'"
                        dgvData.DataSource = dv
                        If dgvData.Columns.Count > 0 Then
                            With dgvData
                                .RowHeadersWidth = 10
                                .Columns("id").Visible = True
                                .Columns("name").AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
                                .Columns("groupType").Visible = False
                                .Columns("info").Width = 200
                                .Columns("set").Width = 20
                                .Columns("set").HeaderText = "X"
                                .Dock = DockStyle.Fill

                                For Each r As DataGridViewRow In dgvData.Rows
                                    For Each i As cTimasGruppe In emp.Gruppen
                                        If r.Cells("id").Value = i.Id Then
                                            r.Cells("set").Value = True
                                        End If
                                    Next
                                Next
                            End With
                            tab.Controls.Add(Me.dgvData)
                        End If
                    End If

                Next
            End If
        End If


    End Sub

    Private Sub PictureBox9_Click_1(sender As Object, e As EventArgs)
        Dim webAddress As String = "https://zeit.verag.ag/"
        Process.Start(webAddress)
    End Sub

    Private Sub TabControl1_SelectedIndexChanged_1(sender As Object, e As EventArgs) Handles TabControl1.SelectedIndexChanged
        If dgvData Is Nothing And dgvData.Rows.Count < 1 Then Exit Sub
        Dim tab As TabPage = Me.TabControl1.SelectedTab
        If ((Not tab Is Nothing) AndAlso (Not tab.Controls.Contains(Me.dgvData))) Then
            Dim dv = New DataView(dtTimas)
            dv.RowFilter = "groupType = '" & tab.Text & "'"
            dgvData.DataSource = dv
            If dgvData.Columns.Count > 0 Then
                With dgvData
                    .RowHeadersWidth = 10
                    .Columns("id").Visible = True
                    .Columns("name").AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
                    .Columns("groupType").Visible = False
                    .Columns("info").Width = 200
                    .Columns("set").Width = 20
                    .Columns("set").HeaderText = "X"
                    .Dock = DockStyle.Fill

                    For Each r As DataGridViewRow In dgvData.Rows
                        For Each i As cTimasGruppe In emp.Gruppen
                            If r.Cells("id").Value = i.Id Then
                                r.Cells("set").Value = True
                            End If
                        Next
                    Next
                End With
                tab.Controls.Add(Me.dgvData)
            End If


        End If
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        If vbYes = MsgBox("Möchten Sie Daten aus AVISO/SDL übernehmen?", vbYesNo) Then
            If avisoEmp IsNot Nothing AndAlso avisoEmp.mit_username <> "" Then
                txtUsername.Text = avisoEmp.mit_username

                If avisoEmp.mit_emailprivat <> "" Then
                    txtEmail.Text = avisoEmp.mit_emailprivat
                ElseIf avisoEmp.mit_email <> "" Then
                    txtEmail.Text = avisoEmp.mit_email
                End If

                If txtInfo.Text = "" Then
                    txtInfo.Text = avisoEmp.mit_firma & " - " & avisoEmp.mit_abteilung
                End If
                cbxAccount.Checked = True
                getMitarbeiter()
            Else
                MsgBox("Mitarbeiterdaten konnten nicht aus AVISO geladen werden!")
            End If
        End If
    End Sub

    Private Sub initcheckedlistbox(dt As DataTable)

        For Each row As DataRow In dt.Rows
            If row.Item("type").ToString.ToLower = "boolean" Then
                CheckedListBox1.Items.Add(row.Item("name"), IIf(row.Item("value") = "true", True, False))
            End If
        Next


    End Sub
End Class

