<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Tanaman;
use App\Models\Kategori;

class TanamanController extends Controller
{
    /**
     * Display a listing of the resource.
     */
   public function index()
    {
        $kategoris = Kategori::all(); // Mengambil semua data kategori untuk ditampilkan di view index tanaman
        // Mengambil data tanaman beserta nama kategorinya
        return view('admin.tanaman.index', compact('kategoris'));
    }

    function data() 
    {
         $data = Tanaman::with('kategori')->latest()->get();
        return datatables()->of($data)
            ->addIndexColumn()
            ->addColumn('kategori', function($data) {
                return $data->kategori->nama_kategori;
            })
            ->addColumn('gambar', function($data){
    if($data->gambar) {
        $url = asset('storage/tanaman/' . $data->gambar);
        return '<img src="'.$url.'" alt="Foto" width="70" class="img-thumbnail">';
    }
    return '<span class="badge bg-secondary">Tidak ada foto</span>';
})
            ->addColumn('aksi', function($data) {
                return '
                    <button type="button" class="btn btn-info btn-sm" onclick="editForm(`'. route('tanaman.update', $data->id) .'`)"><i class="fa fa-pencil"></i></button>
                    <button type="button" class="btn btn-danger btn-sm" onclick="deleteData(`'. route('tanaman.destroy', $data->id) .'`)"><i class="fa fa-trash"></i></button>
                ';
            })
            ->rawColumns(['aksi', 'gambar'])
            ->make(true);
    }
    public function create()
    {
        // Mengambil data kategori untuk dimasukkan ke pilihan dropdown (Select)
        $kategori = Kategori::all();
        return view('admin.tanaman.create', compact('kategori'));
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $request)
    {
        if ($request->hasFile('gambar')) {
            $file = $request->file('gambar');
            $nama_file = time() . "_" . $file->getClientOriginalName();
            
            // Simpan file ke folder storage/app/public/tanaman
            $file->storeAs('public/tanaman', $nama_file);
            
            // Masukkan nama file ke dalam array data untuk disimpan ke database
            $data['gambar'] = $nama_file;
            }
            Tanaman::create($request->all());
        return back()->with('success', 'Tanaman berhasil ditambahkan');
    }

    /**
     * Display the specified resource.
     */
    public function show(string $id)
    {
        $data=Tanaman::find($id);
        return response()->json($data);
    }

    /**
     * Show the form for editing the specified resource.
     */
    public function edit(string $id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(Request $request, string $id)
    {
        $tanaman = Tanaman::find($id);
        $tanaman->update($request->all());
        return back()->with('success', 'Tanaman berhasil diubah');
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy(string $id)
    {
        $tanaman = Tanaman::find($id);
        $tanaman->delete();
        return back()->with('success', 'Tanaman berhasil dihapus');
    }
}
