<?php

namespace App\Http\Controllers\Api\V1;

use Carbon\Carbon;
use App\Http\Controllers\Controller;
use App\Models\CardMember;
use App\Models\TransaksiParkir as TransPark;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;


class TransaksiParkir extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $query = $request->query('find');

        if ($query) {
                
            $data = $request->query('find');
            Log::info($data);
            
            $card = CardMember::where('no_card', Str::upper($data))->first();
            
            if (!$card) {
                $trans = TransPark::where('no_pol', Str::upper($data))->where('status', 1)->first();
                
                if ($trans) {
                    return response()->json([
                        'status' => 'failed',
                        'desc' => 'Nomor Polisi/Kartu masih di dalam'
                    ]);
                }
            } else {
                Log::info($card);
                $validMember = DB::table('mergetransaksistikerdetail')->where('no_id', $card->notrans)->first();
                if (!$validMember) {
                    return response()->json([
                        'status' => 'ok'
                    ]);
                }
            }

            return response()->json([
                'status' => 'failed',
                'desc' => 'Tidak ditemukan data'
            ]);
            
        } else {
            return response()->json([
                'status' => 'failed',
                'desc' => 'parameter tidak ditemukan'
            ]);
        }
    
    }
        
        
        
    /**
     * 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)
    {
        Log::info($request->no_pol);
        $gateID = $request->id_pintu_masuk  ?? '00';
        $dateNow = Carbon::now()->format('Y-m-d H:i:s');
        $idTrans = TransPark::select('id')->orderBy('id', 'desc')->first();
        if (!$idTrans) {
            $newTrans = 0;
        } else {
            Log::info('data sebelumnya ' . $idTrans->id);
            $newTrans = $idTrans->id + 1;
            Log::info('setelah dijumlah ' . $newTrans);
        }
        $trans = str_pad($newTrans, 4, '0', STR_PAD_LEFT);
        Log::info($newTrans);

        Log::info('Tanggal hari ini : ' . $dateNow);
        $transaksi = TransPark::create([
            'id' => $gateID . $trans,
            'no_pol' => $request->no_pol ?? $gateID . $trans . Carbon::now()->format('YmdHis'),
            'id_kendaraan' => 'A',
            'id_pintu_masuk' => $request->id_pintu_masuk ?? $gateID,
            'status' => 1,
            'status_transaksi' => $request->status_transaksi,
            'waktu_masuk' => $request->waktu_masuk ?? $dateNow,
            'tanggal' => $request->waktu_masuk ?? $dateNow,
        ]);

        return $transaksi;
    }

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