; Arbeitszeit Mel
#include <GuiConstantsEx.au3>
#include <AVIConstants.au3>
#include <TreeViewConstants.au3>
#include<WindowsConstants.au3>
#include<ListViewConstants.au3>
#include<ButtonConstants.au3>
#include<GUIConstantsEx.au3>
#include<Guilistview.au3>
#include <ComboConstants.au3>
#include <EditConstants.au3>
#include <GuiComboBox.au3>
#include <WindowsConstants.au3>
#include <File.au3>
#include <Array.au3>
#include <String.au3>
#include <UpdownConstants.au3>

Opt('GUIOnEventMode', 1)

$sDBFile = @ScriptDir & "\datenbank.txt"
$iEdit = -1



#Region Hauptfenster
; GUI
$MainGui=GuiCreate("Arbeitszeit Bosch", 620, 600)
GuiSetIcon(@SystemDir & "\notepad.exe", 0)
GUISetOnEvent($GUI_EVENT_CLOSE, "_END")

; GUI LOGO
GUICtrlCreatePic("logo_de.jpg", 0, 0, 620, 42)

; GUI DATUM
$Date=GUICtrlCreateDate("", 20, 50, 200, 20)

; GUI Auswahl
$combo1=GUICtrlCreateCombo("Art", 230, 50, 150, 20, 0x0003)
GUICtrlSetData($combo1, "Arbeit|Berufsschule Donnerstag|Berufsschule Freitag|Sonstige")
GUICtrlCreateLabel("Pause:", 50, 84)
GUICtrlSetOnEvent($Combo1,"_Sonstige")
$Radio=GuiCtrlCreateRadio("Ja", 100, 80)
GuiCtrlSetState(-1, $GUI_CHECKED)
GuiCtrlCreateRadio("Nein", 140, 80)
$Eingabe=GUICtrlCreateInput("Wenn Sonstige", 230, 80, 150, 20)
GUICtrlSetState(-1, $GUI_DISABLE)
GUICtrlCreateLabel("Beginn:", 390, 53)
$UpDownB1=GUICtrlCreateInput("7", 440, 50, 40, 20,$UDS_WRAP)
GUICtrlCreateUpdown($UpDownB1)
GUICtrlSetLimit(-1, 20, 7)
$UpDownB2=GUICtrlCreateInput("0", 490, 50, 40, 20,$UDS_WRAP)
GUICtrlCreateUpdown($UpDownB2)
GUICtrlSetLimit(-1, 59, 0)
GUICtrlCreateLabel("Ende:", 390, 83)
$UpDownE1=GUICtrlCreateInput("7", 440, 80, 40, 20,$UDS_WRAP)
GUICtrlCreateUpdown($UpDownE1)
GUICtrlSetLimit(-1, 20, 7)
$UpDownE2=GUICtrlCreateInput("0", 490, 80, 40, 20,$UDS_WRAP)
GUICtrlCreateUpdown($UpDownE2)
GUICtrlSetLimit(-1, 59, 0)
GUICtrlCreateLabeL("Summe:", 510, 520)
$Summe=GUICtrlCreateLabel("", 510, 540, 90, 20, 0x1000)

;GUI Button
$Button=GUICtrlCreateButton("OK", 540, 80, 60, 20)
GUICtrlSetOnEvent(-1,"_Anwenden")
GUICtrlSetOnEvent($Button, "_Summe")
GUICtrlSetOnEvent($Button, "Save")

; GUI Liste
$hListView=GUICtrlCreateListView("Datum|Art|Arbeitsbeginn|Arbeitsende|Stunden", 20, 110, 580, 400)
$hLVHandle=GUICtrlGetHandle($hListView)
_GUICtrlListView_SetColumn($hLVHandle, 0, "Datum", 175, 0)
_GUICtrlListView_SetColumn($hLVHandle, 1, "Art", 130, 0)
_GUICtrlListView_SetColumn($hLVHandle, 2, "Arbeitsbeginn", 90, 0)
_GUICtrlListView_SetColumn($hLVHandle, 3, "Arbeitsende", 90, 0)
_GUICtrlListView_SetColumn($hLVHandle, 4, "Stunden", 90, 0)

#EndRegion Hauptfenster

; GUI MESSAGE LOOP
GuiSetState()

_Sonstige()
If FileExists($sDBFile) Then Load()

While 1
	Sleep(1000)
WEnd



Func _Sonstige()
	If  GUICtrlRead($combo1) = "Sonstige" Then
		GUICtrlSetState($Eingabe, $GUI_ENABLE)
		GUICtrlSetState($Eingabe, $GUI_FOCUS)
	ElseIf GUICtrlRead($combo1) = "Berufsschule Donnerstag" Then
		GUICtrlSetData($UpDownB1, "8")
		GUICtrlSetData($UpDownB2, "00")
		GUICtrlSetData($UpDownE1, "15")
		GUICtrlSetData($UpDownE2, "45")
		GUICtrlSetData($Eingabe, "Wenn Sonstige")
		GUICtrlSetState($Eingabe, $GUI_DISABLE)		
	Else
		GUICtrlSetData($Eingabe, "Wenn Sonstige")
		GUICtrlSetState($Eingabe, $GUI_DISABLE)
	EndIf
