<?php

namespace App\Http\Controllers;

use App\Models\Dependencia;
use App\Http\Requests\DependenciaRequest;

/**
 * Class DependenciaController
 * @package App\Http\Controllers
 */
class DependenciaController extends Controller
{
    /**
     * Display a listing of the resource.
     */
    public function index()
    {
        $dependencias = Dependencia::paginate();

        return view('dependencia.index', compact('dependencias'))
            ->with('i', (request()->input('page', 1) - 1) * $dependencias->perPage());
    }

    /**
     * Show the form for creating a new resource.
     */
    public function create()
    {
        $dependencia = new Dependencia();
        return view('dependencia.create', compact('dependencia'));
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(DependenciaRequest $request)
    {
        Dependencia::create($request->validated());

        return redirect()->route('dependencias.index')
            ->with('success', 'Dependencia created successfully.');
    }

    /**
     * Display the specified resource.
     */
    public function show($id)
    {
        $dependencia = Dependencia::find($id);
        $centrosTrabajos = $dependencia->centrosTrabajos;
       
        return view('dependencia.show', compact('dependencia', 'centrosTrabajos'));
    }

    /**
     * Show the form for editing the specified resource.
     */
    public function edit($id)
    {
        $dependencia = Dependencia::find($id);

        return view('dependencia.edit', compact('dependencia'));
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(DependenciaRequest $request, Dependencia $dependencia)
    {
        $dependencia->update($request->validated());

        return redirect()->route('dependencias.index')
            ->with('success', 'Dependencia updated successfully');
    }

    public function destroy($id)
    {
        Dependencia::find($id)->delete();

        return redirect()->route('dependencias.index')
            ->with('success', 'Dependencia deleted successfully');
    }
}
