<?php

namespace App\Http\Controllers\Admin\Botanic;

use App\Datatables\Botanic\SpeciesDataTable;
use App\Repositories\Botanic\Species;
use Illuminate\Http\Request;

class SpecieController extends Controller
{
    public function index(SpeciesDataTable $dataTable)
    {
        return $dataTable->render('Admin.Botanic.Species.list');
    }

    public function getOptions()
    {
        return response()->json(['0' => ''] + Species::getOptions());
    }

    public function create()
    {
        $data = Species::init();

        return view('Admin.Botanic.Species.create', $data);
    }

    public function store(Request $request)
    {
        $data = $request->all();
        $ret = Species::storeFull($data);

        return redirect()->route('Admin.Botanic.Species.index');
    }

    public function edit($id)
    {
        $data = Species::init();
        $data['specie'] = Species::getFull($id);

        return view('Admin.Botanic.Species.edit', $data);
    }

    public function destroy($id)
    {
        return Species::destroy($id);
    }

    public function getImages(Request $request, $id = false, $can_edit = true)
    {
        $id = $id ? $id : $request->input('id');
        $data = [
            'images' => Species::getImages($id),
            'can_edit' => $can_edit,
        ];

        return view('components.uploader.mini-gallery-items', $data);
    }

    public function deleteImage(Request $request)
    {
        $id = $request->input('id');
        $index = $request->input('index');

        return Species::deleteImage($id, $index);
    }
}
