<?php

namespace App\Http\Controllers;

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

class DataSoalController extends Controller
{

    /**
     * Display data for javascript ajax
     */
    public function getData() {
        $data = DataSoalModel::get();
        return response()->json([
            'status' => true,
            'data' => $data->toArray()
        ]);
    }
    /**
     * Display a listing of the resource.
     */
    public function index()
    {
        return view('admin.pages.data_soal.index');
    }

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

    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $request)
    {
        DataSoalModel::create([
            'pertanyaan' => $request->pertanyaan,
            'jawaban_a' => $request->jawaban_a,
            'jawaban_b' => $request->jawaban_b,
            'jawaban_c' => $request->jawaban_c,
            'jawaban_d' => $request->jawaban_d,
            'jawaban_e' => $request->jawaban_e,
            'jawaban_benar' => $request->jawaban_benar,
        ]);

        return response()->json([
            'status' => true,
            'msg' => 'Data berhasil ditambahkan'
        ]);
    }

    /**
     * Display the specified resource.
     */
    public function show(string $id)
    {
        $data = DataSoalModel::where('id_soal', $id)->get();
        return response()->json([
            'status' => true,
            'data' => $data[0]
        ]);
    }

    /**
     * Show the form for editing the specified resource.
     */
    public function edit(string $id)
    {
        $data = DataSoalModel::where('id_soal', $id)->get();
        return response()->json([
            'status' => true,
            'data' => $data[0]
        ]);
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(Request $request, string $id)
    {
        $data = DataSoalModel::where('id_soal', $id)->update([
            'pertanyaan' => $request->pertanyaan,
            'jawaban_a' => $request->jawaban_a,
            'jawaban_b' => $request->jawaban_b,
            'jawaban_c' => $request->jawaban_c,
            'jawaban_d' => $request->jawaban_d,
            'jawaban_e' => $request->jawaban_e,
            'jawaban_benar' => $request->jawaban_benar,
        ]);

        return response()->json([
            'status' => true,
            'msg' => 'Data berhasil di edit'
        ]);
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy(string $id)
    {
        $data = DataSoalModel::where('id_soal', $id)->delete();

        return response()->json([
            'status' => true,
            'msg' => 'Data berhasil di hapus'
        ]);
    }
}
