<?php

namespace App\Controllers;
use App\Models\ModelDarah;
use App\Models\ModelCurah;

class Home extends BaseController
{
    protected $DarahModel;
    protected $CurahModel;

    function __construct()
    {
        $this->DarahModel = new ModelDarah();
        $this->CurahModel = new ModelCurah();
    }

    public function index(): string
    {
        return view('welcome_message');
    } 

    public function index2()
    {
        $data_curah = $this->CurahModel->getCurah()->getResultArray();
        $data_darah = $this->DarahModel->getDarah()->getResultArray();

        // Fetch distinct years from both datasets
        $years = array_unique(array_merge(
            array_column($data_curah, 'Tahun'),
            array_column($data_darah, 'Tahun')
        ));
        sort($years);

        $input_year = $this->request->getPost('year'); 

        if (!isset($input_year) || !is_numeric($input_year)) {
            $input_year = end($years); // Default value if the input is not valid
        }
        
        // Initialize array for chart data
        $chart_data = [
            'labels' => [],
            'datasets' => []
        ];

        $current_year = $input_year;
        $selected_years = [$current_year];
        $colors = [
            'rgba(255, 99, 132, 1)',  // Color for dengue fever data
            'rgba(54, 162, 235, 1)'   // Color for rainfall data
        ];

        // Process dengue fever data
        foreach ($data_darah as $row) {
            if (in_array($row['Tahun'], $selected_years)) {
                $dataset_key = array_search($row['Tahun'], $selected_years);

                foreach ($row as $key => $value) {
                    if ($key !== 'Tahun' && $key !== 'id') {
                        if (!in_array($key, $chart_data['labels'])) {
                            $chart_data['labels'][] = $key;
                        }

                        if (!isset($chart_data['datasets'][$dataset_key])) {
                            $chart_data['datasets'][$dataset_key] = [
                                'label' => "Kasus Demam Berdarah " . $row['Tahun'],
                                'data' => [],
                                'borderColor' => $colors[0],
                                'borderWidth' => 1,
                                'fill' => false
                            ];
                        }
                        $chart_data['datasets'][$dataset_key]['data'][] = $value;
                    }
                }
            }
        }

        // Process rainfall data
        foreach ($data_curah as $row) {
            if (in_array($row['Tahun'], $selected_years)) {
                $dataset_key = count($chart_data['datasets']); // Create a new key for rainfall

                foreach ($row as $key => $value) {
                    if ($key !== 'Tahun' && $key !== 'id') {
                        if (!in_array($key, $chart_data['labels'])) {
                            $chart_data['labels'][] = $key;
                        }

                        if (!isset($chart_data['datasets'][$dataset_key])) {
                            $chart_data['datasets'][$dataset_key] = [
                                'label' => "Curah Hujan " . $row['Tahun'],
                                'data' => [],
                                'borderColor' => $colors[1],
                                'borderWidth' => 1,
                                'fill' => false
                            ];
                        }
                        $chart_data['datasets'][$dataset_key]['data'][] = $value;
                    }
                }
            }
        }

        $data['years'] = $years;
        $data['body'] = 'dashboard';
        $data['chart_data'] = json_encode($chart_data);
        $data['selected_year'] = $input_year;

        return view('template', $data);
    }
}
