<?php

namespace App\Http\Controllers;

use App\Models\AlternatifSolusi;
use App\Models\CaseStudy;
use App\Models\Choice;
use App\Models\Node;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\URL;

class AlternatifSolusiController extends Controller
{
    public function index($id, Request $request)
    {
        if ($id == "new") {
            $data = "new";
            $case_id = Crypt::decrypt($request->case);
            $pilihan = Choice::where('case_study_id', $case_id)->get();
            $nodes = "";
            $case = CaseStudy::find($case_id);
            // dd($data);
            return view('admin.alternatif_solusi.index', compact('data', 'pilihan', 'nodes', 'case'));
        } else {
            $data = "";
            $value = AlternatifSolusi::find(Crypt::decrypt($id));
            $case_id = $value->case_study_id;
            $pilihan = Choice::where('case_study_id', $case_id)->get();
            $nodes = "";
            $case = CaseStudy::find($case_id);
            return view('admin.alternatif_solusi.index', compact('data', 'pilihan', 'nodes', 'case', 'value'));
        }
    }

    public function new(Request $request)
    {
        // dd($request->all());
        if ($request->idnya == "") {
            $store = [
                "case_study_id" => $request->case_study_id,
                "label" => $request->labelNode,
                "pertanyaan_a" => $request->pilihan_a,
                "score_a" => $request->nilai_a,
                "pertanyaan_b" => $request->pilihan_b,
                "score_b" => $request->nilai_b,
                "pertanyaan_c" => $request->pilihan_c,
                "score_c" => $request->nilai_c,
                "pertanyaan_d" => $request->pilihan_d,
                "score_d" => $request->nilai_d,
                "feedback_benar" => $request->feedback_benar,
                "feedback_salah" => $request->feedback_salah,
                "mulai" => $request->mulai,
                "selesai" => $request->selesai
            ];
            $data = AlternatifSolusi::create($store);
            return redirect()->to(URL::to('/study-kasus/alternatif-solusi/' . Crypt::encrypt($data->id)));
        } else {
            $store = [
                "case_study_id" => $request->case_study_id,
                "label" => $request->labelNode,
                "pertanyaan_a" => $request->pilihan_a,
                "score_a" => $request->nilai_a,
                "pertanyaan_b" => $request->pilihan_b,
                "score_b" => $request->nilai_b,
                "pertanyaan_c" => $request->pilihan_c,
                "score_c" => $request->nilai_c,
                "pertanyaan_d" => $request->pilihan_d,
                "score_d" => $request->nilai_d,
                "feedback_benar" => $request->feedback_benar,
                "feedback_salah" => $request->feedback_salah,
                "mulai" => $request->mulai,
                "selesai" => $request->selesai
            ];
            $data = AlternatifSolusi::find($request->idnya);
            $data->update($store);
            return redirect()->to(URL::to('/study-kasus/alternatif-solusi/' . Crypt::encrypt($data->id)));
        }
    }
}
