*       *       *       *       *       *       *       *       *       *       *       *       *       *       *       *       *       *       
*
*              obslouzen sloupec SHOW_ON_WEB; radky s show_on_web=.F. se neodmazavaji, ty patri pepinovi
*  2019-09-26  SHOW_ON_WEB se postuje true/false podle course COURSE_V
*  2019-10-26  adresu datastoru bere z m.datastore_add
*  2018-07-31  name a descript se posilaji z ktexty.meno1 a meno2
*  2021-06-02  ktexty se pouzije jen pokud existuje jako DBF a v jidelaku nejsou fieldy name a descript
*
*       *       *       *       *       *     #> ver. 2021-06-09 <#     *       *       *       *       *       *       *       *       *      

ACTIVATE SCREEN 
DO ppgm_http 


* HTTP_READ_ONLY_MODE = .T.


DO stahni_webovej_jidelak

USE (m.gchom_path + "jidelak") IN 0 &&noupdate

napln_name_a_description()

SELECT LEFT(TTOC(datum, 3), 10) as date, canteen, s_o_v2course(s_o_v) as course, cislojidla as number, cislopril, c_karty, nazev, name, descript ;
       WHERE datum >= DATE() AND BETWEEN(cislojidla, 1, 19) FROM jidelak INTO CURSOR jidelak_local READWRITE 

? "updating "
SELECT jidelak_local
SCAN
   SCATTER MEMVAR 

   radek_jidl = '{"name": "' + ALLTRIM(m.name) + '", "stock_card_number":' + ALLTRIM(STR(m.c_karty)) + ', "description": "' + ALLTRIM(m.descript) + '", "additional_course": '
   IF cislopril # 0
       radek_jidl = radek_jidl + '"' + m.course_P + '" ,"additional_course_default_number":' + ALLTRIM(STR(m.cislopril))
   ELSE
       radek_jidl = radek_jidl +               'null, "additional_course_default_number": null'
   ENDIF 

   SELECT jidelak_web
   LOCATE FOR date = m.date .and. canteen = m.canteen .and. course = m.course .and. number = m.number
   IF FOUND()
       IF c_karty = m.c_karty .and. ALLTRIM(name) = ALLTRIM(m.name) .and. ALLTRIM(descript) = ALLTRIM(m.descript) .and. cislopril = m.cislopril
             * jidlo se nezmenilo, netreba posilat
          ?? "."
          REPLACE co_s_tim WITH "nic", radek_jidl WITH m.radek_jidl
       ELSE
          * musi se patchovat
          ?? "U"
          radek_jidl = radek_jidl + '}'
          id_txt = ALLTRIM(STR(jidelak_web.id))
          json = http_request("canteen/crud/meals/" + m.id_txt, "PATCH", m.radek_jidl)
          REPLACE co_s_tim WITH "PATCH - update", radek_jidl WITH m.radek_jidl, response WITH m.json

          IF .not. json_ok(m.json)
              MESSAGEBOX( ALLTRIM(m.name) + CHR(13) + m.json, 1+16, "Nepodarilo se zapsat jidlo")
          ENDIF 

       ENDIF 
   ELSE 
       * novy radek, musi se postnout
      ?? "I"
      radek_jidl = radek_jidl + ', "number": "' + ALLTRIM(STR(m.number)) + '", "date": "' + m.date + '", "course": "' + allt(m.course) + '", "canteen":' + ALLTRIM(STR(m.canteen))
      radek_jidl = radek_jidl + IIF(m.course # m.course_V, ', "show_on_web":true}', ', "show_on_web":false}')
      co_s_tim = "POST - new record"
      json = http_request("canteen/crud/meals", "POST", m.radek_jidl)
      response = m.json
      INSERT INTO jidelak_web FROM MEMVAR 
      IF .not. json_ok(m.json)
          MESSAGEBOX( ALLTRIM(m.name) + CHR(13) + m.json, 1+16, "Nepodarilo se zapsat jidlo" )
      ENDIF 
   ENDIF 
ENDSCAN 

SELECT jidelak_web
SCAN FOR "DELETE" $ co_s_tim .and. show_onweb
    id_txt = ALLTRIM(STR(jidelak_web.id))
    json = http_request("canteen/crud/meals/" + m.id_txt, "DELETE", "")
    REPLACE response WITH m.json
ENDSCAN 

oznam_hotovo()
