<?php

class BuildingController 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()->setTemplate("Building/Index");
    
    $this->layout->set("filter", $this->request->filter);
    
    if($this->request->filter) {
      $filter = $this->getPreparedFilter($this->request->filter);
    }
    
    // pagination defaults
    $pagination = [];
    $pagination['start'] = 0;
    $pagination['count'] = 20;
    $pagination['maxItems'] = 0;
    
    if(is_numeric($this->request->s)) {
      $pagination['start'] = intval($this->request->s);
    }
    
    $my_networks = [];
    
    if($this->me->is("Admin")) {
      if($filter['network_id']) {
        $my_networks[] = new Network($filter['network_id']);
      } else {
        $my_networks = NetworkModel::getAll();
      }
      $this->layout()->set("mynetworks", NetworkModel::getAll());
      
    } else {
      $use_filter_network = false;
      $my_networks = $this->me->my_networks;
      
      foreach($my_networks as $mn) {
        if($mn->id == $filter['network_id']) {
          $use_filter_network = true;
          continue;
        }
      }
      
      if($use_filter_network) {
        $my_networks = [];
        $my_networks[] = new Network($filter['network_id']);
      }
      
      $this->layout()->set("mynetworks", $this->me->my_networks);
    }
    unset($filter['network_id']);

    // get Buildings in networks
    $my_network_ids = [];
    $buildings = [];
    foreach($my_networks as $network) {
      $my_network_ids[] = $network->id;
    }

    $building_search = [
        "network_id" => $my_network_ids,
        "workflow_finished" => 0
    ];

    if(is_array($filter) && count($filter)) {
      foreach($filter as $name => $value) {
        $building_search[$name] = $value;
      }
    }

    $pagination['maxItems'] = BuildingModel::count($building_search);
    foreach(BuildingModel::search($building_search, $pagination) as $b) {
      if(!array_key_exists($b->id, $buildings)) {
        $buildings[$b->id] = $b;
      }
    }

    //var_dump($buildings);exit;
    $this->layout()->set("buildings", $buildings);
    $this->layout()->set("pagination", $pagination);
  }
  
  private function getPreparedFilter($filter) {
    $new_filter = [];

    if(is_numeric($filter['networksection_id']) && $filter['networksection_id']) {
      $section = new Networksection($filter['networksection_id']);
      if($section->id) {
        $filter['network_id'] = $section->network_id;
      }
    }
    
    foreach($filter as $name => $value) {
      $new_filter[$name] = $value;
    }
    
    return $new_filter;
  }
  
  protected function addAction() {
    $this->layout()->setTemplate("Building/Form");
    
    if($this->me->isAdmin()) {
      $this->layout()->set("networks", NetworkModel::getAll());
      $this->layout()->set("pipeworkers", AddressModel::search(["addresstype" => ["pipeworker"]])); // change to NetworkaddressModel
      $this->layout()->set("lineworkers", AddressModel::search(["addresstype" => ["lineworker"]])); // change to NetworkaddressModel
    } else {
      $this->layout()->set("networks", $this->me->my_networks);
      $pipeworkers = [];
      $lineworkers = [];
      foreach($this->me->my_networks as $network) {
        //var_dump($network->addresstypes);exit;
        if(is_array($network->addresstypes)) {
          foreach($network->getTypeAddresses("pipeworker") as $address) {
            if(!array_key_exists($address->id, $pipeworkers)) {
              $pipeworkers[$address->id] = $address;
            }
          }
          foreach($network->getTypeAddresses("lineworker") as $address) {
            if(!array_key_exists($address->id, $lineworkers)) {
              $lineworkers[$address->id] = $address;
            }
          }
        }
      }
      
      $this->layout()->set("pipeworkers", $pipeworkers); // change to NetworkaddressModel
      $this->layout()->set("lineworkers", $lineworkers); // change to NetworkaddressModel
      
      
    }
    
    $this->layout()->set("types", BuildingtypeModel::getAll());
    $this->layout()->set("statuses", BuildingstatusModel::getAll());
    
    $this->layout()->set("networksections", NetworksectionModel::getAll());

    
  }
  
  protected function editAction() {
    $id = $this->request->id;
    if(!is_numeric($id) || !$id) {
      $this->layout()->setFlash("Objekt nicht gefunden", "error");
      $this->redirect("Building");
    }
    
    $building = new Building($id);
    if($building->id != $id) {
      $this->layout()->setFlash("Objekt nicht gefunden", "error");
      $this->redirect("Building");
    }
    
    $this->layout()->set("building", $building);
    return $this->addAction();
  }
  
  protected function saveAction() {
    $r = $this->request;
    $id = $r->id;
    //var_dump($r);exit;
    if(is_numeric($id) && $id > 0) {
      $mode = "edit";
      $building = new Building($id);
      if(!$building->id) {
        $this->layout()->setFlash("Objekt nicht gefunden", "error");
        $this->redirect("Objekt");
      }
    } else {
      $mode = "add";
    }
    
    if(!$r->network_id || !$r->type_id) {
      $this->layout()->setFlash("Bitte Netzgebiet und Typ auswählen", "error");
      $this->layout()->set("building", $building);
      return $this->add();
    }
    
    $data = [];
    $data['network_id'] = $r->network_id;
    $data['pop_id'] = ($r->pop_id) ? $r->pop_id : null;
    $data['type_id'] = $r->type_id;
    
    if($r->status_id) {
      $data['status_id'] = $r->status_id;
    }
    $data['pipeworker_id'] = ($r->pipeworker_id) ? $r->pipeworker_id : null;
    $data['lineworker_id'] = ($r->lineworker_id) ? $r->lineworker_id : null;
    $data['networksection_id'] = ($r->networksection_id) ? $r->networksection_id : null;
    
    $data['oan_id'] = trim($r->oan_id);
    $data['street'] = trim($r->street);
    $data['zip'] = trim($r->zip);
    $data['city'] = trim($r->city);
    
    $data['contact'] = trim($r->contact);
    $data['phone'] = trim($r->phone);
    $data['email'] = trim($r->email);
    $data['units'] = trim($r->units);
    $data['description'] = trim($r->description);
    $data['note'] = trim($r->note);
    
    $data['edit_by'] = 1;
    
    if($this->me->is("Admin")) {
      if($r->gps_lat) $data['gps_lat'] = trim($r->gps_lat);
      if($r->gps_long) $data['gps_long'] = trim($r->gps_long);
      if($r->code) $data['code'] = trim($r->code);
      if($r->laea) $data['laea'] = trim($r->laea);
    }
    
    if($mode == "add") {
      $data['create_by'] = $this->me->id;
      $building = BuildingModel::create($data);
      
      // check if building exists already
      $checkBuilding = BuildingModel::search(['=street' => $data['street'], '=city' => $data['city'], '=zip' => $data['zip']]);
      
      if($checkBuilding) {
        $this->layout()->setFlash("Objekt ist <a target='_blank' href='".self::getUrl("Building")."#building=".$checkBuilding[0]->id."'>bereits vorhanden</a>!", "error");
        $this->layout()->set("building", $building);
        return $this->add();
      }
    } else {
      $building->update($data);
    }
    
    
    
    //var_dump($address);exit;
    $new_id = $building->save();
    if(!$new_id) {
      $this->layout()->setFlash("Fehler beim Speichern", "error");
      $this->layout()->set("building", $building);
      return $this->add();
    }

    // get GPS location
    if((!$building->gps_lat && !$building->gps_long) || $r->gps_again ) {
      $search = [
        'country' => "AT",
        'city' => $building->city,
        'zip' => $building->zip,
        'street' => $building->street
      ];
      $coords = Gmaps_Geocoding::getCoords($search);
      
      if(is_array($coords) && count($coords) == 2) {
        $building->gps_lat = str_replace(",",".",$coords[0]);
        $building->gps_long = str_replace(",",".",$coords[1]);
        $building->save();
      }
    }
    
    // generate object code and LAEA coords
    if(!$building->code) {
      $building->code = $building->getNewObjectCode();
      $building->save();
    }
    if(!$building->laea || $r->gps_again) {
      $building->laea = $building->getLaeaCoordinates();
      $building->save();
    }
    
    // Anschlüsse anlegen
    
    if(!$building->terminations && $building->units > 0) {
      for($i = 1; $i <= $building->units; $i++) {
        $data = [];
        $data['building_id'] = $building->id;
        $data['code'] = $building->code . "." . sprintf("%03d", $i);
        
        if($building->units == 1) {
          $data['contact'] = $building->contact;
          $data['phone'] = $building->phone;
          $data['email'] = $building->email;
        }
        /*
	// no more lineworker_id in Termination
	if($building->lineworker_id) {
          $data['lineworker_id'] = $building->lineworker_id;
	}*/
        if($building->oan_id) {
          $data['oan_id'] = $building->oan_id. "." . sprintf("%03d", $i);
        }
        
        $term = TerminationModel::create($data);
        $term->save();
      }
    }
    
    
    $this->layout()->setFlash("Objekt erfolgreich gespeichert.", "success");
    $this->redirect("Building", "Edit", ['id' => $new_id]);
  }
  
  protected function deleteAction() {
    if(!$this->me->is("Admin") && !$this->me->is("pipeplanner")) {
      $this->layout()->setFlash("Keine Berechtigung", "error");
      $this->redirect("Building");
    }
    
    $id = $this->request->id;
    
    if(!is_numeric($id) || !$id) {
      $this->layout()->setFlash("Objekt nicht gefunden", "error");
      $this->redirect("Building");
    }
    
    $building = new Building($id);
    if(!$building->id) {
      $this->layout()->setFlash("Objekt nicht gefunden", "error");
      $this->redirect("Building");
    }
    
    if(TerminationModel::search(["building_id" => $id])) {
      $this->layout()->setFlash("Das Objekt kann nicht gelöscht werden, da noch Anschlüsse zugeordnet sind.", "error");
      $this->redirect("Building");
    }
    
    $building->delete();
    
    $this->layout()->setFlash("Objekt gelöscht", "success");
    $this->redirect("Building");
  }
  
  
}
