<?php

namespace App\Http\Controllers;

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

class HasilTesController extends Controller
{
    public function fetchData() {
        $data_booking = BookingModel::orderBy('updated_at', 'desc')->get();
        $result = [];
        foreach($data_booking as $booking) {
            $data_test = TestSessionsModel::where('id_booking', $booking->id_booking)->first();
            if($data_test != null) {
                $result[] = [
                    'id_test_session' => $data_test->id_test_sessions,
                    'nama_lengkap' => $booking->nama_lengkap,
                    'tgl_tes' => $data_test->created_at->format('d-m-Y'),
                    'jam_tes' => $data_test->created_at->format('H:i'),
                    'kode_booking' => $booking->kode_unik,
                    'status_tes' => $booking->status_tes
                ];
            }
        }

        return response()->json([
            'status' => true,
            'data' => $result
        ]);
    }
    /**
     * Display a listing of the resource.
     */
    public function index()
    {
        return view('admin.pages.data_hasil_kecemasan.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 = TestSessionsModel::where('id_test_sessions', $id)->first();

        if($data != null) {
            $data_booking = BookingModel::where('id_booking', $data->id_booking)->first();
            return response()->json([
                'status' => true,
                'nama' => $data_booking->nama_lengkap
            ]);
        }

        return response()->json([
            'status' => false,
            'msg' => 'Data tidak ditemukan!'
        ]);
    }

    /**
     * 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)
    {
        $data = TestSessionsModel::where('id_test_sessions', $id)->first();
        if($data != null) {
            $data_booking = BookingModel::where('id_booking', $data->id_booking)->first();
            $data_booking->update([
                'status_tes' => '0'
            ]);
            return response()->json([
                'status' => true,
                'msg' => 'Data berhasil dinonaktifkan'
            ]);
        }

        return response()->json([
            'status' => false,
            'msg' => 'Data tidak ditemukan!'
        ]);
    }
}
