<?php

namespace App\Exports;

use App\Models\Nilai;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithHeadings;

class NilaiExport implements FromCollection, WithHeadings
{
    /**
     * @return \Illuminate\Support\Collection
     */
    public function collection()
    {
        $nilai = Nilai::join("siswa", "siswa.user_id", "=", "nilai.user_id")->get(["nilai.id", "siswa.nama", "nilai.writing", "nilai.reading", "nilai.listening", "nilai.speaking"]);
        for ($i = 0; $i < count($nilai); $i++) {
            $nilai[$i]["ratarata"] = ($nilai[$i]["reading"] + $nilai[$i]["writing"] + $nilai[$i]["speaking"] + $nilai[$i]["listening"]) / 4;
        }

        return $nilai;
    }

    public function headings(): array
    {
        return [
            '#',
            'NAMA',
            'WRITING',
            'READING',
            'LISTENING',
            'SPEAKING',
            "RATARATA"
        ];
    }
}
