
Func _EVH_start()
	Global $hSession = _WinHttpOpen("Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:63.0) Gecko/20100101 Firefox/63.0")

	Global $aHosts[2][2]
	$aHosts[1][0] = "www.evergabe.telekom.de"
	$aHosts[1][1] = _WinHttpConnect($hSession, $aHosts[1][0])
	Global $sHeader = "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8" & @CRLF & "Accept-Language: de,en-US;q=0.7,en;q=0.3" & @CRLF & "Accept-Encoding: gzip, deflate, br" & @CRLF


	Global $sPfadIni = @ScriptDir & "\daten\conf.ini"

	_start_EVH_Evergabe()

	$sSMnr = GUICtrlRead($txt_sm_EVH)
	$aArrayList = _EV_Search_SM($sSMnr)
;~ 	_DebugArrayDisplay($aArrayList)
;~ 	Grettenacker 488284 Wolpertswende SM 206254233|206254233|4209902345|17177645|4209902345/00010 Grettenacker 4,88284 Wolpertswende,Hartl|4650001474|27.01.2021|06.04.2021|Angenommen

	If UBound($aArrayList) > 1 Then
;~ 			ConsoleWrite(UBound($aArrayList) & @CRLF)
		$sDetailsID = _create_Gui_Ausw($aArrayList)
;~ 			MsgBox(64, "", $sDetailsID_Return)
	Else
		$sDetailsID = $aArrayList[0][3]
	EndIf
	
	$aDaten =  _EV_PDF_Kopfdaten_DDL2Array($sDetailsID)
;~ 	
;~ 	_DebugArrayDisplay($aDaten)
;~ 	MsgBox(64, "", $sDetailsID)
	_EV_Kopfdaten_Array2Gui($aDaten)
	

EndFunc   ;==>_EVH_start

Func _EV_EVH_Suche_SM($sSM)
	;0203648377

	$sHTML = _send_WinhttpS_GET($aHosts[1][1], "/framework-agreement-call/index?OrderRecallSearch%5Bhead_line%5D=" & $sSM & "&OrderRecallSearch%5Bdocument_no%5D=&OrderRecallSearch%5Bincoming_date%5D=&OrderRecallSearch%5Border_date%5D=&OrderRecallSearch%5Bgeneral_agreement_id%5D=&OrderRecallSearch%5Bdocument_state%5D=-1", "https://www.evergabe.telekom.de/framework-agreement-call")


	$aTitle = _EV_Abrufliste_GET_Title($sHTML)
	$aBedarfnr = _EV_Abrufliste_GET_Bedarfnr($sHTML) ;SMnr
	$aBelegEinDatum = _EV_Abrufliste_GET_Belegeingang($sHTML) ;Eingang E-Vergabe
	$aBelegnr = _EV_Abrufliste_GET_Belegnr($sHTML) ;42Nr
	$aAusfuehrungsfrist = _EV_Abrufliste_GET_Ausfuehrungsfrist($sHTML) ;Ausführungsfrist
	$aDetailID = _EV_Abrufliste_GET_DetailsID($sHTML) ;DetailsID intern für E-Vergabe
	$aRV = _EV_Abrufliste_GET_RV($sHTML) ;Rahmenvertrag
	$aStatus = _EV_Abrufliste_GET_Status($sHTML) ;Status Angenommen Abgelehnt usw.
	Dim $aReturn[UBound($aTitle)][8]
;~ 	_DebugArrayDisplay($aReturn)

	For $i = 0 To UBound($aTitle) - 1
		$aReturn[$i][0] = $aTitle[$i]
		$aReturn[$i][1] = $aBedarfnr[$i]
		$aReturn[$i][2] = $aBelegnr[$i]
		$aReturn[$i][3] = $aDetailID[$i]
		$aReturn[$i][4] = $aRV[$i]
		$aReturn[$i][5] = $aBelegEinDatum[$i]
		$aReturn[$i][6] = $aAusfuehrungsfrist[$i]
		$aReturn[$i][7] = $aStatus[$i]
	Next

;~ 	_DebugArrayDisplay($aReturn)
	Return $aReturn
EndFunc   ;==>_EV_EVH_Suche_SM

