<?php

$url = $_SERVER['PHP_SELF'] . "?" . $_SERVER['QUERY_STRING'];
if ('gaby' == $_GET['section']) {
    $table = "Kal_Gaby";
} elseif ('amelie' == $_GET['section']) {
    $table = "Kal_Amelie";
}

$sql1 = 'SELECT Von FROM Saison ORDER BY Von ASC';
$result1 = $db->query($sql1);
$nt1 = $result1->fetch_assoc();
$sql2 = "SELECT Bis FROM Saison ORDER BY Bis DESC";
$result2 = $db->query($sql2);
$nt2 = $result2->fetch_assoc();
$sql3 = 'SELECT DATE_FORMAT(Start, "%Y%m%d") AS Start, DATE_FORMAT(Ende, "%Y%m%d") AS Ende FROM ' . $table . ' ORDER BY Start ASC';
$result3 = $db->query($sql3);
$nt3 = $result3->fetch_assoc();
$sql4 = 'SELECT DATE_FORMAT(Von, "%Y%m%d") AS Von, DATE_FORMAT(Bis, "%Y%m%d") AS Bis FROM Saison WHERE Typ = "Hauptsaison" ORDER BY Von ASC';
$result4 = $db->query($sql4);
$nt4 = $result4->fetch_assoc();
$sql5 = 'SELECT DATE_FORMAT(Von, "%Y%m%d") AS Von, DATE_FORMAT(Bis, "%Y%m%d") AS Bis FROM Saison WHERE Typ = "Nebensaison" ORDER BY Von ASC';
$result5 = $db->query($sql5);
$nt5 = $result5->fetch_assoc();
$sql6 = 'SELECT DATE_FORMAT(Von, "%Y%m%d") AS Von, DATE_FORMAT(Bis, "%Y%m%d") AS Bis FROM Saison WHERE Typ = "Weihnachten" ORDER BY Von ASC';
$result6 = $db->query($sql6);
$nt6 = $result6->fetch_assoc();


$start['jahr'] = (int)date("Y", strtotime($nt1['Von']));
$start['monat'] = (int)date("m", strtotime($nt1['Von']));
$start['tag'] = (int)date("d", strtotime($nt1['Von']));
$ende['jahr'] = (int)date("Y", strtotime($nt2['Bis']));
$ende['monat'] = (int)date("m", strtotime($nt2['Bis']));
$ende['tag'] = (int)date("d", strtotime($nt2['Bis']));
$belegt['start'] = (int)$nt3['Start'];
$belegt['ende'] = (int)$nt3['Ende'];
$hs['start'] = (int)$nt4['Von'];
$hs['ende'] = (int)$nt4['Bis'];
$ns['start'] = (int)$nt5['Von'];
$ns['ende'] = (int)$nt5['Bis'];
$aw['start'] = (int)$nt6['Von'];
$aw['ende'] = (int)$nt6['Bis'];
$heute['jahr'] = (int)date("Y");
$heute['monat'] = (int)date("m");
$heute['tag'] = (int)date("d");


if ((!isset($_POST['<'])) and (!isset($_POST['>']))) {
    $anzeige['jahr'] = $heute['jahr'];
    $anzeige['monat'] = $heute['monat'];
} else {
    if (isset($_POST['<'])) {
        $anzeige['jahr'] = (int)substr($_POST['prev'], 0, 4);
        $anzeige['monat'] = (int)substr($_POST['prev'], 4, 2);
    } elseif (isset($_POST['>'])) {
        $anzeige['jahr'] = (int)substr($_POST['next'], 0, 4);
        $anzeige['monat'] = (int)substr($_POST['next'], 4, 2);
    }
}

