<?php
$EDIT_ID=(isset($_GET['edit_id'])) ? $_GET['edit_id']:0;

if (LOGIN!="") {
  if (TO_DO=="save_edit") {
    $edit_location=$_POST['edit_location'];
    $edit_datetime=$_POST['edit_datetime'];
    $edit_player=$_REQUEST['edit_player'];
    $edit_scoregame=(isset($_POST['edit_scoregame'])) ? $_POST['edit_scoregame']:"FALSE";
    $help1="";$help2="";
    foreach ($edit_player as $help1)
      $help2.=$help1;
    $edit_player=$help2;
    $edit_base_bill=money_to_bill($_POST['edit_base_bill']);
    $edit_first_id=$_POST['edit_first_id'];
    $edit_first_bill=money_to_bill($_POST['edit_first_bill']);
    $edit_second_id=$_POST['edit_second_id'];
    $edit_second_bill=money_to_bill($_POST['edit_second_bill']);
    $edit_third_id=$_POST['edit_third_id'];
    $edit_third_bill=money_to_bill($_POST['edit_third_bill']);
    mysqli_query($db, "UPDATE `games` SET `scoregame` = '$edit_scoregame', `location` = '$edit_location', `datetime` = '$edit_datetime', `player` = '$edit_player', `base_bill` = '$edit_base_bill', `first_id` = '$edit_first_id', `first_bill` = '$edit_first_bill', `second_id` = '$edit_second_id', `second_bill` = '$edit_second_bill', `third_id` = '$edit_third_id', `third_bill` = '$edit_third_bill' WHERE `games`.`id` =$EDIT_ID LIMIT 1 ; ");
  }

  if (TO_DO=="new_game") {
    $query=mysqli_query($db, "SELECT `location` , `datetime` , `player` , `base_bill` , `first_id` , `first_bill` , `second_id` , `second_bill` , `third_id` , `third_bill` FROM `games` ORDER BY `games`.`id` DESC LIMIT 0 , 1");
    $data=mysqli_fetch_row($query);
    $hit=0;
    if ($data){
      foreach($data as $field) {
        if ($field=="0000-00-00 00:00:00") continue;
        if ($field) $hit=1;
      }
    }
    else $hit=1;
    if ($hit) {
      mysqli_query($db, "INSERT INTO `games` ( `location`, `datetime`, `player`, `base_bill`, `first_id`, `first_bill`, `second_id`, `second_bill`, `third_id`, `third_bill` ) VALUES ( 'Bei Wem?', '1989-10-03 12:00:00', '', '200', '0', '0', '0', '0', '0', '0' );");
      echo "HERE" . mysqli_error($db);
      $query=mysqli_query($db, "SELECT `id` FROM `games` ORDER BY `games`.`id` DESC LIMIT 0 , 1");
      $data=mysqli_fetch_row($query);
      $EDIT_ID=$data[0];
    }
    else {
      $EDIT_ID=0;
      $ERROR.="Ein neues Spiel wurde bereits angelegt!<br>";
    }
  }

  if ($EDIT_ID) {
    $query=mysqli_query($db, "SELECT * FROM `games` WHERE `id` = $EDIT_ID LIMIT 0 , 1");
    $data =mysqli_fetch_array($query);
    $BODY.='<form action="index.php?page=edit_game&to_do=save_edit&edit_id='.$EDIT_ID.'" method="post">';
    $BODY.='<table class="list">';
    $BODY.='<tr><th>Location:</th><td colspan="3"><input type="text" name="edit_location" value="'.$data['location'].'"></td></tr>';
    $BODY.='<tr><th>Datum/Zeit:</th><td colspan="3"><input type="text" name="edit_datetime" value="'.$data['datetime'].'"></td></tr>';
    $BODY.='<tr><th>Spieler:</th><td colspan="3"><select size="10" name="edit_player[]" multiple>';
      $query=mysqli_query($db, "SELECT * FROM `player` ORDER BY alias");
      $player_of_game=player_of_game($EDIT_ID);
      while ($playerlist=mysqli_fetch_array($query)) {
        $selected="";
        foreach ($player_of_game as $player_in_game) $selected=($player_in_game==$playerlist['id']) ? " selected":$selected;
        $BODY.='<option value="'.$playerlist['id'].'," '.$selected.'>'.$playerlist['alias'].'</option>';
      }
    $BODY.='</select>';
    $BODY.='<tr><th>Rangspiel:</th><td colspan="3"><input type="checkbox" name="edit_scoregame" value="TRUE" '.(($data['scoregame']=="TRUE") ? "checked":"").'></td></tr>';
    $BODY.='<tr><th>Einsatz:</th><td colspan="3"><input type="text" name="edit_base_bill" value="'.print_bill($data['base_bill']).'"></td></tr>';
    $BODY.='<tr><th>&nbsp;</th><th>1. Platz</th><th>2. Platz</th><th>3. Platz</th></tr>';
    $BODY.='<tr><th>Spieler:</th>';
      $BODY.='<td><select size="1" name="edit_first_id">';
      $query=mysqli_query($db, "SELECT * FROM `player` ORDER BY alias");
      while ($playerlist=mysqli_fetch_array($query)) {
        $selected=($playerlist['id']==$data['first_id']) ? " selected":"";
        $BODY.='<option value="'.$playerlist['id'].'"'.$selected.'>'.$playerlist['alias'].'</option>';
      }
      $BODY.='</select></td>';
      $BODY.='<td><select size="1" name="edit_second_id">';
      $query=mysqli_query($db, "SELECT * FROM `player` ORDER BY alias");
      while ($playerlist=mysqli_fetch_array($query)) {
        $selected=($playerlist['id']==$data['second_id']) ? " selected":"";
        $BODY.='<option value="'.$playerlist['id'].'"'.$selected.'>'.$playerlist['alias'].'</option>';
      }
      $BODY.='</select></td>';
      $BODY.='<td><select size="1" name="edit_third_id">';
      $query=mysqli_query($db, "SELECT * FROM `player` ORDER BY alias");
      while ($playerlist=mysqli_fetch_array($query)) {
        $selected=($playerlist['id']==$data['third_id']) ? " selected":"";
        $BODY.='<option value="'.$playerlist['id'].'"'.$selected.'>'.$playerlist['alias'].'</option>';
      }
      $BODY.='</select></td>';
    $BODY.='</tr>';
    $BODY.='<tr><th>Preisgeld:</th>';
      $BODY.='<td><input size="4" type="text" name="edit_first_bill" value="'.print_bill($data['first_bill']).'"></td>';
      $BODY.='<td><input size="4" type="text" name="edit_second_bill" value="'.print_bill($data['second_bill']).'"></td>';
      $BODY.='<td><input size="4" type="text" name="edit_third_bill" value="'.print_bill($data['third_bill']).'"></td>';
    $BODY.='</tr>';
    $BODY.='<tr><th>Speichern:</th><td><input type="submit" value="" class="submit_save"</td></tr>';

    $BODY.='</table></form>';
  }
}
else {
  $ERROR.="Sie m&uuml;ssen sich einloggen!<br>";
}
?>