Func _start_EVH_Evergabe()
	Global $hSession = _WinHttpOpen("Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:63.0) Gecko/20100101 Firefox/63.0")

	Global $aHosts[2][2]
	$aHosts[1][0] = "www.evergabe.telekom.de"
	$aHosts[1][1] = _WinHttpConnect($hSession, $aHosts[1][0])
	Global $sHeader = "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8" & @CRLF & "Accept-Language: de,en-US;q=0.7,en;q=0.3" & @CRLF & "Accept-Encoding: gzip, deflate, br" & @CRLF

	$sUsername = IniRead($sPfadIni, "EVergabe", "Benutzer", "xxx")
	$sPasswort = IniRead($sPfadIni, "EVergabe", "Passwort", "xxx")

	$sRetrunLogin = _EV_Login($sUsername, $sPasswort)
	If $sRetrunLogin = "True" Then
;~ 		ConsoleWrite("Login Erfolgreich" & @CRLF)
		_Write_to_Console_EVH("Login Erfolgreich")
		$sHTML = _send_WinhttpS_GET($aHosts[1][1], "/framework-agreement-call", "https://www.evergabe.telekom.de/")
;~ 		ClipPut($sHTML)
;~ 		MsgBox(64,"erstmal","fertig")


	Else
		_Write_to_Console_EVH("Login Fehler!!!")
		MsgBox(64, "Login Fehler", "Login war nicht erfolgreich")
	EndIf
EndFunc   ;==>_start_EVH_Evergabe

Func _EVH_ausgabe1()
	$t_SM = GUICtrlRead($txt_sm_EVH)
	$t_Beleg = GUICtrlRead($txt_beleg_EVH)
	$t_BelegEin = GUICtrlRead($txt_beleg_eingang_EVH)
	$t_aus = GUICtrlRead($txt_ausfuehrungsfrist_EVH)
	$t_rv = GUICtrlRead($txt_rv_EVH)
	$t_Preis = GUICtrlRead($txt_plan_preis_EVH)
	$t_ort = GUICtrlRead($txt_ort_EVH)
	$t_str = GUICtrlRead($txt_strasse_EVH)
	$t_hsnr = GUICtrlRead($txt_hsnr_EVH)
	$t_ncpfad = GUICtrlRead($txt_ncpfad_EVH)
	$t_nco = GUICtrlRead($txt_nc_ordner_EVH)
;~ 	GUICtrlSetData($txt_ausgabe01_EVH, $t_SM & "|" & $t_Beleg & "|" & $t_BelegEin & "|" & $t_aus & "|" & $t_rv & "|" & $t_Preis)
	
	GUICtrlSetData($txt_ausgabe01_EVH, $t_nco & "|" & $t_ncpfad & "||" & $t_ort & "|" & $t_str & "|" & $t_hsnr & "|" & $t_SM & "|" & $t_Beleg & "|" & $t_BelegEin & "|" & $t_aus & "|" & $t_rv & "|" & $t_Preis)
	_Write_to_Console_EVH("SM: " & $t_SM)
	_Write_to_Console_EVH("Beleg: " & $t_Beleg)
	_Write_to_Console_EVH("Beleg Start: " & $t_BelegEin)
	_Write_to_Console_EVH("Ende Datum: " & $t_aus)
	_Write_to_Console_EVH("RV: " & $t_rv)
	_Write_to_Console_EVH("Preis: " & $t_Preis)
EndFunc   ;==>_EVH_ausgabe1

Func _EVH_ausgabe2()
	$t_SM = GUICtrlRead($txt_sm_EVH)
	$t_Beleg = GUICtrlRead($txt_beleg_EVH)
	$t_BelegEin = GUICtrlRead($txt_beleg_eingang_EVH)
	$t_aus = GUICtrlRead($txt_ausfuehrungsfrist_EVH)
	$t_rv = GUICtrlRead($txt_rv_EVH)
	$t_Preis = GUICtrlRead($txt_plan_preis_EVH)
	GUICtrlSetData($txt_ausgabe02_EVH, $t_SM & "|" & $t_Beleg & "|" & $t_rv & "||" & $t_Preis)

EndFunc   ;==>_EVH_ausgabe2

Func _EVH_open_NC()
	
	$sPfadNextCloud = IniRead($sPfadIni, "NC", "pfad", "")
	Local $sSuchtext = GUICtrlRead($txt_sm_EVH)
	If $sSuchtext = "" Then
		$sSuchtext = "*"
	EndIf
