<?php

class NetworksectionController 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 saveAction() {
    $r = $this->request;
    //var_dump($r);exit;
    if(!$r->network_id) {
      $this->layout()->setFlash("Netzgebiet nicht gefunden", "error");
      $this->redirect("Network", "Index");
    }
    
    $network_id = $r->network_id;
    $network = new Network($network_id);
    if(!$network->id) {
      $this->layout()->setFlash("Netzgebiet nicht gefunden", "error");
      $this->redirect("Network", "Index");
    }
    
    $sections = $r->sections;
    
    if(!is_array($sections) || !count($sections)) {
      $this->redirect("Network", "Index", [], "view=sections&net=".$network_id);
      
      
    }
    //var_dump($sections);exit;
    
    foreach($sections as $section_id => $name) {
      if($section_id == "new" && $name) {
        $section = NetworksectionModel::create(["network_id" => $network_id, "name" => $name]);
        //var_dump($section);exit;
        $section->save();
        continue;
      }
      
      if(is_numeric($section_id)) {
        $section = new Networksection($section_id);
        if(!$section->id) {
          continue;
        }
        $section->name = $name;
        $section->edit_by = $this->me->id;
        $section->save();
      }
    }
    
    $this->redirect("Network", "Index", [], "view=sections&net=".$network_id);
    
  }
  
  protected function deleteAction() {
    $id = $this->request->id;
    
    if(!is_numeric($id) || !$id) {
      $this->layout()->setFlash("Bauabschnitt nicht gefunden", "error");
      $this->redirect("Network", "Index");
    }
    
    $section = new Networksection($id);
    if(!$section->id) {
      $this->layout()->setFlash("Bauabschnitt nicht gefunden", "error");
      $this->redirect("Network", "Index");
    }
    
    
    
    $network_id = $section->network_id;
    
    $section->delete();
    
    $this->layout()->setFlash("Bauabschnitt gelöscht", "success");
    $this->redirect("Network", "Index", [], "view=sections&net=".$network_id);
  }
  
  protected function apiAction() {
    $do = $this->request->do;
    $data = [];
    
    switch($do) {
      case "getSections":
        $return = $this->getSectionsApi();
        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 getSectionsApi() {
    $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;
    }
    
    $sections = NetworksectionModel::search(['network_id' => $network_id]);
    $return = [];
    foreach($sections as $section) {
      $return[$section->id] = $section->network->name." - ".$section->name;
    }
    return ["sections" => $return];
  }
}