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

    protected $parameterHeightVillage;

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

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

        foreach ($this->parameterHeightVillage as $village => $data) {
            $row = [
                'sub_district' => $data['sub_district'],
                'village' => $data['village'],
                'height' => $data['height'],
                'year' => $data['year']
            ];

            $excelData[] = $row;
        }
        return view('website.export.excel-parameter-height-village', [
            'parameterHeightVillage' => $excelData
        ]);
    }

    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);
        }
    }
}
