<?php

namespace App\Exports;

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

class ExportSuppliersProfitCentersExcel implements FromView, ShouldAutoSize
{
    public function __construct($data)
    {
        $this->data = $data;
    }

    public function view(): View
    {
        return view('admin.export.SupplierProfitCenterExcel')->with($this->data);

    }

    private function setItemRowFormat(Worksheet $sheet, $dataRange)
    {
        $sheet->getStyle($dataRange)->applyFromArray([
            'borders' => [
                'outline' => [
                    'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_MEDIUM,
                ],
                'inside' => [
                    'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN,
                ],
            ],
        ]);

    }

    private function formatItemsRows(Worksheet $sheet)
    {

        $pointerText = 'Végösszeg';
        $sumValueInRow = $this->searchByValue($sheet, $pointerText, true);
        $lastCell = $sheet->getHighestColumn();

        if ($this->data['orderType'] == 'weekly') {
            $pointerText = 'Szállítási dátum:';
            $dateValueInRow = $this->searchByValue($sheet, $pointerText, true);
            for ($i = 0; $i < count($sumValueInRow); $i++) {
                $dataRange = 'A'.($dateValueInRow[$i]['row'] + 1).':'.$lastCell.($sumValueInRow[$i]['row'] - 1);
                $this->setItemRowFormat($sheet, $dataRange);
            }
        } else {
            $headValueInRow = $this->searchByValue($sheet, 'Cikkszám', true)[0]['row'];
            $dataRange = 'A'.($headValueInRow + 1).':'.$lastCell.($sumValueInRow[0]['row'] - 1);
            $this->setItemRowFormat($sheet, $dataRange);
        }

    }

    public function styles(Worksheet $sheet) {}

    public function map($row): array
    {
        // TODO: Implement map() method.
    }

    public function columnFormats(): array
    {
        // TODO: Implement columnFormats() method.
        return [];
    }

    public function bindValue(Cell $cell, $value)
    {
        return true;
    }
}
