<?php

namespace App\Exports;

use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
use Illuminate\Contracts\View\View;
use Maatwebsite\Excel\Concerns\FromView;
use Maatwebsite\Excel\Concerns\WithStyles;

class ParameterPhVillageExport implements FromView, WithStyles
{
    /**
     * @return \Illuminate\Support\Collection
     */

    protected $parameterPhVillage;

    public function __construct($parameterPhVillage)
    {
        $this->parameterPhVillage = $parameterPhVillage;
    }

    public function view(): View
    {
        $excelDataPh = [];

        foreach ($this->parameterPhVillage as $village => $data) {
            $PhH20Data = $data[2];
            foreach ($PhH20Data as $parameter => $values) {
                $row = [
                    'village' => $data[0],
                    'sub_district' => $data[1],
                    'label' => $values['label'],
                    'mean' => $values['values']['mean'] / 10
                ];

                $excelDataPh[] = $row;
            }
        }

        return view('website.export.excel-parameter-ph-village', [
            'parameterPhVillage' => $excelDataPh,
        ]);
    }

    public function styles(Worksheet $sheet)
    {
        $highestColumn = $sheet->getHighestColumn();
        $highestColumnIndex = Coordinate::columnIndexFromString($highestColumn);

        for ($col = 1; $col <= $highestColumnIndex; $col++) {
            $columnLetter = Coordinate::stringFromColumnIndex($col);
            $sheet->getColumnDimension($columnLetter)->setAutoSize(true);
        }
    }
}
