<?php

namespace App\Http\Controllers;

use App\Models\Admin;
use App\Models\Mesin;
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 mesinController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        if (Auth::user()->role == 'superadmin') {
            $data = Mesin::all();
            $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 = Mesin::join('laundry', 'laundry.id', '=', 'mesin.laundry_id')->where('laundry.brand_id', $admin_brand)->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 = Mesin::join('laundry', 'laundry.id', '=', 'mesin.laundry_id')->where('laundry.id', $laundry_id)->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 = Mesin::where('laundry_id', $laundry_id)->get();
            }
        }
        return view('mesin.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(), [
            'laundry_id' => 'required',
            'no_mesin' => 'required',
            'nama_mesin' => 'required',
        ]);
        if ($validasi->fails()) {
            return redirect()
                ->back()
                ->with('error', $validasi->errors()->first());
        }

        Mesin::create([
            'laundry_id' => $request->laundry_id,
            'no_mesin' => $request->no_mesin,
            'nama_mesin' => $request->nama_mesin
        ]);
        return redirect()
            ->route('mesin.index')
            ->with('success', 'Data Mesin Telah 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)
    {

        $user = Auth::user();
        $role = $user->role;
        $akunId = $user->id;

        if ($role == 'superadmin') {
            $data = Mesin::findOrFail($id);
            $laundry = Laundry::all();
        } elseif ($role == 'admin') {
            $admin = Admin::where('users_id', $akunId)->first();
            $admin_brand = $admin->brand_id;
            $laundry = Laundry::where('brand_id', $admin_brand)->get();
            $data = Mesin::join('laundry', 'laundry.id', '=', 'mesin.laundry_id')
                ->where('mesin.id', $id)
                ->where('laundry.brand_id', $admin_brand)
                ->firstOrFail();
        } else {
            $laundry_id = null;

            if ($role == 'pegawai') {
                $pegawai = Pegawai::where('users_id', $akunId)->first();
                $laundry_id = $pegawai->laundry_id;
            } elseif ($role == 'kepala_toko') {
                $kepala_toko = KepalaToko::where('users_id', $akunId)->first();
                $laundry_id = $kepala_toko->laundry_id;
            }

            if ($laundry_id) {
                $laundry = Laundry::where('id', $laundry_id)->get();
                $data = Mesin::join('laundry', 'laundry.id', '=', 'mesin.laundry_id')
                    ->where('mesin.id', $id)
                    ->where('laundry.id', $laundry_id)
                    ->firstOrFail();
            } else {
                abort(403, 'Unauthorized access');
            }
        }

        return view('mesin.edit', compact('data', 'laundry'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $validasi = Validator::make($request->all(), [
            'laundry_id' => 'required',
            'no_mesin' => 'required',
            'nama_mesin' => 'required',
        ]);

        if ($validasi->fails()) {
            return redirect()
                ->back()
                ->with('error', $validasi->errors()->first());
        }

        $data = Mesin::findOrFail($id);
        $data->update([
            'laundry_id' => $request->laundry_id,
            'no_mesin' => $request->no_mesin,
            'nama_mesin' => $request->nama_mesin
        ]);
        return redirect()
            ->route('mesin.index')
            ->with('success', 'Data Mesin Telah Diubah');
    }

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