<?php

namespace App\Http\Controllers;

use App\Models\BookingModel;
use Illuminate\Http\Request;

class CekStatusController extends Controller
{
    /**
     * Display a listing of the resource.
     */
    public function index()
    {
        return view('user.pages.cek_status.index');
    }

    /**
     * Show the form for creating a new resource.
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     */
    public function show(string $id)
    {
        $data = BookingModel::where('kode_unik', $id)->first();
        if($data != null) {
            if($data->status_tes == '1' && $data->unix_expired_time < time()) { // Lebih dari exp
                $data->update([
                    'status_tes' => '0'
                ]);
            } else if($data->status_tes == '1' && time() < ($data->unix_expired_time - 86399)) { // Kurang dari exp 00.00
                $data->update([
                    'status_tes' => '0'
                ]);
            } else if($data->status_tes == '0' && time() > ($data->unix_expired_time - 86399) && time() < ($data->unix_expired_time)) {
                $data->update([
                    'status_tes' => '1'
                ]);
            }
            return response()->json([
                'status' => true,
                'msg' => 'Data ditemukan (' . $id . ')',
                'id_booking' => $data->id_booking,
                'status_data' => $data->status_tes,
                'nama_lengkap' => $data->nama_lengkap,
                'kode_unik' => $data->kode_unik,
                'tgl_booking' => $data->tgl_booking,
                'start_test' => date('H:i:s', $data->unix_expired_time - 86399),
                'end_test' => date('H:i:s', $data->unix_expired_time),
                'deskripsi_tambahan' => $data->deskripsi_tambahan,
            ]);
        } else {
            return response()->json([
                'status' => false,
                'msg' => 'Data tidak ditemukan (' . $id . ')'
            ]);
        }
    }

    /**
     * Show the form for editing the specified resource.
     */
    public function edit(string $id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(Request $request, string $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy(string $id)
    {
        //
    }
}
