<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Http\Controllers\GetDataApiController;
use Illuminate\Http\Request;

class DosenController extends Controller
{

    public function getDosen(Request $request)
    {
        $keyword    = $request->q;
        $getDosen   = GetDataApiController::getDosen();

        $total_count = 0;
        $incomplete_results =  true;

        $items = [];
        if (empty($getDosen)) {

            $items = array();

        } else {
            $no = 0;
            foreach ($getDosen['data'] as $key => $item) {

                if(str_contains(strtolower($item['nm_sdm']), strtolower($keyword))){
                    $itemData = [
                        'id' => encrypt($item['id_sdm'].'_'.$item['nm_sdm'].'_'.$item['nidn']),
                        'full_name' => $item['nm_sdm'],
                        'description' => $item['prodi'],
                        'nidn' => $item['nidn'],
                    ];

                    $items[] = $itemData;

                    $total_count = $total_count + 1;
                }
            }
        }

        $data = [
            'total_count' => $total_count,
            'incomplete_results' => false,
            'items' => $items,
        ];

        //dd($data);
        return response($data, 200);
    }
}
