#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <GuiStatusBar.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <GuiListView.au3>
#include <File.au3>
Opt("GUIOnEventMode", 1)

global $Products=[["Test1",0,0,0],["Test2",0,0,0],["Test3",0,0,0],["Test4",0,0,0], _
["Test1",0,0,0],["Test1",0,0,0],["Test1",0,0,0],["Test1",0,0,0],["Test1",0,0,0], _
["Test1",0,0,0],["Test1",0,0,0],["Test1",0,0,0],["Test1",0,0,0],["Test1",0,0,0], _
["Test1",0,0,0],["Test1",0,0,0],["Test1",0,0,0],["Test1",0,0,0],["Test1",0,0,0], _
["Test1",0,0,0],["Test1",0,0,0],["Test1",0,0,0],["Test1",0,0,0],["Test1",0,0,0],["Test1",0,0,0] _
]

global $InfosCSVFile="Infos.csv"
global $StandortSchrift="StandortSchriftCount.txt"
global $StandortCalls="StandortCallsCount.txt"
global $RohDatenCSV="Daten.xlsx"
global $RohDatenCSV_single="Daten_Single.csv"
global $Settings="Settings.ini"
global $Settings_Global="Settings_global.ini"
global $LastCalls[10],$ShowGui
global $Single[7]
if FileExists($Settings) and IniRead($Settings,"General","Datum",-1)=@MDAY&"."&@MON&"."&@YEAR then
for $i=0 to UBound($Single)-1 step 1
$Single[$i]=IniRead($Settings,"General","Single_"&$i,0)
next
else
if NOT FileExists($Settings) then
FileClose(FileOpen($Settings,8))
endif
IniWrite($Settings,"General","Datum",@MDAY&"."&@MON&"."&@YEAR)
FileDelete($RohDatenCSV_single)
for $i=0 to UBound($Single)-1 step 1
$Single[$i]=IniWrite($Settings,"General","Single_"&$i,0)
$Single[$i]=0
next
endif
if FileExists($Settings_Global) and IniRead($Settings_Global,"General","Datum",-1)=@MDAY&"."&@MON&"."&@YEAR then
;nichts
else
if NOT FileExists($Settings_Global) then
FileClose(FileOpen($Settings_Global,8))
endif
IniWrite($Settings_Global,"General","Datum",@MDAY&"."&@MON&"."&@YEAR)
FileDelete($StandortCalls)
FileDelete($StandortSchrift)
endif
$KlickTool = GUICreate("KlickTool", 949, 914, 100, 115)
GUISetOnEvent($GUI_EVENT_CLOSE, "_close")

GUICtrlCreateGroup("Tagesziel",812,30,127,50)
GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
$LabelTagesziel=GUICtrlCreateLabel("",828,45,100,30,$SS_CENTER)
GUICtrlSetColor(-1, 0x0033cc)
GUICtrlSetFont(-1, 50, 800, 0, "MS Sans Serif")

GUICtrlCreateGroup("Standort Schrift",679,90,125,60)
GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
$LabelStandortSchrift=GUICtrlCreateLabel("",692,107,100,30,$SS_CENTER)
GUICtrlSetColor(-1, 0x0033cc)
GUICtrlSetFont(-1, 60, 805, 0, "MS Sans Serif")
GUICtrlCreateGroup("Standort Calls",812,90,127,60)
GUICtrlSetFont(-1, 8, 812, 0, "MS Sans Serif")
$LabelStandortCalls=GUICtrlCreateLabel("",828,107,100,30,$SS_CENTER)
GUICtrlSetColor(-1, 0x0033cc)
GUICtrlSetFont(-1, 60, 800, 0, "MS Sans Serif")
GUICtrlCreateGroup("Aktuelle Infos",142,50,528,100)
GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
$LabelInfos=GUICtrlCreateLabel("",145,77,520,70,$SS_CENTER)
GUICtrlSetColor(-1, 0x0033cc)
GUICtrlSetFont(-1, 12, 800, 0, "MS Sans Serif")
GUICtrlSetColor(-1, 0x0033cc)
GUICtrlCreateLabel('Klick Tool V4.2 Berlin',145,12,300,20)
GUICtrlSetFont(-1, 14, 800, 0, "MS Sans Serif")
GUICtrlSetColor(-1, 0x006666)
GUICtrlCreateLabel('www.sopy.de ',145,32,300,15)
GUICtrlSetFont(-1, 4, 800, 0, "MS Sans Serif")
GUICtrlSetColor(-1, 0x006666)

