<?php

namespace App\Http\Controllers;

use App\Models\Biodata;
use Exception;
use Illuminate\Http\Request;

class GetDataController extends Controller
{
    public function getBiodata(Request $request)
    {
        try {
            $biodata = Biodata::query()->where('nidn', $request->nidn)->first();
            $biodata->id = encrypt($biodata->id);

            return response()->json($biodata);
        } catch (Exception $ex) {
            return response('Not Found', 404);
        }
    }

    public function getChildren(Request $request)
    {
        if(!is_null($request->id)){
            $class = 'App\\Models\\Admin\\'.$request->parent;
            $parent = $class::query()->with('children')->find(decrypt($request->id));
            $children = $parent->children;
            $str = '';
            foreach ($children as $child) {
                $str .= '<option value="'.encrypt($child->id).'">'.$child->nama.'</option>';
            }

            return response($str, 200);
        }
        $str = '';
        return response($str, 200);
    }
}
