<?php
namespace App\Controllers\Kewilayahan\Sebaran;

class JenisStatus extends \App\Controllers\Kewilayahan\Kytp

{
    protected function data($request, $jenisOrStatus)
    {
        $opsiWilZona  = $request->getPost('opsiWilZona');
        $adm4_pcode   = $request->getPost('adm4_pcode') ?? [];
        $id_poly_zona = $request->getPost('id_poly_zona') ?? [];

        $db = \Config\Database::connect();

        $kolom = "JNS_WP_MFWP";
        switch ($jenisOrStatus) {
            case 'jenis':
                $kolom = "JNS_WP_MFWP";
                break;
            case 'status':
                $kolom = "STATUS_WP_MFWP";
                break;
            default:

                break;
        }

        $npwp = $db->table('KPDL_MV_LOKASI_SUBJEK')->select($kolom . ", count(1) JML", false)
            ->where($kolom . " IS NOT NULL")
            ->groupBy($kolom);

        if ($opsiWilZona == 'wilayah') {
            $npwp->whereIn('ADM4_PCODE', $adm4_pcode);
        }

        if ($opsiWilZona == 'zona') {
            $npwp->whereIn('ID_POLY_ZONA', $id_poly_zona);
        }

        $data = $npwp->get()->getResult();

        $ret = [];

        foreach ($data as $b) {

            array_push($ret, ['name' => $b->{$kolom},
                // 'color'                  => ,
                'y'                      => floatval($b->JML),
            ]
            );
        }

        return $ret;
    }
}