*       *       *       *       *       *       *       *       *       *       *       *       *       *       *       *       *       *       
*
*  2021-06-09 - sloupec SHOW_ON_WEB posilam natvrdo TRUE (pouzilo se jen v parlamentu)
*             - adresu datastoru bere z m.datastore_add
*  2021-08-22 - name a descript pripravi uz editacni formular jidelaku
*
*       *       *       *       *       *     #> ver. 2022-11-11 <#     *       *       *       *       *       *       *       *       *      

DO ppgm_http 

DO stahni_webovej_jidelak

? "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 + '"' + ALLTRIM(add_course) + '", "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
       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 + ', "show_on_web":true}'
      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 
   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()

