<?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 GeneralExport implements FromCollection, WithHeadings, WithStyles
{
    protected $type;
    protected $columns;

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

    public function collection()
    {
        switch ($this->type) { 
            case 'System Logs':
                return $this->datas->map(function ($item, $key) {
                    return [
                        'no'            => $key + 1,
                        'created'       => $item->created,
                        'store_name'   => $item->store_name,
                        'key'           => $item->key,
                        'code'          => $item->code,
                        'msg'           => $item->msg,
                    ];
                });
                break;
            case 'System Errors':
                return $this->datas->map(function ($item, $key) {
                    return [
                        'no'            => $key + 1,
                        'created'       => $item->created,
                        'type'          => $item->type,
                        'file'          => $item->file,
                        'message'       => $item->message,
                    ];
                });
                break;
            default:
                # code...
                break;
        }
    }

    public function headings(): array
    {
        return $this->columns; // ambil label heading
    }

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