<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class VisualWAController extends Controller
{
    public function index()
    {
        $file = storage_path('app/public/dataWA_matang_dengan_klasifikasi.csv');
        $data = $this->readCsv($file);

        // Menghitung jumlah masing-masing kelas di setiap field
        $manualCounts = ['fitur' => 0, 'performa' => 0, 'tampilan' => 0];
        $svmCounts = ['fitur' => 0, 'performa' => 0, 'tampilan' => 0];

        foreach ($data as $key => $row) {
            if ($key > 0) { // Lewati header
                $manualCounts[$row[3]]++; // Asumsikan 'label_Manual' ada di kolom ke-4 (index 1)
                $svmCounts[$row[4]]++; // Asumsikan 'label_svm' ada di kolom ke-5 (index 4)
            }
        }

        return view('grafikWA', [
            'manualCounts' => array_values($manualCounts),
            'svmCounts' => array_values($svmCounts)
        ]);
    }

    private function readCsv($file)
    {
        $rows = [];
        if (($handle = fopen($file, 'r')) !== false) {
            while (($data = fgetcsv($handle, 1000, ',')) !== false) {
                $rows[] = $data;
            }
            fclose($handle);
        }
        return $rows;
    }


}
