<?php

namespace App\Models\Dataset;

use Illuminate\Database\Eloquent\SoftDeletes;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class DatasetTable32 extends Model
{
    use HasFactory;
use SoftDeletes;
    protected $table = 'dataset_table32';
    protected $primaryKey = 'DatasetTable32Id';

    protected $guarded = [];

    function dataset() {
        return $this->belongsTo('App\Models\Dataset','dataset_id');
    }

    public static function dataChart($modelClass,$datasetId,$templateId){

        $datasets = \App\Models\Dataset::where('template_id', $templateId)
            ->orderBy('tahun', 'asc')
            ->get()
            ->map(function ($dataset) use ($modelClass) {
                $dataset->items = (new $modelClass)->where('dataset_id', $dataset->id)->get();
                return $dataset;
            });
        $years  = $datasets->pluck('tahun')->toArray();
        $model = self::with('dataset')->whereHas('dataset',function($query) use ($years){
            $query->whereIn('tahun',$years);
        })->where('dataset_id',$datasetId)
       ->selectRaw('
            SUM(COALESCE(NULLIF(REPLACE(tidak_sekolah_l, \',\', \'.\'), \'\')::numeric,0)) as ts_l,
            SUM(COALESCE(NULLIF(REPLACE(tidak_sekolah_p, \',\', \'.\'), \'\')::numeric,0)) as ts_p,
            SUM(COALESCE(NULLIF(REPLACE(sd_l, \',\', \'.\'), \'\')::numeric,0)) as dasar_l,
            SUM(COALESCE(NULLIF(REPLACE(sd_p, \',\', \'.\'), \'\')::numeric,0)) as dasar_p,
            SUM(COALESCE(NULLIF(REPLACE(sltp_l, \',\', \'.\'), \'\')::numeric,0)) as smp_l,
            SUM(COALESCE(NULLIF(REPLACE(sltp_p, \',\', \'.\'), \'\')::numeric,0)) as smp_p,
            SUM(COALESCE(NULLIF(REPLACE(slta_smk_l, \',\', \'.\'), \'\')::numeric,0)) as sma_l,
            SUM(COALESCE(NULLIF(REPLACE(slta_smk_p, \',\', \'.\'), \'\')::numeric,0)) as sma_p,
            SUM(COALESCE(NULLIF(REPLACE(pt_l, \',\', \'.\'), \'\')::numeric,0)) as univ_l,
            SUM(COALESCE(NULLIF(REPLACE(pt_p, \',\', \'.\'), \'\')::numeric,0)) as univ_p
        ')->get();
        // Sekolah::selectRaw('SUM(jumlah_siswa) as total_siswa, SUM(jumlah_guru) as total_guru')->first();

        $result = [];
        foreach ($model as $k => $row) {
            // $lokasi = $row->lokasi ?? 'Unknown';

            if (!isset($result)) {
                $result = [
                    'tidak_sekolah' => [],
                    'sd' => [],
                    'sltp' => [],
                    'slta' => [],
                    'pt' => [],
                ];
            }

            $result[$k]['tidak_sekolah'][] = (float) $row->ts_l+(float) $row->ts_p;
            $result[$k]['sd'][] = (float) $row->dasar_p+(float) $row->dasar_l;
            $result[$k]['sltp'][] = (float) $row->smp_p+(float) $row->smp_l;
            $result[$k]['slta'][] = (float) $row->sma_p+(float) $row->sma_l;
            $result[$k]['pt'][] = (float) $row->univ_p+(float) $row->univ_l;
        }

        return [
            'title'     => '',
            'years'     => $years,
            'yTitle'    => 'Jumlah',
            'yOpposite'    => '',
            'series'    => collect($result)->flatMap(function ($item) {
                return [
                    [
                        'name' => "Tidak Sekolah",
                        'type' => 'column',
                        'data' => $item['tidak_sekolah']
                    ],[
                        'name' => "SD",
                        'type' => 'column',
                        'data' => $item['sd']
                    ],[
                        'name' => "SMP",
                        'type' => 'column',
                        'data' => $item['sltp']
                    ],[
                        'name' => "SMA/SMK",
                        'type' => 'column',
                        'data' => $item['slta']
                    ],[
                        'name' => "Perguruan Tinggi",
                        'type' => 'column',
                        'data' => $item['pt']
                    ],
                ];
            })->values()
        ];
    }

    public static function legend() {
        $html = "
            Keterangan:<br>
            Dalam satuan Persen (%)<br>
            L = Laki-Laki<br>
            P = Perempuan<br><br>
            
            Penjelasan Isi Tabel:<br>
            (1) Cukup jelas<br>
            (2) Diisi dengan nama kabupaten/kota yang ada di provinsi penyusun laporan<br>
            (3)-(18) Diisi berdasarkan kelompok tingkat pendidikan dan jenis kelamin dalam satuan jiwa<br>
        ";
        return $html;
    }

}
