<?php
add_shortcode('LG_Hund', 'lg_hund');
add_action('wp', 'lg_set_doglist');

function lg_set_doglist()
{
  $d1_name = ($_POST['d1_name'] ?? '');
  $d1_wtag = ($_POST['d1_wtag'] ?? '');
  $d1_sex = ($_POST['d1_sex'] ?? '');
  $d1_zbnr = ($_POST['d1_zbnr'] ?? '');
  $d1_chip = ($_POST['d1_chip'] ?? '');
  $d1_vater = ($_POST['d1_vater'] ?? '');
  $d1_mutter = ($_POST['d1_mutter'] ?? '');
  $isdog = ($_POST['isdog'] ?? '');
  $d2_name = ($_POST['d2_name'] ?? '');
  $d2_wtag = ($_POST['d2_wtag'] ?? '');
  $d2_sex = ($_POST['d2_sex']  ?? '');
  $d2_zbnr = ($_POST['d2_zbnr'] ?? '');
  $d2_chip = ($_POST['d2_chip']  ?? '');
  $d2_vater = ($_POST['d2_vater']  ?? '');
  $d2_mutter = ($_POST['d2_mutter'] ?? '');
  $d3_name = ($_POST['d3_name'] ?? '');
  $d3_wtag = ($_POST['d3_wtag'] ?? '');
  $d3_sex = ($_POST['d3_sex'] ?? '');
  $d3_zbnr = ($_POST['d3_zbnr']  ?? '');
  $d3_chip = ($_POST['d3_chip'] ?? '');
  $d3_vater = ($_POST['d3_vater'] ?? '');
  $d3_mutter = ($_POST['d3_mutter'] ?? '');
  $daktiv = ($_POST['daktiv'] ?? '');
  if ($daktiv == '') {
    $daktiv = 'dog1';
  }
  if ($isdog == 'ok') {
    $mydog = array(
      "d1_name" => $d1_name,
      "d1_wtag" => $d1_wtag,
      "d1_sex" => $d1_sex,
      "d1_zbnr" => $d1_zbnr,
      "d1_chip" => $d1_chip,
      "d1_vater" => $d1_vater,
      "d1_mutter" => $d1_mutter,
      "d2_name" => $d2_name,
      "d2_wtag" => $d2_wtag,
      "d2_sex" => $d2_sex,
      "d2_zbnr" => $d2_zbnr,
      "d2_chip" => $d2_chip,
      "d2_vater" => $d2_vater,
      "d2_mutter" => $d2_mutter,
      "d3_name" => $d3_name,
      "d3_wtag" => $d3_wtag,
      "d3_sex" => $d3_sex,
      "d3_zbnr" => $d3_zbnr,
      "d3_chip" => $d3_chip,
      "d3_vater" => $d3_vater,
      "d3_mutter" => $d3_mutter,
      "daktiv" => $daktiv
    );
    $cjson = json_encode($mydog);
    $name = 'doglist';
    $wert = $cjson;
    $t = time() + 31556926;
    setcookie($name, $wert, $t, '/');
  }
}
function lg_hund()
{
  $tab1 = "'H1'";
  $tab2 = "'H2'";
  $tab3 = "'H3'";
  wp_enqueue_style("zz-shortcode-jscss-style");
  wp_enqueue_script('jquery');
  wp_enqueue_script("zz-shortcode-jscss-script", array('jquery'), '1.0', true);
  if (isset($_COOKIE['doglist'])) {
    $cjson = $_COOKIE['doglist'];
    $cjson = str_replace('\\"', '"', $cjson);
    $cjson = str_replace('\\\\', '\\', $cjson);
    $cjson = iconv("UTF-8", "Windows-1252", $cjson);
    //$cjson = str_replace('\\`','`',$cjson);
    $cjson = str_replace("\\\\", "\\", $cjson);
    //  $lg_content .= var_dump (json_decode($cjson,true)).'<br>';
    $mydog = json_decode($cjson, true);
    $d1_name = stripslashes($mydog["d1_name"]);
    $d1_wtag = $mydog["d1_wtag"];
    $d1_sex = $mydog["d1_sex"];
    $d1_zbnr = $mydog["d1_zbnr"];
    $d1_chip = $mydog["d1_chip"];
    $d1_vater = stripslashes($mydog["d1_vater"]);
    $d1_mutter = stripslashes($mydog["d1_mutter"]);
    $d2_name = stripslashes($mydog["d2_name"]);
    $d2_wtag = $mydog["d2_wtag"];
    $d2_sex = $mydog["d2_sex"];
    $d2_zbnr = $mydog["d2_zbnr"];
    $d2_chip = $mydog["d2_chip"];
    $d2_vater = stripslashes($mydog["d2_vater"]);
    $d2_mutter = stripslashes($mydog["d2_mutter"]);
    $d3_name = stripslashes($mydog["d3_name"]);
    $d3_wtag = $mydog["d3_wtag"];
    $d3_sex = $mydog["d3_sex"];
    $d3_zbnr = $mydog["d3_zbnr"];
    $d3_chip = $mydog["d3_chip"];
    $d3_vater = stripslashes($mydog["d3_vater"]);
    $d3_mutter = stripslashes($mydog["d3_mutter"]);
    $daktiv = $mydog["daktiv"];
  }

  if (($_POST['isdog'] ?? '') == 'ok') {
    $lg_content = '
    <div class="MeinParson">
    <table width=100%>
    <tr><td align="center">Daten zu ihrem Hund / Hunden wurde gespeichert.....</td></tr>
    <tr><td align="center"><a href="" class="button">zurück zum Formular</a></td></tr>
    </table>
    </div>
    ';
  } else {

    $lg_content = '
<div class="MeinParson">
  <div class="tab">
    <button class="tablinks" onclick="openCity(event, ' . $tab1 . ')" id="defaultOpen">Hund 1</button>
    <button class="tablinks" onclick="openCity(event, ' . $tab2 . ')">Hund 2</button>
    <button class="tablinks" onclick="openCity(event, ' . $tab3 . ')">Hund 3</button>
  </div>
  <form name="dogs" method="post" id="dogs">
    <div id="H1" class="tabcontent">
      <input type="hidden" id="isdog" name="isdog" value="ok">
      <label for="d1_name" id="d1_namelabel">Name:</label>
      <input type="text" name="d1_name" id="d1_name" class="textinput" value="' . $d1_name . '">
      <label for="d1_wtag" id="d1_wtaglabel">Wurftag:</label>
      <input type="text" name="d1_wtag" id="d1_wtag" class="textinput" value="' . $d1_wtag . '">
      <label for="d1_sex" id="d1_sexlabel">Rüde:</label>
      <input type="checkbox" name="d1_sex" id="d1_sex" ';
    if ($d1_sex == "on")
      $lg_content .= 'checked';
    $lg_content .= '><br>
      <label for="d1_zbnr" id="d1_zbnrlabel">Zuchtbuch-Nummer:</label>
      <input type="text" name="d1_zbnr" id="d1_zbnr" class="textinput" value="' . $d1_zbnr . '">
      <label for="d1_chip" id="d1_chiplabel">Chipnummer:</label>
      <input type="text" name="d1_chip" id="d1_chip" class="textinput" value="' . $d1_chip . '">
      <label for="d1_mutter" id="d1_mutterlabel">Mutter:</label>
      <input type="text" name="d1_mutter" id="d1_mutter" class="textinput" value="' . $d1_mutter . '">
      <label for="d1_vater" id="d1_vaterlabel">Vater:</label>
      <input type="text" name="d1_vater" id="d1_vater" class="textinput" value="' . $d1_vater . '">
      <label for="daktiv1" id="daktiv_sexlabel">aktiv:</label>
      <input type="radio" name="daktiv" id="daktiv1" value="dog1" ';
    if ($daktiv == "dog1")
      $lg_content .= 'checked';
    $lg_content .= '><br><label for="daktivno" id="daktiv_sexlabel">kein Hund aktivieren:</label>
      <input type="radio" name="daktiv" id="daktivno" value="nodog" ';
    if ($daktiv == "nodog")
      $lg_content .= 'checked';
    $lg_content .= '><br>
    </div>

    <div id="H2" class="tabcontent">
      <label for="d2_name" id="d2_namelabel">Name:</label>
      <input type="text" name="d2_name" id="d2_name" class="textinput" value="' . $d2_name . '">
      <label for="d2_wtag" id="d2_wtaglabel">Wurftag:</label>
      <input type="text" name="d2_wtag" id="d2_wtag" class="textinput" value="' . $d2_wtag . '">
      <label for="d2_sex" id="d2_sexlabel">Rüde:</label>
      <input type="checkbox" name="d2_sex" id="d2_sex" ';
    if ($d2_sex == "on")
      $lg_content .= 'checked';
    $lg_content .= '><br>
      <label for="d2_zbnr" id="d2_zbnrlabel">Zuchtbuch-Nummer:</label>
      <input type="text" name="d2_zbnr" id="d2_zbnr" class="textinput" value="' . $d2_zbnr . '">
      <label for="d2_chip" id="d2_chiplabel">Chipnummer:</label>
      <input type="text" name="d2_chip" id="d2_chip" class="textinput" value="' . $d2_chip . '">
      <label for="d2_mutter" id="d2_mutterlabel">Mutter:</label>
      <input type="text" name="d2_mutter" id="d2_mutter" class="textinput" value="' . $d2_mutter . '">
      <label for="d2_vater" id="d2_vaterlabel">Vater:</label>
      <input type="text" name="d2_vater" id="d2_vater" class="textinput" value="' . $d2_vater . '">
      <label for="daktiv2" id="daktiv_sexlabel">aktiv:</label>
      <input type="radio" name="daktiv" id="daktiv2" value="dog2" ';
    if ($daktiv == "dog2")
      $lg_content .= 'checked';
    $lg_content .= '><br>
    </div>

    <div id="H3" class="tabcontent">
      <label for="d3_name" id="d3_namelabel">Name:</label>
      <input type="text" name="d3_name" id="d3_name" class="textinput" value="' . $d3_name . '">
      <label for="d3_wtag" id="d3_wtaglabel">Wurftag:</label>
      <input type="text" name="d3_wtag" id="d3_wtag" class="textinput" value="' . $d3_wtag . '">
      <label for="d3_sex" id="d3_sexlabel">Rüde:</label>
      <input type="checkbox" name="d3_sex" id="d3_sex" ';
    if ($d3_sex == "on")
      $lg_content .= 'checked';
    $lg_content .= '><br>
      <label for="d3_zbnr" id="d3_zbnrlabel">Zuchtbuch-Nummer:</label>
      <input type="text" name="d3_zbnr" id="d3_zbnr" class="textinput" value="' . $d3_zbnr . '">
      <label for="d3_chip" id="d3_chiplabel">Chipnummer:</label>
      <input type="text" name="d3_chip" id="d3_chip" class="textinput" value="' . $d3_chip . '">
      <label for="d3_mutter" id="d3_mutterlabel">Mutter:</label>
      <input type="text" name="d3_mutter" id="d3_mutter" class="textinput" value="' . $d3_mutter . '">
      <label for="d3_vater" id="d3_vaterlabel">Vater:</label>
      <input type="text" name="d3_vater" id="d3_vater" class="textinput" value="' . $d3_vater . '">
      <label for="daktiv3" id="daktiv_sexlabel">aktiv:</label>
      <input type="radio" name="daktiv" id="daktiv3" value="dog3"';
    if ($daktiv == "dog3")
      $lg_content .= 'checked';
    $lg_content .= '><br>
    </div>
    <input type="submit" name="submitbutton" id="submitbutton" value="Speichern">
  </form>
</div>
<script>
document.getElementById("defaultOpen").click();
</script>
';
  }
  return $lg_content;
}