EndFunc

Func _Anwenden()
	If GUICtrlRead($Radio) = "4" Then
		If GUICtrlRead($combo1) = "Sonstige" Then
			GUICtrlCreateListViewItem(GUICtrlRead($Date) & "|" & GUICtrlRead($Eingabe) & "|" & GUICtrlRead($UpDownB1) & ":" & GUICtrlRead($UpDownB2) & "|" & GUICtrlRead($UpDownE1) & ":" & GUICtrlRead($UpDownE2) & "|" & ((GUICtrlRead($UpDownE1)*60 + GUICtrlRead($UpDownE2))-(GUICtrlRead($UpDownB1)*60 + GUICtrlRead($UpDownB2)))/60, $hListView)
		Else
			GUICtrlCreateListViewItem(GUICtrlRead($Date) & "|" & GUICtrlRead($combo1) & "|" & GUICtrlRead($UpDownB1) & ":" & GUICtrlRead($UpDownB2) & "|" & GUICtrlRead($UpDownE1) & ":" & GUICtrlRead($UpDownE2) & "|" & ((GUICtrlRead($UpDownE1)*60 + GUICtrlRead($UpDownE2))-(GUICtrlRead($UpDownB1)*60 + GUICtrlRead($UpDownB2)))/60, $hListView)
		EndIf
	Else
		If GUICtrlRead($combo1) = "Sonstige" Then
			GUICtrlCreateListViewItem(GUICtrlRead($Date) & "|" & GUICtrlRead($Eingabe) & "|" & GUICtrlRead($UpDownB1) & ":" & GUICtrlRead($UpDownB2) & "|" & GUICtrlRead($UpDownE1) & ":" & GUICtrlRead($UpDownE2) & "|" & ((GUICtrlRead($UpDownE1)*60 + GUICtrlRead($UpDownE2))-(GUICtrlRead($UpDownB1)*60 + GUICtrlRead($UpDownB2)))/60-0.75, $hListView)
		Else
			GUICtrlCreateListViewItem(GUICtrlRead($Date) & "|" & GUICtrlRead($combo1) & "|" & GUICtrlRead($UpDownB1) & ":" & GUICtrlRead($UpDownB2) & "|" & GUICtrlRead($UpDownE1) & ":" & GUICtrlRead($UpDownE2) & "|" & ((GUICtrlRead($UpDownE1)*60 + GUICtrlRead($UpDownE2))-(GUICtrlRead($UpDownB1)*60 + GUICtrlRead($UpDownB2)))/60-0.75, $hListView)
		EndIf
	EndIf
EndFunc
	
Func _End()
	Save()
    GUIDelete($MainGui)
    Exit
EndFunc

Func Load() ; Datenbank-Datei laden
	Local $sItem
	Local $hFile = FileOpen($sDBFile, 0) ; Datei zum lesen öffnen
	If $hFile <> -1 Then ; wenn das öffnen erfolgreich war, dann...
		_GUICtrlListView_BeginUpdate($hLVHandle) ; Listview sperren 
		While True ; Endlosschleife
			$sItem = FileReadLine($hFile) ; Zeile aus der Datei lesen
			If @error Then ExitLoop ; wenn das Ende der Datei erreicht ist, dann Endlosschleife verlassen
			GUICtrlCreateListViewItem($sItem, $hListView) ; mit den eingelesenen Daten einen neuen Listview-Eintrag erstellen 
		WEnd
		_GUICtrlListView_EndUpdate($hLVHandle) ; Listview wieder freigeben
		FileClose($hFile) ; Datei schließen
	EndIf
EndFunc   ;==>Load

Func Save() ; Datenbank-Datei speichern
	Local $sItem
	Local $iCount = _GUICtrlListView_GetItemCount($hLVHandle) - 1 ; Anzahl der Listview-Einträge holen
	Local $hFile = FileOpen($sDBFile, 2) ; Datei zum speichern (überschreiben) öffnen
	If $hFile <> -1 Then ; wenn das öffnen erfolgreich war, dann...
		For $i = 0 To $iCount ; Schleife, um alle Listview-Einträge durchzugehen
			$sItem = _GUICtrlListView_GetItemTextString($hLVHandle, $i) ; die Werte aus dem Listview-Eintrag holen
			FileWriteLine($hFile, $sItem) ; und in die Datei schreiben
		Next
		FileClose($hFile) ; Datei schließen
	EndIf
EndFunc   ;==>Save
