*!*****************************************************************************
FUNCTION citajantikzony
PARAMETERS antik_adr
DO FORM snake NAME lsnakex LINKED WITH jaz('form_citam_zony')
m.antik_adr="https://tense.rc.api.smartcityportal.sk"
lsnakex.DRAW()
LOCAL m.vysledok, m.zaciatok, m.koniec, textik
http = CREATEOBJECT("WinHttp.WinHttpRequest.5.1")
http.OPEN("GET",m.antik_adr+"/api/integrator/zones", .F.)
Http.Option(4) = 13056
http.settimeouts(0, 0, 0, 0)
http.setrequestheader("content-type", "application/json;charset=utf-8;")
*http.setrequestheader("Authorization", "Basic " + STRCONV(m.api_usr + ":" + m.api_pwd,13))
TRY 
	lsnakex.DRAW()
	http.SEND("")
	lsnakex.DRAW()
	m.httpodpoved=http.statustext
	m.vysledok=http.status
CATCH
	m.vysledok=-100
	m.httpodpoved=jaz("form_antik_neodpoveda")
ENDTRY 
IF m.vysledok=200
	SET CLASSLIB TO "JSON.VCX" ADDITIVE 
	m.ret = .t.
	m.loJSON = CREATEOBJECT("JSON")
	lsnakex.DRAW()
	m.textik=http.responsetext
	DO WHILE '"turnstiles":['$m.textik
		m.zaciatok=AT(',"turnstiles":[',m.textik)
		m.koniec=AT(']',SUBSTR(m.textik,m.zaciatok))
		m.textik=LEFT(m.textik,m.zaciatok-1)+SUBSTR(m.textik,m.zaciatok+m.koniec)
	ENDDO 
	m.vysledok=lojson.parse(m.textik,"")
	lsnakex.DRAW()
	RELEASE lojson
	CREATE CURSOR zoznamzon (id c(50), name c(50))
	FOR m.i=1 TO ALEN(vysledok.data)
		TRY 
			INSERT INTO zoznamzon (id, name) VALUES(vysledok.data[m.i].id, vysledok.data[m.i].name)
			lsnakex.DRAW()
		CATCH 
		ENDTRY 
	ENDFOR 
	RELEASE m.vysledok
	RELEASE CLASSLIB JSON.VCX
ELSE
	m.ret=.f.
ENDIF 	
RELEASE m.http
lsnakex.release()
RETURN m.ret


*!*****************************************************************************
FUNCTION citajantikturnikety
PARAMETERS antik_adr
m.antik_adr="https://tense.rc.api.smartcityportal.sk"
DO FORM snake NAME lsnakex LINKED WITH jaz('form_citam_turnikety')
lsnakex.DRAW()
LOCAL m.vysledok
http = CREATEOBJECT("WinHttp.WinHttpRequest.5.1")
http.OPEN("GET",m.antik_adr+"/api/integrator/turnstiles", .F.)
Http.Option(4) = 13056
http.settimeouts(0, 0, 0, 0)
http.setrequestheader("content-type", "application/json;charset=utf-8;")
*http.setrequestheader("Authorization", "Basic " + STRCONV(m.api_usr + ":" + m.api_pwd,13))
TRY 
	lsnakex.DRAW()
	http.SEND("")
	lsnakex.DRAW()
	m.httpodpoved=http.statustext
	m.vysledok=http.status
CATCH
	m.vysledok=-100
	m.httpodpoved=jaz("form_antik_neodpoveda")
ENDTRY 
IF m.vysledok=200
	SET CLASSLIB TO "JSON.VCX" ADDITIVE 
	m.ret = .t.
	m.loJSON = CREATEOBJECT("JSON")
	lsnakex.DRAW()
	m.vysledok=lojson.parse(http.responsetext,"")
	lsnakex.DRAW()
	RELEASE lojson
	CREATE CURSOR zoznamturn (sn i, id i)
	FOR m.i=1 TO ALEN(vysledok.data)
		TRY 
			INSERT INTO zoznamturn (id, sn) VALUES(vysledok.data[m.i].id, vysledok.data[m.i].sn)
			lsnakex.DRAW()
		CATCH 
		ENDTRY 
	ENDFOR 
	RELEASE m.vysledok
	RELEASE CLASSLIB JSON.VCX
