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

    protected $parameterHeightSubDistrict;

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

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

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

            $excelData[] = $row;
        }

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