<?php

namespace App\Exports;

use Illuminate\Contracts\View\View;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithStyles;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;

class BalanceHistoriesExport implements FromCollection, WithHeadings, WithStyles
{
    protected $datas;

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

    public function collection()
    {
        return $this->datas->map(function ($item, $key) {
            return [
                'no'            => $key + 1,
                'store_name'   => $item->store_name,
                'mutation_date' => $item->mutation_date,
                'from_balance'  => $item->from_balance,
                'mutation'      => $item->mutation,
                'to_balance'    => $item->to_balance,
                'doc_type'      => $item->doc_type,
                'doc_number'    => "'" . $item->doc_number
            ];
        });
    }

    public function headings(): array
    {
        return [
            'No',
            'Store Name',
            'Mutation Date',
            'From Balance (Rp)',
            'Mutation (Rp)',
            'To Balance (Rp)',
            'Doc. Type',
            'Doc. Number',
        ];
    }

    public function styles(Worksheet $sheet)
    {
        return [
            1 => ['font' => ['bold' => true]], // Baris 1 = heading
        ];
    }
}
