<?php

namespace App\Http\Controllers;

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

class HomeController extends Controller
{
    protected $template = 'modules.dashboard';
    protected $route = 'modules.dashboard';

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth');
    }

    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Contracts\Support\Renderable
     */
    public function dashboard()
    {
        $data['title']   = 'Dashboard';
        if(session('group_id') == 1){
            $data['dataset'] = Dataset::orderBy('DatasetId','DESC')->limit(10)->get();
        }else{
            $data['dataset'] = Dataset::orderBy('DatasetId','DESC')->whereIn('instansi_id',[auth()->user()->ms_instansi_id])->limit(10)->get();
        }
        // $data['dataset'] = Dataset::orderBy('DatasetId','DESC')->limit(10)->get();
        $data['group']          = Group::where('MsGroupId','!=',1)->where('name','!=','Balai')->get();
        if(session('group_id') == 1){
            $data['resource']       = Template::with(['dataset'])->where('show_dashboard',1)->get();
        }else{
            $data['resource']       = Template::with(['dataset'])->whereHas('dataset',function($query){
                $query->where('instansi_id',auth()->user()->ms_instansi_id);
            })->where('show_dashboard',1)->get();
        }
        $data['classDataset']   = Dataset::class;

        return view($this->template.'.dashboard',$data);
    }

    public function dataset($alias)
    {
        $data['title']   = 'Dashboard';
        $data['dataset'] = Dataset::orderBy('DatasetId','DESC')->limit(10)->get();
        $data['group']   = Group::where('MsGroupId','!=',1)->get();
        $data['alias']   = $alias;

        return view($this->template.'.dataset',$data);
    }

    public function grid(Request $request)
    {
        if(session('group_id') == 1){
            $data = Dataset::orderBy('DatasetId','DESC')->limit(10)->get();
        }else{
            $data = Dataset::orderBy('DatasetId','DESC')->limit(10)->whereIn('created_by',[auth()->user()->id,1])->get();
        }
        // $data = User::with(['group'])->orderBy('id','DESC')->get();
        $_data = [];

        foreach ($data as $key => $row) {

                $action = '';
                $action .= '<div class="flex gap-3 justify-center items-center flex-row">';
                $action .= '<a href="'.url('opendata/dataset/view/'.encode_id($row->DatasetId)).'" data-toggle="tooltip" title="Lihat Data" class="btn btn-sm btn-block bg-info"><i class="ri-eye-line text-white"></i></a>';
                $action .= '</div>';
                $status = '';
                
                if($row->status == 1){
                    $status = '<small class="p-1 w-full bg-success text-white rounded" title="Aktif"><i class="ri-check-line"></i></small>';
                }else{
                    $status = '<small class="p-1 w-full bg-danger text-white rounded" title="Tidak Aktif"><i class="ri-close-line"></i></small>';
                }
                
            $_data[] = [
                'no'                => $key+1,
                'id'                => encode_id($row->id),
                'name'              => @$row->name,
                'publik'            => @$row->publik,
                'tahun'            => @$row->tahun,
                'created_at'            => date('d-m-Y H:i:s',strtotime(@$row->created_at)),
                'instansi'            => @$row->instansi->name,
                'action'            => @$action,
                'status'            => @$status,
            ];

        }

        // return response()->json($_data);  // Return the data as a JSON response
        return response()->json($_data);

    }

    public function datasetGrid($alias,Request $request)
    {
        if(session('group_id') == 1){
            $data = Dataset::whereHas('instansi',function($query) use ($alias){
                $query->where('parent','ilike','%'.$alias.'%');
            })->orderBy('DatasetId','DESC')->get();
        }else{
            $data = Dataset::whereHas('instansi',function($query) use ($alias){
                $query->where('parent','ilike','%'.$alias.'%');
            })->orderBy('DatasetId','DESC')->whereIn('created_by',[auth()->user()->id,1])->get();
        }
        // $data = User::with(['group'])->orderBy('id','DESC')->get();
        $_data = [];

        foreach ($data as $key => $row) {

                $action = '';
                $action .= '<div class="flex gap-3 justify-center items-center flex-row">';
                $action .= '<a href="'.url('opendata/dataset/view/'.encode_id($row->DatasetId)).'" data-toggle="tooltip" title="Lihat Data" class="btn btn-sm btn-block bg-info"><i class="ri-eye-line text-white"></i></a>';
                $action .= '</div>';
                $status = '';
                
                if($row->status == 1){
                    $status = '<small class="p-1 w-full bg-success text-white rounded" title="Aktif"><i class="ri-check-line"></i></small>';
                }else{
                    $status = '<small class="p-1 w-full bg-danger text-white rounded" title="Tidak Aktif"><i class="ri-close-line"></i></small>';
                }
                
            $_data[] = [
                'no'                => $key+1,
                'id'                => encode_id($row->id),
                'name'              => @$row->name,
                'publik'            => @$row->publik,
                'tahun'            => @$row->tahun,
                'created_at'            => date('d-m-Y H:i:s',strtotime(@$row->created_at)),
                'instansi'            => @$row->instansi->name,
                'action'            => @$action,
                'status'            => @$status,
            ];

        }

        // return response()->json($_data);  // Return the data as a JSON response
        return response()->json($_data);

    }

    function resource($id){
        $keyId = decode_id($id);
        $data['id'] = $id;

        $data['title']   = 'Dashboard';
        $data['dataset']  = Dataset::where('template_id',$keyId)->pluck('instansi_id')->toArray();
        $data['instansi']  = Instansi::whereIn('MsInstansiId',$data['dataset'])->paginate(9);

        return view($this->template.'.resource',$data);
    }
    function resource_instansi($instansi_id,$id){

        $data['id']          = decode_id($id);
        $data['instansi_id'] = decode_id($instansi_id);
        $data['dataset']     = Dataset::with(['template'])->where('instansi_id',decode_id($instansi_id))->where('template_id',decode_id($id))->first();
        $data['title']       = 'Dashboard '.@$data['dataset']->template->name;

        return view($this->template.'.resource_detail',$data);

    }
}
