<?php

namespace {{ namespace }};

use {{ namespacedModel }};
use {{ rootNamespace }}Http\Controllers\Controller;
use {{ namespacedRequests }}
use App\Helper\Upload;
use Illuminate\Http\Request;
use Yajra\DataTables\Facades\DataTables;

class {{ class }} extends Controller
{
    public function index(Request $request)
    {
        if ($request->ajax()) {
            ${{ model }}s = {{ model }}::query();

            return DataTables::of(${{ model }}s)
                ->addColumn('action', function (${{ model }}) {
                    return '<a href="' . route('{{ modelVariable }}.show', ${{ model }}->id) . '"><i class="fas fa-eye"></i></a>
                            <a href="' . route('{{ modelVariable }}.edit', ${{ model }}->id) . '"><i class="fa-solid fa-pen-to-square"></i></a>
                            <form method="POST" action="' . route('{{ modelVariable }}.destroy', ${{ model }}->id) . '">
                                ' . csrf_field() . '
                                <input name="_method" type="hidden" value="DELETE">
                                <button type="button" class="btn btn-flat show_confirm" data-toggle="tooltip" title="Delete">
                                    <i class="fa-solid fa-eraser"></i>
                                </button>
                            </form>';
                })
                ->addColumn('image', function (${{ model }}) {
                    return '<img style="height: 100px" src="' . ${{ model }}['image'] . '" alt="IMG" width="150">';
                })
                ->editColumn('display', function (${{ model }}) {
                    if (${{ model }}->display) {
                        return '<label data-id="' . ${{ model }}->id . '" class="switch toggleswitch bg-dark"><input id="checkbox' . ${{ model }}->id . '" type="checkbox" checked ><span class="slider"></span></label>';
                    } else {
                        return '<label data-id="' . ${{ model }}->id . '" class="switch toggleswitch bg-dark"><input id="checkbox' . ${{ model }}->id . '" type="checkbox" ><span class="slider"></span></label>';
                    }
                })
                ->editColumn('foreign_id', function (${{ model }}) {
                    return '<a style="color: blue;" href="' . route("category.show", ${{ model }}->Category['id']) . '">' . mb_strimwidth(${{ model }}->Category['name_' . lang()], 0, 50, "...") . '</a>';
                })
                ->escapeColumns('action', 'image')
                ->addIndexColumn()
                ->filter(function ($instance) use ($request) {

                    if ($request->get('time_from') && !is_null($request->get('time_from'))) {
                        $instance->where('created_at', '>=' ,  $request->get('time_from'));
                    }
                    if ($request->get('time_to') && !is_null($request->get('time_to'))) {
                        $instance->where('created_at', '<=' ,  $request->get('time_to'));
                    }

                })
                ->toJson();
        }
        return view('admin.{{ modelVariable }}s.index');
    }

    public function create()
    {
        return view('admin.{{ modelVariable }}s.create');
    }

    public function store({{ storeRequest }} $request)
    {
        ${{ model }} = {{ model }}::create(['image' => Upload::uploadImage($request['images'], '{{ model }}s'), ${{ model }}['id'] ] + $request->validated());
        toastr()->success(__('messages.addedSuccessfully'));
        return redirect()->back();
    }

    public function show({{ model }} ${{ model }})
    {
        return view('admin.{{ modelVariable }}s.show', compact('{{ model }}'));
    }

    public function edit({{ model }} ${{ model }})
    {
        return view('admin.{{ modelVariable }}s.edit', compact('{{ model }}'));
    }

    public function update({{ updateRequest }} $request, {{ model }} ${{ model }})
    {
        if ($request->hasFile('images')) {
            ${{ model }}->update( ['image' => Upload::uploadImage($request['images'] , '{{ model }}s'), ${{ model }}['id'] ] + $request->validated());
        }else{
            ${{ model }}->update($request->validated());
        }

        toastr()->success(__('messages.updatedSuccessfully'));
        return redirect()->back();
    }

    public function destroy({{ model }} ${{ model }})
    {
        ${{ model }}->delete();
        toastr()->success(__('messages.DeletedSuccessfully'));
        return redirect()->back();
    }

    public function switch () {
        ${{ model }} = {{ model }}::findOrFail(request('id'));
        ${{ model }}->display = request('display');
        ${{ model }}->save();
    }
}
