<?php

class ProductsController extends BaseController {

	protected $rules		 = array(
			'name'			 => 'required',
			'price'			 => 'numeric',
			'price_back' => 'numeric',
//			'article'		 => 'unique:products',
	);
	protected $messages	 = array(
			'required' => 'Неообходимо ввести название',
			'numeric'	 => 'Цена вводится цифрами',
//			'article'	 => 'Артукул должен быть уникальным'
	);
	private $project		 = null;

	/**
	 * Constructor
	 */
	public function __construct() {
		$id			 = Request::segment(2);
		$project = Project::find($id);
		if ($project == null || !is_numeric($id)) {
			return \App::abort(404);
		}
		else {
			$this->project = $project;
		}
		// Establish Filters
		$this->beforeFilter('auth');

		parent::__construct();

		if ($this->user && !$this->user->isAdmin()) {
			if (!$this->user->hasAnyRole(array(Role::ROLE_PROJECTS_EDITOR, Role::ROLE_PRODUCTS_EDITOR))) {
				$this->beforeFilter('hasRole:' . Role::ROLE_PROJECTS_EDITOR);
				$this->beforeFilter('hasRole:' . Role::ROLE_PRODUCTS_EDITOR);
			}
		}
	}

	/**
	 * Display a listing of the resource.
	 *
	 * @return Response
	 */
	public function index() {
		return View::make('products.index')->with('project', $this->project);
	}

	/**
	 * Show the form for creating a new resource.
	 *
	 * @return Response
	 */
	public function create() {
		$suppliers = array();
		foreach ($this->project->suppliers as $supplier) {
			$suppliers[$supplier->id] = $supplier->name;
		}

		return View::make('products.create')->with('project', $this->project)->with('suppliers', $suppliers);
	}

	/**
	 * 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('ProductsController@create', array($this->project->id))
											->withInput()
											->withErrors($validation->errors());
		}
		else {
			$input						 = Input::all();
			$product					 = new Product(array(
					'supplier_id'		 => isset($input['supplier_id']) ? e($input['supplier_id']) : DB::raw('NULL'),
					'name'					 => e($input['name']),
					'price'					 => e($input['price']),
//					'price_back'		 => e($input['price_back']),
					'price_purchase' => e($input['price_purchase']),
					'weight'				 => e($input['weight'])
			));
			$product					 = $this->project->products()->save($product);
			$product->article	 = 'L' . str_pad($product->id, 5, '0', STR_PAD_LEFT);
			if ($input['action_name1'] && $input['action_price1']) {
				$product->action_name1	 = e($input['action_name1']);
				$product->action_price1	 = e($input['action_price1']);
			}
			if ($input['action_name2'] && $input['action_price2']) {
				$product->action_name2	 = e($input['action_name2']);
				$product->action_price2	 = e($input['action_price2']);
			}
			if ($input['action_name3'] && $input['action_price3']) {
				$product->action_name3	 = e($input['action_name3']);
				$product->action_price3	 = e($input['action_price3']);
			}
			$product->save();

			return Redirect::action('ProductsController@index', array($this->project->id));
		}
	}

	/**
	 * Show the form for editing the specified resource.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function edit($projectId, $id) {
		$product = Product::find($id);
		if ($product == null || !is_numeric($id) || $product->project_id != $projectId) {
			return \App::abort(404);
		}

		$suppliers = array();
		foreach ($this->project->suppliers as $supplier) {
			$suppliers[$supplier->id] = $supplier->name;
		}

		return View::make('products.edit')
										->with('product', $product)
										->with('suppliers', $suppliers)
										->with('project', $this->project);
	}

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

		if ($validation->fails()) {
			return Redirect::action('ProductsController@edit', array($projectId, $id))
											->withInput()
											->withErrors($validation->errors());
		}
		else {
			$product = Product::find($id);
			if ($product == null || !is_numeric($id) || $product->project_id != $projectId) {
				return \App::abort(404);
			}
//			$articleProduct = Product::whereArticle($input['article'])->where('id', '!=', $id)->first();
//			if ($articleProduct) {
//				return Redirect::action('ProductsController@edit', array($projectId, $id))
//												->withInput();
//			}

			$input									 = Input::all();
			$product->supplier_id		 = isset($input['supplier_id']) ? e($input['supplier_id']) : DB::raw('NULL');
			$product->article				 = e($input['article']);
			$product->name					 = e($input['name']);
			$product->price					 = e($input['price']);
//			$product->price_back		 = e($input['price_back']);
			$product->price_purchase = e($input['price_purchase']);
			$product->weight				 = e($input['weight']);
			if ($input['action_name1'] && $input['action_price1']) {
				$product->action_name1	 = e($input['action_name1']);
				$product->action_price1	 = e($input['action_price1']);
			}
			else {
				$product->action_name1	 = DB::raw('NULL');
				$product->action_price1	 = DB::raw('NULL');
			}
			if ($input['action_name2'] && $input['action_price2']) {
				$product->action_name2	 = e($input['action_name2']);
				$product->action_price2	 = e($input['action_price2']);
			}
			else {
				$product->action_name2	 = DB::raw('NULL');
				$product->action_price2	 = DB::raw('NULL');
			}
			if ($input['action_name3'] && $input['action_price3']) {
				$product->action_name3	 = e($input['action_name3']);
				$product->action_price3	 = e($input['action_price3']);
			}
			else {
				$product->action_name3	 = DB::raw('NULL');
				$product->action_price3	 = DB::raw('NULL');
			}
			$product->save();
			return Redirect::action('ProductsController@index', array($projectId));
		}
	}

	/**
	 * Remove the specified resource from storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function destroy($projectId, $id) {
//		Product::destroy($id);
		return Redirect::action('ProductsController@index', array($projectId));
	}

}
