<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller; 
use App\Http\Requests\User\StoreUserRequest;
use App\Http\Requests\User\UpdateUserRequest;
use App\Http\Requests\User\UpdatePasswordUserRequest;
use App\Models\User; 
use App\Models\LeadModel; 
use App\Models\Role; 

class LeadsController extends Controller
{
 
    public function index()
    { 
        $this->authorize('show-user', LeadModel::class);

        $leads = LeadModel::paginate(15);

        return view('leads.index', compact('leads'));
    }

    public function show($id)
    { 
    	// $this->authorize('show-user', LeadModel::class);

    	$lead = LeadModel::find($id);

    	if(!$user){
        	$this->flashMessage('warning', 'User not found!', 'danger');            
            return redirect()->route('lead');
        }  

        // $roles = Role::all();

		// $roles_ids = Role::rolesUser($user);      	               

        return view('leads.show',compact('lead'));
    }

    public function create()
    {
        $this->authorize('create-user', LeadModel::class);

        $roles = Role::all();

        return view('leads.create',compact('roles'));
    }

    public function store(Request $request)
    {
        //$this->authorize('create-user', LeadModel::class);

        //$request->merge(['password' => bcrypt($request->get('password'))]);

        $user = LeadModel::create($request->all());

        //$roles = $request->input('roles') ? $request->input('roles') : [];

        //$user->roles()->sync($roles);

        $this->flashMessage('check', 'Lead successfully added!', 'success');

        return redirect()->route('lead.create');
    }

    public function edit($id)
    { 
    	$this->authorize('edit-user', LeadModel::class);

    	$user = LeadModel::find($id);

    	if(!$user){
        	$this->flashMessage('warning', 'User not found!', 'danger');            
            return redirect()->route('lead');
        }  

        $roles = Role::all();

		$roles_ids = Role::rolesUser($user);       	               

        return view('leads.edit',compact('lead', 'roles', 'roles_ids'));
    }

    public function update(UpdateUserRequest $request,$id)
    {
    	$this->authorize('edit-user', LeadModel::class);

    	$user = LeadModel::find($id);

        if(!$user){
        	$this->flashMessage('warning', 'User not found!', 'danger');            
            return redirect()->route('lead');
        }

        $user->update($request->all());

        $roles = $request->input('roles') ? $request->input('roles') : [];

        $user->roles()->sync($roles);

        $this->flashMessage('check', 'User updated successfully!', 'success');

        return redirect()->route('lead');
    }

    public function updatePassword(UpdatePasswordUserRequest $request,$id)
    {
    	$this->authorize('edit-user', LeadModel::class);

    	$user = LeadModel::find($id);

        if(!$user){
        	$this->flashMessage('warning', 'User not found!', 'danger');            
            return redirect()->route('lead');
        }

        $request->merge(['password' => bcrypt($request->get('password'))]);

        $user->update($request->all());

        $this->flashMessage('check', 'User password updated successfully!', 'success');

        return redirect()->route('lead');
    }

    public function editPassword($id)
    { 
    	$this->authorize('edit-user', LeadModel::class);

    	$user = LeadModel::find($id);

    	if(!$user){
        	$this->flashMessage('warning', 'User not found!', 'danger');            
            return redirect()->route('lead');
        }              	               

        return view('leads.edit_password',compact('lead'));
    }

    public function destroy($id)
    {
        $this->authorize('destroy-user', LeadModel::class);

        $user = LeadModel::find($id);

        if(!$user){
            $this->flashMessage('warning', 'User not found!', 'danger');            
            return redirect()->route('lead');
        }

        $user->delete();

        $this->flashMessage('check', 'User successfully deleted!', 'success');

        return redirect()->route('lead');
    }

}
