*
<?php
  $thisPage="showlist";
  include __DIR__ . "/../layout/header.php";
  include __DIR__ . "/../layout/navigation.php";
  include __DIR__ . "/../inc/const.php"; // Konstanten

  include __DIR__ . "/../layout/css/navi.css";
  include __DIR__ ."../../layout/css/css_screen.css"; ?>
</style>

</head>

<body><?php
global $ladezeit;

 $name = "";  // Current name (for groups of last name)
 $nbp = 0;    // Number of people
 $nbf = 0;    // Number of females
 $nbm = 0;    // Number of males
 $nbu = 0;    // Number of unknown
?>

<?php

function remarks($remarks) {
  $anz = count($remarks);
  $bol_first = false;
  $m_id = "";
  foreach($remarks AS $remark):
    if ($remark->member_id_f <> $m_id){$bol_first = false;}
    if ($bol_first == false){
      $m_id = $remark->member_id_f;
        if ($remark->member_id == $m_id) {
          $link = "<a href=\"remarks?id="
            .$remark->member_id ."\">"
            .$remark->last_name.  ",  "
            .$remark->first_name ."</a>";
          if ($remark->gender == 1) {
            $icon = "<img src='../../img/m.jpg'>";
          } else {
            $icon = "<img src='../../img/f.jpg'>";
          }
        }
    } ?>

    <div class="col-md-12"><?php
      if ($bol_first == false) {
        echo "<hr>";
        echo $icon." ";
        echo $link;
        $bol_first = true;
      } ?>
      <li> <?php
        echo "<b>".date_german($remark->rem_date).": </b>".$remark->rem_remark;?>
      </li>
    </div><?php
  endforeach;
} ?>


<?php
function familie($parents,$partners,$children_all) {
  $anz = count($parents);
  $i = 0;
  $nbm = $nbf = $nbu = 0;
  $ehen = array();
  for ($i = 0; $i <= $anz-1; $i++) {        // alle Eltern
    $link = "<a href=\"baum?anz=4&id=";
    switch($parents[$i]->gender) {
      case 1: $icon = "<img src='../../img/m.jpg'>"; $gen = 1; $nbm++; break;
      case 2: $icon = "<img src='../../img/f.jpg'>"; $gen = 2; $nbf++; break;
      case 0: $icon = "<img src='../../img/u.jpg'>"; $gen = 0; $nbu++; break;
    }
    echo $link.$parents[$i]->member_id;?>"><?php
    echo "<h1>".$icon." ".$parents[$i]->first_name." "
                         .$parents[$i]->last_name;
    if ($gen == 2) {
      echo ", geb. ".$parents[$i]->birth_name."</h1></a>";
    } else { echo "</h1></a>"; } ?>
    <div class='bd'><b>   *</b><?php
       echo $parents[$i]["day_of_birth"],"&nbsp;&nbsp;"; //." in "
       //.$parents[$i]["place_of_birth"]."<br/>";
       $day = $parents[$i]["day_of_death"];
       //$place = $parents[$i]["place_of_death"];
       if (($day >= 1)) {echo "<b> †</b>".$parents[$i]["day_of_death"];} ?>
    </div>

    <div style="margin-top:8px; margin-bottom:8px">
      Vater: <img src='../../img/m.jpg'><?php
      if ($parents[$i]->m_member_id!==NULL) {
        echo $link.$parents[$i]->m_member_id;?>"><?php
        echo " ".$parents[$i]->m_first_name.
             " ".$parents[$i]->m_last_name."</a> <b> &nbsp;&nbsp; *</b>"
                .$parents[$i]->m_day_of_birth;
             if ($parents[$i]->m_day_of_death >= 1 ) {
              echo "<b> &nbsp&nbsp †</b>"
                .$parents[$i]->m_day_of_death;}
      }?></br>
      Mutter: <img src='../../img/f.jpg'><?php
      if ($parents[$i]->f_member_id!==NULL) {
        echo $link.$parents[$i]->f_member_id;?>"><?php
        echo " ".$parents[$i]->f_first_name.
             " "//.$parents[$i]->f_last_name."</a>, geb. "
                .$parents[$i]->f_birth_name." "."</a><b> &nbsp;&nbsp; *</b>"
                .$parents[$i]->f_day_of_birth;
             if ($parents[$i]->f_day_of_death >= 1) {
                echo "<b> &nbsp&nbsp †</b>"
                .$parents[$i]->f_day_of_death;}
      }?></br>
    </div>

    <!-- ********************** Partner anzeigen *****************************-->
    <div style="margin-top:8px; margin-bottom:8px"><?php
      $mo = $von = $ort = "";
      $fa = $parents[$i]->first_name;     // Person
      foreach ($partners[$i] as $part) {
        if ($part->gender == 1) {$icon = "<img src='../../img/m.jpg'>";} else {$icon = "<img src='../../img/f.jpg'>";}?>
        Partner: <?php echo $icon.$link.$part->member_id;?>"><?php            // Partnername
          echo " ".$part->first_name." ".$part->last_name."</a>";
          if ($part->birth_name != "") { echo ", geb. ".$part->birth_name;} // Geburtsname
          echo "&nbsp;&nbsp; * ".$part->day_of_birth;
          if ($part->day_of_death >= 1) {echo "<b> &nbsp&nbsp † </b>".$part->day_of_death;}
          echo "</br>";

          if ($part->par_devorce != "0000-00-00") {
            $bis = " - <b>geschieden</b> am ".$part->par_devorce;}
          else {$bis = "";}
          $str_ehe = "<b>⚮</b> am ".$part->par_von." in ".$part->par_ort.$bis."<br>";?>
          <div class="enfant"><?php
            echo $str_ehe;              // Ausgabe Partner
            $n = 0;       // Anzahl Kinder zählen
            foreach ($children_all[$i] as $child) {
              if (($child->mother_id == $part->member_id) OR ($child->father_id == $part->member_id)){
                $n++;
              }
            }
            $mo = $part->first_name;    // Mother
            echo "<b>".$fa."</b> und <b>".$mo. "</b> haben <b>".$n." Kind(er)</b><br/>";
            $k = 1;     // 1. Kind
            foreach ($children_all[$i] as $child) {
              if (($child->mother_id == $part->member_id) OR ($child->father_id == $part->member_id)){
                if ($child->gender == 1) {$icon = "<img src='../../img/m.jpg'>";}
                else {$icon = "<img src='../../img/f.jpg'>";}
                echo "<tr><td>".$k."&nbsp;&nbsp;".$icon;
                echo $link.$child->member_id;?>"><?php
                echo " ".$child->first_name."</td><td>".
                     " ".$child->last_name."</a></td><td><b>&nbsp;&nbsp; * </b>"
                        .$child->day_of_birth."</td>";
                     if ($child->day_of_death >= 1) {
                      echo "<td><b> &nbsp&nbsp † </b>"
                        .$child->day_of_death."</td>";}
                      echo "</br></tr>";
                $k++;
              }
            }?>
          </div><?php
      }
      //echo "</table>";?>
    </div>
  <?php
  }
  echo $anz." Personen  (";?>
  <img src='../../img/m.jpg'>: <?php echo " ".$nbm;?>
  <img src='../../img/f.jpg'>: <?php echo " ".$nbf;?>
  <img src='../../img/u.jpg'>: <?php echo " ".$nbu. ")";
}?>

