<?php

namespace App\Http\Controllers;

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

class kepalaTokoController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */

    public function index()
    {
        if (Auth::user()->role == 'superadmin') {
            $laundry = Laundry::all();
            $data = User::where('role', 'kepala_toko')->get();
            $kepala_toko = [];
            foreach ($data as $d) {
                $peg = KepalaToko::where('users_id', $d->id)->first();
                $kepala_toko[] = $peg;
            }
        } elseif (Auth::user()->role == 'admin') {
            $akun = Auth::user()->id;
            $admin = Admin::where('users_id', $akun)->first();
            $admin_brand = $admin->brand_id;
            $kepala_toko = KepalaToko::join('laundry', 'laundry.id', '=', 'kepala_toko.laundry_id')
                ->where('laundry.brand_id', $admin_brand)
                ->get(['kepala_toko.*', 'kepala_toko.status as pegawai_status', 'laundry.status as laundry_status']);
            // dd($kepala_toko);
            $laundry = Laundry::where('brand_id', $admin->brand_id)->first();
        } else {
            if (Auth::user()->role == 'kepala_toko') {
                $pegawai_on = KepalaToko::where('users_id', Auth::user()->id)->first();
                $kepala_toko = KepalaToko::where('laundry_id', $pegawai_on->laundry_id)->get();
                $laundry = Laundry::where('id', $pegawai_on->laundry_id)->first();
            }
        }
        return view('kepala_toko.index', compact('laundry', 'kepala_toko'));
    }

    /**
     * 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(), [
            'username' => 'required',
            'password' => 'required',
            'email' => 'required',
            'nama' => 'required',
            'alamat' => 'required',
        ]);

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

        $user = User::create([
            'username' => $request->input('username'),
            'password' => bcrypt($request->input('password')),
            'email' => $request->input('email'),
            'role' => 'kepala_toko',
        ]);
        $user->save();

        $kepala_toko = new \App\Models\KepalaToko();
        $kepala_toko->users_id = $user->id;
        $kepala_toko->laundry_id = $request->input('laundry_id');
        $kepala_toko->nama = $request->input('nama');
        $kepala_toko->alamat = $request->input('alamat');

        if ($request->hasFile('foto')) {
            $file = $request->file('foto');
            $extension = $file->getClientOriginalExtension();
            $filename = 'profile-' . time() . '.' . $extension;
            $file->storeAs('public/img', $filename);
            $kepala_toko->foto = $filename;
        } else {
            $kepala_toko->foto = 'default.png';
        }
        $kepala_toko->no_telp = $request->input('no_telp');
        $kepala_toko->save();

        return redirect('/kepala_toko')->with('success', 'Data 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)
    {
        if (Auth::user()->role == 'superadmin') {
            $akun = User::find($id);
            $data = KepalaToko::where('users_id', $id)->first();
            $laundry = Laundry::all();
        } elseif (Auth::user()->role == 'admin') {
            $akun = User::find($id);
            $data = KepalaToko::where('users_id', $id)->first();
            $admin = Admin::where('users_id', auth()->user()->id)->first();
            $admin_brand = $admin->brand_id;
            $laundry = Laundry::where('brand_id', $admin_brand)->get();
        } else {
            $akun = User::find($id);
            $data = KepalaToko::where('users_id', $id)->first();
            $laundry = [];
            foreach ($data as $d) {
                $peg = KepalaToko::where('users_id', $d->id)->first();
                $laundry[] = $peg;
            }
        }
        return view('kepala_toko.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)
    {
        $kepala_toko = KepalaToko::find($id);
        if (!$kepala_toko) {
            return redirect()->back()->with('error', 'KepalaToko tidak ditemukan.');
        }

        // Update data KepalaToko
        $kepala_toko->laundry_id = $request->input('laundry_id');
        $kepala_toko->nama = $request->input('nama');
        $kepala_toko->alamat = $request->input('alamat');
        $kepala_toko->no_telp = $request->input('no_telp');

        if ($request->hasFile('foto')) {
            $file = $request->file('foto');
            $extension = $file->getClientOriginalExtension();
            $filename = 'profile-' . time() . '.' . $extension;
            $file->storeAs('public/img', $filename);
            $kepala_toko->foto = $filename;
        }
        $kepala_toko->update();

        $akun = User::find($kepala_toko->users_id);
        if (!$akun) {
            return redirect()->back()->with('error', 'Akun pengguna tidak ditemukan.');
        }

        // Update data User
        $akun->username = $request->input('username');
        $akun->email = $request->input('email');


        if ($request->input('password')) {
            $akun->password = bcrypt($request->input('password'));
        }
        $akun->update();

        return redirect('/kepala_toko')->with('success', 'Data Berhasil Diubah');
    }

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

    public function aktifasi($id)
    {
        $kepala_toko = KepalaToko::find($id);
        if ($kepala_toko->status == 'aktif') {
            $kepala_toko->status = 'tidak aktif';
        } else {
            $kepala_toko->status = 'aktif';
        }
        $kepala_toko->update();
        return redirect()->route('kepala_toko.index')->with('success', 'Status pegawai berhasil diubah');
    }
}
