<?php

namespace App\Http\Controllers\Client;

use App\Http\Controllers\Controller;
use App\Models\Company;
use App\Models\SpecialCurrency;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;


class CurrencyController extends Controller
{
    public function index()
    {
        $company_id = Auth::user()->company_id;
        $company = Company::FindOrFail($company_id);
        $currencies = $company->currencies;
        return view('client.currencies.index', compact('company', 'company_id', 'currencies'));
    }

    public function create()
    {
        $company_id = Auth::user()->company_id;
        $company = Company::FindOrFail($company_id);
        return view('client.currencies.create', compact('company_id', 'company'));
    }

    public function store(Request $request)
    {
        $this->validate($request, [
            'currency_name' => 'required',
        ]);
        $data = $request->all();
        $SpecialCurrency = SpecialCurrency::create($data);
        return redirect()->route('client.currencies.index')
            ->with('success', 'تم اضافة العملة بنجاح');
    }

    public function edit($id)
    {
        $company_id = Auth::user()->company_id;
        $company = Company::FindOrFail($company_id);
        $SpecialCurrency = SpecialCurrency::findOrFail($id);
        return view('client.currencies.edit', compact('SpecialCurrency', 'company_id', 'company'));
    }

    public function update(Request $request, $id)
    {
        $this->validate($request, [
            'currency_name' => 'required',
        ]);
        $input = $request->all();
        $SpecialCurrency = SpecialCurrency::findOrFail($id);
        $SpecialCurrency->update($input);
        return redirect()->route('client.currencies.index')
            ->with('success', 'تم تعديل بيانات العملة بنجاح');
    }

    public function destroy(Request $request)
    {
        $SpecialCurrency = SpecialCurrency::findOrFail($request->currencyid);
        $SpecialCurrency->delete();
        return redirect()->route('client.currencies.index')
            ->with('success', 'تم حذف العملة بنجاح');
    }
}
