<?php

namespace App\Http\Controllers;

use App\Models\TypeItems; // Asumsikan ada model TypeItem (misal: "Makanan Berat", "Minuman Dingin", "Dessert")
use Illuminate\Http\Request;

class TypeItemsController extends Controller
{
    /**
     * Menampilkan daftar semua TypeItems.
     */
    public function index()
    {
        $typeItems = TypeItems::all();
        return view('pages.back.typeitems.index', compact('typeItems'));
    }

    /**
     * Menampilkan form untuk membuat TypeItem baru.
     */
    public function create()
    {
        return view('pages.back.typeitems.create');
    }

    /**
     * Menyimpan TypeItem baru ke database.
     */
    public function store(Request $request)
    {
        $validatedData = $request->validate([
            'name' => 'required|string|max:255|unique:type_items,name', // Nama type item harus unik
            'description' => 'nullable|string',
        ]);

        TypeItems::create($validatedData);

        return redirect()->route('pages.back.typeitems.index')->with('success', 'Tipe Item berhasil ditambahkan!');
    }

    /**
     * Menampilkan detail satu TypeItem tertentu.
     */
    public function show(TypeItem $typeItem)
    {
        return view('pages.back.typeitems.show', compact('typeItem'));
    }

    /**
     * Menampilkan form untuk mengedit TypeItem yang sudah ada.
     */
    public function edit(TypeItem $typeItem)
    {
        return view('pages.back.typeitems.edit', compact('typeItem'));
    }

    /**
     * Memperbarui data TypeItem di database.
     */
    public function update(Request $request, TypeItem $typeItem)
    {
        $validatedData = $request->validate([
            'name' => 'required|string|max:255|unique:type_items,name,' . $typeItem->id, // Kecualikan ID type item saat ini
            'description' => 'nullable|string',
        ]);

        $typeItem->update($validatedData);

        return redirect()->route('pages.back.typeitems.index')->with('success', 'Tipe Item berhasil diperbarui!');
    }

    /**
     * Menghapus TypeItem dari database.
     */
    public function destroy(TypeItem $typeItem)
    {
        $typeItem->delete();
        return redirect()->route('pages.back.typeitems.index')->with('success', 'Tipe Item berhasil dihapus!');
    }
}
