<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;

use App\PotentialCustomer;
use App\User;
use App\Http\Requests\PotentialCustomerRequest;
use App\BoatType;
use Auth;

class PotentialCustomerController extends Controller
{
	public function __construct()
    {
        $this->middleware('auth');
    }
	
    public function index()
    {
		if(Auth::user()->role->role_permission('view_users')){
			if(Auth::user()->role->role_permission('view_users')->level == 'all'){
				$customers = PotentialCustomer::paginate(30);
				$boatTypes = BoatType::lists('name', 'id')->toArray();
			}
			return view('customers.index', compact('customers', 'boatTypes'));
		}else{
			abort(403);
		}
    }

	public function assign(Request $request)
	{
		
		$potential = PotentialCustomer::find($request->user);
		
		$existe = User::where('email', $potential->email)->count(); 
		
		if ($existe < 1){
		
		$usuario = new User();
		$usuario->first_name = $potential->first_name;
		$usuario->last_name = $potential->last_name;
		$usuario->email = $potential->email;
		$usuario->telephone = $potential->telephone;
		$usuario->cellphone = $potential->cellphone;
		$usuario->role_id = 2;
		$usuario->comment = 'Cliente potencial.';
		$usuario->username = $potential->email;
		$usuario->password = 'Sk1pp_2019';
		$usuario->sendemail = 1;
		$usuario->sendsms = 1;
		$usuario->save();

		$potential->delete();
		
		return redirect('users')->withSuccess($usuario->first_name .' '.$usuario->last_name . ' ha sido creado. La contraseña del usuario es: Sk1pp_2019');
		
		}else{
			
			return redirect('users')->withSuccess('Error. Ya existe un usuario con el mismo email de contacto.');
		}

		
	}	

	public function search(Request $request)
	{
		if(Auth::user()->role->role_permission('view_users')){
			if(Auth::user()->role->role_permission('view_users')->level == 'all'){
				$customers = PotentialCustomer::whereRaw("(CONCAT(first_name,' ',last_name) like ?)", ['%'.$request->get('term').'%'])->paginate(50);
				$boatTypes = BoatType::lists('name', 'id')->toArray();
			}
			return view('customers.index', compact('customers','boatTypes'));
		}else{
			abort(403);
		}
	}
    
    public function create()
    {
		if(Auth::user()->role->role_permission('create_users')){
			$boatTypes = BoatType::lists('name', 'id')->toArray();

			return view('customers.create', compact('boatTypes'));
		}else{
			abort(403);
		}
    }

    public function store(PotentialCustomerRequest $request)
    {
		if(Auth::user()->role->role_permission('create_users')){
            $customer = PotentialCustomer::create($request->all());
            return redirect('customers')->withSuccess($customer->first_name.' '.$customer->last_name.' has been saved.');
		}else{
			abort(403);
		}
    }

    public function show(PotentialCustomer $customer)
    {
		if(Auth::user()->role->role_permission('view_users')){
			//return view('customers.show', compact('customer'));
			return redirect('customers')->withSuccess('Customer has been updated.');
		}else{
			abort(403);
		}
    }
	
    public function edit(Request $request)
    {
		if(Auth::user()->role->role_permission('edit_users')){
			$boatTypes = BoatType::lists('name', 'id')->toArray();
			$customer = PotentialCustomer::find($request->user);
			return view('customers.edit', compact('customer', 'boatTypes'));
		}else{
			abort(403);
		}
    }
	
    public function update(PotentialCustomerRequest $request, PotentialCustomer $customer)
    {
			
		if(Auth::user()->role->role_permission('edit_users')){
			$customer->update($request->all());
			return redirect('customers/'.$customer->id)->withSuccess($customer->first_name.' '.$customer->last_name.' has been updated.');
		}else{
			abort(403);
		}
    }
	
    public function destroy(PotentialCustomer $customer)
    {
		if(Auth::user()->role->role_permission('delete_users')){
			$customer->delete();
			return redirect('customers')->withSuccess($customer->first_name.' '.$customer->last_name.' has been deleted.');
		}else{
			abort(403);
		}
    }

}
