<?php

namespace App\Exports;

use Maatwebsite\Excel\Concerns\FromCollection;
use App\Models\OrderedPassport;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithStyles;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;


class dashboard_selected_export_user implements FromCollection , WithHeadings , WithStyles
{

    protected $ids;

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

    /**
    * @return \Illuminate\Support\Collection
    */
    public function collection()
    {
        $selectedCareIds = explode(',', $this->ids);

        return OrderedPassport::with('order_passport', 'carts.passport', 'iamPrincipal')
        ->whereIn('id', $selectedCareIds)
        ->get()
        ->map(function ($transaction) {
            return [
                'Name' => $transaction->iamPrincipal->first_name ?? 'Unknown',
                'Passport Name' => $transaction->is_cart_order == 1 ? $transaction->carts->passport->passport_name : $transaction->order_passport->passport_name,
                'Amount' => $transaction->is_cart_order == 1 ? $transaction->carts->passport->price : $transaction->order_passport->price,
            ];
        });
    }


    public function headings(): array
    {
        return [
            'Name',
            'Passport Name',
            'Amount',
        ];
    }

    public function styles(Worksheet $sheet)
    {
        return [
            // Style the first row (headings)
            1 => ['font' => ['bold' => true]],
        ];
    }
}
