<?php 
$translation   = \DynCom\mysyde\common\classes\Registry::get("translation");
$query = "SELECT *  FROM calendar_serial WHERE main_collection_id = " . $input_collection['id'];
$result = @mysqli_query($GLOBALS['mysql_con'], $query);
if(@mysqli_num_rows($result) > 0) {
    $row = mysqli_fetch_array($result);
}

$date_from = new DateTime($row['date_from']);
$date_to = new DateTime($row['date_to']);

$weekday = array("Montag", "Dienstag", "Mittwoch", "Donnerstag", "Freitag", "Samstag", "Sonntag");
$weekday_en = array("monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday");
$month = array("Januar", "Februar", "März", "April", "Mai", "Juni", "Juli", "August", "September", "Oktober", "November", "Dezember");
$ordinal = array("ersten", "zweiten", "dritten", "vierten", "letzten");
$repeating_type_en= array("daily", "weekly", "monthly", "yearly");
$repeating_type = array("Tächlich", "Wöchentlich", "Monatlich", "Jährlich");

$default_value = 1;
$value = $default_value;

function getCalendarValue($value, $type, $classname = ""){
    $amount = 0;
    switch ($value['repeating_type']) {
        case 'daily':
            $amount = $value['day_nr'];
            break;
        case 'weekly':
            $amount = $value['week_nr'];
            break;
        case 'monthly':
            if($value['repeating_radio'] == 1){
                if($classname == 'first_day'){
                    $amount = $value['day_nr'];
                }else if($classname == 'first_month'){
                    $amount = $value['month_nr'];
                }
            }else{
                if($classname == 'second_day'){
                    $amount = $value['day_nr'];
                }else if($classname == 'second_week'){
                    $amount = $value['week_nr'];
                }else if($classname == 'second_month'){
                    $amount = $value['month_nr'];
                }
            }
            break;
        case 'yearly':
            if($classname == 'year_nr'){
                $amount = $value['year_nr'];
            }
            if($value['repeating_radio'] == 1){
                if($classname == 'first_day'){
                    $amount = $value['day_nr'];
                }else if($classname == 'first_month'){
                    $amount = $value['month_nr'];
                }
            }else{
                if($classname == 'second_day'){
                    $amount = $value['day_nr'];
                }else if($classname == 'second_week'){
                    $amount = $value['week_nr'];
                }else if($classname == 'second_month'){
                    $amount = $value['month_nr'];
                }
            }
            break;
        default:
            break;
    }
    if($type != $value['repeating_type']){
        $amount = 0;
    }
    return $amount;
}
?>

<button id="serientermin">Serientermin erstellen</button>
<div class='serial_date'>
    <div class="serial_heading">
        <h3>Serienmuster auswählen</h3>
        <button onclick="closeSerialize();saveCollection(false);" id="serienterminclose"></button>
    </div>
    <label id='date_from'>Von</label>
    <input id='date_from' type="date" name="date_from" value="<?= $date_from->format('Y-m-d') ?>">
    <label id='date_to'>Bis</label>
    <input id='date_to' type="date" name="date_to" value="<?= $date_to->format('Y-m-d') ?>">
    <label id='start_time'>Beginn</label>
    <input id='start_time' type="time" name="start_time" value="<?= $row['start_time']?>">
    <label id='end_time'>Ende</label>
    <input id='end_time' type="time" name="end_time" value="<?= $row['end_time']?>">
    <div class="serial-content">
        <div class="radio-buttons">
            <?php createRadioButtons($repeating_type, $repeating_type_en, $row['repeating_type'])?>
            <input type='hidden' value='<?= $row['repeating_type'] ?>' name='input_repeating_type' id='repeating_type'>
            <input type='hidden' value='<?= $row['repeating_radio'] ?>' name='input_repeating_radio' id='repeating_radio'>
        </div>
        <div class="option-wrapper">
            <div class='daily repeating_type' style='display: none'>
                <h4>Täglich</h4>
                Jede/Alle <input min="0" type="number" name="daily_count" value="<?= getCalendarValue($row, 'daily') ?>"> Tag(e)
            </div>
            <div class='weekly repeating_type' style='display: none'>
                <h4>Wöchentlich</h4>
                Jede/Alle <input min="0" type="number" name="weekly_count" value="<?= getCalendarValue($row, 'weekly') ?>"> Woche(n) am
                <div class="week-wrapper">
                    <?php createCheckbox($weekday, $weekday_en, $row)?>
                </div>
            </div>
            <div class='monthly repeating_type' style='display: none'>
                <h4>Monatlich</h4>
                <div class="monthly-option">
                <input type="radio" <?= getRadioChecked($row['repeating_radio'], '1') ?> name="monthly_type" id="monthly_first" value="1"/>

                <label for='monthly_first'>Am</label>
                <input min="0" type="number" name="monthly_day_1" value="<?= getCalendarValue($row, 'monthly', 'first_day') ?>">. Tag jedes 
                <input min="0" type="number" name="monthly_count_1" value="<?= getCalendarValue($row, 'monthly', 'first_month') ?>">. Monats
                </div>
                <div class="monthly-option">
                    <input type="radio" <?= getRadioChecked($row['repeating_radio'], '2') ?> name="monthly_type" id="monthly_second" value="2"/>

                    <label for='monthly_second'>Am</label>
                    <select name='monthly_day_2'>
                        <?php createDropdown($ordinal, getCalendarValue($row, 'monthly', 'second_day'))?>
                    </select>
                    <select name='monthly_week_2'>
                        <?php createDropdown($weekday, getCalendarValue($row, 'monthly', 'second_week'))?>
                    </select>
                    jeden/alle <input min="0" type="number" name="montly_count_2" value="<?= getCalendarValue($row, 'monthly', 'second_month') ?>"> Monat(e)
                </div>
            </div>
            <div class='yearly repeating_type' style='display: none'>
                <h4>Jährlich</h4>
                <div class="yearly-options">
                Jedes/Alle <input min="0" type="number" name="yearly_count" value="<?= getCalendarValue($row, 'yearly', 'year_nr') ?>"> Jahr(n)
                </div>
                <div class="yearly-options">
                <label for='yearly_first'>Am</label>
                <input type="radio" <?= getRadioChecked($row['repeating_radio'], '1') ?> name="yearly_type" id="yearly_first" value="1"/>
                <input type="number" name="yearly_day_1" value="<?= getCalendarValue($row, 'yearly', 'first_day') ?>">.  
                <select name='yearly_month_1'>
                    <?php createDropdown($month, getCalendarValue($row, 'yearly', 'first_month'))?>
                </select>
                </div>
                <div class="yearly-options">
                <label for='yearly_second'>Am</label>
                <input type="radio" <?= getRadioChecked($row['repeating_radio'], '2') ?> name="yearly_type" id="yearly_second" value="2"/>
                <select name='yearly_day_2'>
                    <?php createDropdown($ordinal, getCalendarValue($row, 'yearly', 'second_day'))?>
                </select>
                <select name='yearly_week_2'>
                    <?php createDropdown($weekday, getCalendarValue($row, 'yearly', 'second_week'))?>
                </select>
                im
                <select name='yearly_month_2'>
                    <?php createDropdown($month, getCalendarValue($row, 'yearly', 'second_month'))?>
                </select>
                </div>
            </div>
        </div>
    </div>
