<?php

class BudgetCostGroupController extends BaseController {

	protected $rules		 = array(
			'name' => 'required|max:50',
	);
	protected $messages	 = array(
			'required' => 'Неообходимо ввести группу расходов'
	);
  protected $table = 'budget_cost_groups';
	/**
	 * Constructor
	 */
	public function __construct() {
		// Establish Filters
		$this->beforeFilter('auth');
		$this->beforeFilter('hasRole:' . Role::ROLE_BUDGET_REFRENCE);

		if (Auth::check()) {
			$this->user	    = Auth::user();
			$this->projects = $this->user->budgetProjects;
		}
		$this->projectPrefix = 'budget';
		parent::__construct();
	}

	/**
	 * Display a listing of the resource.
	 *
	 * @return Response
	 */
	public function index() {
		$input = Input::all();

		if ($this->user->projects->first() == null) {
			return Redirect::to("budget/cost_group");
		}
		if (Input::has('projectId')) {
			Session::put('work_projectId', $input['projectId']);
			return Redirect::to('budget/cost_group');
		}

		$budgetCostGroups = BudgetCostGroup::whereIn('project_id', array($this->workProject->id, 0))->orderBy('name')->get();
		if ($budgetCostGroups == null) {
			return Redirect::to("budget/cost_group");
		}
		return $this->setupLayout('budget.cost_group.index')
						   ->with('input', $input)
						   ->with('user', $this->user)
						   ->with('budgetCostGroups', $budgetCostGroups)
					     ->with('workProject', $this->workProject);
	}

	/**
	 * Show the form for creating a new resource.
	 *
	 * @return Response
	 */
	public function create() {
		return $this->setupLayout('budget.cost_group.create');
	}

	/**
	 * Store a newly created resource in storage.
	 *
	 * @return Response
	 */
	public function store() {
		$input			 = Input::all();
		$validation	 = Validator::make($input, $this->rules, $this->messages);

		if ($validation->fails()) {
			return Redirect::action('BudgetCostGroupController@create')
											->withInput()
											->withErrors($validation->errors());
		}
		else {
			$input									  = Input::all();
			$budgetCostGroup					= BudgetCostGroup::create(array(
									'name'		    => e($input['name']),
									'project_id'	=> $this->workProject->id,
									'plan_amount' => $input['plan_amount']
			));

      return Redirect::to('budget/cost_group');
		}
	}

	/**
	 * Show the form for editing the specified resource.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function edit($id) {
		$budgetCostGroup = BudgetCostGroup::find($id);
		if ( $budgetCostGroup == null || !is_numeric($id) ){
			return Redirect::to("budget/cost_group");
		}
		return $this->setupLayout('budget.cost_group.edit')
										->with('budgetCostGroup', $budgetCostGroup);
	}

	/**
	 * Update the specified resource in storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function update($id) {
		$input			 = Input::all();
		$validation	 = Validator::make($input, $this->rules, $this->messages);

		if ($validation->fails()) {
			return Redirect::action('BudgetCostGroupController@edit', $id)
											->withInput()
											->withErrors($validation->errors());
		}
		else {
			$budgetCostGroup = BudgetCostGroup::find($id);
			if ( $budgetCostGroup == null || !is_numeric($id) ) {
				return \App::abort(404);
			}

			$budgetCostGroup->name				= e($input['name']);
			$budgetCostGroup->plan_amount = e($input['plan_amount']);
			$budgetCostGroup->save();

			return Redirect::to('budget/cost_group');
		}
	}

	/**
	 * Remove the specified resource from storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function destroy($id) {
		$budgetCostGroup = BudgetCostGroup::find($id);
		if ( $budgetCostGroup == null || !is_numeric($id) ) {
			$response = array(
					'status' => 'fail'
			);
			return Response::json($response);
		}

		BudgetCost::whereBudgetCostGroupId($id)->update(array('budget_cost_group_id' => 0));
		BudgetCostGroup::destroy($id);

		$response = array(
				'status' => 'success'
		);
		return Response::json($response);
	}

}
