<?php

class AddressController extends mfBaseController {
  private $filter;
  
  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() {
    $rfilter = $this->request->filter;
    iF(!is_array($rfilter)) {
      $rfilter = [];
    }
    if(!array_key_exists("addresstype", $rfilter)) {
      $rfilter["addresstype"] = [];
    }
    
    $this->layout->set("filter", $rfilter);
    
    $filter = $this->getPreparedFilter($rfilter);
    
    // pagination defaults
    $pagination = [];
    $pagination['start'] = 0;
    $pagination['count'] = 25;
    $pagination['maxItems'] = 0;
    
    if(is_numeric($this->request->s)) {
      $pagination['start'] = intval($this->request->s);
    }
    //var_dump($filter);exit;
    $pagination['maxItems'] = AddressModel::count($filter);
    $addresses = AddressModel::search($filter, $pagination);
    
    $this->layout()->set("addresses", $addresses);
    $this->layout()->set("request", $this->request);
    $this->layout()->set("pagination", $pagination);
    
    return true;



    //var_dump($this->request->filter);
    $default_filter = ['parents_only' => 1];
    if(is_array($this->request->filter) && count($this->request->filter)) {
      $filter = array_merge($default_filter, $this->request->filter);
    } else {
      $filter = $default_filter;
    }
    //var_dump($filter);exit;
    $addresses = AddressModel::search($filter);
    
    $this->layout()->set("addresses", $addresses);
    $this->layout()->set("filter", $filter);
    $this->layout()->set("request", $this->request);
  }
  
  
  private function getPreparedFilter($filter) {
    $new_filter = [];
    
    if(is_array($filter) && count($filter)) {
      if(!array_key_exists("parents_only", $filter)) {
        $new_filter["parents_only"] = 1;
      }
      
      foreach($filter as $name => $value) {
        $new_filter[$name] = $value;
      }
    }
    
    return $new_filter;
  }
  
  protected function addAction() {
    $this->layout()->setTemplate("Address/Form");

    $parents = AddressModel::search(['parent_id' => null]);
    $this->layout()->set("parents", $parents);
  }
  
  protected function editAction() {
    $address = new Address($this->request->id);
    
    $this->layout()->set("address", $address);
    
    if(!$address->id) {
      $this->layout()->setFlash("Addresse nicht gefunden", "error");
      return $this->addAction();
    }
    
    return $this->addAction();
  }
  
  protected function saveAction() {
    $r = $this->request;
    $id = $r->id;
    //var_dump($r);
    if(is_numeric($id) && $id > 0) {
      $mode = "edit";
      $address = new Address($id);
      if(!$address->id) {
        $this->layout()->setFlash("Addresse nicht gefunden", "error");
        $this->redirect("Address");
      }
    } else {
      $mode = "add";
    }
    
    //var_dump($r->addresstypes);exit;
    
    $data = [];
    $data['parent_id'] = (!$r->parent_id) ? null : $r->parent_id;
    $data['company'] = $r->company;
    $data['firstname'] = $r->firstname;
    $data['lastname'] = $r->lastname;
    $data['street'] = $r->street;
    $data['zip'] = $r->zip;
    $data['city'] = $r->city;
    $data['country'] = $r->country;
    $data['phone'] = $r->phone;
    $data['fax'] = $r->fax;
    $data['mobile'] = $r->mobile;
    $data['email'] = $r->email;
    $data['note'] = $r->note;
    
    $data['edit_by'] = 1;
    
    if($mode == "add") {
      $data['create_by'] = 1;
      $address = AddressModel::create($data);
    } else {
      $address->update($data);
    }
    
    //var_dump($address);exit;
    
    $new_id = $address->save();
    if(!$new_id) {
      $this->layout()->setFlash("Fehler beim Speichern", "error");
      $this->layout()->set("address", $address);
      return $this->add();
    }
    
    $new_types = $r->addresstypes;
    if(is_array($new_types)) {
      foreach($address->types as $existing_type) {
        //var_dump($existing_type);
        //var_dump($new_types);
        //echo $existing_type->type;
        if(!in_array($existing_type->type, $new_types)) {
          $existing_type->delete();
        } else {
          // remove existing type from new_types array (dont need to create again)
          $new_types = array_diff($new_types, [$existing_type->type]);
          //unset($new_types[$existing_type]);
        }
      }
      //exit;
      foreach($new_types as $type) {
        $type_object = AddresstypeModel::create(['address_id' => $address->id, 'type' => $type]);
        $type_object->save();
        $address->types[$type] = $type_object;
      }
    }
    
    $attribs = $r->attributes;
    //var_dump($attribs);exit;
    if(is_array($attribs) && count($attribs)) {
      foreach($attribs as $attrib => $value) {
        $aa = AddressattributeModel::getFirst(["address_id" => $new_id, "name" => $attrib]);
        
        if(!$aa) {
          $aa = AddressattributeModel::create(["address_id" => $new_id, "name" => $attrib]);
        }
        $aa->value = $value;
        $aa->save();
      }
    }
    
    $this->layout()->setFlash("Adresse erfolgreich gespeichert.", "success");
    $this->redirect("Address", "Edit", ['id' => $new_id]);
  }
 
  
  
}