<?php
$current_date = new DateTime();

function get_contact($contact_id){
    $query = "SELECT * FROM main_contact WHERE id = ".$contact_id;
    $result = @mysqli_query($GLOBALS['mysql_con'], $query);
    $contact = @mysqli_fetch_array($result);
    return $contact;
}


function get_payment_type($payment_type){
    switch ($payment_type) {
        case '0':
            $payment_type = 'Barzahlung';
            break;
        case '1':
            $payment_type = 'PayPal';
            break;
        default:
            break;
    }
    return $payment_type;
}

function get_restaurant($date){
    $query = "SELECT * FROM main_collection LEFT JOIN meal_delivery_setup ON meal_delivery_setup.restaurant_id = main_collection.id WHERE meal_delivery_setup.id = ".$date;
    $result = @mysqli_query($GLOBALS['mysql_con'], $query);
    $row = @mysqli_fetch_array($result);
    return $row['description'];
}

function get_list(){
    $current_date = new DateTime();

    $query = "SELECT * FROM meal_delivery WHERE date LIKE '".$current_date->format('Y-m-d')."%' ORDER BY date ASC";
    $result = @mysqli_query($GLOBALS['mysql_con'], $query);
    $i = 1;
    while($meal = @mysqli_fetch_array($result)) {?>
    <div class='list_item'>
        <div class="liefer_row">
            <p class="liefer_anzahl"><?= $i?></p>
            <p class="liefer_name"><?= $meal['purchase_order']?></p>
        </div>
        <p class="liefer_contact"><?= get_contact($meal['main_contact_id'])['name']?></p>
        <p class="liefer_mail"><?= get_contact($meal['main_contact_id'])['email']?></p>
        <p class="liefer_payment"><?= get_payment_type($meal['payment_type'])?></p>
    </div>
    <div class="stars">***************************************</div>
    <?php 
    $i++;
    }   
}

?>

<div id='printableArea' class="receipt">
    <div class="receipt_headline">
        <h2>Bestellungen <?= $current_date->format('d.m.Y')?></h2>
    </div>
    <div class="receipt_location">
        <?= get_restaurant($current_date->format('d'))?>
    </div>
    <?php
        get_list();
    ?>
    <div class="barcode"><?= $current_date->format('d.m.Y')?></div>
    <div class="id_number">2003 0998 9000 8730 21</div>
    <div class="receipt_print"><button onclick="printDiv('printableArea')">Bestellungsliste drucken</button></div>
    

</div>

<script>
function printDiv(divName) {
     var printContents = document.getElementById(divName).innerHTML;
     var originalContents = document.body.innerHTML;

     document.body.innerHTML = printContents;

     window.print();

     document.body.innerHTML = originalContents;

     
}
</script>
