<?php

class TerminationController extends mfBaseController {
  
  protected function init() {
    $this->needlogin=true;
    $me = new User();
    $me->loadMe();
    $this->me = $me;
    $this->layout()->set("me",$me);
    
    if(!$me->isAdmin() && !$me->is("netowner") && !$me->is("lineplanner") && !$me->is("lineworker")) {
      $this->redirect("Dashboard");
    }
  }
  
  protected function saveAction() {
    $r = $this->request;
    
    $id = $r->id;
    if(is_numeric($id) && $id > 0) {
      $mode = "edit";
      $term = new Termination($id);
      if(!$term->id) {
        $this->layout()->setFlash("Anschluss nicht gefunden", "error");
        $this->redirect("Building");
      }
    } else {
      $id = false;
      $mode = "add";
    }
    
    $building = new Building($r->building_id);
    if(!$building->id) {
      $this->layout()->setFlash("Anschluss nicht gefunden", "error");
      $this->redirect("Building");
    }
    
    $data = [];
    $data['building_id'] = $r->building_id;
    $data['name'] = $r->name;
    $data['contact'] = $r->contact;
    $data['phone'] = $r->phone;
    $data['email'] = $r->email;
    
    $data['edit_by'] = $this->me->id;
    
    if($mode == "add") {
      $data['create_by'] = $this->me->id;
      $term = TerminationModel::create($data);
    } else {
      $term->update($data);
    }
    
    $new_id = $term->save();
    if(!$new_id) {
      $this->layout()->setFlash("Fehler beim Speichern", "error");
      $this->redirect("Building", "Index", [], "building=".$term->building_id);
    }

    
    if(!$term->code) {
      $term->code = $term->getNewObjectCode();
      $term->save();
    }
    
    // increment Building::units
    if(is_numeric($building->units)) {
      ++$building->units;
      $building->save();
    }
    
    $this->layout()->setFlash("Anschluss gespeichert.", "success");
    $this->redirect("Building", "Index", [], "building=".$term->building_id);
    
  }
  
  
  protected function delete() {
    if(!$this->me->is(["Admin", "netowner", "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");
    }
    
    $term = new Termination($id);
    if(!$term->id) {
      $this->layout()->setFlash("Anschluss nicht gefunden", "error");
      $this->redirect("Building");
    }
    
    $building_id = $term->building_id;
    $building = $term->building;
    
    // if user is not admin, check if they have permission for this network
    if(!$this->me->is("Admin")) {
      $allowed = false;
      
      $network = $building->network;
      foreach(["netowner", "pipeplanner"] as $type) {
        $perms = $network->getTypeAddresses($type);
        foreach($perms as $address_id => $perm) {
          if($this->me->address_id != $address_id) {
            continue;
          }
          $allowed = true;
        }
      }
      
      if(!$allowed) {
        $this->layout()->setFlash("Keine Berechtigung", "error");
        $this->redirect("Building", "Index", [], "building=".$building_id);
      }
    }

    
    // check for dependencies
    if(OrderProductModel::search(["termination_id" => $id])) {
      $this->layout()->setFlash("Anschluss kann nicht gelöscht werden, da abhängige Objekte gefunden wurden.", "error");
      $this->redirect("Building", "Index", [], "building=".$building_id);
    }

    
    $term->delete();
    if($building->units) {
      --$building->units;
      $building->save();
    }
    
    
    $this->layout()->setFlash("Anschluss gelöscht", "success");
    $this->redirect("Building", "Index", [], "building=".$building_id);
  }
  
  protected function apiAction() {
    $do = $this->request->do;
    $data = [];
    
    switch($do) {
      case "setValue":
        $return = $this->setValueApi();
        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 setValueApi() {
    $term_id = $this->request->id;
    $field = $this->request->type;
    $value = $this->db->escape($this->request->value);
    
    if(!is_numeric($term_id) || $term_id < 1) {
      $this->log->debug(__FILE__.": id not a number");
      return false;
    }
    
    if(!in_array($field, ["name","contact","phone","email"])) {
      $this->log->debug(__FILE__.": invalid field");
      return false;
    }
    
    $term = new Termination($term_id);
    if(!$term->id) {
      $this->log->debug(__FILE__.": no termination");
      return false;
    }
    
    $term->$field = $value;
    
    if(!$term->save()) {
      $this->log->debug(__FILE__.": error saving");
      return false;
    }
    
    return ["msg" => "Saved successfully"];
  }
}
