<?php

class PatchingController 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", "netoperator", "lineworker"])) {
      $this->redirect("Dashboard");
    }
  }
  
  protected function indexAction() {
    $this->layout()->setTemplate("Patching/Index");
    
    $this->layout->set("filter", $this->request->filter);
    
    $filter = $this->getPreparedFilter($this->request->filter);
    //var_dump($filter);exit;
    // 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 = [];
    $terms = [];
    foreach($my_networks as $network) {
      $my_network_ids[] = $network->id;
    }
    
    $terms_search = [
        "network_id" => $my_network_ids,
    ];
    
    if(is_array($filter) && count($filter)) {
      foreach($filter as $name => $value) {
        $terms_search[$name] = $value;
      }
    }
    
    //var_dump($terms_search);exit;
    
    if($filter["prepatched"]) {
      $pagination['maxItems'] = PatchingModel::searchByEnabledPatchingCount($terms_search);
      foreach(PatchingModel::searchByEnabledPatching($terms_search, $pagination) as $t) {
        if(!array_key_exists($t->id, $terms)) {
          $terms[$t->id] = $t;
        }
      }
    } else {
      $pagination['maxItems'] = PatchingModel::searchByFinishedLineworkCount($terms_search);
      foreach(PatchingModel::searchByFinishedLinework($terms_search, $pagination) as $t) {
        if(!array_key_exists($t->id, $terms)) {
          $terms[$t->id] = $t;
        }
      }
    }
    
    //$this->layout()->set("terminations", $terms);
    $this->layout()->set("pagination", $pagination);
    $terminations = [];
    foreach($terms as $term) {
      $term->order = OrderModel::getFirst(["termination_id" => $term->id]);
      $terminations[] = $term;
    }
    
    
    $this->layout()->set("terminations", $terminations);
    
  }
  
  private function getPreparedFilter($filter) {
    $new_filter = [];
    //var_dump($filter);
    
    if(array_key_exists("hide_delayed_finish", $filter)) {
      if($filter["hide_delayed_finish"] == "1") {
        $new_filter["hide_delayed_finish"] = true;
      } else {
        $new_filter["hide_delayed_finish"] = false;
      }
      unset($filter["hide_delayed_finish"]);
    } else {
      $new_filter["hide_delayed_finish"] = true;
    }
    
    if(array_key_exists("patched", $filter)) {
      if($filter["patched"] == "1") {
        $new_filter["patched"] = true;
        unset($filter["patched"]);
        $new_filter["hide_delayed_finish"] = false;
      } elseif ($filter["patched"] == "2") {
        $new_filter["patched"] = false;
        unset($filter["patched"]);
        $new_filter["prepatched"] = true;
        $new_filter["hide_delayed_finish"] = false;
        unset($filter['hide_delayed_finish']);
      } else {
        unset($filter["patched"]);
      }
      
    } else {
      $new_filter["patched"] = false;
    }
    
    foreach($filter as $name => $value) {
      $new_filter[$name] = $value;
    }
    //var_dump($new_filter);exit;
    return $new_filter;
  }
  
  protected function saveAction() {
    if(!$this->me->is(["Admin", "netowner", "netoperator", "lineworker"])) {
      $this->layout()->setFlash("Keine Berechtigung", "error");
      $this->redirect("Dashboard");
    }
    
    $r = $this->request;
    //var_dump($r);
    
    $term_id = $r->termination_id;
    //var_dump($r);exit;
    if(is_numeric($term_id) && $term_id > 0) {
      $termination = new Termination($term_id);
      if(!$termination->id) {
        $this->layout()->setFlash("Anschluss nicht gefunden", "error");
        $this->redirect("Patching");
      }
    } else {
      $this->layout()->setFlash("Anschluss nicht gefunden", "error");
      $this->redirect("Patching");
    }
    
    $patch_data = [];
    
    
    $patch_data["linework_port"] = $r->linework_port;
    $patch_data["device_name"] = $r->device_name;
    $patch_data["device_port"] = $r->device_port;
    $patch_data["patched"] = ($r->patched == "1") ? 1 : 0;
    
    if(strlen($r->device_type)) {
      $patch_data["device_type"] = $r->device_type;
    } else {
      $patch_data["device_type"] = null;
    }
    
    $patching = PatchingModel::getFirst(["termination_id" => $term_id]);
    if($patching) {
      $mode = "edit";
    } else {
      $mode = "add";
      $patch_data["termination_id"] = $term_id;
    }
    //var_dump($mode, $r);exit;
    if($mode == "add") {
      if($patch_data["patched"]) {
        $patch_data["patched_by"] = $this->me->id;
        $patch_data["patched_date"] = date("U");
      }
      $patch_data['create_by'] = $this->me_id;
      $patching = PatchingModel::create($patch_data);
      
    } else {
      if($patch_data["patched"] && !$patching->patched) {
        $patch_data["patched_by"] = $this->me->id;
        $patch_data["patched_date"] = date("U");
      }
      $patching->update($patch_data);
    }
    //var_dump($patching);exit;
    $id = $patching->save();
    if(!$id) {
      $this->layout()->setFlash("Fehler beim Speichern", "error");
      return $this->indexAction();
    }
    
    $query = [];
    if(is_numeric($this->request->s) && $this->request->s > 0) {
      $query["s"] = $this->request->s;
    }
    if (is_array($this->request->filter)) {
      $query["filter"] = $this->request->filter;
    }
    
    $qs = http_build_query($query);
    
    $this->layout()->setFlash("Patchung erfolgreich gespeichert.", "success");
    $this->redirect("Patching","Index", $qs);
    
  }
  
  
}