<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use App\BankAccount;
use App\Http\Requests\BankAccountRequest;
use App\Customer;
use Auth;

class BankAccountController extends Controller
{
	public function __construct()
    {
        $this->middleware('auth');
    }
	
    public function index()
    {
        if(Auth::user()->role->role_permission('view_bank_accounts')){
			$bank_accounts = BankAccount::paginate(30);
			return view('bank-accounts.index', compact('bank_accounts'));
		}else{
			abort(403);
		}
    }
	
	public function search(Request $request)
	{
		if(Auth::user()->role->role_permission('view_bank_accounts')){
			$customers = Customer::whereRaw("fiscal_reason like ?)", ['%'.$request->get('term').'%'])->paginate(50);
			$bank_accounts = BankAccount::whereIn('customer_id', $customers->pluck('customer_id'))->paginate(30);
			return view('bank-accounts.index', compact('bank_accounts'));
		}else{
			abort(403);
		}
	}
    
    public function create(Customer $customer)
    {

		if(Auth::user()->role->role_permission('create_bank_accounts')){
			if($customer->bank_account){
				return redirect('companies/'.$customer->id.'/bank-account/'.$customer->bank_account->id.'/edit');
			}else{
				return view('bank-accounts.create', compact('customer'));
			}
		}else{
			abort(403);
		}
    }
	
    public function store(BankAccountRequest $request, Customer $customer)
    {
		if(Auth::user()->role->role_permission('create_bank_accounts')){
			$bank_account = BankAccount::create($request->all());
			return redirect(isset($customer->id) ? 'companies/'.$customer->id : 'bank-accounts')->withSuccess($bank_account->account_name.' has been saved.');
		}else{
			abort(403);
		}
    }
	
    public function show(BankAccount $bank_account)
    {
        if(Auth::user()->role->role_permission('view_bank_accounts')){
			return $bank_account;
		}else{
			abort(403);
		}
    }
	
    public function edit(Customer $customer, BankAccount $bank_account)
    {
        if(Auth::user()->role->role_permission('edit_bank_accounts')){
			return view('bank-accounts.edit', compact('bank_account', 'customer'));
		}else{
			abort(403);
		}
    }
	
    public function update(BankAccountRequest $request, Customer $customer, BankAccount $bank_account)
    {
        if(Auth::user()->role->role_permission('edit_bank_accounts')){
			$bank_account->update($request->all());
			return redirect(isset($customer->id) ? 'companies/'.$customer->id : 'bank-accounts')->withSuccess($bank_account->account_name.' has been updated.');
		}else{
			abort(403);
		}
    }
	
    public function destroy(BankAccount $bank_account)
    {
        if(Auth::user()->role->role_permission('delete_bank_accounts')){
			$bank_account->delete();
			return redirect('bank-accounts')->withSuccess($bank_account->account_name.' has been deleted.');
		}else{
			abort(403);
		}
    }
}