ELSE
	m.ret=.f.
ENDIF 	
RELEASE m.http
lsnakex.release()
RETURN m.ret

*!*****************************************************************************
FUNCTION vytvorantikzonu
PARAMETERS antik_adr, meno, id_oprava
m.antik_adr="https://tense.rc.api.smartcityportal.sk"
LOCAL m.vysledok
SET CLASSLIB TO "JSON.VCX" ADDITIVE 
http = CREATEOBJECT("WinHttp.WinHttpRequest.5.1")
IF EMPTY(m.id_oprava)
	http.OPEN("POST",m.antik_adr+"/api/integrator/zones/" , .F.)
ELSE
	http.OPEN("PUT",m.antik_adr+"/api/integrator/zones/"+ALLTRIM(id_oprava) , .F.)
ENDIF 
Http.Option(4) = 13056
http.settimeouts(0, 0, 0, 0)
http.setrequestheader("content-type", "application/json;charset=utf-8;")
*http.setrequestheader("Authorization", "Basic " + STRCONV(m.api_usr + ":" + m.api_pwd,13))
m.loJSON = CREATEOBJECT("JSON")
CREATE CURSOR jsontxt (jsontxt M)
INSERT INTO jsontxt (jsontxt) VALUES ("") 
REPLACE jsontxt.jsontxt WITH '{"name":"'+ALLTRIM(m.meno)+'",'+CHR(13)+CHR(10)    &&meno kasy
REPLACE jsontxt.jsontxt WITH jsontxt.jsontxt+'"turnstiles":'+CHR(13)+CHR(10)
REPLACE jsontxt.jsontxt WITH jsontxt.jsontxt+'['+CHR(13)+CHR(10)
SELECT turnikety
SCAN ALL 
	SELECT jsontxt
	REPLACE jsontxt.jsontxt WITH jsontxt.jsontxt+ALLTRIM(STR(turnikety.sn,50,0))+","
	SELECT turnikety
ENDSCAN 
SELECT jsontxt
IF RIGHT(jsontxt.jsontxt,1)==","
	REPLACE jsontxt.jsontxt WITH LEFT(jsontxt.jsontxt,LEN(jsontxt.jsontxt)-1)
ENDIF 
REPLACE jsontxt.jsontxt WITH jsontxt.jsontxt+']}'+CHR(13)+CHR(10)
TRY 
	http.SEND(jsontxt.jsontxt)
	m.httpodpoved=http.statustext
	m.vysledok=http.status
CATCH
	m.vysledok=-100
	m.httpodpoved=jaz("form_antik_neodpoveda")
ENDTRY 
USE IN jsontxt
IF m.vysledok=200
	IF !ISNULL(http.responsetext)
		m.vysledok=lojson.parse(http.responsetext)
		TRY 
			m.ret=vysledok.data.id
			m.odpoved=jaz("form_zona_vytvorena")
		CATCH
			m.ret=""
			m.odpoved="Nepodarilo sa vytvoriť zónu."
		ENDTRY 
		RELEASE lojson
	ELSE
		m.ret=""
		m.odpoved=http.statustext
	ENDIF 
ELSE
	m.ret=""
	m.odpoved=http.statustext
ENDIF 
RELEASE m.http
RELEASE CLASSLIB json.vcx
RETURN m.ret 


*!*****************************************************************************
FUNCTION zmazantikzonu
PARAMETERS antik_adr, zona
m.antik_adr="https://tense.rc.api.smartcityportal.sk"
LOCAL m.vysledok
SET CLASSLIB TO "JSON.VCX" ADDITIVE 
http = CREATEOBJECT("WinHttp.WinHttpRequest.5.1")
http.OPEN("DELETE",m.antik_adr+"/api/integrator/zones/"+ALLTRIM(m.zona) , .F.)
Http.Option(4) = 13056
http.settimeouts(0, 0, 0, 0)
http.setrequestheader("content-type", "application/json;charset=utf-8;")
TRY 
	http.SEND("")
	m.httpodpoved=http.statustext
	m.vysledok=http.status
CATCH
	m.vysledok=-100
	m.httpodpoved=jaz("form_antik_neodpoveda")
