<?php

class NetworkController 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() {
    $this->layout()->set("owners", AddressModel::search(['addresstype' => ["netowner"]]));
    $this->layout()->set("networks", NetworkModel::getAll());
    
    
    
    /*$net = new Network(1);
    $at = $net->addresstypes; //[1]->addresstype->address;
    var_dump($at);exit;*/
  }
  
  protected function addAction() {
    $this->layout()->setTemplate("Network/Form");
    $this->layout()->set("owners", AddressModel::search(['addresstype' => ["netowner"]]));
    
  }
  
  protected function editAction() {
    $id = $this->request->id;
    if(!is_numeric($id) || !$id) {
      $this->layout()->setFlash("Netzgebiet nicht gefunden", "error");
      $this->redirect("Network");
    }
    
    $network = new Network($id);
    if($network->id != $id) {
      $this->layout()->setFlash("Netzgebiet nicht gefunden", "error");
      $this->redirect("Network");
    }
    
    $this->layout()->set("network", $network);
    return $this->addAction();
  }
  
  protected function saveAction() {
    $r = $this->request;
    $id = $r->id;
    //var_dump($r);
    if(is_numeric($id) && $id > 0) {
      $mode = "edit";
      $network = new Network($id);
      if(!$network->id) {
        $this->layout()->setFlash("Netzgebiet nicht gefunden", "error");
        $this->redirect("Network");
      }
    } else {
      $mode = "add";
    }
    
    //var_dump($r->addresstypes);exit;
    
    if(!$r->owner_id || !$r->name) {
      $this->layout()->setFlash("Bitte Name und Besitzer eintragen", "error");
      $this->layout()->set("network", $network);
      return $this->add();
    }
    
    $data = [];
    $data['owner_id'] = $r->owner_id;
    $data['name'] = $r->name;
    $data['note'] = $r->note;
    
    if($r->sytemowner_action_status) {
      $data['sytemowner_action_status'] = $r->sytemowner_action_status;
    } else {
      $data['sytemowner_action_status'] = null;
    }
    
    $data['edit_by'] = 1;
    
    if($mode == "add") {
      $data['create_by'] = 1;
      $network = NetworkModel::create($data);
    } else {
      $network->update($data);
    }
    
    //var_dump($address);exit;
    
    $new_id = $network->save();
    if(!$new_id) {
      $this->layout()->setFlash("Fehler beim Speichern", "error");
      $this->layout()->set("network", $network);
      return $this->add();
    }
    
    
    $this->layout()->setFlash("Netzgebiet erfolgreich gespeichert.", "success");
    $this->redirect("Network", "Edit", ['id' => $new_id]);
  }
  
  protected function apiAction() {
    $do = $this->request->do;
    $data = [];
    
    switch($do) {
      case "getPermissions":
        $return = $this->getPermissionsApi();
        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 getPermissionsApi() {
    if(!$this->request->types) {
      return false;
    }
    
    $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;
    }
    
    $types = explode("|", $this->request->types);
    
    $permissions = [];
    $nas = NetworkAddressModel::search(['network_id' => $network_id, "addresstype" => $types]);
    foreach($nas as $na) {
      if(!array_key_exists($na->type, $permissions)) {
        $permissions[$na->type] = [];
      }
      $address = new Address($na->address_id);
      $permissions[$na->type][$na->address_id] = str_replace(["\r","\n"]," ",$address->getCompanyOrName());
    }
    
    return ["permissions" => $permissions];
  }
}