</div>

<div class="modal-overlay"></div>

<script>
    function show_type(type){
        $(".repeating_type").hide();
        $("."+type).show();
    }

    $(".repeating_type").each(function(){
        let repeating_type = $('input[name="repeating_type"]:checked').val();
        if($(this).hasClass(repeating_type)){
            $(this).show();
        }
    });

$("button#serientermin").click(function(){
    $(".serial_date").addClass("modal-open");
    $(".serial_date.modal-open").prependTo("body");
    $(".modal-overlay").prependTo("body");
    $(".modal-overlay").show();
    $(this).hide()
});

function closeSerialize(){
    $(".serial_date.modal-open").appendTo(".collection_form");
    $(".serial_date").removeClass("modal-open");
    $("button#serientermin").show();
    $(".modal-overlay").hide();
}

$('input[name="repeating_type"]').change(function() {
    $('#repeating_type').val($(this).val());
});

$('input[name="monthly_type"]').change(function() {
    $('#repeating_radio').val($(this).val());
});

$('input[name="yearly_type"]').change(function() {
    $('#repeating_radio').val($(this).val());
});
</script>

<?php 
function getRadioChecked($repeating_radio, $value){
    $checked = 'checked';
    if($repeating_radio == $value){
        return $checked;
    }
    return;
}

function createDropdown($array, $active){
    echo "<option selected disabled>---</option>";
    foreach ($array as $key => $value) {
        $key = $key+1;
        $selected = "";
        if($key == $active){
            $selected = "selected";
        }
        echo "<option ".$selected." value='".$key."'>".$value."</option>";
    }
}

function createCheckbox($array, $weekday_en, $row){
    foreach ($array as $key => $value) {
        $checked = "";
        if($row[$weekday_en[$key]] == 1){
            $checked = "checked";
        }
        echo "<label for='".$weekday_en[$key]."'>".$value."</label>";
        echo "<input ".$checked." type='checkbox' name='".$weekday_en[$key]."' id='".$weekday_en[$key]."'>";
    }
}

function createRadioButtons($array, $repeating_type_en, $selected_type){
    foreach ($array as $key => $value) {
        $checked = "";
        if($repeating_type_en[$key] == $selected_type){
            $checked = "checked";
        }
        echo "<div class='radio-group'>";
        echo "<input ".$checked." type='radio' name='repeating_type' id='".$repeating_type_en[$key]."' value='".$repeating_type_en[$key]."' onclick='show_type(\"{$repeating_type_en[$key]}\");'/>";
        echo "<label for='".$repeating_type_en[$key]."'>".$value."</label>";
        echo "</div>";
    }
}