<?php
function personen($persons) {
    $i = 0;
    foreach($persons AS $rs): ?>
      <li>
        <div class="col-md-6"> <?php
        if ($rs->gender == 1) {$icon = "<img src='../../img/m.jpg'>";}
        else {$icon = "<img src='../../img/f.jpg'>";}
        echo $icon." ";
          $link = "<a href=\"familienblatt?id="
            . $rs->member_id ."\">"
            . $rs->last_name.  ",  "
            . $rs->first_name ."</a>";
          echo $link; ?>
        </div>
        <?php if (isset($rs["day_of_birth"])) { ?>
          <div class="col-md-6">
            <?php
            if ($rs->day_of_birth != 0) {
              echo $rs->day_of_birth;  } else {
                echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;?
                      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";}
            if (isset($rs->day_of_death)) {
              if ($rs->day_of_death >= 1) {
                echo "&nbsp; - &nbsp;".$rs->day_of_death;  }
              }?>
          </div>
          <?php
        } ?>
      </li>	            	            <?php
      $i++;
    endforeach;
    echo "<tr><td>Es wurden ".$i." Personen gezählt.</td></tr>";?>
  <?php
}?>

<?php
function personen_baum($persons){ //Namenliste als Link zur Baumdarstellung
    $name = "";
      $nbp = $nbm = $nbf = $nbu = 0;  // Anzahl = 0
      foreach ($persons as $person) {
       	if($person->last_name != $name) {
       		$name = $person->last_name;?>
       		<div class="nom" ><?php echo $name ;?> </div><?php
       	}
       	switch($person->gender) {
       		case 1: $icon = "<img src='../../img/m.jpg'>"; $nbm++; break;
       		case 2: $icon = "<img src='../../img/f.jpg'>"; $nbf++; break;
       		case 0: $icon = "<img src='../../img/u.jpg'>"; $nbu++; break;
       	}
        //$doc = "<img src='../../img/nopic.jpg'>";
        $photo = "<img src='../../img/nopic.jpg'>";
        $photo = "<img src='../../photos/".$person->member_img."' width='12' height='16'>";
        //$bdate = $person->day_of_birth; ?>
        <div class="col-md-6"><?php
          //echo "$doc.$photo&nbsp";
          echo "$photo&nbsp";
          echo $icon ?> <a href="baum?anz=4&id=<?php echo $person->member_id; ?>">
            <?php echo $person->first_name." ".$person->last_name; ?></a>
          </div>
          <div class="col-md-6">
            <?php //echo $person->day_of_birth;
            if ($person->day_of_birth != 0) {
              echo $person->day_of_birth;  } else {
                echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                ? &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";}

            if (isset($person->day_of_death)) {
              if ($person->day_of_death != 0) {
                echo "&nbsp; - &nbsp;".$person->day_of_death;  }
              }?>
          </div> <?php
          $nbp++;
          $doc='';
    } ?>
    <span><?php  echo count ($persons)." Personen - davon  "
      .$nbm." "."<img src='../../img/m.jpg'>  - "
      .$nbf." "."<img src='../../img/f.jpg'>  - "
      .$nbu." "."<img src='../../img/u.jpg'> " ;?>
    </span><?php
}?>

<?php
function paare($couples) { //Liste der Verheirateten
  global $td0, $td1, $td2;?>
  <table style="margin-left:50px">
    <tr><td colspan="3"><strong><h4><strong>Liste der Verheirateten / Partnerschaften</strong></h4></strong></td></tr>
    <tr><td>
      <?php echo $td1 ?> <center><img src='../../img/m.jpg'></center></td><td>
      <?php echo $td2 ?> <center><img src='../../img/f.jpg'></center></td><td>
      <?php echo $td0 ?> <strong>Mädchenname</strong></td>
    </tr><?php
        $i = 0;
        foreach ($couples as $par) {?>
          <tr>
            <td style="padding:2px; padding-right:10px";><?php //echo $par['m_member_id']." ";?>
              <a href="baum?anz=4&id=<?php echo $par['m_member_id']; ?>"><?php
              echo $par['m_last_name'].", ".$par['m_first_name'] ?>
            </td></a>
            <td  style="padding:2px; padding-right:10px";>
              <a href="baum?anz=4&id=<?php echo $par['f_member_id']; ?>"> <?php
              echo $par['f_first_name']." "//.$par['f_last_name']?></a>
            </td>
            <td>geb. <?php
              echo $par['f_birth_name'];  ?>
            </td></tr>
            <?php
            $i++;
          }
          echo "<tr><td>Es wurden ".$i." Paare gezählt.</td></tr>";?>
  </table>
<?php } ?>

<!-- ***************************************************-->

<div class="container-fluid" id="myTable">
  <div class="col-sm-12">

    <div class="well well-sm"><?php
    //echo "</br></br>";
      if (!isset($zeichen)) {$zeichen = "alle";}
        //if (!isset($liste)) {$liste = 1;}?>
        <form name="eingabe" action="showlist"  method="get">
          &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
          <input style="background-color:lightgrey;" type="submit"
              value="Wähle Liste: "/>

          <strong> &nbspledige<img src='../../img/m.jpg'><input type="radio" name="liste" value="1" onchange="submit()"
            <?php if ($liste == 1) { echo "checked=checked"; } ?>/>
           &nbsp&nbspledige <img src='../../img/f.jpg'><input type="radio" name="liste" value="2" onchange="submit()"
            <?php if ($liste == 2) { echo "checked=checked"; } ?>/>
           &nbsp&nbspverh/gesch <input type="radio" name="liste" value="3" onchange="submit()"
            <?php if ($liste == 3) { echo "checked=checked"; } ?> />
           &nbsp&nbspEltern<input type="radio" name="liste" value="4" onchange="submit()"
            <?php if ($liste == 4) { echo "checked=checked"; } ?> />
           &nbsp&nbspPersonen (gruppiert)<input type="radio" name="liste" value="5" onchange="submit()"
            <?php if ($liste == 5) { echo "checked=checked"; } ?> />
           &nbsp&nbspFamilen<input type="radio" name="liste" value="6" onchange="submit()"
            <?php if ($liste == 6) { echo "checked=checked"; } ?> />
           &nbsp&nbspAnmerkungen<input type="radio" name="liste" value="7" onchange="submit()"
            <?php if ($liste == 7) { echo "checked=checked"; } ?> />
          </td></strong>
        </form>
      </tr>
<?php
      $alphabet = array();  // leeres array
      $alphabet[] = 'alle';   // erster Eintrag
      $buchstabe_e = "";
      foreach ($persons as $pers) {
        $name = $pers->last_name;
        $buchstabe = ucfirst(substr($name,0,1));   // 1. Buchstabe des Namens
        if ($buchstabe != $buchstabe_e) {          // schon im array ?
          $alphabet[] = $buchstabe;
          $buchstabe_e = $buchstabe;
        }
      } ?>

      <table>
        <tr>
          <form name="zeichen" action="showlist"  method="get">
            <input style="background-color:lightgrey;" type="submit"
                value="Anfangsbuchstabe: "/><strong><?php
              foreach ($alphabet as $buchstaben){
                echo "&nbsp&nbsp&nbsp".$buchstaben;?>
                <input type="radio" name="zeichen" value="<?php echo $buchstaben;?>"<?php
                if ($zeichen == $buchstaben) { echo "checked=checked"; } ?> onchange="submit();"/><?php
              } ?>
          </form></strong>
          <?php //echo "&nbsp;&nbsp;&nbsp;&nbsp; Ladezeit: ".$ladezeit." ";?>
        </tr>
      </table>
    </div>
  </div>

<!--***************************** Ende Kopfbereich *****************************-->
  <div class="row">
    <div class="col-sm-6 ">
      <div class="well well-sm">
        <tr><td colspan="3"><?php
          switch($liste) {
          case 1:
               echo "<h4><strong>Liste aller unverheirateten Männer!</strong></h4>";?>
               <table>
                 <ul > <?php
                   personen($ledige_men);?>
                 </ul>
               </table><?php
               break;

          case 2:
               echo "<h4><strong>Liste aller unverheirateten Frauen!</strong></h4>";?>
               <table>
                 <ul > <?php
                   personen($ledige_women);?>
                 </ul>
               </table><?php
               break;

          case 3:
               //echo "<strong>Liste aller verheirateten oder geschiedenen Paare!</strong>";
               paare($couples);
               break;

          case 4:
               //eltern(); ?>
               <table>
                 <tr>
                   <td colspan="3"><h4><strong>Liste aller Personen und Eltern (Link zum Stammbaum)</strong></h4></td>
                 </tr>
                  <tr>
                    <?php echo $td0 ?> <strong>Person</strong></td>
                    <?php echo $td1 ?> <center><img src='../../img/m.jpg'></center></td>
                    <?php echo $td2 ?> <center><img src='../../img/f.jpg'></center></td>
                  </tr> <?php
                  $i = 1;
                  foreach ($parents as $parent => $pers) { ?>
                    <tr><td style="padding-right: 10px">
                      <a href="baum?anz=4&id=<?php echo $pers->member_id; ?>"><?php
                      echo $i." ".
                           $pers->first_name. " ".
                           $pers->last_name. "</td><td style=\"padding-right: 10px\">".
                           $pers->m_first_name. " ".
                           $pers->m_last_name. " </td><td>".
                           $pers->f_first_name. " ".
                           $pers->f_last_name. "</br>";?>
                    </td></tr><?php
                    $i++;
                  } ?>
               </table><?php
               break;

          case 5:
            echo "<h4><strong>Personen mit Link zum Stammbaum - Anfangbuchstabe (".$zeichen.")</strong></h4>";
            personen_baum($persons_char);
            break;

          case 6:
            echo "<strong>Personen (nach Alter) mit Link zum Familienblatt - Anfangbuchstabe (".$zeichen.")</strong>";
            familie($parents,$partners,$children_all);
            break;

          case 7:
            echo "<h4><strong>Anmerkungen zu Personen</strong></h4>";
            //familie($parents,$partners,$children_all);
            //remarks($remarks, $persons);
            remarks($remarks);
            break;

          default:
            echo '<strong>Du hast vergessen eine Liste auszuwählen.';
          }   ?>
        </div>
      </div>

<!-- ***************************** rechte Seite ********************************-->
      <div class="col-sm-6 " >
        <div class="well well-sm">
          <strong><h4><strong>Personen (nach Alter) mit Link zum Familienblatt - Buchstabe <?php echo "(".$zeichen.")"?></strong></h4></strong>
          <table>
            <ul > <?php
              personen($persons_char);?>
            </ul>
          </table>
          <hr>
        </div>
      </div>
  </div>
  <div class="container">
    <div class="starter-template"></div>
  </div>
</div>
    <!-- Abschluss <div> aus Navigation-->

<?php

  include __DIR__ . "/../js/hochschieben.js";   // Navigation nach oben schieben
  //$t_footer = 1000;
  include __DIR__ . "/../layout/footer_einfach.php";
?>