;~ 	MsgBox(64, "Suche los...",$sSuchtext & @CRLF & $sPfadNextCloud )
;~ 	$aSearchInfo = _RecursiveFileListToArray($sPfadNextCloud, $sSuchtext, 1)
	$aSearchInfo = _RecursiveFileListToArray($sPfadNextCloud, '', 2)
;~ _ArrayDisplay($aSearchInfo)
;~     _ArrayDisplay($aSearchInfo, "$aFileList")
	Dim $aSuchErg[1]
	For $i = 1 To UBound($aSearchInfo) - 1
		If StringInStr($aSearchInfo[$i], $sSuchtext) Then
			_ArrayAdd($aSuchErg, $aSearchInfo[$i])
		EndIf
	Next
;~ 	_ArrayDisplay($aSuchErg)
;~ 	MsgBox(64, "", UBound($aSuchErg))
	If UBound($aSuchErg) > 1 Then
		_ArrayDelete($aSuchErg, 0)
;~ 	_ArrayDisplay($aSuchErg)
		
		GUICtrlSetData($txt_ncpfad_EVH, $aSuchErg[0])
		
		$aNCO = StringSplit($aSuchErg[0], "\")
		$sNCO = $aNCO[$aNCO[0] - 1]
		GUICtrlSetData($txt_nc_ordner_EVH, $sNCO)
;~ 	_DebugArrayDisplay($aNCO)
		$aOrt = StringRegExp($sNCO, '([^,]+),', 3)
		If IsArray($aOrt) Then
			GUICtrlSetData($txt_ort_EVH, $aOrt[0])
		EndIf
		
		$aStr = StringRegExp($sNCO, '[^,]+, ([^ ]+)', 3)
		If IsArray($aStr) Then
			GUICtrlSetData($txt_strasse_EVH, $aStr[0])
		EndIf
		
		$aHsnr = StringRegExp($sNCO, '[^,]+, [^ ]+ (.*) SM', 3)
		If IsArray($aHsnr) Then
			GUICtrlSetData($txt_hsnr_EVH, $aHsnr[0])
		EndIf



		_Write_to_Console("Pfad NC; " & $aSuchErg[0])
		ShellExecute($aSuchErg[0])
		
	Else
		GUICtrlSetData($txt_ncpfad_EVH, "nicht gefunden")
	EndIf
	
EndFunc   ;==>_EVH_open_NC

Func _Write_to_Console_EVH($sString)
	$sText = @YEAR & "." & @MON & "." & @MDAY & " | " & @HOUR & ":" & @MIN & ":" & @SEC & " " & $sString
	GUICtrlSetData($ed_aus_EVH, GUICtrlRead($ed_aus_EVH) & @CRLF & $sText)
;~ 	ConsoleWrite($sText & " " & $sString & @CRLF)
;~ 	FileWrite(@ScriptDir & "\Daten\_LOG_File.txt", $sText & @CRLF)
	_GUICtrlEdit_LineScroll($ed_aus_EVH, 0, _GUICtrlEdit_GetLineCount($ed_aus_EVH))
EndFunc   ;==>_Write_to_Console_EVH

Func _EV_PDF_Kopfdaten_DDL2Array($sDetailsID)
	Dim $aArray[10]
	$sHTML = _send_WinhttpS_GET($aHosts[1][1], "/framework-agreement-call/details?id=" & $sDetailsID & "&c=1", "https://www.evergabe.telekom.de/")
	If Not StringInStr($sHTML, 'LERF nicht möglich') Then
		$aFileDL = StringRegExp($sHTML, 'href="(/file[^"]+)" title="D', 3)
		If IsArray($aFileDL) Then
			;download File + Umwandel inhalt Speichern in $sFileRead
			For $i = 0 To UBound($aFileDL) - 1
				$sHeader = ""
				$aHTML = _WinHttpSimpleSSLRequest($aHosts[1][1], "GET", $aFileDL[$i], "https://www.evergabe.telekom.de/framework-agreement-call", Default, $sHeader, False, 2)
				_Write_to_Console("File Download => " & $aFileDL[$i])
				$hBild = FileOpen(@ScriptDir & "\test_" & $i & ".pdf", 18)
				FileWrite($hBild, $aHTML)
				FileClose($hBild)
				
				$sTel = ""
				Sleep(1500)
				_Write_to_Console("PDF-File convert to TXT-File ")
				_XPDF_ToText(@ScriptDir & "\test_" & $i & ".pdf", @ScriptDir & "\test_" & $i & ".txt")
				Local $sFileRead = FileRead(@ScriptDir & "\test_" & $i & ".txt")
				FileDelete(@ScriptDir & "\test_" & $i & ".pdf")
				FileDelete(@ScriptDir & "\test_" & $i & ".txt")

				If StringInStr($sFileRead, "ext  geändert") Then
					ExitLoop
				EndIf
			Next
			
			$aArray[0] = _EV_Kopfdaten_GET_AbrufNr($sFileRead)
			$aArray[1] = _EV_Kopfdaten_GET_SMnr($sFileRead)
			$aArray[2] = _EV_Kopfdaten_GET_RV($sFileRead)
			$aArray[3] = $sDetailsID
			$aArray[4] = _EV_Kopfdaten_GET_SDatum($sFileRead)
			$aArray[5] = _EV_Kopfdaten_GET_EDatum($sFileRead)
			$aArray[6] = _EV_Kopfdaten_GET_GesamtS($sFileRead)
			$aArray[7] = _EV_Kopfdaten_GET_Bvt($sFileRead)
			$aArray[8] = _EV_Kopfdaten_GET_Bvt_tel($sFileRead)
			$aArray[9] = _EV_Kopfdaten_GET_Email($sFileRead)
			
			Return $aArray
			
		Else 
			_Write_to_Console("Pattern Filedownload prüfen=> | " & 'href="(/file[^"]+)" title="F')
		EndIf
	Else 
		_Write_to_Console("Lern ist nicht angenommen")	
	EndIf
EndFunc

Func _EV_Kopfdaten_Array2Gui($aArray)
;~ Row 0|4209902345
;~ Row 1|NA
;~ Row 2|4650001474
;~ Row 3|17177645
;~ Row 4|04.06.2020
;~ Row 5|06.04.2021
;~ Row 6|536,46
;~ Row 7|Sinan Frank Haase
;~ Row 8|
;~ Row 9|NA
	If $aArray[1] = "" or $aArray[1] = "NA" Then 
;~ 		GUICtrlSetData($txt_sm_EVH)
	Else 
		GUICtrlSetData($txt_sm_EVH, $aArray[1])
	EndIf
	GUICtrlSetData($txt_beleg_EVH, $aArray[0])
	GUICtrlSetData($txt_beleg_eingang_EVH, $aArray[4])
	GUICtrlSetData($txt_ausfuehrungsfrist_EVH, $aArray[5])
	GUICtrlSetData($txt_rv_EVH, $aArray[2])
	GUICtrlSetData($txt_plan_preis_EVH, $aArray[6])
	GUICtrlSetData($txt_abruf_details_ev_EVH, "https://www.evergabe.telekom.de/framework-agreement-call/details?id=" & $aArray[3] & "&c=1")
	GUICtrlSetData($txt_Leistungserfassung_EVH, "https://www.evergabe.telekom.de/sheet/index?c=1&importId=" & $aArray[3])
	GUICtrlSetData($txt_lv_geab_EVH, "https://www.evergabe.telekom.de/gaeb-viewer/index?c=1&id=" & $aArray[3])


EndFunc

Func _XPDF_ToText($sPDFFile, $sTXTFile, $iFirstPage = 1, $iLastPage = 0, $bLayout = True)
	Local $sXPDFToText = @ScriptDir & "\daten\pdftotext.exe"
	Local $sOptions

	If Not FileExists($sPDFFile) Then Return SetError(1, 0, 0)
	If Not FileExists($sXPDFToText) Then Return SetError(2, 0, 0)

	If $iFirstPage <> 1 Then $sOptions &= " -f " & $iFirstPage
	If $iLastPage <> 0 Then $sOptions &= " -l " & $iLastPage
	If $bLayout = True Then $sOptions &= " -layout"     ;das war standart
;~ 		If $bLayout = True Then $sOptions &= " −simple" ;ganz gut viele leerzeichen
;~ 			If $bLayout = True Then $sOptions &= " −raw" ;fast perfekt
	If $bLayout = False Then $sOptions &= " −table"     ;fast perfekt

	Local $iReturn = ShellExecuteWait($sXPDFToText, $sOptions & ' "' & $sPDFFile & '" "' & $sTXTFile & '"', @ScriptDir, "", @SW_HIDE)
	If $iReturn = 0 Then Return 1

	Return 0

EndFunc   ;==>_XPDF_ToText
