<div class="module">Pr&auml;mien bearbeiten</div><br>
<label for="showdeleted">Gel&ouml;schte anzeigen</label><input type="checkbox" id="showdeleted" name="showdeleted" onclick='showdeleted()' <?= (isset($showdeleted))
        ? "checked='checked'"
        : '' ?> /><br/>
<script type="text/javascript">
    function showdeleted()
    {
        checkbox = document.getElementById('showdeleted');
        if (checkbox.checked)
        {
            href = window.location + '&showdeleted=1';
            window.location = href;
        } else {
            window.history.back();
        }

    }
</script>
<script>document.addEventListener('click', function (event) {
        //alert(event.target.id);
        inputid = 'up_' + event.target.id;
        sortelem = document.getElementById(inputid);
        if (sortelem !== null) {
            href = event.target.href;
            event.target.href = href.replace('#', '&sort=' + sortelem.value + '#');
        }
    }
    )</script>
<?php
if (!isset($praemie)) {
    $praemie = 0;
}

if (!isset($showdeleted))
    $showdeleted = false;

showPraemien($praemie, $showdeleted);


function showPraemien($praemienId, $showdeleted)
{
    global $_SERVER, $command, $module, $type, $sort;

    $first = true;
    $color[0] = "#eeeeee";
    $color[1] = "#f2f2f2";
    $colnr = 0;

    if ($command == 'up') {

        $praemienliste = new praemienliste($type);
        $praemienliste->showdeleted = $showdeleted;
        $praemienliste->getPraemien($type);
        if (isset($sort)) {
            $praemienliste->setSortId($praemienId, $sort);
        }
        else {
            for ($i = 0; $i < sizeof($praemienliste->praemien); $i++)
            {
                $praemie = $praemienliste->praemien[$i];
                if ($praemie->id == $praemienId) {
                    $prevpraemie = $praemienliste->praemien[$i - 1];
                    $prevpraemienId = $prevpraemie->id;
                    break;
                }
            }
            $praemienliste->switchentry($praemienId, $prevpraemienId);
        }
    }
    if ($command == 'down') {
        $praemienliste = new Praemienliste($type);
        $praemienliste->showdeleted = $showdeleted;
        $praemienliste->getPraemien($type);
        for ($i = 0; $i < sizeof($praemienliste->praemien); $i++)
        {
            $praemie = $praemienliste->praemien[$i];
            if ($praemie->id == $praemienId) {
                $nextpraemie = $praemienliste->praemien[$i + 1];
                $nextpraemienId = $nextpraemie->id;
                break;
            }
        }
        $praemienliste->switchentry($praemienId, $nextpraemienId);
    }
    if ($command == 'delete') {
        $myPraemie = new praemie($praemienId);
        if ($myPraemie->id) {
            $myPraemie->delete();
        }
    }


    foreach (Array(O18PRAEMIEN, U18PRAEMIEN, UPRAEMIEN) as $type)
    {
        $praemienliste = new Praemienliste($type);
        $praemienliste->normaliseSortOrders();
        $praemienliste->showdeleted = $showdeleted;
        $praemienliste->getPraemien($type);
        $praemien = $praemienliste->praemien;
        if (sizeof($praemien) == 0) {
            continue;
        }

        if ($type == O18PRAEMIEN) {
            print "<h2>Standardpr&auml;mien</h2>\n";
        }
        if ($type == U18PRAEMIEN) {
            print "<h2>U18-Pr&auml;mien</h2>\n";
        }
        if ($type == UPRAEMIEN) {
            print "<h2>U-Pr&auml;mien</h2>\n";
        }
        ?>
        <hr />
        <a href="<?= $_SERVER['PHP_SELF'] ?>?module=<?= $module ?>&action=new">Neue Pr&auml;mie erstellen</a><br/><br />
        <table>
            <tr bgcolor='#dddddd'>
                <td>Nr</td><td>Sort</td><td>Name</td><td>Punkte</td><td>&nbsp;</td>
            </tr>
        <?php
        $i = 0;
        foreach ($praemien as $myPraemie)
        {
            $colnr = 1 - $colnr;
            $myColor = $color[$colnr];
            if ($praemienId == $myPraemie->id)
                $myColor = '#fff2f2';
            if ($myPraemie->status == 'DELETED')
                $myColor = '#ffa0a0';
            ?>
                <tr bgcolor="<?= $myColor ?>">
                    <td align="right"><a name="<?= $myPraemie->id . '_' . $myPraemie->praemientyp . $type ?>"><?= $myPraemie->id ?></a></td>
                    <td><?php echo $praemienliste->getSortId($myPraemie->id, $type); ?></td>
                    <td><a href="<?= $_SERVER['PHP_SELF'] ?>?module=<?= $module ?>&action=edit&praemie=<?= $myPraemie->id ?>"><?= $myPraemie->name ?></a></td>
                    <td><?= $myPraemie->punkte ?></td>
                    <td align="right">
                        <?php if ($type == O18PRAEMIEN && $myPraemie->praemientyp == 'U18') { ?>
                            <img src="/grafik/u18/praemien_stempel_u18.gif" alt="U18" />
                        <?php }
                        else { ?>
                            <a href="<?= $_SERVER['PHP_SELF'] ?>?module=<?= $module ?>&action=edit&praemie=<?= $myPraemie->id ?>"><img src="/grafik/button_edit.png" border="0" alt="Pr&auml;mie editieren" /></a>
                        <?php } ?>

                        <?php if ($myPraemie->status != 'DELETED') : ?>
                            <input id="up_<?php echo $type . '_' . $myPraemie->id; ?>" type="text" style="width: 32px" maxlength="4" value="<?php echo $praemienliste->getSortId($myPraemie->id,
                                                                                                                                                                  $type); ?>" />
                    <?php if (!$first) { ?>
                                <a id="<?php echo $type . '_' . $myPraemie->id; ?>" href="<?= $_SERVER['PHP_SELF'] ?>?module=<?= $module ?>&action=show&command=up&praemie=<?= $myPraemie->id ?>&type=<?= $type ?>&rand=<?= rand(0,
                                                                                                                                                                                                              20000) ?>#<?= $myPraemie->id . '_' . $myPraemie->praemientyp . $type ?>">^</a>
                    <?php }
                    else { ?>&nbsp; <?php }
                if ($i != count($praemien) - 1) {
                    ?>
                                <a href="<?= $_SERVER['PHP_SELF'] ?>?module=<?= $module ?>&action=show&command=down&praemie=<?= $myPraemie->id ?>&type=<?= $type ?>&rand=<?= rand(0,
                                                                                                                                                               20000) ?>#<?= $myPraemie->id . '_' . $myPraemie->praemientyp . $type ?>">V</a>
                <?php }
                else { ?>&nbsp; <?php } ?>
                <?php if ($type == O18PRAEMIEN && $myPraemie->praemientyp == 'U18') { ?>
                                &nbsp;
                <?php }
                else { ?>
                                <a href="<?= $_SERVER['PHP_SELF'] ?>?module=<?= $module ?>&action=show&command=delete&praemie=<?= $myPraemie->id ?>" onclick="return confirmdelete()"><img src="/grafik/button_drop.png" border="0" alt="Pr&auml;mie l&ouml;schen" /></a>
                <?php } ?>
            <?php endif ?>
                    </td>
                </tr>
            <?php
            $i++;
            $first = false;
        }
        ?>
        </table>
    <?php
    }
}
