<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Master\Template;
use App\Models\Master\Topik;
use App\Models\Master\Instansi;
use App\Models\Dataset;
use App\Models\DatasetTambahan;

class FrontController extends Controller
{
    function index() {

        $data = [];
        // $data['template'] = Template::where('show_dashboard',1)->limit(4)->inRandomOrder()->get();
        $data['template'] = Template::limit(4)->inRandomOrder()->get();
        $data['dataset'] = Dataset::orderBy('DatasetId','DESC')->limit(4)->get();
        $data['count'] = [
            'dataset' => Dataset::count(),
            'resource' => Template::count(),
            'instansi' => Instansi::count(),
        ];

        return view('index',$data);
    }
    function resource() {
        $data = [];
        $data['template'] = Template::simplePaginate(12);
        return view('resource',$data);
    }
    function dataset_detail($id) {
        $keyId = decode_id($id);
        $data['dataset'] = Dataset::find($keyId);
        $data['lainnya'] = Dataset::with(['instansi'])->where('DatasetId','!=',$keyId)->limit(4)->get();
        $data['keyId'] = $keyId;
        
        return view('dataset-detail',$data);
    }
    public function gridDetail(Request $request)
    {
        $id = $request->query('id'); 
        $keyId = $id;
        $item = Dataset::find($keyId); // atau pakai ->pluck(), ->find(), dll

        $json = json_decode($item->data, true);
        $limit = $request->input('limit', 10);
        $offset = $request->input('offset', 0);

        $rows = array_slice($json, $offset, $limit);
        return response()->json([
            'total' => count($json),
            'rows' => $rows
        ]);

    }
    function datasetTambahan_detail($id) {
        $keyId = decode_id($id);
        $data['dataset'] = DatasetTambahan::find($keyId);
        $data['keyId'] = $keyId;
        $data['lainnya'] = DatasetTambahan::with(['instansi'])->where('DatasetTambahanId','!=',$keyId)->limit(4)->get();

        return view('dataset-tambahan-detail',$data);
    }
    function dataset() {
        $data = [];

        if(@request()->key){
            $data['dataset'] = Dataset::where('name','ilike','%'.request()->key.'%')->orderBy('DatasetId','DESC')->paginate(10);    
        }else{
            $data['dataset'] = Dataset::orderBy('DatasetId','DESC')->paginate(10);
        }

        $data['template'] = Template::simplePaginate(12);
        $data['instansi'] = Instansi::all();
        
        return view('dataset',$data);
    }
    function datasetTambahan() {
        $data = [];
        if(@request()->key){
            $data['dataset'] = DatasetTambahan::where('name','ilike','%'.request()->key.'%')->orderBy('DatasetTambahanId','DESC')->paginate(10);    
        }else{
            $data['dataset'] = DatasetTambahan::orderBy('DatasetTambahanId','DESC')->paginate(10);
        }
        $data['instansi'] = Instansi::all();
        return view('dataset-tambahan',$data);
    }
    function search() {
        $data['topik'] = Topik::all();
        $data['template'] = Template::all();
        $data['instansi'] = Instansi::all();
        $data['dataset'] = Dataset::orderBy('DatasetId','DESC')->limit(5)->get();
        return view('search',$data);
    }
}