if ($anzeige['monat'] <= 9) {
    $anzeige['zahl'] = (int)($anzeige['jahr'] . "0" . $anzeige['monat'] . "01");
} else {
    $anzeige['zahl'] = (int)($anzeige['jahr'] . $anzeige['monat'] . "01");
}
if ($anzeige['monat'] == 1) {
    $prev_month = (int)$anzeige['jahr'] - 1 . '12';
    $next_month = (int)$anzeige['jahr'] . '02';
} elseif ($anzeige['monat'] == 12) {
    $prev_month = (int)$anzeige['jahr'] . '11';
    $next_month = (int)$anzeige['jahr'] + 1 . '01';
} else {
    $prev_month = (int)$anzeige['jahr'] . '' . (int)($anzeige['monat'] - 1);
    $next_month = (int)$anzeige['jahr'] . '' . (int)($anzeige['monat'] + 1);
}
if (strlen($prev_month) == 5) {
    $prev_month = substr($prev_month, 0, 4) . '0' . substr($prev_month, 4, 1);
}
if (strlen($next_month) == 5) {
    $next_month = substr($next_month, 0, 4) . '0' . substr($next_month, 4, 1);
}
while ($belegt['ende'] < $anzeige['zahl']) {
    if ($nt3 = $result3->fetch_assoc()) {
        $belegt['start'] = (int)$nt3['Start'];
        $belegt['ende'] = (int)$nt3['Ende'];
    } else {
        break;
    }
}
while ($hs['ende'] < $anzeige['zahl']) {
    if ($nt4 = $result4->fetch_assoc()) {
        $hs['start'] = (int)$nt4['Von'];
        $hs['ende'] = (int)$nt4['Bis'];
    } else {
        break;
    }
}
while ($ns['ende'] < $anzeige['zahl']) {
    if ($nt5 = $result5->fetch_assoc()) {
        $ns['start'] = (int)$nt5['Von'];
        $ns['ende'] = (int)$nt5['Bis'];
    } else {
        break;
    }
}
while ($aw['ende'] < $anzeige['zahl']) {
    if ($nt6 = $result6->fetch_assoc()) {
        $aw['start'] = (int)$nt6['Von'];
        $aw['ende'] = (int)$nt6['Bis'];
    } else {
        break;
    }
}
$tag1 = $anzeige['jahr'] . "-" . $anzeige['monat'];
$anzahl = (int)date("t", strtotime($tag1));
$tag1 = date("l", strtotime($tag1));
if (1 == $anzeige['monat']) {
    $kal_mon = "Januar " . $anzeige['jahr'];
} elseif (2 == $anzeige['monat']) {
    $kal_mon = "Februar " . $anzeige['jahr'];
} elseif (3 == $anzeige['monat']) {
    $kal_mon = "Maerz " . $anzeige['jahr'];
} elseif (4 == $anzeige['monat']) {
    $kal_mon = "April " . $anzeige['jahr'];
} elseif (5 == $anzeige['monat']) {
    $kal_mon = "Mai " . $anzeige['jahr'];
} elseif (6 == $anzeige['monat']) {
    $kal_mon = "Juni " . $anzeige['jahr'];
} elseif (7 == $anzeige['monat']) {
    $kal_mon = "Juli " . $anzeige['jahr'];
} elseif (8 == $anzeige['monat']) {
    $kal_mon = "August " . $anzeige['jahr'];
} elseif (9 == $anzeige['monat']) {
    $kal_mon = "September " . $anzeige['jahr'];
} elseif (10 == $anzeige['monat']) {
    $kal_mon = "Oktober " . $anzeige['jahr'];
} elseif (11 == $anzeige['monat']) {
    $kal_mon = "November " . $anzeige['jahr'];
} else {
    $kal_mon = "Dezember " . $anzeige['jahr'];
}