GUICtrlCreateGroup("Calls",8,152,126,110)
GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
$LabelCalls=GUICtrlCreateLabel("0",8+15,152+47,126-25,110-50,$SS_CENTER)
GUICtrlSetColor(-1, 0x0033cc)
GUICtrlSetFont(-1, 60, 800, 0, "MS Sans Serif")
GUICtrlCreateGroup("Schrift",8+134,152,126,110)
GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
$LabelSchrift=GUICtrlCreateLabel("0",8+134+15,152+47,126-25,110-50,$SS_CENTER)
GUICtrlSetColor(-1, 0x0033cc)
GUICtrlSetFont(-1, 60, 800, 0, "MS Sans Serif")
GUICtrlCreateGroup("Ansprache",8+134*2,152,126,110)
GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
$LabelAnsprache=GUICtrlCreateLabel("0",8+134*2+15,152+47,126-25,110-50,$SS_CENTER)
GUICtrlSetColor(-1, 0x0033cc)
GUICtrlSetFont(-1, 60, 800, 0, "MS Sans Serif")
GUICtrlCreateGroup("High",8+134*3,152,126,110)
GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
$LabelSaveQ=GUICtrlCreateLabel("0",8+134*3+15,152+47,126-25,110-50,$SS_CENTER)
GUICtrlSetColor(-1, 0x0033cc)
GUICtrlSetFont(-1, 60, 800, 0, "MS Sans Serif")
GUICtrlCreateGroup("Low",8+134*4,152,126,110)
GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
$LabelOptIn=GUICtrlCreateLabel("0",8+134*4+15,152+47,126-25,110-50,$SS_CENTER)
GUICtrlSetColor(-1, 0x0033cc)
GUICtrlSetFont(-1, 60, 800, 0, "MS Sans Serif")
;$GUICtrlCreateGroup("KIM",8+134*5,152,126,110)
;$GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
$LabelKIM=GUICtrlCreateLabel("0",8+134*5+15,152+47,126-25,110-50,$SS_CENTER)
GUICtrlSetColor(-1, 0xf0f0f0)
GUICtrlSetFont(-1, 60, 800, 0, "MS Sans Serif")
GUICtrlCreateGroup("Erfolg",8+134*6,152,126,110)
GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
$LabelErfolg=GUICtrlCreateLabel("0",8+134*6+15,152+47,126-25,110-50,$SS_CENTER)
GUICtrlSetColor(-1, 0x0033cc)
GUICtrlSetFont(-1, 60, 800, 0, "MS Sans Serif")

