<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Barang;
use App\Models\Kategori;
use App\Models\SubKategori;
use Illuminate\Http\Request;

class ItemController extends Controller
{
    public function index($idk, $ids)
    {
        $barang = Barang::where("id_sub_kategori", $ids)->get();
        $kategori = Kategori::where("id_kategori", $idk)->first();
        $sub_kategori = SubKategori::where("id_sub_kategori", $ids)->first();
        return view('admin.item.index', ['barang' => $barang, 'kategori' => $kategori, 'sub_kategori' => $sub_kategori]);
    }
    public function create(Request $request, $ids)
    {
        // return $request->all();
        Barang::create([
            'id_sub_kategori' => $ids,
            'nama_barang' => $request->nama_barang,
            'nomor_barang' => $request->nomor_barang,
            'barang_portion' => $request->barang_portion,
            'barang_portion_from' => $request->barang_portion_from,
            'barang_portion_to' => $request->barang_portion_to,
        ]);
        return redirect()->back();
    }
    public function update(Request $request, $id)
    {
        Barang::where("id_barang", $id)->update([
            // "id_kategori" => $request->id_kategori,
            "nama_barang" => $request->nama_barang,
            "nomor_barang" => $request->nomor_barang,
            "barang_portion" => $request->barang_portion,
            "barang_portion_from" => $request->barang_portion_from,
            "barang_portion_to" => $request->barang_portion_to,
        ]);
        return redirect()->back();
    }
    public function delete($id)
    {
        Barang::where('id_barang', $id)->delete();
        return redirect()->back();
    }
}
