<?php defined('SYSPATH') or die('No direct access allowed.');

/**
 * This class provides the Directors ability to setup families
 *
 * @package    Membership Database
 * @category   Controllers/Director
 * @author     Deon George
 * @copyright  (c) 2014 Deon George
 * @license    http://dev.leenooks.net/license.html
 */
class Controller_Director_Account extends Controller_Account {
	protected $secure_actions = array(
		'add'=>TRUE,
		'ajaxlist'=>FALSE,
		'list'=>TRUE,
	);

	/**
	 * Add an Account
	 */
	public function action_add() {
		Block::factory()
			->type('form-horizontal')
			->title('Add/Edit Record')
			->title_icon('fa-wrench')
			->body($this->add_edit());
	}

	public function action_ajaxlist() {
		$result = array();

		if ($this->request->query('query'))
			$result = Arr::merge($result,ORM::factory('Account')->list_autocomplete($this->request->query('query'),'id','id',array('%s: %s'=>array('id','name(TRUE)'))));

		$this->response->headers('Content-Type','application/json');
		$this->response->body(json_encode(array_values($result)));
	}

	/**
	 * List Accounts
	 */
	public function action_list() {
		$output = __METHOD__;

		Block::factory()
			->title('List Families')
			->body($output);
	}

	private function add_edit($id=NULL) {
		$co = ORM::factory('Child',$id);

		if ($this->request->post() AND $co->values($this->request->post())->changed() AND (! $this->save($co)))
			$co->reload()->values($this->request->post());

		// If there are no room records, we'll create a waitlist one that can be completed.
		if (! $co->subitems())
			$co->subitem_add($co->room->values(array('child_id'=>$co->id,'code'=>'W')));

		Style::factory()
			->type('file')
			->data('media/theme/bootstrap/css/bootstrap.datepicker.css');

		Script::factory()
			->type('file')
			->data('media/theme/bootstrap/js/bootstrap.datepicker.js');

		return View::factory('account/user/add_edit')
			->set('o',$co)
			->set('so',Company::instance()->so());
	}
}
?>