<?php
namespace App\Exports;

use Illuminate\Contracts\View\View;
use Maatwebsite\Excel\Concerns\FromView;
use Maatwebsite\Excel\Concerns\WithColumnWidths;
use Maatwebsite\Excel\Concerns\WithDefaultStyles;
use Maatwebsite\Excel\Concerns\WithEvents;
use Maatwebsite\Excel\Events\AfterSheet;
use PhpOffice\PhpSpreadsheet\Style\Alignment;
use PhpOffice\PhpSpreadsheet\Style\Style;

class ContractPLExport implements FromView, WithColumnWidths, WithDefaultStyles, WithEvents
{
    private $order_info;
    private $order_item_num;

    public function __construct($data=[])
    {
        $this->data=$data;
    }

    public function view(): View
    {
        return view('export.order_contract_PL', [
            "ship"=>$this->data["ship"],
            "bill"=>$this->data["bill"],
            "info"=>$this->data["info"],
        ]);
    }

    public function columnWidths(): array
    {
        return [
            'A' => 14,
            'B' => 17,
            'C' => 14,
            'D' => 17,
            'E' => 12,
            'F' => 12,
            'G' => 10,
            'H' => 10,
            'I' => 10,
        ];
    }

    public function defaultStyles(Style $defaultStyle)
    {
        return $defaultStyle->getFont()->setSize(8);
    }

    public function registerEvents(): array
    {
        return [
            AfterSheet::class => function (AfterSheet $event) {
//                 $cellRange = $this->specific_cells;
//                 foreach ($cellRange as $cell) {
//                     // 单个单元格设置格式
//                     $event->sheet->getDelegate()->getStyle($cell)->getNumberFormat()->setFormatCode('0.000000');
//                 }

                // 垂直居中
//                $last_line = $this->order_item_num + 8; // 最后行 = 明细数量 + (起始行 - 1)
//                $event->sheet->getDelegate()->getStyle('A9:I' . $last_line)->getAlignment()->setVertical(Alignment::VERTICAL_CENTER);
            }
        ];
    }

}