GUICtrlCreateGroup("Produkte", 8, 376, 769, 513)
GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
for $i=0 to UBound($Products)-1 step 1
GUICtrlCreateGroup($Products[$i][0],5+(10+$i*150)-(150*5*INT($i/5)),390+(INT($i/5)*100),145,89)
GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
$Products[$i][1]=GUICtrlCreateRadio("erfolgreich",5+(10+$i*150)-(150*5*INT($i/5))+10,390+(INT($i/5)*100)+20,97,17)
$Products[$i][2]=GUICtrlCreateRadio("erfolglos",5+(10+$i*150)-(150*5*INT($i/5))+10,390+(INT($i/5)*100)+20+20,97,17)
$Products[$i][3]=GUICtrlCreateRadio(" ",5+(10+$i*150)-(150*5*INT($i/5))+10,390+(INT($i/5)*100)+40+20,97,17)
GUICtrlSetState($Products[$i][3],$GUI_CHECKED)
GUICtrlCreateGroup("", -99, -99, 1, 1)
next
GUICtrlCreateGroup("gespeicherte Rufnummern", 784, 532+40, 153,25*(UBound($LastCalls)-1+2))
GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
for $i=0 to UBound($LastCalls)-1 step 1
$LastCalls[$i]=GUICtrlCreateLabel(" - ",784+5, 532+25*($i+1)+40, 145,20,$SS_CENTER)
next
GUICtrlCreateGroup("", -99, -99, 1, 1)
GUICtrlCreateGroup("Kunde", 8, 264, 930, 105)
GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
GUICtrlCreateGroup("Call/Schrift", 16, 288, 129, 73)
GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
$RadioCallW = GUICtrlCreateRadio("Call", 24, 312, 121, 17)
$RadioCallF = GUICtrlCreateRadio("Schrift", 24, 336, 121, 17)
GUICtrlCreateGroup("", -99, -99, 1, 1)
GUICtrlCreateGroup("Rufnummer/Mail/Kundennummer...", 264, 288, 257, 73)
GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
$Nummer = GUICtrlCreateInput("", 280, 320, 201, 21)
GUICtrlSendMsg(-1, 0x1501, 0, "Eingabe")
GUICtrlCreateGroup("", -99, -99, 1, 1)
GUICtrlCreateGroup("Low", 664, 288, 129, 73)
GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
$RadioOptInW = GUICtrlCreateRadio("erfolgreich", 672, 280+20, 97, 17)
$RadioOptInF = GUICtrlCreateRadio("erfolglos", 672, 280+40, 97, 17)
$RadioOptInN = GUICtrlCreateRadio(" ", 672, 280+60, 97, 17)
GUICtrlSetState(-1,$GUI_CHECKED)
GUICtrlCreateGroup("", -99, -99, 1, 1)
GUICtrlCreateGroup("", -99, -99, 1, 1)
$Group4 = GUICtrlCreateGroup("High", 800, 288, 121, 73)
GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
$RadioSaveQW = GUICtrlCreateRadio("erfolgreich", 808, 280+20, 97, 17)
$RadioSaveQF = GUICtrlCreateRadio("erfolglos", 808, 280+40, 97, 17)
$RadioSaveQN = GUICtrlCreateRadio(" ", 808, 280+60, 97, 17)
GUICtrlSetState(-1,$GUI_CHECKED)
GUICtrlCreateGroup("", -99, -99, 1, 1)
$ButtonSave = GUICtrlCreateButton("speichern/neu", 784, 848, 155, 41)
GUICtrlSetOnEvent(-1,"_NewEntry")
GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
;$ButtonAkt = GUICtrlCreateButton("aktualisieren", 784, 392, 155, 25)
$ButtonData = GUICtrlCreateButton("Rohdaten", 784, 424, 155, 25)
GUICtrlSetOnEvent(-1,"_RohDaten")
$Pic1 = GUICtrlCreatePic("C:\KlickTool\Logo.jpg", 28, 15, 103, 132)
GUISetState(@SW_SHOW)
_getInfoData($InfosCSVFile)
_refreshSingles()

$LoopCounter=1000
While 1
if $LoopCounter=1000 then
GUICtrlSetData($LabelStandortSchrift,_tryRead())
GUICtrlSetData($LabelStandortCalls,_tryRead(true))
$LoopCounter=0
EndIf
Sleep(10)
$LoopCounter+=1
WEnd

Func _close()
exit
EndFunc

Func _tryWrite($Call=false)
local $data=-1
$File=$StandortSchrift
if $Call then $File=$StandortCalls
if NOT FileExists($File) then
FileOpen($File,2+8)
$data=0
EndIf
if $data<>0 then
$data=FileRead($File)
While @error
$data=FileRead($File)
WEnd
EndIf
$File=FileOpen($File,2+8)
While @error
$File=FileOpen($File,2+8)
WEnd
FileWrite($File,int($data)+1)
FileClose($File)
EndFunc

Func _tryRead($Call=false)
$File=$StandortSchrift
if $Call then $File=$StandortCalls
if NOT FileExists($File) then
FileOpen($File,2+8)
FileWrite($File,0)
EndIf
$data=FileRead($File)
While @error
$data=FileRead($File)
WEnd
return $data
EndFunc

