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

class RemarkRepository extends AbstractRepository
{

  public function getTableName() {
    return "gen_remark";
  }

  public function getModelName() {
    return "App\\Person\\RemarkModel";
  }

  public function allByPerson($id) {
    $table = $this->getTableName();
    $model = $this->getModelName();

    $stmt = $this->pdo->prepare("SELECT * FROM `$table` WHERE member_id_f = :id");  // :id ist Platzhalter
    $stmt->execute(['id' =>$id]);
    $remarks = $stmt->fetchALL(PDO::FETCH_CLASS, $model);
    return $remarks;
  }

  public function every_remark(){
    $table = $this->getTableName();
    $model = $this->getModelName();

    $WHERE = " gen_member.member_id = gen_remark.member_id_f ";
    $strSQL = "SELECT
          gen_remark.rem_id,
          gen_remark.rem_date,
          gen_remark.rem_remark,
          gen_remark.rem_erl,
          gen_remark.member_id_f,
          gen_member.member_id,
          gen_member.first_name,
          gen_member.last_name,
          gen_member.gender
      FROM gen_remark, gen_member
      WHERE ".$WHERE." ORDER BY gen_member.last_name, gen_member.first_name";

    //  echo $strSQL; die;
    $statement = $this->pdo->query($strSQL);
    $remarks = $statement->fetchALL(PDO::FETCH_CLASS, $model);
    return $remarks;
  }

  public function insertForRemark() {
    $table = $this->getTableName();
    $model = $this->getModelName();

    $member_id_f = test_input($_POST['member_id_f']);
    $rem_date = test_input($_POST['rem_date']);
    $rem_remark = test_input($_POST['content']);

    if ($_POST['submit'] == "submit") {
      $rem_erl = 0;
      //var_dump($_POST); die();
      $strSQL = "INSERT INTO gen_remark (member_id_f, rem_date, rem_remark, rem_erl)
                  VALUES ('".$member_id_f."', '".$rem_date."', '".$rem_remark."', '".$rem_erl."')";
    }

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

  public function insertUpdateForRemark() {
    $table = $this->getTableName();
    $model = $this->getModelName();

    $member_id_f = test_input($_POST['member_id_f']);
    $rem_date = test_input($_POST['rem_date']);
    $rem_remark = test_input($_POST['rem_remark']);

    if ($_POST['submit'] == "update") {
      $rem_id = test_input($_POST['rem_id']);
      $rem_erl = test_input($_POST['rem_erl']);
      $strSQL = "UPDATE gen_remark
                  SET member_id_f='".$member_id_f."', rem_date='".$rem_date."', rem_remark='".$rem_remark."', rem_erl='".$rem_erl."'
                  WHERE rem_id ='".$rem_id."'";
    }

    if ($_POST['submit'] == "submit") {
      $rem_erl = 0;
      $strSQL = "INSERT INTO gen_remark (member_id_f, rem_date, rem_remark, rem_erl)
                  VALUES ('".$member_id_f."', '".$rem_date."', '".$rem_remark."', '".$rem_erl."')";
    }

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

}

 ?>
