<?php

namespace App\Exports;

use Log;
use Maatwebsite\Excel\Concerns\FromQuery;
use Maatwebsite\Excel\Concerns\WithMapping;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithEvents;
use Maatwebsite\Excel\Events\AfterSheet;
use Maatwebsite\Excel\Concerns\FromCollection;
use App\Models\UserWinnersAmount;

class userWinnerExport implements FromQuery, WithHeadings, ShouldAutoSize, WithEvents, WithMapping
{
   private $search_date;
	public function __construct($search_date)
	{
		$this->search_date = $search_date;
	}

    /**
    * @return \Illuminate\Support\Collection
    */  
    // use Exportable;

    public function headings(): array
    {
        return [
            '#',
            'Stores Name',
            'User Name',
            'Bit Place Open',
            'Bit Place Close',
            'Winning Amount',
        ];
    }
    public function query()
    {
    	$relation = ['Stores','GameGroupName','GameName','Day','username','gameWinner'];
         $bulk = UserWinnersAmount::query()->with($relation);
         $bulk->where('created_at','like',$this->search_date.'%');
         return $bulk;
    }
    public function map($bulk): array
    {
        return [
            $bulk->id,
            $bulk->Stores->name,
            $bulk->username->name,
            $bulk->open_time,
            $bulk->close_time,
            $bulk->amount,
        ];
    }
     public function registerEvents(): array
    {
        return [
            AfterSheet::class    => function(AfterSheet $event) {
                $cellRange = 'A1:W1'; // All headers
                $event->sheet->getDelegate()->getStyle($cellRange)->getFont()->setSize(12);
                $event->sheet->getDelegate()->getStyle($cellRange)->getFont()->setBold(true);
            },
        ];
    }
}