?>
<table class="cal">
    <colgroup>
        <col span="7" width="33"/>
    </colgroup>
    <tr>

        <?php
        echo "<form action=\"$url\" method=\"post\" />\n";
        echo "    <input type=\"hidden\" name=\"prev\" value=\"" . $prev_month . "\" />\n";
        echo "    <input type=\"hidden\" name=\"next\" value=\"" . $next_month . "\" />\n";
        if (($anzeige['monat'] == $start['monat']) and ($anzeige['jahr'] == $start['jahr'])) {
            echo "<td></td>\n";
        } else {
            echo "<td>";
            echo "    <input type=\"submit\" name=\"<\" value=\"<\" />\n";
            echo "</td>";
        }
        echo "<th colspan=\"5\">";
        echo $kal_mon;
        echo "</th>";
        if (($anzeige['monat'] == $ende['monat']) and ($anzeige['jahr'] == $ende['jahr'])) {
            echo "<td></td>\n";
        } else {
            echo "<td>";
            echo "    <input type=\"submit\" name=\">\" value=\">\" />\n";
            echo "</td>";
        }
        echo "</form>\n";
        echo "</tr><tr>";
        ?>
    <tr>
        <th>Mo</th>
        <th>Di</th>
        <th>Mi</th>
        <th>Do</th>
        <th>Fr</th>
        <th>Sa</th>
        <th>So</th>
    </tr>
    <?php

    if ('Monday' == $tag1) {
        $j = 0;
    } elseif ('Tuesday' == $tag1) {
        echo "<td></td>\n";
        $j = 1;
    } elseif ('Wednesday' == $tag1) {
        echo "<td></td><td></td>\n";
        $j = 2;
    } elseif ('Thursday' == $tag1) {
        echo "<td></td><td></td><td></td>\n";
        $j = 3;
    } elseif ('Friday' == $tag1) {
        echo "<td></td><td></td><td></td><td></td>\n";
        $j = 4;
    } elseif ('Saturday' == $tag1) {
        echo "<td></td><td></td><td></td><td></td><td></td>\n";
        $j = 5;
    } elseif ('Sunday' == $tag1) {
        echo "<td></td><td></td><td></td><td></td><td></td><td></td>\n";
        $j = 6;
    }

    for ($i = 1; $i <= $anzahl; $i++) {
// $anzeige['zahl'] anpassen
        if ($i <= 9) {
            if ($anzeige['monat'] <= 9) {
                $anzeige['zahl'] = (int)($anzeige['jahr'] . "0" . $anzeige['monat'] . "0" . $i);
            } else {
                $anzeige['zahl'] = (int)($anzeige['jahr'] . $anzeige['monat'] . "0" . $i);
            }
        } else {
            if ($anzeige['monat'] <= 9) {
                $anzeige['zahl'] = (int)($anzeige['jahr'] . "0" . $anzeige['monat'] . $i);
            } else {
                $anzeige['zahl'] = (int)($anzeige['jahr'] . $anzeige['monat'] . $i);
            }
        }
// Ist Belegt?
        if (($belegt['start'] <= $anzeige['zahl']) and ($belegt['ende'] >= $anzeige['zahl'])) {
            echo "<td class=\"bel\">";
        } // Wenn nicht belegt, ist Hauptsaison?
        elseif (($hs['start'] <= $anzeige['zahl']) and ($hs['ende'] >= $anzeige['zahl'])) {
            echo "<td class=\"hs\">";
        } // Wenn nicht belegt und nicht Hauptsaison, ist Nebensaison?
        elseif (($ns['start'] <= $anzeige['zahl']) and ($ns['ende'] >= $anzeige['zahl'])) {
            echo "<td class=\"ns\">";
        } elseif (($aw['start'] <= $anzeige['zahl']) and ($aw['ende'] >= $anzeige['zahl'])) {
            echo "<td class=\"aw\">";
        } else {
            echo "<td>";
        }

        if ($belegt['ende'] <= $anzeige['zahl']) {
            if ($nt3 = $result3->fetch_assoc()) {
                $belegt['start'] = (int)$nt3['Start'];
                $belegt['ende'] = (int)$nt3['Ende'];
            }
        }

        echo $i;
        echo "</td>";
        $j++;
        if (7 == $j) {
            echo "</tr>\n<tr>\n";
            $j = 0;
        }

    }
    for ($rest = $j; $rest < 7; $rest++) {
        echo "<td></td>";
    }
    ?>
    </tr>
</table>
