<?php
defined('BASEPATH') or exit('No direct script access allowed');

class TabelPerhitungan extends CI_Controller
{

    public function __construct()
    {
        parent::__construct();
        $this->load->model("ModelDataKriteria");
        $this->load->model("ModelDataDesa");
        $this->load->model("ModelDataStunting");
    }

    function index()
    {
        $stunting = $this->ModelDataStunting->get_data();
        $data = array(
            'body' => 'TabelPerhitungan/list',
            'stunting' => $stunting
        );
        $this->load->view('index', $data);
    }

    function proseshitung()
    {
        $dataDesa = $this->ModelDataDesa->get_data();
        $startYear = 2020;
        $lastYear = 2024;
        $alpha = 0.9;
        $dataPerhitungan = array();

        foreach ($dataDesa as $key => $value) {
            $temp = array();
            $temp['id_desa'] = $value->id_desa;
            $temp['nama_desa'] = $value->nama_desa;
            $temp['alpha'] = $alpha;
            $min = PHP_INT_MAX;
            $max = PHP_INT_MIN;
            $errorSum = 0;
            $errorCount = 0;

            // Mengambil data aktual dan menentukan nilai min dan max
            for ($i = $startYear; $i <= $lastYear; $i++) {
                $aktual = 0;
                if ($this->ModelDataStunting->getbyDesaTahun($temp['id_desa'], $i)) {
                    $aktual = (int) $this->ModelDataStunting->getbyDesaTahun($temp['id_desa'], $i)['total'];
                }
                $temp['aktual'][$i] = $aktual;
                if ($aktual < $min) {
                    $min = $aktual;
                }
                if ($aktual > $max) {
                    $max = $aktual;
                }
            }

            // Menghitung normalisasi, peramalan SES, denormalisasi, dan MAPE
            for ($i = $startYear; $i <= $lastYear; $i++) {
                // Normalisasi
                if (($max - $min) <= 0) {
                    $temp['normalisasi'][$i] = 0;
                } else {
                    $temp['normalisasi'][$i] = ($temp['aktual'][$i] - $min) / ($max - $min);
                }

                // Peramalan SES
                if ($i == $startYear) {
                    $temp['peramalan'][$i] = $temp['normalisasi'][$i];
                } else {
                    $temp['peramalan'][$i] = $alpha * $temp['normalisasi'][$i - 1] + (1 - $alpha) * $temp['peramalan'][$i - 1];
                }

                // Denormalisasi
                $temp['de_peramalan'][$i] = $temp['peramalan'][$i] * ($max - $min) + $min;

                // Menghitung error untuk MAPE
                if ($i > $startYear && $temp['aktual'][$i] > 0) {
                    $error = abs($temp['aktual'][$i] - $temp['de_peramalan'][$i]) / $temp['aktual'][$i] * 100;
                    $temp['error'][$i] = $error;
                    $errorSum += $error;
                    $errorCount++;
                }
            }

            // Menghitung MAPE
            if ($errorCount > 0) {
                $temp['mape'] = round($errorSum / $errorCount, 2);
            } else {
                $temp['mape'] = 0;
            }

            $temp['min'] = $min;
            $temp['max'] = $max;
            $temp['forecast'] = $alpha * $temp['normalisasi'][$lastYear] + (1 - $alpha) * $temp['peramalan'][$lastYear];
            $temp['forecast'] = floor($temp['forecast'] * ($max - $min) + $min);
            $dataPerhitungan[] = $temp;
        }

        // Mengirim data ke view
        $stunting = $this->ModelDataStunting->get_data();
        $data = array(
            'body' => 'TabelPerhitungan/list',
            'stunting' => $stunting,
            'dataPerhitungan' => $dataPerhitungan
        );
        $this->load->view('index', $data);

    }
}
