<?php

namespace App\Http\Controllers;

use App\Models\Note;
use App\Models\Project;
use Illuminate\Http\Request;

class NoteController extends Controller
{
    public function createOrEdit(Note $note)
    {
        $request = request();
        $projectId = $request->get('project_id');
        $project = null;
        if($projectId) {
            $project = Project::find($projectId);
        }
        return view('note.edit', ['note' => $note, 'project' => $project]);
    }

    public function create()
    {
        return $this->createOrEdit(app(Note::class));
    }

    public function edit($id)
    {
        return $this->createOrEdit(Note::find($id));
    }

    public function store(Request $request)
    {
        $note = Note::create($request->all());
        return redirect()->route('adm.project.show', [$note->project->id]);
    }

    public function update($id, Request $request)
    {
        $note = Note::find($id);
        $note->fill($request->all());
        $note->save();
        return redirect()->route('adm.project.show', [$note->project->id]);
    }

    public function destroy($id)
    {
        Note::find($id)->delete();
        return response()->json([]);
    }
}
