<?php
$translation = \DynCom\mysyde\common\classes\Registry::get("translation");
$formname    = "form_meal_delivery_list";
$inputname   = "input_id";

switch ($_GET["action"]) {
    case 'save':
        save_meal_delivery();
        break;
    default:
        break;
}

function save_meal_delivery(){
    $query = "SELECT * FROM meal_delivery_setup";
    $result = @mysqli_query($GLOBALS['mysql_con'], $query);
    while($meal_delivery = @mysqli_fetch_array($result)){
        $id = $meal_delivery['id'];
        $query = "UPDATE meal_delivery_setup SET
        main_contact_id = '".$_POST['input_contact_'.$id]."',
        restaurant_id = '".$_POST['input_restaurant_'.$id]."'
        WHERE id = ".$id;
        var_dump($query);
        @mysqli_query($GLOBALS['mysql_con'], $query);
    }
}

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

    $query = "SELECT * FROM meal_delivery_setup";
    $result = @mysqli_query($GLOBALS['mysql_con'], $query);
    ?>     
    <div class="table_row label_wrapper">
        <div class="label_day">Tag</div>
        <div class="label_lieferandude">Abholer</div>
        <div class="label_restaurant">Restaurant</div>
    </div>

    <?php
    while($meal_delivery = @mysqli_fetch_array($result)){
        $id = $meal_delivery['id'];
        $active = ($id == $current_date->format('j')) ? "row_active" : "";
    ?>

    <div class='table_row <?= $active ?>'>
        <div class="day"><?= $id; ?>.</div>
        <?= input_select_from_sql("", "input_contact_".$id, 'SELECT DISTINCT main_contact.id,name AS name FROM main_contact LEFT JOIN main_contact_department ON main_contact.id = main_contact_department.main_contact_id WHERE main_role_id = 2', $meal_delivery['main_contact_id'], FALSE, FALSE); ?>
        <?= input_select_from_sql("", "input_restaurant_".$id, 'SELECT id, description as name FROM main_collection WHERE main_collection_setup_id = 2', $meal_delivery['restaurant_id'], FALSE, FALSE); ?>
    </div>
    <?php }
}


?>
<ul class="toolbar_menu">
    <?= button("save", $translation->get("save"), $formname, "loadCard('save', true, '', true)"); ?>
    <?= button("new", $translation->get("new_restaurant"), $formname, "loadSite('".get_menu_link('collections/2', $site, $language, '?action=open_card_collection')."')"); ?>
</ul>

<div id="mainContent">

    <?php echo current_website_language($site, $language); ?>
    <h1><?php echo get_translation('top_meal_delivery'); ?></h1>

    <form id="<?= $formname ?>" name="<?= $formname ?>" method="post">
        <?php load_meal_delivery(); ?>
    </form>
</div>