<?php

namespace App\Http\Controllers;

use App\Models\Concepto;
use App\Models\Plane;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use Illuminate\View\View;

use function Symfony\Component\Clock\now;

class ConceptoController extends Controller
{

    public function __construct() {
        $this->middleware('can:concepto.index')->only('index','show');
        $this->middleware('can:concepto.create')->only('create','store');
        $this->middleware('can:concepto.edit')->only('edit','update');
        $this->middleware('can:concepto.destroy')->only('destroy');
    }


    public function index(): View
    {
        $conceptos = Concepto::where('tipo','=','3')->get();
        return view('concepto.index',compact('conceptos'));
    }

    public function create(): View
    {
        return view('concepto.create');
    }

    public function store(Request $request)
    {
        if($request->plane != 0){
            $concepto =Concepto::create($request->except(['plane']));
            $concepto->planConceptos()->sync($request->plane);
            $plane= Plane::where('id','=',$request->plane)->first();
            return redirect()->route('plane.edit',compact('plane'))->with('info','Se anexó el concepto');
        }else{
            $concepto =Concepto::create([
                'name' => $request->name,
                'fecha_pago' => date('Y-m-d'),
                'monto' => 0,
                'recargo' => 0,
                'tipo' => 3
            ]);
            return redirect()->route('concepto.edit',compact('concepto'))->with('info','Se creó el concepto');

        }

    }
    public function edit(Concepto $concepto)
    {

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

    public function update(Request $request,Concepto $concepto)
    {
        if($request->plane != 0 ){
             $concepto->update($request->except(['plane']));
        $concepto->planConceptos()->sync($request->plane);
        $plane= Plane::where('id','=',$request->plane)->first();
        return redirect()->route('plane.edit',compact('plane'))->with('info','Se modificó el concepto');

        }else{
            $concepto->update([
                'name' => $request->name,
                'fecha_pago' => date('Y-m-d'),
                'monto' => 0,
                'recargo' => 0,
                'tipo' => 3
            ]);
        return redirect()->route('concepto.edit',compact('concepto'))->with('info','Se modificó el concepto');

        }

       }

    public function show(Concepto $concepto )
    {
        return view('concepto.show',compact('concepto'));
    }

    public function destroy(Concepto $concepto,Request $request)
    {

        if($request->plane != 0){
            $plane=$request->plane;
            $concepto->planConceptos()->detach();
            $concepto->delete();
            session()->flash('swal',[
                'icon'=>'success',
                'title'=>'Eliminado!',
                'text'=>'Se ha eliminado el registro correctamente'
            ]);
            return redirect()->route('plane.edit',compact('plane'));
        }else{
            $concepto->delete();
            session()->flash('swal',[
                'icon'=>'success',
                'title'=>'Eliminado!',
                'text'=>'Se ha eliminado el registro correctamente'
            ]);
            return redirect()->route('concepto.index');
        }



    }
}
