<?php
namespace App\Person;
use PDO;
use App\Core\AbstractRepository;

class PartnerRepository extends AbstractRepository
{
  public function getTableName() {
    return "gen_partner";
  }

  public function getModelName() {
    return "App\\Person\\PartnerModel";
  }
// *************************************************************************
/*
  public function allPartners() {
    $table = $this->getTableName();
    $model = $this->getModelName();

    $strSQL = "SELECT * FROM `$table` ORDER BY par_von DESC";

    $stmt = $this->pdo->query($strSQL);
    $partners_id = $stmt->fetchALL(PDO::FETCH_CLASS, $model);
    return $partners_id;
  }
*/
// **************************************************************************
  public function allByPartner($id,$m_gender) {
    $table = $this->getTableName();
    $model = $this->getModelName();

    if ($m_gender == 01) {
      $str_id = "gen_partner.woman_id";
    } else {
      $str_id = "gen_partner.man_id";}

    $strSQL = "SELECT * FROM gen_member
               LEFT JOIN gen_partner ON gen_member.member_id = $str_id
               WHERE ((man_id = $id) OR (woman_id = $id))
               ORDER BY par_von DESC";

    $stmt = $this->pdo->query($strSQL);
    $partner = $stmt->fetchALL(PDO::FETCH_CLASS, $model);
    return $partner;
  }
// ***********************************************************************
/*
  public function allPartnerBySex($sex) {
    $table = $this->getTableName();
    $model = $this->getModelName();

    if ($sex == 01) {
      $str_id = "gen_partner.woman_id";
    } else { $str_id = "gen_partner.man_id";}

    $strSQL = "SELECT * FROM gen_member
               LEFT JOIN gen_partner ON gen_member.member_id = $str_id
               WHERE ((gen_partner.man_id = gen_member.member_id) OR (gen_partner.woman_id = gen_member.member_id))
               ORDER BY par_id ASC";

    $stmt = $this->pdo->query($strSQL);
    $partner = $stmt->fetchALL(PDO::FETCH_CLASS, $model);
    return $partner;
  }
*/
// ********************************************************************
    public function insertForPartner() {
      $table = $this->getTableName();

      $par_id = test_input($_POST['par_id']);
      $man_id = test_input($_POST['man_id']);
      $woman_id = test_input($_POST['woman_id']);
      $par_von = test_input($_POST['par_von']);
      $par_ort = test_input($_POST['par_ort']);
      $par_devorce = test_input($_POST['par_devorce']);

      $data = [
              'man_id' => $man_id,
              'woman_id' => $woman_id,
              'par_von' => $par_von,
              'par_ort' => $par_ort,
              'par_devorce' => $par_devorce
              ];

      //var_dump($_POST); die();
      if ($_POST['submit'] == "submit") {
        $sql = "INSERT INTO `$table` (`man_id`, `woman_id`, `par_von`, `par_ort`, `par_devorce`)
                VALUES (:man_id, :woman_id, :par_von, :par_ort, :par_devorce)";

        $stmt= $this->pdo->prepare($sql);
        $stmt->execute($data);
      }

      if ($_POST['submit'] == "update") {

        $data['par_id'] =  $par_id;
        $sql  = "UPDATE gen_partner
                 SET man_id = :man_id, woman_id = :woman_id, par_von = :par_von, par_ort = :par_ort, par_devorce = :par_devorce
                 WHERE par_id = :par_id";

        $stmt= $this->pdo->prepare($sql);
        $stmt->execute($data);
      }
  }

}

 ?>
