<?php

namespace App\Http\Controllers\Admin;

use App\Currency;
use App\CurrencyRate;
use App\Http\Controllers\Controller;
use App\Http\Requests\Admin\MassDestroyCurrencyRateRequest;
use App\Http\Requests\Admin\StoreCurrencyRateRequest;
use App\Http\Requests\Admin\UpdateCurrencyRateRequest;
use Gate;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;

class CurrencyRateController extends Controller
{
    public function index()
    {
        abort_if(Gate::denies('currency_rate_access'), Response::HTTP_FORBIDDEN, '403 Forbidden');

//        $currencyRates = CurrencyRate::with(['currency_from', 'currency_to'])->get();
        $currencyRates = CurrencyRate::with(['currency_from', 'currency_to'])->paginate(20);
//        $currencyRates::paginate(20);

        return view('admin.currencyRates.index', compact('currencyRates'));
    }

    public function create()
    {
        abort_if(Gate::denies('currency_rate_create'), Response::HTTP_FORBIDDEN, '403 Forbidden');

        $currency_froms = Currency::pluck('name', 'id')->prepend(trans('quickadmin.qa_please_select'), '');

        $currency_tos = Currency::pluck('name', 'id')->prepend(trans('quickadmin.qa_please_select'), '');

        return view('admin.currencyRates.create', compact('currency_froms', 'currency_tos'));
    }

    public function store(StoreCurrencyRateRequest $request)
    {
        $currencyRate = CurrencyRate::create($request->all());

        return redirect()->route('admin.currency-rates.index');
    }

    public function edit(CurrencyRate $currencyRate)
    {
        abort_if(Gate::denies('currency_rate_edit'), Response::HTTP_FORBIDDEN, '403 Forbidden');

        $currency_froms = Currency::pluck('name', 'id')->prepend(trans('quickadmin.qa_please_select'), '');

        $currency_tos = Currency::pluck('name', 'id')->prepend(trans('quickadmin.qa_please_select'), '');

        $currencyRate->load('currency_from', 'currency_to');

        return view('admin.currencyRates.edit', compact('currencyRate', 'currency_froms', 'currency_tos'));
    }

    public function update(UpdateCurrencyRateRequest $request, CurrencyRate $currencyRate)
    {
        $currencyRate->update($request->all());

        return redirect()->route('admin.currency-rates.index');
    }

    public function show(CurrencyRate $currencyRate)
    {
        abort_if(Gate::denies('currency_rate_show'), Response::HTTP_FORBIDDEN, '403 Forbidden');

        $currencyRate->load('currency_from', 'currency_to');

        return view('admin.currencyRates.show', compact('currencyRate'));
    }

    public function destroy(CurrencyRate $currencyRate)
    {
        abort_if(Gate::denies('currency_rate_delete'), Response::HTTP_FORBIDDEN, '403 Forbidden');

        $currencyRate->delete();

        return back();
    }

    public function massDestroy(MassDestroyCurrencyRateRequest $request)
    {
        $currencyRates = CurrencyRate::find(request('ids'));

        foreach ($currencyRates as $currencyRate) {
            $currencyRate->delete();
        }

        return response(null, Response::HTTP_NO_CONTENT);
    }
}
