<?php
$pdo = new PDO("mysql:host=".$GLOBALS['myservername'].";dbname=".$GLOBALS['mydb'].';charset=utf8', $GLOBALS['mylogin'], $GLOBALS['mypass']);
$GLOBALS["pdo_conn"] = $pdo;

$current_date = new DateTime();

$select_current_meal = $GLOBALS['pdo_conn']->prepare("SELECT * FROM meal_delivery_setup WHERE id = :id");
$select_current_meal->execute(array(':id' => $current_date->format('j')));
$res_current_meal = $select_current_meal->fetch(PDO::FETCH_ASSOC);

function getDocumentsFromCollection($main_collection_id){
    $statement_filegallery = $GLOBALS['pdo_conn']->prepare("SELECT * FROM filegallery_line LEFT JOIN main_collection_link ON main_collection_link.main_sitepart_header_id = filegallery_line.header_id WHERE main_collection_id = :main_collection_id AND main_collection_setup_content_id = :main_collection_setup_content_id");
    $statement_filegallery->execute(array(':main_collection_id' => $main_collection_id, ':main_collection_setup_content_id' => 2));
    $res_filegallery = $statement_filegallery->fetch(PDO::FETCH_ASSOC);
    return $res_filegallery['filename'];
}

function getContactFromID($user_id){
    $statement_user = $GLOBALS['pdo_conn']->prepare("SELECT * FROM main_contact WHERE id = :id");
    $statement_user->execute(array(':id' => $user_id));
    $res_user = $statement_user->fetch(PDO::FETCH_ASSOC);
    return $res_user;
}

function getRestaurant($main_collection_id){
    $statement = $GLOBALS['pdo_conn']->prepare("SELECT * FROM main_collection WHERE id = :main_collection_id");
    $statement->execute(array(':main_collection_id' => $main_collection_id));
    $res_statement = $statement->fetch(PDO::FETCH_ASSOC);
    return $res_statement;
}

function getCollectionData($main_collection_id, $setup_id){
    $statement = $GLOBALS['pdo_conn']->prepare("SELECT * FROM main_collection LEFT JOIN main_collection_link ON main_collection.id = main_collection_link.main_collection_id WHERE main_collection_id = :main_collection_id AND main_collection_link.main_collection_setup_content_id = :setup_id");
    $statement->execute(array(':main_collection_id' => $main_collection_id, ':setup_id' => $setup_id));
    $res_statement = $statement->fetch(PDO::FETCH_ASSOC);
    return $res_statement['data'];
}

function getInfo(){
    $select_all = $GLOBALS['pdo_conn']->prepare("SELECT * FROM meal_delivery_setup");
    $select_all->execute();
    $res_all_days = $select_all->fetchALL(PDO::FETCH_ASSOC);

    $current_date = new DateTime();

    echo "<table>";
    echo "<tr>";
    echo "<th>Tag</th>";
    echo "<th>Restaurant</th>";
    echo "<th>Person</th>";
    echo "</tr>";
    foreach ($res_all_days as $key_all_days => $value_all_days) {
      $date = new DateTime($current_date->format('Y-m-').$value_all_days['id']);
      $contact = getContactFromID($value_all_days['main_contact_id']);
      $restaurant = getRestaurant($value_all_days['restaurant_id']);
      $date_filter = '';
      if($current_date->format('j') == $value_all_days['id']){
          $date_filter = 'current_day';
      }
      echo "<tr class='food_list_row ".$date_filter."'>";
      echo "<td>".$date->format('d.m.Y')."</td>";
      echo "<td>".$restaurant['description']."</td>";
      echo "<td>".$contact['name']."</td>";
      echo "</tr>";
    }
    echo "</table>";
}

