<?php
  $thisPage="admin";
  $thissheet = "/admin/edit_c";
  require(__DIR__ . "/../../layout/header.php");
  include __DIR__ . "./../../../views/layout/css/navi.css";
  require __DIR__ . "/../../layout/navigation.php";

  $c_id = getAndSetSession('c_id', 'c_id', '1'); ?>

  <div class="container">
    <div class="starter-template">
      <h3>Kommentare editieren</h3><?php
      if(!empty($savedSuccess)): ?>
        <p>Der Kommentar wurde erfolgreich gespeichert</p><?php
      endif; ?>

      <h3> <?php echo "Post ".e($post->id).": ".e($post->title); ?></h3>
      <h4> <?php echo "Content: ".e($post->content); ?></h4>

      <!-- ***************************** Beginn commente *****************************-->
      <div>
        <table>
          <tr style="font-weight: bold; background-color:lightgrey">
          </tr><?php
          $anzahl = count($comments);  // Anzahl
          $com = array();
          $comment = array();
          if ($anzahl != 0) {  // Anzahl > 0 dann
            foreach ($comments as $mem){       //($commente as $mem) {
              $comment = [
                'post_id' => $mem->post_id,     // id des posts
                'id' => $mem->id,               // id des Kommentars
                'content' => $mem->content,
                'original' => $mem->original
                ];
              $com[] = $comment;
            }
          }
          $id_o_no = 0; // Zähler für array
          $tr0 = "<tr style=\"background-color:lightcyan;\">"; ?>
        <hr>
      </div>
    </div>
  </div>

  <div><?php
    $anz = count($com);
    if ($c_id >= $anz ) {$c_id = 0;}
    $action = 1; ?>
    <form method="post">


      <p><b>content</b></p>
      <p><textarea autofocus id="content" name="content" cols="150" rows="4" > <?php echo $com["$c_id"]['content'];?>
      </textarea></p>


      <table style="width:600px">

        <tr>
          <td><input style="background-color:yellow;" type="submit" name="submit"
              value="<?php if (count($com)!=0) {echo "update";}else {echo "update not possible";} ?>"
              placeholder="update" size="20%"/></td>

          <td><input type="text" name="post_id" id="post_id" hidden="true"
              value="<?php echo $com["$c_id"]["post_id"];?>" placeholder="post_id"  size="5%" readonly />&nbsp;</td>

          <td><input type="text" name="id" id="id" hidden="true"
              value="<?php echo $com["$c_id"]["id"];?>" size="5%" readonly >&nbsp;</td>

          <td><input style="text-align: center; background-color:lightyellow width:3%" type="text" name="original" id="original"
              value="<?php if (count($com) != 0) {
                  echo $com["$c_id"]["original"];}?>" placeholder="erl"  size="5%" />
                  &nbsp;veränderter Text: > 0 </td>



          <td style=size:"40%" >&nbsp; </td>
        </tr>
      </table>
    </form>

    </br>
    <table style="width:100%">
      <tr>
        <td colspan="5"><?php echo "Anzahl Kommentare: " .$anzahl; ?>
        </td>
      </tr>
      <tr style="font-weight:bold; background-color:lightgrey">
        <td>s &nbsp;</td><td>&nbsp;post&nbsp;</td><td>&nbsp;id&nbsp;</td><td>&nbsp;Kommentar&nbsp;</td>
        <td>&nbsp;original</td>
      </tr>

      <form name="c_id" action="posts-edit_c" id="c_id" method="get"><?php
        $id_no = 0; // Zähler für array
        foreach ($com AS $key => $issue) {
          echo $tr0; ?>
            <td><input type="radio" name="c_id" id="c_id" value="<?php echo $id_no;?>"<?php
               if ($c_id == $id_no) { echo "checked=checked";} ?> onchange="submit()" /></td><?php
            $id_no = $id_no + 1;
            foreach ($issue AS $key1 => $data) {
              echo "<td>&nbsp;".$data."&nbsp;</td>";
            }
          echo "</tr>";
        } ?>

      </form>
    </table>

  </div>
  </br>
  <div class="w3-container">
    <a href="posts-admin" class="w3-btn w3-red">zurück</a>
  </div>

  <?php include (__DIR__ . "/../../layout/footer.php"); ?>
