<?php

namespace {{ namespace }};

use {{ namespacedModel }};
use {{ rootNamespace }}Http\Controllers\Controller;
{{ requestUses }}

class {{ class }} extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @param {{ filterRequest }} $request
     * @return \Illuminate\View\View
     */
    public function index({{ filterRequest }} $request)
    {
				$validated = $request->validated();
        $data = [];
        $data['items'] = {{ model }}::get_data($validated);
				$data = array_merge($data, {{ model }}::load_index());

        return view('{{ tableName }}.index', $data);
    }

    public function get_data()
    {
        $data = [];
        $data['records'] = {{ model }}::get_data([]);
        $data['total_records'] = count($data['records']);

        return $data;
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\View\View
     */
    public function create()
    {
        $data = [];
				$data = array_merge($data, {{ model }}::load_create());

        return view('{{ tableName }}.create_edit', $data);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param {{ storeRequest }} $request
     * @return \Illuminate\Http\RedirectResponse
     */
    public function store({{ storeRequest }} $request)
    {
        $validated = $request->validated();

        {{ model }}::create($validated);

        return redirect()->route('{{ tableName }}.index');
    }

    /**
     * Display the specified resource.
     *
     * @param {{ model }} ${{ modelVariable }}
     * @return \Illuminate\View\View
     */
    public function show({{ model }} ${{ modelVariable }})
    {
        $data = [];
        $data['item'] = ${{ modelVariable }};
				$data['fields'] = (new {{ model }}())->getFillable();

        return view('{{ tableName }}.show', $data);
    }

		/**
		 * Returns the resource in JSON format.
		 *
		 * @param ModelType $modelVariable
		 * @return string
		 */
    public function load({{ model }} ${{ modelVariable }})
    {
        return ${{ modelVariable }}->toJson();
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param {{ model }} ${{ modelVariable }}
     * @return \Illuminate\View\View
     */
    public function edit({{ model }} ${{ modelVariable }})
    {
        $data = [];
        $data['item'] = ${{ modelVariable }};

        // Load data for relationships
				$data = array_merge($data, {{ model }}::load_edit());

        return view('{{ tableName }}.create_edit', $data);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param {{ updateRequest }} $request
     * @param {{ model }} ${{ modelVariable }}
     * @return \Illuminate\Http\RedirectResponse
     */
    public function update({{ updateRequest }} $request, {{ model }} ${{ modelVariable }})
    {
        $validated = $request->validated();

        ${{ modelVariable }}->update($validated);

        return redirect()->route('{{ tableName }}.index');
    }

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

        return redirect()->route('{{ tableName }}.index');
    }
}