if(isset($_POST['submit_send_order']) && $_POST['input_order'] != ""){
    $payment_type = ($_POST["payment_type"] == "on") ? 1 : 0;

    $statement_select = $GLOBALS['pdo_conn']->prepare("SELECT * FROM meal_delivery WHERE date LIKE '%".$current_date->format('Y-m-d')."%' AND main_contact_id = :main_contact_id");
    $statement_select->execute(array(':main_contact_id' => $GLOBALS['main_contact']['id']));
    $res_select = $statement_select->fetch(PDO::FETCH_ASSOC);

    if($res_select){
        $statement_update = $GLOBALS['pdo_conn']->prepare("UPDATE meal_delivery SET date = :date, purchase_order = :purchase_order, payment_type = :payment_type WHERE id = :id");
        $statement_update->execute(array(':purchase_order' => $_POST['input_order'], ':payment_type' => $payment_type, ':date' => $current_date->format("Y-m-d H:i:s"), ':id' => $res_select['id']));
    }else {
        $statement_insert = $GLOBALS['pdo_conn']->prepare("INSERT INTO meal_delivery VALUES(NULL, :date, :main_contact_id, :restaurant_id, :purchase_order, :payment_type)");
        $statement_insert->execute(array(':date' => $current_date->format("Y-m-d H:i:s"), ':main_contact_id' => $GLOBALS['main_contact']['id'], ':restaurant_id' => $_POST['restaurant_id'], ':purchase_order' => $_POST['input_order'], ':payment_type' => $payment_type)); 
    }

    echo "<div class='response container_car'>
    <div class='car-wrapper'>
      <div class='car-wrapper_inner'>
  
        <div class='car_outter'>  
          <div class='car'>
            <div class='body'>
              <div></div>
            </div>
            <div class='decos'>
              <div class='line-bot'></div>
              <div class='door'>
                <div class='handle'></div>
                <div class='bottom'></div>
              </div>
              <div class='window'></div> 
              <div class='light'></div>
              <div class='light-front'></div>
              <div class='antenna'></div>
              <div class='ice-cream'>
                <div class='cone'></div>
              </div>  
            </div>
            <div>
              <div class='wheel'></div>
              <div class='wheel'></div>
            </div>    
            <div class='wind'>
              <div class='p p1'></div>
              <div class='p p2'></div>
              <div class='p p3'></div>
              <div class='p p4'></div>
              <div class='p p5'></div>
            </div>
          </div>
        </div>
      </div>
  
    </div>
  
    <div class='background-stuff'>
      <div class='bg'></div>
      <div class='bg bg-2'></div>
      <div class='bg bg-3'></div>
      <div class='ground'></div>
        <h3>
            <div id='countdown'></div>
            Deine Bestellung wurde erfolgreich versendet.
        <h3>
    </div>
  
  </div>";

}

$contact = getContactFromID($res_current_meal['main_contact_id']);
$restaurant = getRestaurant($res_current_meal['restaurant_id']);
$link = getCollectionData($res_current_meal['restaurant_id'], 1);
$time = getCollectionData($res_current_meal['restaurant_id'], 64);
$file = getDocumentsFromCollection($res_current_meal['restaurant_id']);

?>


