<?php

namespace App\Http\Controllers;

use App\Models\Admin;
use App\Models\Barang;
use App\Models\Laundry;
use App\Models\Pegawai;
use App\Models\KepalaToko;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Validator;

class barangController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        if (Auth::user()->role == 'superadmin') {
            $data = Barang::where('is_bahan_baku', 1)->get();
            $laundry = Laundry::all();
        } elseif (Auth::user()->role == 'admin') {
            $akun = Auth::user()->id;
            $admin = Admin::where('users_id', $akun)->first();
            $admin_brand = $admin->brand_id;
            $laundry = Laundry::where('brand_id', $admin_brand)->get();
            $data = Barang::join('laundry', 'laundry.id', '=', 'barang.id_laundry')
                ->where('laundry.brand_id', $admin_brand)
                ->where('is_bahan_baku', 1)->get();
        } else {
            if (Auth::user()->role == 'pegawai') {
                $akun = Auth::user()->id;
                $pegawai = Pegawai::where('users_id', $akun)->first();
                $laundry_id = $pegawai->laundry_id;
                $laundry = Laundry::where('id', $laundry_id)->get();
                $data = Barang::where('id_laundry', $laundry_id)
                    ->where('is_bahan_baku', 1)->get();
            } else {
                $akun = Auth::user()->id;
                $kepala_toko = KepalaToko::where('users_id', $akun)->first();
                $laundry_id = $kepala_toko->laundry_id;
                $laundry = Laundry::where('id', $laundry_id)->get();
                $data = Barang::where('id_laundry', $laundry_id)
                    ->where('is_bahan_baku', 1)
                    ->orderBy('id', 'desc')->get();
            }
        }
        return view('barang.index', compact('data', 'laundry'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $validasi = Validator::make($request->all(), [
            'id_laundry' => 'required',
            'nama_barang' => 'required',
            'sku' => 'required',
            'merek' => 'required',
        ]);

        if ($validasi->fails()) {
            return redirect()->back()->with('error', $validasi->errors()->first());
        } else {
            $data = new Barang();
            $data->id_laundry = $request->id_laundry;
            $data->nama_barang = $request->nama_barang;
            $data->sku = $request->sku;
            $data->merek = $request->merek;
            $data->is_bahan_baku = 1;
            $data->volume_total = 0;


            $data->save();
            return redirect()->back()->with('success', 'Data Barang Berhasil Ditambahkan');
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }

    public function barangnya($id)
    {
        $data = Barang::find($id);
        if ($data) {
            return response()->json($data);
        } else {
            return response()->json(['error' => 'Barang tidak ditemukan'], 404);
        }
    }
}
