<?php

namespace App\Http\Controllers\Apps;

use App\Http\Controllers\Controller;
use App\Models\JenisMobil;
use App\Models\TransaksiParkir;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;

class ModifiedTransaksi extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        // dd($id);
        return view ('apps.modified-trans', ['data' => null]);
    }

    /**
     * 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)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        Log::info(json_encode($id));
        $vehicle = JenisMobil::all();
        $data = TransaksiParkir::select('no_pol','waktu_masuk','waktu_keluar','jm.nama as vehicle','id_kendaraan','np.nama as gateIn', 'id_pintu_masuk')
            ->leftJoin('jenis_mobil as jm', 'jm.id', '=', 'transaksi_parkir.id_kendaraan')
            ->leftJoin('nama_pos as np', 'np.id', '=', 'transaksi_parkir.id_pintu_masuk')
            ->where('no_pol', $id)
                ->where('status', 1)
                    ->first();
            Log::info(json_encode($data));
        return view('apps.modified-trans', [
            'data' => $data,
            'vehicle' => $vehicle
            ]);
    }

    /**
     * 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)
    {
        $request->validate([
            'id_kendaraan_baru' => 'required|exists:jenis_mobils,id', // validasi agar ID ada di database
        ]);

        $no_pol=$id;
        // Ambil data transaksi berdasarkan ID di URL
        $transaksi = TransaksiParkir::findOrFail($no_pol);

        // Update jenis kendaraan
        $transaksi->id_kendaraan = $request->id_kendaraan_baru;
        $transaksi->save();
    }

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