<?php #pgs/xx_spinde/class.php

class SpindePage extends BasePage
{
	protected function remove($id) {
		// Removal not allowed for now
		return 501;
	}

  protected function update($id) {
    $this->get($id);
    
    $qry = "UPDATE Spinde SET Besitzer = ? WHERE ID = ?";
    if ($stmt = $this->db->prepare($qry)) {
      $Besitzer = array_key_exists("BESITZER", $this->man->input)
                ? $this->man->input["BESITZER"]
                : (is_null($this->output["MAIN"]["BESITZER"]) ? null : $this->output["MAIN"]["BESITZER"]["ID"]);
      $stmt->bind_param(
        "ii", 
        $Besitzer,
        $id
      );
      if ($stmt->execute()) {
        $this->man->AddMessage("Spindbesitzer wurde aktualisiert!");
        $this->get($id);
        return 200;
      } else {
        $this->man->AddMessage("Mysql error: ".$this->db->error);
      }
    } else {
      $this->man->AddMessage("Mysql error: ".$this->db->error);
    }
    return 500;
  }
  
  protected function get($id = null) {
  	$this->output = [];
  	if ($id===null && $this->man->Main()!=null) {
      $id = $this->man->Main();
    }
    
    $where = "";
    $having = "";
    if ($id) {
      $where = "AND s.ID = ? ";
    } else if (sizeof($this->man->Filter())>0) {
      /*$where = "WHERE 1 ";*/
      foreach ($this->man->Filter() as $filter) {
        $and = "or"==$filter["and"] ? "OR " : "AND ";
        if ("none"==$filter["field"]) {
          $where.= $and."1 ";
        } else {
          switch ($filter["field"]) {
            case "RAUM":
              $where.= $and.$this->getFilterString($filter["op"], ["=", "<>"], "s", "Raum", $filter["value"]);
              break;
            case "BESITZER":
              $where.= $and.$this->getFilterString($filter["op"], ["=", "<>"], "s", "Besitzer", $filter["value"]);
              break;
            default:
              $where.= $and."1 ";
    } } } }
    
    $qry = "SELECT s.ID, s.Raum, s.Besitzer, s.Bezeichnung, s.Links, s.Oben, s.Höhe, s.Breite, s.Orientation, p.Pnr, p.Nachnamen, p.Vornamen, sr.Name srName "
          ."FROM Spinde s "
          ."LEFT JOIN Spinde_Räume sr ON sr.ID=s.Raum "
          ."LEFT JOIN Personal p ON p.ID=s.Besitzer "
          ."WHERE 1 ".$where
          ."ORDER BY s.Raum ASC ";
    if ($stmt = $this->db->prepare($qry)) {
      if ($id) {
        $stmt->bind_param("i", $id);
      }
      $stmt->execute();
      $res = $stmt->get_result();
      while ($row = $res->fetch_assoc()) {
        $entry = [
          "ID" => strval($row["ID"]),
          "GROUP" => $row["srName"],
          /*"POS" => [
            "LEFT" => $row["Links"],
            "TOP"=> $row["Oben"],
            "WIDTH" => $row["Breite"],
            "HEIGHT" => $row["Höhe"],
            "ROT" => $row["Orientation"],
          ],*/
          "MAIN" => [
            "FREI" => is_null($row["Besitzer"]),
            "RAUM" => $row["srName"],
            "BEZEICHNUNG" => $row["Bezeichnung"],
            "BESITZER" => [
              "ID" => $row["Besitzer"],
              "PNR" => $row["Pnr"],
              "VORNAMEN" => $row["Vornamen"],
              "NACHNAMEN" => $row["Nachnamen"],
              "NAME" => (is_null($row["Vornamen"]) ? "" : preg_replace("/(?<![ -])\p{Ll}+/u", ".", $row["Vornamen"]))." ".$row["Nachnamen"]." (".$row["Pnr"].")",
            ],
          ],
          "SUB" => [],
        ];
        $this->addEntryToOutput($row["srName"], $entry, $id);
      }
      if ($id && $res->num_rows==0) {
        $this->man->AddMessage("Couldn't find requested resource!");
        return 404;
      }
    } else {
      $this->man->AddMessage("Mysql error: ".$this->db->error." / Qry: ".$qry);
      return 500;
    }
    return 200;
  }
  
  protected function fillOptions($admin = false) {
    $ret = array();
    $ret["BESITZER"][] = ["ID" => "__NULL__", "NAME" => "Frei"];
    $res = $this->db->query("SELECT ID, Pnr, Vornamen, Nachnamen FROM Personal ORDER BY Nachnamen ASC, Vornamen ASC");
    while ($row = $res->fetch_assoc()) {
      $ret["BESITZER"][] = ["ID" => $row["ID"], "NAME" => preg_replace("/(?<![ -])\p{Ll}+/u", ".", $row["Vornamen"])." ".$row["Nachnamen"]." (".$row["Pnr"].")"];
    }
    return $ret;
  }
}
