<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

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

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

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

        return view('grafiktele', [
            'manualCounts1' => array_values($manualCounts1),
            'svmCounts1' => array_values($svmCounts1)
        ]);
    }

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

}
