<?php

class BudgetPartnersController extends BaseController {

	protected $rules		 = array(
			'name' => 'required|max:50',
			'city' => 'required|max:50',
	);
	protected $messages	 = array(
			'name.required'	 => 'Неообходимо ввести название контрагента',
			'city.required'	 => 'Неообходимо указать город'
	);

	/**
	 * Constructor
	 */
	public function __construct() {
		// Establish Filters
		$this->beforeFilter('auth');
		$this->beforeFilter('hasRole:' . Role::ROLE_BUDGET);

		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('/');
		}
		if (Input::has('projectId')) {
			Session::put('work_projectId', $input['projectId']);
			return Redirect::to('budget/partners');
		}

		return $this->setupLayout('budget.partners.index')
										->with('input', $input)
										->with('user', $this->user)
										->with("budgetPartners", $this->workProject->budgetPartners)
										->with('workProject', $this->workProject);
	}

	/**
	 * Show the form for creating a new resource.
	 *
	 * @return Response
	 */
	public function create() {
		$costs = array(0 => 'Нет');
		foreach ($this->workProject->budgetCosts as $budgetCost) {
			$costs[$budgetCost->id] = $budgetCost->name;
		}

		return $this->setupLayout('budget.partners.create')
										->with('costs', $costs)
										->with('user', $this->user)
										->with('workProject', $this->workProject);
	}

	/**
	 * 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('BudgetPartnersController@create')
											->withInput()
											->withErrors($validation->errors());
		}
		else {
			$input				 = Input::all();
			$budgetCost		 = BudgetCost::find($input['cost_id']);
			$budgetPartner = BudgetPartner::create(array(
									'name'						 => e($input['name']),
									'city'						 => e($input['city']),
									'current_account'	 => e($input['current_account'])
			));
			$budgetPartner->project()->associate($this->workProject);
			if ($budgetCost) {
				$budgetPartner->cost()->associate($budgetCost);
			}
			$budgetPartner->save();

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

	/**
	 * Show the form for editing the specified resource.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function edit($id) {
		$budgetPartner = BudgetPartner::find($id);
		if ($budgetPartner == null || !is_numeric($id) || $budgetPartner->project_id != $this->workProject->id || !$this->workProject->isMy()) {
			return Redirect::to("budget/partners");
		}

		$costs = array(0 => 'Нет');
		foreach ($this->workProject->budgetCosts as $budgetCost) {
			$costs[$budgetCost->id] = $budgetCost->name;
		}

		return $this->setupLayout('budget.partners.edit')
										->with('costs', $costs)
										->with('budgetPartner', $budgetPartner)
										->with('user', $this->user)
										->with('workProject', $this->workProject);
	}

	/**
	 * 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('BudgetPartnersController@edit', $id)
											->withInput()
											->withErrors($validation->errors());
		}
		else {
			$budgetPartner = BudgetPartner::find($id);
			$budgetCost		 = BudgetCost::find($input['cost_id']);
			if ($budgetPartner == null || !is_numeric($id) || $budgetPartner->project_id != $this->workProject->id || !$this->workProject->isMy()) {
			return Redirect::to("budget/partners");
			}

			$budgetPartner->name						 = e($input['name']);
			$budgetPartner->city						 = e($input['city']);
			$budgetPartner->current_account	 = e($input['current_account']);
			if ($budgetCost) {
				$budgetPartner->cost()->associate($budgetCost);
			}
			$budgetPartner->save();

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

	/**
	 * Remove the specified resource from storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function destroy($id) {
		$budgetPartner = BudgetPartner::find($id);
		if ($budgetPartner == null || !is_numeric($id) || $budgetPartner->project_id != $this->workProject->id || !$this->workProject->isMy()) {
			return \App::abort(404);
		}

		BudgetPartner::destroy($id);

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

}
