<?php

class ProducttechController extends mfBaseController {
  
  protected function init() {
    $this->needlogin=true;
    $me = new User();
    $me->loadMe();
    $this->me = $me;
    $this->layout()->set("me",$me);
    
    if(!$me->isAdmin()) {
      $this->redirect("Dashboard");
    }
  }
  
  protected function indexAction() {
    $this->layout()->setTemplate("Producttech/Index");
    $this->layout()->set("producttechs", ProducttechModel::getAll());
  }
  
  protected function addAction() {
    $this->layout()->setTemplate("Producttech/Form");
    
  }
  
  protected function editAction() {
    $tech_id = $this->request->id;
    $tech = new Producttech($tech_id);
    if(!$tech->id) {
      $this->layout()->setFlash("Technologie nicht gefunden.", "error");
      $this->redirect("Producttech");
    }
    
    $this->layout()->set("producttech", $tech);
    return $this->addAction();
  }
  
  protected function saveAction() {
    $r = $this->request;
    //var_dump($r);
    //var_dump($r->attributes);
    //exit;
    $id = $r->id;
    if(is_numeric($id) && $id > 0) {
      $mode = "edit";
      $producttech = new Producttech($id);
      if(!$producttech->id) {
        $this->layout()->setFlash("Technologie nicht gefunden", "error");
        $this->redirect("Producttech");
      }
    } else {
      $id = false;
      $mode = "add";
    }
    
    $data = [];
    $data['name'] = $r->name;
    $data['customer_type'] = $r->customer_type;
    $data['description'] = $r->description;
    $data['note'] = $r->note;
    
    $data['edit_by'] = $this->me->id;
    
    if($mode == "add") {
      $data['create_by'] = $this->me->id;
      $producttech = ProducttechModel::create($data);
    } else {
      $producttech->update($data);
    }
    
    //var_dump($address);exit;
    
    $new_id = $producttech->save();
    if(!$new_id) {
      $this->layout()->setFlash("Fehler beim Speichern", "error");
      $this->layout()->set("producttech", $producttech);
      return $this->add();
    }
    
    // handle ProductAttributes
    if(is_array($r->attributes) && count($r->attributes)) {
      foreach($r->attributes as $aid => $attribute) {
        if(!trim($attribute['name']) || !trim($attribute['displayname'])) {
          continue;
        }
        
        $a = [];
        $a['name'] = $attribute['name'];
        $a['producttech_id'] = $new_id;
        $a['displayname'] = $attribute['displayname'];
        $a['value'] = $attribute['value'];
        $a['description'] = $attribute['description'];
        $a['note'] = $attribute['note'];
        $a['edit_by'] = $this->me->id;
        
        if($aid == "new") {
          $attrib = ProducttechAttributeModel::create($a);
        } elseif(is_numeric($aid) && $aid > 0) {
          $attrib = new ProducttechAttribute($aid);
          $attrib->update($a);
        } else {
          continue;
        }
        
        $attrib->save();
      }
    }
    
    $this->layout()->setFlash("Technologie erfolgreich gespeichert.", "success");
    $this->redirect("Producttech", "edit", ['id' => $new_id]);
    
  }
  
  protected function delete() {
    $id = $this->request->id;
    
    $tech = new Producttech($id);
    if(!$tech->id || $tech->id != $id) {
      $this->layout()->setFlash("Technologie nicht gefunden.", "error");
      $this->redirect("Producttech");
    }
    
    if(ProductModel::search(["producttech_id" => $tech->id])) {
      $this->layout()->setFlash("Technologie kann nicht gelöscht werden, da sie in Verwendung ist.", "error");
      $this->redirect("Producttech");
    }
    
    // check if Product is unused
    $tech->delete();
    $this->redirect("Producttech");
  }
}