<?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 ParameterPhSubDistrictExport implements FromView, WithStyles
{
    /**
     * @return \Illuminate\Support\Collection
     */

    protected $parameterPhSubDistrict;

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

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

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

                $excelDataPh[] = $row;
            }
        }

        return view('website.export.excel-parameter-ph-sub-district', [
            'parameterPhSubDistrict' => $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);
        }
    }
}