ENDTRY 
DO CASE 
	CASE m.vysledok=202
		m.ret=.t.
		m.odpoved=jaz("form_zona_odstranena")
	CASE m.vysledok=404
		m.ret=.f.
		m.odpoved=jaz("form_zona_neexistuje")
	OTHERWISE 
		m.ret=.f.
		m.odpoved=jaz("form_zona_mazenie_nepreslo")
ENDCASE 
RELEASE m.http
RELEASE CLASSLIB json.vcx
RETURN m.ret 

*!*****************************************************************************
FUNCTION vytvorantikkartu
PARAMETERS antik_adr, karta, oprava
m.antik_adr="https://tense.rc.api.smartcityportal.sk"
LOCAL m.vysledok
SET ASSERTS ON
ASSERT .f.
SET CLASSLIB TO "JSON.VCX" ADDITIVE 
http = CREATEOBJECT("WinHttp.WinHttpRequest.5.1")
IF EMPTY(m.oprava)
	http.OPEN("POST",m.antik_adr+"/api/integrator/tickets/" , .F.)
ELSE
	http.OPEN("PUT",m.antik_adr+"/api/integrator/tickets/"+ALLTRIM(m.karta) , .F.)
ENDIF 
Http.Option(4) = 13056
http.settimeouts(0, 0, 0, 0)
http.setrequestheader("content-type", "application/json;charset=utf-8;")
*http.setrequestheader("Authorization", "Basic " + STRCONV(m.api_usr + ":" + m.api_pwd,13))
m.loJSON = CREATEOBJECT("JSON")
CREATE CURSOR jsontxt (jsontxt M)
INSERT INTO jsontxt (jsontxt) VALUES ("") 
IF EMPTY(m.oprava)
	REPLACE jsontxt.jsontxt WITH '{"reference_number":"'+ALLTRIM(m.karta)+'",'+CHR(13)+CHR(10)    &&meno kasy
	REPLACE jsontxt.jsontxt WITH jsontxt.jsontxt+'"from":"'+STRTRAN(TTOC(DATETIME(),3),"T"," ")+'",'+CHR(13)+CHR(10)
ELSE 
	REPLACE jsontxt.jsontxt WITH '{"from":"'+STRTRAN(TTOC(DATETIME(),3),"T"," ")+'",'+CHR(13)+CHR(10)
ENDIF 
REPLACE jsontxt.jsontxt WITH jsontxt.jsontxt+'"to":"'+STRTRAN(TTOC(DATE()+1,3),"T"," ")+'",'+CHR(13)+CHR(10)
REPLACE jsontxt.jsontxt WITH jsontxt.jsontxt+'"zones":'+CHR(13)+CHR(10)
REPLACE jsontxt.jsontxt WITH jsontxt.jsontxt+'['+CHR(13)+CHR(10)
SELECT povolenezony
SCAN ALL 
	SELECT jsontxt
	REPLACE jsontxt.jsontxt WITH jsontxt.jsontxt+'"'+ALLTRIM(povolenezony.id)+'",'
	SELECT povolenezony
ENDSCAN 
SELECT jsontxt
IF RIGHT(jsontxt.jsontxt,1)==","
	REPLACE jsontxt.jsontxt WITH LEFT(jsontxt.jsontxt,LEN(jsontxt.jsontxt)-1)
ENDIF 
REPLACE jsontxt.jsontxt WITH jsontxt.jsontxt+']}'+CHR(13)+CHR(10)
TRY 
	http.SEND(jsontxt.jsontxt)
	m.httpodpoved=http.statustext
	m.vysledok=http.status
CATCH
	m.vysledok=-100
	m.httpodpoved=jaz("form_antik_neodpoveda")
ENDTRY 
USE IN jsontxt
IF m.vysledok=200
	IF !ISNULL(http.responsetext)
		m.textik=http.responsetext
		DO WHILE '"zones":['$m.textik
			m.zaciatok=AT(',"zones":[',m.textik)
			m.koniec=AT(']',SUBSTR(m.textik,m.zaciatok))
			m.textik=LEFT(m.textik,m.zaciatok-1)+SUBSTR(m.textik,m.zaciatok+m.koniec)
		ENDDO 
		m.vysledok=lojson.parse(m.textik,"")
		TRY 
			m.ret=vysledok.data.id
			m.odpoved=jaz("form_karta_vytvorena")
		CATCH
			m.ret=0
			m.odpoved=vysledok.message
		ENDTRY 
		RELEASE lojson
	ELSE
		m.ret=0
		m.odpoved=http.statustext
	ENDIF 