Func _getInfoData($File)
local $data,$ar,$count,$Tagesziel="",$Info=""
$data=FileRead($File)
$ar=StringSplit($data,@crlf,1+2)
$count=0
for $i=0 to UBound($ar)-1 step 1
if $ar[$i]='Heutiges Tagesziel an Calls:' then
$count=1
elseif $ar[$i]="Infos:" then
$count=2
else
if $count=1 then
if $ar[$i]<>"" then
$Tagesziel&=$ar[$i]
$count=3
endif
ElseIf $count=2 then
if $ar[$i]<>"" then
$Info&=$ar[$i]&@crlf
endif
endif
endif
next
GUICtrlSetData($LabelTagesziel,$Tagesziel)
GUICtrlSetData($LabelInfos,$Info)
EndFunc

Func _addLastCall($Nummer)
for $i=0 to UBound($LastCalls)-2 step 1
GUICtrlSetData($LastCalls[$i],GUICtrlRead($LastCalls[$i+1]))
next
GUICtrlSetData($LastCalls[UBound($LastCalls)-1],$Nummer)
EndFunc

Func _RohDaten()
;Mit Usernamen Filtern und in neuer gui
local $array
_FileReadToArray($RohDatenCSV_single,$array,0)
if UBound($array)>0 then
$ShowGui=GUICreate("Heutige Calls/Schriften",600,500)
GUISetOnEvent($GUI_EVENT_CLOSE, "_closeShow")
$ShowListView=GUICtrlCreateListView(StringReplace($array[0],";","|"),5,5,590,490)
for $i=1 to UBound($array)-1 step 1
GUICtrlCreateListViewItem(StringReplace($array[$i],";","|"),$ShowListView)
next
GUISetState(@SW_SHOW,$ShowGui)
else
MsgBox(48,"Fehler","Momentan sind keine Daten gespeichert.")
EndIf
EndFunc

Func _closeShow()
GUIDelete($ShowGui)
EndFunc

Func _NewEntry()
if NOT BitAND(GUICtrlRead($RadioCalLF),$GUI_CHECKED)=$GUI_CHECKED and NOT BitAND(GUICtrlRead($RadioCalLW),$GUI_CHECKED)=$GUI_CHECKED then
MsgBox(16,"Error","Wählen sie Call oder Schrift aus")
return -1
endif
local $array[UBound($Products)],$bArList2
for $i=0 to UBound($Products)-1 step 1
if BitAND(GUICtrlRead($Products[$i][1]), $GUI_CHECKED) = $GUI_CHECKED then
$array[$i]=1;erfolgreich
$Single[4]+=1
$Single[5]+=1
$Single[6]+=1
ElseIf BitAND(GUICtrlRead($Products[$i][2]), $GUI_CHECKED) = $GUI_CHECKED then
$array[$i]=0;erfolglos
$Single[6]+=1
$bArList2=true
endif
next
_addLastCall(GUICtrlRead($Nummer))
;"Calls|Schrift|Ansprache|SaveQ|OptIn|Kim|Erfolg"
$call=BitAND(GUICtrlRead($RadioCalLW),$GUI_CHECKED)=$GUI_CHECKED?"Call":"Schrift"
$call=BitAND(GUICtrlRead($RadioCalLW),$GUI_CHECKED)=$GUI_CHECKED?_tryWrite(true):_tryWrite()
$Call=BitAND(GUICtrlRead($RadioCalLW),$GUI_CHECKED)=$GUI_CHECKED?GUICtrlRead($Nummer):""
$Schrift=BitAND(GUICtrlRead($RadioCalLW),$GUI_CHECKED)=$GUI_CHECKED?"":GUICtrlRead($Nummer)
$ArList=""
for $i=0 to UBound($array)-1 step 1
$ArList&=$array[$i]
if $i<>UBound($array)-1 then
$ArList&=";"
endif
next
if NOT FileExists($RohDatenCSV) then
$ArList2=""
for $i=0 to UBound($Products)-1 step 1
$ArList2&=$Products[$i][0]
if $i<>UBound($Products)-1 then
$ArList2&=";"
endif
next
FileWriteLine($RohDatenCSV,"Datum;Uhrzeit;Username;Call;Schrift;Low;SaveQ;"&$ArList2)
endif
local $OptIn="",$SaveQ=""
if BitAND(GUICtrlRead($RadioOptInW), $GUI_CHECKED) = $GUI_CHECKED then
$OptIn="1";erfolgreich
ElseIf BitAND(GUICtrlRead($RadioOptInF), $GUI_CHECKED) = $GUI_CHECKED then
$OptIn="0";erfolglos
endif
if BitAND(GUICtrlRead($RadioSaveQW), $GUI_CHECKED) = $GUI_CHECKED then
$SaveQ="1";erfolgreich
ElseIf BitAND(GUICtrlRead($RadioSaveQF), $GUI_CHECKED) = $GUI_CHECKED then
$SaveQ="0";erfolglos
endif
;Datum;Uhrzeit;Rechnername;Call;Schrift;Daten...
FileWriteLine($RohDatenCSV,@MDAY&"."&@MON&"."&@YEAR&";"&@HOUR&":"&@MIN&":"&@SEC&";"&@UserName&";"&$Call&";"&$Schrift&";"&$OptIn&";"&$SaveQ&";"&$ArList)
while @error
FileWriteLine($RohDatenCSV,@MDAY&"."&@MON&"."&@YEAR&";"&@HOUR&":"&@MIN&":"&@SEC&";"&@UserName&";"&$Call&";"&$Schrift&";"&$OptIn&";"&$SaveQ&";"&$ArList)
WEnd
if NOT FileExists($RohDatenCSV_single) then
$ArList2=""
for $i=0 to UBound($Products)-1 step 1
$ArList2&=$Products[$i][0]
if $i<>UBound($Products)-1 then
$ArList2&=";"
endif
next
FileWriteLine($RohDatenCSV_single,"Datum;Uhrzeit;Username;Call;Schrift;OptIn;SaveQ;"&$ArList2)
endif
FileWriteLine($RohDatenCSV_single,@MDAY&"."&@MON&"."&@YEAR&";"&@HOUR&":"&@MIN&":"&@SEC&";"&@UserName&";"&$Call&";"&$Schrift&";"&$OptIn&";"&$SaveQ&";"&$ArList)