<div id="food"class='meal_delivery'>
    <div class="title_with_icon">
        <div class="textcontent">
            <h2><?= $restaurant['description'] ?></h2>
        </div>
        <div class="svg_content">
            <svg id="fast-food" xmlns="https://www.w3.org/2000/svg" width="24.037" height="24.036" viewBox="0 0 24.037 24.036">
            <path id="Pfad_46" data-name="Pfad 46" d="M25.875,9h0" transform="translate(-8.249 -3.392)"/>
            <path id="Pfad_47" data-name="Pfad 47" d="M28.223,5.131h-4.56l.447-1.785,1.919-.654a.818.818,0,0,0,.533-.987.8.8,0,0,0-1.028-.537l-2.354.8a.8.8,0,0,0-.518.564l-.649,2.6H15.449a.817.817,0,0,0-.823.763.8.8,0,0,0,.8.84h.137l.05.434a.416.416,0,0,0,.414.367,8.056,8.056,0,0,1,5.014,1.578,6.1,6.1,0,0,1,1.746,2.131,6.18,6.18,0,0,1,.507,1.478.392.392,0,0,0,.165.244,3.606,3.606,0,0,1,1.321,4.328.4.4,0,0,0-.008.277,4.807,4.807,0,0,1,.268,1.582,4.639,4.639,0,0,1-1.072,3,4.012,4.012,0,0,1-1.441,1.085.4.4,0,0,0-.211.219,5.418,5.418,0,0,1-.87,1.5.125.125,0,0,0,.1.206h2.464a2.414,2.414,0,0,0,2.4-2.211L28.115,6.733h.13a.8.8,0,0,0,.8-.84A.818.818,0,0,0,28.223,5.131Z" transform="translate(-5.011 -1.125)"/>
            <path id="Pfad_48" data-name="Pfad 48" d="M5.766,19.586a1.2,1.2,0,0,1,.851.35l.777.777a.2.2,0,0,0,.283,0l.776-.777a1.2,1.2,0,0,1,.851-.35h9.844a.4.4,0,0,0,.4-.4v0a2.006,2.006,0,0,0-1.6-1.963,4.561,4.561,0,0,0-1.778-3.606,6.469,6.469,0,0,0-4.03-1.242H8.536c-3.416,0-5.73,1.941-5.809,4.847a2.006,2.006,0,0,0-1.6,1.963.4.4,0,0,0,.4.4Z" transform="translate(-1.125 -4.363)"/>
            <path id="Pfad_49" data-name="Pfad 49" d="M9.635,24.75a.4.4,0,0,0-.283.118L8.243,25.976a1,1,0,0,1-1.416,0L5.718,24.868a.4.4,0,0,0-.283-.118H1.959a.8.8,0,0,0-.789.659,3.266,3.266,0,0,0-.045.543,2.64,2.64,0,0,0,2.367,2.8,4.1,4.1,0,0,0,.86,1.976,3.413,3.413,0,0,0,2.683,1.228h6.61a3.409,3.409,0,0,0,2.683-1.229,4.113,4.113,0,0,0,.86-1.976,2.638,2.638,0,0,0,2.367-2.8,3.266,3.266,0,0,0-.045-.542.8.8,0,0,0-.789-.659Z" transform="translate(-1.125 -7.925)"/>
        </svg>
        </div>
    </div>

    <div style='display: none' class='food_list'>
      <?php getInfo(); ?>
    </div>

    <div class="lieferandude">
        <div class="lieferungs_content">
            <p>Bestellung bis:</p>
            <p class="strong"><?= $time ?> Uhr</p>
        </div>
        <div class="lieferungs_content">
            <p>Abholende Person:</p>
            <p class="strong"><?= $contact['name']?></p>
        </div>
    </div>
    <div class="lieferungs_rezept">
        <?php 
            if($link != NULL){
                echo "<a class='button' href='".$link."' target='_blank'>Zur Speisekarte</a>";
            }else {
                if($file != NULL) {
                    echo "<a class='button' href='https://".$GLOBALS["_SERVER"]["HTTP_HOST"]."/userdata/filegallery/original/".$file."' target='_blank'>Zur Speisekarte</a>";
                }
            }
        ?>
    </div>

    <form method="post" autocomplete="off">
        <input type='hidden' name='restaurant_id' value='<?= $res_current_meal['restaurant_id'] ?>'>
        <textarea name='input_order' required placeholder="Bestellung (Gericht, Sonderwünsche, Anzahl, usw.)"></textarea>
        <?php 
        $disabled = "";
        if($current_date->format('H:i') >= $time){
            $disabled = "disabled='disabled'";
        }
        ?>
        <div class="payment_type">
            Barzahlung
            <input type='checkbox' class="checkbox" name='payment_type'>
            PayPal
        </div>
        <input class="button" type='submit' <?= $disabled ?> name='submit_send_order' value='Jetzt bestellen'>
    </form>
</div>

<script>


if ($(".response").length) {

  var countDownDate = new Date("<?= $current_date->format('Y-m-d')?>T10:00:00Z").getTime();

  var x = setInterval(function() {

    var now = new Date().getTime();

    var distance = countDownDate - now;

    var days = Math.floor(distance / (1000 * 60 * 60 * 24));
    var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
    var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
    var seconds = Math.floor((distance % (1000 * 60)) / 1000);

    document.getElementById("countdown").innerHTML = hours + "h " + minutes + "m " + seconds + "s ";

    if (distance < 0) {
      clearInterval(x);
      document.getElementById("countdown").innerHTML = "";
    }
  }, 1000);
}
</script>