<?php
class AutomobilsController extends AppController {

	var $name = 'Automobils';
	var $uses = array('Automobil');
	var $components = array(
		'Filter' => array(
			'index' => array(
				'customer_id' => array(
					'form_options' => array('label' => 'Customer', 'options' => 'Customers'),
					'filter_options' => array(),
				),
			)
		),
	);

	function beforeFilter() {
		parent::beforeFilter();
		$this->Auth->allow('*'); // Privremeno dozvoli sve akcije
	}

	function index($customer_id = null) {
		$this->Automobil->recursive = 0;
		
		$conditions = array();
		if ($customer_id) {
			$conditions['Automobil.customer_id'] = $customer_id;
		}
		
		$this->paginate = array('conditions' => $conditions);
		$this->set('items', $this->paginate());
		$this->set('customer_id', $customer_id);
	}

	function view($id = null) {
		$this->Automobil->contain('Customer', 'Gorivo');
		if (!$id || !($item = $this->Automobil->read(null, $id))) {
			$this->Session->setFlash(__('Invalid Automobil', true) . ($id ? " #{$id}" : ""), 'default', array('class' => 'error'));
			$this->redirect(array('action' => 'index'));
		}
		
		// Dohvati radne naloge za ovaj automobil
		$this->loadModel('RadniNalog');
		$radniNalozi = $this->RadniNalog->find('all', array(
			'conditions' => array('RadniNalog.automobil_id' => $id),
			'contain' => array('Customer', 'RadniNalogStatus'),
			'order' => array('RadniNalog.created' => 'DESC'),
			'limit' => 20
		));
		
		// Dohvati ponude za ovaj automobil
		$this->loadModel('Offer');
		$ponude = $this->Offer->find('all', array(
			'conditions' => array('Offer.automobil_id' => $id),
			'contain' => array('Customer'),
			'order' => array('Offer.created' => 'DESC'),
			'limit' => 20
		));
		
		$this->set('item', $item);
		$this->set('radniNalozi', $radniNalozi);
		$this->set('ponude', $ponude);
	}

	function add() {
		if (!empty($this->data)) {
			$this->Automobil->create();
			if ($this->Automobil->save($this->data)) {
				$this->Session->setFlash(__('Automobil je spremljen', true), 'default', array('class' => 'success'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('Automobil nije mogao biti spremljen. Pokušajte ponovo.', true), 'default', array('class' => 'error'));
			}
		}
		
		$customers = $this->Automobil->Customer->find('list');
		$goriva = $this->Automobil->Gorivo->find('list');
		$this->set(compact('customers', 'goriva'));
	}

	function edit($id = null) {
		if ((!$id && empty($this->data)) || ($id && !($item = $this->Automobil->read(null, $id)))) {
			$this->Session->setFlash(__('Invalid Automobil', true) . ($id ? " #{$id}" : ""), 'default', array('class' => 'error'));
			$this->redirect(array('action' => 'index'));
		}
		
		if (!empty($this->data)) {
			if ($this->Automobil->save($this->data)) {
				$this->Session->setFlash(__('Automobil je spremljen', true), 'default', array('class' => 'success'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('Automobil nije mogao biti spremljen. Pokušajte ponovo.', true), 'default', array('class' => 'error'));
			}
		}
		
		if (empty($this->data)) {
			$this->data = $item;
		}
		
		$customers = $this->Automobil->Customer->find('list');
		$goriva = $this->Automobil->Gorivo->find('list');
		$this->set(compact('customers', 'goriva'));
	}

	function delete($id = null) {
		if (!$id || !$this->Automobil->read(null, $id)) {
			$this->Session->setFlash(__('Invalid Automobil', true) . ($id ? " #{$id}" : ""), 'default', array('class' => 'error'));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Automobil->delete($id)) {
			$this->Session->setFlash(__('Automobil obrisan', true), 'default', array('class' => 'success'));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Brisanje automobila nije uspjelo. Pokušajte ponovo.', true), 'default', array('class' => 'error'));
		$this->redirect(array('action' => 'index'));
	}
}