if BitAND(GUICtrlRead($RadioCalLW),$GUI_CHECKED)=$GUI_CHECKED then
$Single[0]+=1
else
$Single[1]+=1
endif
;$LabelAnsprache
if BitAND(GUICtrlRead($RadioSaveQW),$GUI_CHECKED)=$GUI_CHECKED then
$Single[2]+=1
$Single[5]+=0
$Single[6]+=0
ElseIf BitAND(GUICtrlRead($RadioSaveQF),$GUI_CHECKED)=$GUI_CHECKED then
$Single[6]+=0
endif
if BitAND(GUICtrlRead($RadioOptInW),$GUI_CHECKED)=$GUI_CHECKED then
$Single[3]+=1
$Single[5]+=0
$Single[6]+=0
ElseIf BitAND(GUICtrlRead($RadioOptInF),$GUI_CHECKED)=$GUI_CHECKED then
$Single[6]+=0
endif
_refreshSingles()
_refreshControls()
EndFunc

Func _refreshControls()
for $i=0 to UBound($Products)-1 step 1
GUICtrlSetState($Products[$i][3],$GUI_CHECKED)
next
GUICtrlSetState($RadioOptInN,$GUI_CHECKED)
GUICtrlSetState($RadioSaveQN,$GUI_CHECKED)
GUICtrlSetData($Nummer,"")
EndFunc

Func _refreshSingles()
GUICtrlSetData($LabelCalls,$Single[0])
GUICtrlSetData($LabelSchrift,$Single[1])
GUICtrlSetData($LabelSaveQ,$Single[2])
GUICtrlSetData($LabelOptIn,$Single[3])
GUICtrlSetData($LabelKIM,$Single[4])
GUICtrlSetData($LabelErfolg,$Single[5])
if $Single[0]=0 then
GUICtrlSetData($LabelAnsprache,"0%")
else
GUICtrlSetData($LabelAnsprache,Round((($Single[6]/$Single[0])*100),0)&"%")
endif
ConsoleWrite($Single[6]&@crlf)
for $i=0 to UBound($Single)-1 step 1
IniWrite($Settings,"General","Single_"&$i,$Single[$i])
next
EndFunc