<?php

class PopController extends mfBaseController {
  
  protected function init() {
    $this->needlogin=true;
    $me = new User();
    $me->loadMe();
    $this->me = $me;
    $this->layout()->set("me",$me);
    
    if(!$me->is(["Admin", "netowner", "pipeplanner"])) {
      $this->redirect("Dashboard");
    }
  }

  protected function indexAction() {
    
  }
  
  protected function addAction() {
    $this->layout()->setTemplate("Pop/Form");
    $this->layout()->set("networks", NetworkModel::getAll());
    if($this->request->network_id) {
      $pop = new Pop();
      $pop->network_id = $this->request->network_id;
      $this->layout()->set("pop", $pop);
    }
  }
  
  protected function editAction() {
    $id = $this->request->id;
    if(!is_numeric($id) || !$id) {
      $this->layout()->setFlash("POP nicht gefunden", "error");
      $this->redirect("Network");
    }
    
    $pop = new Pop($id);
    if($pop->id != $id) {
      $this->layout()->setFlash("POP nicht gefunden", "error");
      $this->redirect("Network");
    }
    
    $this->layout()->set("pop", $pop);
    return $this->addAction();
  }
  
  protected function saveAction() {
    $r = $this->request;
    $id = $r->id;
    //var_dump($r);exit;
    if(is_numeric($id) && $id > 0) {
      $mode = "edit";
      $pop = new Pop($id);
      if(!$pop->id) {
        $this->layout()->setFlash("POP nicht gefunden", "error");
        $this->redirect("Network");
      }
    } else {
      $mode = "add";
    }
    
    //var_dump($r->addresstypes);exit;
    
    if(!$r->network_id || !$r->name) {
      $this->layout()->setFlash("Bitte Name und Netzgebiet eintragen", "error");
      $this->layout()->set("pop", $pop);
      unset($r->network_id);
      return $this->add();
    }
    
    $data = [];
    $data['network_id'] = $r->network_id;
    $data['name'] = $r->name;
    $data['gps_lat'] = ($r->gps_lat) ? $r->gps_lat : null;
    $data['gps_long'] = ($r->gps_long) ? $r->gps_long : null;
    $data['location'] = $r->location;
    $data['vlan_public'] = ($r->vlan_public) ? $r->vlan_public : null;
    $data['vlan_nat'] = ($r->vlan_nat) ? $r->vlan_nat: null;
    $data['vlan_ipv6'] = ($r->vlan_ipv6) ? $r->vlan_ipv6: null;
    $data['note'] = $r->note;
    
    $data['edit_by'] = 1;
    
    if($mode == "add") {
      $data['create_by'] = 1;
      $pop = PopModel::create($data);
    } else {
      $pop->update($data);
    }
    
    //var_dump($address);exit;
    
    $new_id = $pop->save();
    if(!$new_id) {
      $this->layout()->setFlash("Fehler beim Speichern", "error");
      $this->layout()->set("network", $network);
      return $this->addAction();
    }
    
    $this->layout()->setFlash("Netzgebiet erfolgreich gespeichert.", "success");
    $this->redirect("Network", "Index", [], "view=pops&net=".$pop->network_id);
  }
  
  protected function apiAction() {
    $do = $this->request->do;
    $data = [];
    
    switch($do) {
      case "getPops":
        $return = $this->getPopsApi();
        break;
      default:
        $return = false;
    }
    
    if(!is_array($return) || !count($return)) {
      $data = ["status" => "error"];
      $this->returnJson($data);
    }
    $data['status'] = "OK";
    $data['result'] = $return;
    $this->returnJson($data);
  }
  
  private function getPopsApi() {
    $network_id = $this->request->network_id;
    if(!is_numeric($network_id) || $network_id < 1) {
      return false;
    }
    
    $network = new Network($network_id);
    if(!$network->id) {
      return false;
    }
    
    $pops = [];
    foreach($network->pops as $pop) {
      $pops[$pop->id] = $pop->name;
    }
    
    return ["pops" => $pops];
  }
}