ELSE
	m.ret=0
	m.odpoved=http.statustext
ENDIF 
RELEASE m.http
RELEASE CLASSLIB json.vcx
RETURN m.ret 

*!*****************************************************************************
FUNCTION zmazantikkartu
PARAMETERS antik_adr, karta
m.antik_adr="https://tense.rc.api.smartcityportal.sk"
LOCAL m.vysledok
SET CLASSLIB TO "JSON.VCX" ADDITIVE 
http = CREATEOBJECT("WinHttp.WinHttpRequest.5.1")
http.OPEN("DELETE",m.antik_adr+"/api/integrator/tickets/"+ALLTRIM(m.karta) , .F.)
Http.Option(4) = 13056
http.settimeouts(0, 0, 0, 0)
http.setrequestheader("content-type", "application/json;charset=utf-8;")
*http.setrequestheader("Authorization", "Basic " + STRCONV(m.api_usr + ":" + m.api_pwd,13))
TRY 
	http.SEND("")
	m.httpodpoved=http.statustext
	m.vysledok=http.status
CATCH
	m.vysledok=-100
	m.httpodpoved=jaz("form_antik_neodpoveda")
ENDTRY 
DO CASE 
	CASE m.vysledok=202
		m.ret=.t.
		m.odpoved=jaz("form_karta_odstranena")
	CASE m.vysledok=404
		m.ret=.f.
		m.odpoved=jaz("form_karta_neexistuje")
	OTHERWISE 
		m.ret=.f.
		m.odpoved=jaz("form_karta_mazenie_nepreslo")
ENDCASE 
RELEASE m.http
RELEASE CLASSLIB json.vcx
RETURN m.ret 

*!*****************************************************************************
FUNCTION citajantikpohyby
PARAMETERS antik_adr, karta, vstup
m.antik_adr="https://tense.rc.api.smartcityportal.sk"
DO FORM snake NAME lsnakex LINKED WITH jaz('form_citam_pohyby')
lsnakex.DRAW()
LOCAL m.vysledok
http = CREATEOBJECT("WinHttp.WinHttpRequest.5.1")
http.OPEN("GET",m.antik_adr+"/api/integrator/tickets/"+ALLTRIM(karta)+"/timeline", .F.)
Http.Option(4) = 13056
http.settimeouts(0, 0, 0, 0)
http.setrequestheader("content-type", "application/json;charset=utf-8;")
*http.setrequestheader("Authorization", "Basic " + STRCONV(m.api_usr + ":" + m.api_pwd,13))
TRY 
	lsnakex.DRAW()
	http.SEND("")
	lsnakex.DRAW()
	m.httpodpoved=http.statustext
	m.vysledok=http.status
CATCH
	m.vysledok=-100
	m.httpodpoved=jaz("form_antik_neodpoveda")
ENDTRY 
SET ASSERTS ON
ASSERT .f.
IF m.vysledok=200
	SET CLASSLIB TO "JSON.VCX" ADDITIVE 
	m.ret = .t.
	m.loJSON = CREATEOBJECT("JSON")
	lsnakex.DRAW()
	m.vysledok=lojson.parse(http.responsetext,"")
	lsnakex.DRAW()
	RELEASE lojson
	CREATE CURSOR zoznampohybov (zona character(50), start T, end T, cas I)
		IF !ISNULL(vysledok.data.data.id) AND !ISNULL(vysledok.data.data.sn)
			INSERT INTO zoznampohybov (id, sn) VALUES (vysledok.data.data.id,vysledok.data.data.sn)
			lsnakex.DRAW()
		ENDIF 
	RELEASE m.vysledok
	RELEASE CLASSLIB JSON.VCX
ELSE
	m.ret=.f.
ENDIF 	
RELEASE m.http
lsnakex.release()
RETURN m.ret