<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Credit;
use App\Customer;
use App\Schedule;
use App\Http\Requests\CreditRequest;
use Auth;use App\CreditRegister;

class CreditController extends Controller
{
	public function __construct()
    {
        $this->middleware('auth');
    }

    public function index()
    {
		if(Auth::user()->role->role_permission('view_credit')){
			$credit = Credit::orderBy('created_at', 'desc')->paginate(30);
			return view('credits.index', compact('credit'));
		}else{
			abort(403);
		}
    }

	public function search(Request $request)
	{
		if(Auth::user()->role->role_permission('view_credit')){
			$credit = Credit::where('name', 'LIKE', '%'. $request->get('term') .'%')->paginate(30);
			return view('credits.index', compact('credit'));
		}else{
			abort(403);
		}
	}

    public function create()
    {
		if(Auth::user()->role->role_permission('create_credit')){
			$schedule = Schedule::lists('name', 'id')->toArray();
			return view('credits.create', compact('schedule'));
		}else{
			abort(403);
		}
    }

    public function store(CreditRequest $request)
    {
		if(Auth::user()->role->role_permission('create_credit')){
			if($request->shift == 1){
			 $request->merge(['type' => 'Mañana']);
			}
			else {
			 $request->merge(['type' => 'Tarde']);
			}
			$credit = Credit::create($request->all());

			return redirect('credits')->withSuccess($credit->name.' has been saved.');
		}else{
			abort(403);
		}
    }


    public function listado(Customer $customer)	{

	if(!Auth::user()->role->role_permission('create_credit')){
	if ($customer->id != Auth::user()->customer_id)
		return redirect('/');
	}
	$boats = $customer->boats()->get();
       return view('credits.listado', compact('customer', 'boats'));
    }

    public function dispo() {
	return view('credits.dispo');
    }

	public function assign(Customer $customer)
	{
		//$user = $request->credit;
		$user = $customer;
		$boats = $customer->boats()->lists('name', 'id')->toArray();
		return view('credits.assign', compact('user','boats'));
	}


    public function show(Credit $credit)
    {
		if(Auth::user()->role->role_permission('view_credit')){
			return view('credits.show', compact('credit'));
		}else{
			abort(403);
		}
    }

    public function edit(Credit $credit)
    {
		if(Auth::user()->role->role_permission('edit_credit')){
			$schedule = Schedule::lists('name', 'id')->toArray();
			return view('credits.edit', compact('credit','schedule'));
		}else{
			abort(403);
		}
    }

    public function update(CreditRequest $request, Credit $credit)
    {
		if(Auth::user()->role->role_permission('edit_credit')){

			if($request->shift == 1){
			 $request->merge(['type' => 'Mañana']);
			}
			else {
			 $request->merge(['type' => 'Tarde']);
			}
			$credit->update($request->all());
			return redirect('credits')->withSuccess($credit->name.' has been updated.');
		}else{
			abort(403);
		}
    }

    public function destroy(Credit $credit)
    {
		if(Auth::user()->role->role_permission('delete_credit')){
			$credit->delete();
			return redirect('credits')->withSuccess($credit->name.' has been deleted.');
		}else{
			abort(403);
		}
    }
}
