<?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\DB;
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,
            'cameraText' => $cameraText ?? 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));
        // $char = strlen($id);

        // if($char = 10) {
        //     $condition = where('no_pol','LIKE',"'%$id%'");
        // } else (
        //     $condition = where('no_pol', $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','jm.nama as vehiclename')
        //     ->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
        //     ]);
        Log::info('ID: ' . $id);

        $vehicle = JenisMobil::all();

        $query = TransaksiParkir::select(
            'no_pol',
            'waktu_masuk',
            'waktu_keluar',
            'jm.nama as vehicle',
            'id_kendaraan',
            'np.nama as gateIn',
            'id_pintu_masuk',
            'jm.nama as vehiclename'
        )
        ->leftJoin('jenis_mobil as jm', 'jm.id', '=', 'transaksi_parkir.id_kendaraan')
        ->leftJoin('nama_pos as np', 'np.id', '=', 'transaksi_parkir.id_pintu_masuk')
        ->where('status', 1);

        // cek panjang id
        if (strlen($id) == 10) {
            $query->where('no_pol', 'LIKE', "%$id%");
        } else {
            $query->where('no_pol', $id);
        }

        $data = $query->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_mobil,id', // validasi agar ID ada di database
        // ]);

        $no_pol=$id;
        Log::info($request->all());
        Log::info($no_pol);
        // Ambil data transaksi berdasarkan ID di URL
        // $transaksi = TransaksiParkir::findOrFail($no_pol);

        // // Update jenis kendaraan
        // $transaksi->id_kendaraan = $request->id_kendaraan_baru;
        // $transaksi->save();
        // TransaksiParkir::where('no_pol', $id)
        // ->update([
        //     'id_kendaraan' => $request->id_kendaraan_baru
        // ]);
        DB::table('transaksi_parkir')
            ->where('no_pol', $id)
            ->update([
            'id_kendaraan' => $request->id_kendaraan_baru
        ]);

        // return response()->json([
        //     'status' => 'success',
        //     'message' => 'data berhasil di ubah'
        // ]);
        return redirect()
            ->route('apps.index')
            ->with('success', 'Data berhasil diubah');
    }

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

    public function scan(Request $request)
{
    $code = $request->code;

    $transaksi = TransaksiParkir::where('no_pol','LIKE' . "%{$code}%")->first();

    if (!$transaksi) {
        return response()->json([
            'status' => 'error',
            'message' => 'Tiket tidak ditemukan tidak terdaftar'
        ]);
    }

    return response()->json([
        'status' => 'success',
        'data' => $transaksi
    ]);
}
}
