<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Apples</title>
  <style>
    body { font-family: helvetica, sans-serif; }
  </style>
  <script src="https://cdn.jsdelivr.net/npm/handlebars@4.7.7/dist/handlebars.min.js" integrity="sha256-ZSnrWNaPzGe8v25yP0S6YaMaDLMTDHC+4mHTw0xydEk=" crossorigin="anonymous"></script>
  <script>
    "use strict";
    let applesUrl = "https://cscie12.dce.harvard.edu/apples-pyo.php";
    document.addEventListener('DOMContentLoaded',function(){
      fetch(applesUrl)
        .then(response => response.json())
        .then(data => buildApplesList(data));
    });

    function buildApplesList(data){
      let template = document.getElementById('apple-template').innerHTML ;
      console.log(template);
      let compiledTemplate = Handlebars.compile(template);
      let rendered = compiledTemplate(data);
      document.getElementById('applelist').innerHTML = rendered;
    }
  </script>
</head>

<body>
  <h1>Apples</h1>
  <div id="applelist"> </div>
  <template id="apple-template">
  <h2>Picking Now</h2>
  <ul>
  {{#apples.picking_now}}
    <li>{{ . }}
  {{/apples.picking_now}}
  </ul>
  <h2>Picking Soon</h2>
  <ul>
  {{#apples.picking_soon}}
    <li>{{ . }}
  {{/apples.picking_soon}}
  </ul>
  </template>
</body>

</html>
