<?php

namespace App\Http\Controllers;

use App\DataTables\CurrencyDataTable;
use App\Models\Currency;
use Illuminate\Http\Request;

class CurrencyController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(CurrencyDataTable $dataTable)
    {
        if(auth()->user()->can('show_currency')){
          return $dataTable->render('currencies.index');
            
        }
        else{
              return abort(404);

        }
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $data = $this->validate($request, [
            'name' => 'required',
            'code' => 'required',
            'exchange_rate' => 'required',
            'symbol' => 'required',
            'decimal_mark' => 'required',
            'thousands_separator' => 'required',
            'precision' => 'required',
            'status' => 'required|boolean',
        ]);


        $currency = Currency::create($data);
        return $currency;
    }

    /**
     * Display the specified resource.
     *
     * @param  Currency $currency
     * @return \Illuminate\Http\Response
     */
    public function show(Currency $currency)
    {
        return $currency;
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  Currency $currency
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Currency $currency)
    {
        $data = $this->validate($request, [
            'name' => 'required',
            'code' => 'required',
            'exchange_rate' => 'required',
            'symbol' => 'required',
            'decimal_mark' => 'required',
            'thousands_separator' => 'required',
            'precision' => 'required',
            'status' => 'required|boolean',
        ]);
        $currency->update($data);
        return  $currency;
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  Currency $currency
     * @return \Illuminate\Http\Response
     */
    public function destroy(Currency $currency)
    {
        return $currency->delete();
    }
}
