<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Master\Instansi;
use App\Models\Master\Template;
use App\Models\Master\TabelData;
use App\Models\Dataset;
use App\Models\Dataset\DatasetTable45;
use App\Models\Dataset\DatasetTable47;

class AjaxController extends Controller
{
    function getResourceDataDashboard(Request $request,$id){
        $_data = [];

        $keyId = decode_id($id);
        $instansi = Instansi::find($keyId);
        $resource = json_decode($instansi->resource_data);
        // dd($resource);

        $data = [];

        if(@$resource){
            $data = TabelData::whereIn('MsTabelDataId',$resource)->count();
        
            if(@$data != 0){
                $data = TabelData::whereIn('MsTabelDataId',$resource)->get();
            }
        }

        // $data = TabelData::all();
        
        foreach ($data as $key => $value) {
            $cek = Dataset::where('instansi_id',$keyId)->where('ms_tabel_data_id',$value->MsTabelDataId)->first();
            if(@$cek){
                $status = '<span class="p-2 bg-emerald-500 text-[10px] rounded text-white w-full"><i class="ri-check-line"></i></span>';
            }else{
                $status = '<span class="p-2 bg-red-500 text-[10px] rounded text-white w-full"><i class="ri-close-line"></i></span>';
            }

            $_data[] = [
                'no'    => $key+1,
                'tabel' => @$value->name,
                'status' => @$status,
                'tanggal' => @$cek ? date('d-m-Y',strtotime(@$cek->created_at)) : '-',
            ];
        }

        return response()->json($_data);
    }

    function getResourceData(Request $request){
        $_data = [];

        $keyId = decode_id($request->id);
        $instansi = Instansi::find($keyId);
        $resource = json_decode($instansi->resource_data);
        // dd($resource);

        if(@$resource){
            $data = TabelData::whereIn('MsTabelDataId',$resource)->count();
        
            if(@$data != 0){
                $data = TabelData::whereIn('MsTabelDataId',$resource)->get();
            }else{
                $data = TabelData::all();
            }
        }else{
            $data = TabelData::all();
        }

        // $data = TabelData::all();
        
        foreach ($data as $key => $value) {
            // $filename = pathinfo($value->template_url, PATHINFO_FILENAME); 

            $_data[$key]['id'] = encode_id($value->MsTabelDataId);
            $_data[$key]['name'] = 'Tabel '.$value->nomor_tabel.' - '.$value->name;
            $_data[$key]['name2'] = $value->name;
            $_data[$key]['excel'] = url('opendata/dataset/generateTemplate/'.encode_id($value->MsTabelDataId));
        }

        return response()->json(['data' => $_data,'status' => true]);
    }

    function getInstansi($type){
        $data = [];

        $instansi = Instansi::where('parent',$type)->get();

        foreach ($instansi as $key => $value) {
            $data[$key]['id'] = encode_id($value->MsInstansiId);
            $data[$key]['name'] = $value->name;
        }

        return response()->json(['data' => $data,'status' => true]);
    }

    function getPenduduk($tahun){
        $_data = [];

        $dataset = Dataset::whereIn('tahun',[$tahun])->pluck('DatasetId')->toArray();

        $data = DatasetTable45::whereIn('dataset_id',$dataset)->select('kabupaten_kota', 'jumlah_penduduk')->get();

        $labels = $data->pluck('kabupaten_kota');
        $series =  $data->pluck('jumlah_penduduk')->map(fn($v) => (int)$v);

        return response()->json([
            'labels' => $labels,
            'series' => $series,
        ]);

    }

    function getTimbulan($tahun){
        $_data = [];

        $dataset = Dataset::whereIn('tahun',[$tahun])->pluck('DatasetId')->toArray();

        $data = DatasetTable47::whereIn('dataset_id',$dataset)->select('kabupaten_kota', 'timbulan_sampah')->get();

        $labels = $data->pluck('kabupaten_kota');
        $series =  $data->pluck('timbulan_sampah')->map(fn($v) => (int)$v);

        return response()->json([
            'labels' => $labels,
            'series' => $series,
        ]);

    }
}
