<?php
class UredjajsController extends AppController {

	var $name = 'Uredjajs';
	var $uses = array('Uredjaj');
	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->Uredjaj->recursive = 0;
		
		$conditions = array();
		if ($customer_id) {
			$conditions['Uredjaj.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->Uredjaj->contain('Customer');
		if (!$id || !($item = $this->Uredjaj->read(null, $id))) {
			$this->Session->setFlash(__('Invalid Uredjaj', true) . ($id ? " #{$id}" : ""), 'default', array('class' => 'error'));
			$this->redirect(array('action' => 'index'));
		}
		
		// Dohvati radne naloge za ovaj uređaj
		$this->loadModel('RadniNalog');
		$radniNalozi = $this->RadniNalog->find('all', array(
			'conditions' => array('RadniNalog.uredjaj_id' => $id),
			'contain' => array('Customer', 'RadniNalogStatus'),
			'order' => array('RadniNalog.created' => 'DESC'),
			'limit' => 20
		));
		
		// Dohvati ponude za ovaj uređaj
		$this->loadModel('Offer');
		$ponude = $this->Offer->find('all', array(
			'conditions' => array('Offer.uredjaj_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->Uredjaj->create();
			if ($this->Uredjaj->save($this->data)) {
				$this->Session->setFlash(__('Uređaj je spremljen', true), 'default', array('class' => 'success'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('Uređaj nije mogao biti spremljen. Pokušajte ponovo.', true), 'default', array('class' => 'error'));
			}
		}
		
		$customers = $this->Uredjaj->Customer->find('list');
		$this->set(compact('customers'));
	}

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

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