<h3>neues Angebot anlegen</h3>

<?php
if (!isset($_POST['Speichern'])) {
    ?>

    <form action="index.php?section=angebote" method="post" enctype="multipart/form-data">
        <label>Foto auswählen</label>
        <input type="hidden" name="new" value="speichern"/>
        <input type="file" name="Foto"/>
        <br/>
        <label>Überschrift 1</label>
        <input type="text" name="head1"/>
        <br/>
        <label>Überschrift 2</label>
        <input type="text" name="head2"/>
        <br/>
        <label>Text</label>
        <br/>
        <textarea name="text" cols="90" rows="10"></textarea>
        <br/>
        <p>Gültig für die Wohnungen:</p>
        <label>Amelie</label>
        <input type="checkbox" name="amelie"/>
        <br/>
        <label>Gaby</label>
        <input type="checkbox" name="gaby"/>
        <br/>
        <label>Preis Wohnung Amelie</label>
        <input type="text" name="preisA"/>
        <br/>
        <label>Preis Wohnung Gaby</label>
        <input type="text" name="preisG"/>
        <br/>
        <label>Anzahl Personen</label>
        <input type="text" name="Personen"/>
        <br/>
        <label>Leistungen (durch Enter von einander trennen)</label>
        <br/>
        <textarea name="Leistungen" cols="90" rows="10"></textarea>
        <br/>
        <label>Gültigkeit</label>
        <input type="text" name="Valid"/>
        <br/>
        <input type="submit" name="Speichern" value="Speichern"/>
    </form>

    <?php
} else {
    if ('' != $_FILES['Foto']['name']) {
        $head1 = $_POST['head1'];
        $head2 = $_POST['head2'];
        $text = $_POST['text'];
        if ("on" == $_POST['amelie']) {
            $amelie = 1;
        } else {
            $amelie = 0;
        }
        if ("on" == $_POST['gaby']) {
            $gaby = 1;
        } else {
            $gaby = 0;
        }
        $preisA = $_POST['preisA'];
        $preisG = $_POST['preisG'];
        $Personen = $_POST['Personen'];
        $Leistungen = $_POST['Leistungen'];
        $Valid = $_POST['Valid'];
        $sql1 = 'INSERT INTO Angebot (ID, head1, head2, text, amelie, gaby, preisA, preisG, Personen, Leistungen, Valid) VALUES (NULL, "' . $head1 . '", "' . $head2 . '", "' . $text . '", "' . $amelie . '", "' . $gaby . '", "' . $preisA . '", "' . $preisG . '", "' . $Personen . '", "' . $Leistungen . '", "' . $Valid . '");';
        $stmt1 = $db->prepare($sql1);
        if (!$stmt1) {
            die ('Es konnte kein SQL-Query vorbereitet werden: ' . $db->error);
        }
        if (!$stmt1->execute()) {
            die ('Query konnte nicht ausgef�hrt werden: ' . $stmt1->error);
        }

        $sql2 = 'SELECT last_insert_id()';
        $result2 = $db->query($sql2);
        $last = $result2->fetch_assoc();
        $id = $last['last_insert_id()'];

        $uploaddir = '../img/angebot/';
        $uploadfile = $uploaddir . $_FILES['Foto']['name'];
        move_uploaded_file($_FILES['Foto']['tmp_name'], $uploadfile);
        $src = imagecreatefromjpeg($uploadfile);
        list($width, $height) = getimagesize($uploadfile);
        if ($height > $width) {
            $newheight = 280;
            $newwidth = ($width / $height) * 280;
        } else {
            $newwidth = 280;
            $newheight = ($height / $width) * 280;
        }
        $tmp = imagecreatetruecolor($newwidth, $newheight);
        imagecopyresampled($tmp, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
        $filename = $uploaddir . $id . ".jpg";
        imagejpeg($tmp, $filename, 80);
        imagedestroy($src);
        imagedestroy($tmp);
        unlink($uploaddir . $_FILES['Foto']['name']);
    }
    echo '<a href="index.php?section=angebote">Zur�ck zur �bersicht</a>';

}
?>
