<?php

namespace App\Exports;

use App\Models\DataPenjualan;
use Maatwebsite\Excel\Concerns\FromQuery;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithMapping;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;

class DataPenjualanExport implements FromQuery, WithHeadings, WithMapping, ShouldAutoSize
{
    protected $userId;

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

    public function query()
    {
        return DataPenjualan::query()
            ->where('user_id', $this->userId)
            ->orderBy('tanggal', 'asc');
    }

    
    public function headings(): array
    {
        return [
            'No',
            'Tanggal Penjualan',
            'Jumlah Terjual (Tabung)',
        ];
    }

    private $rowNumber = 0;
    public function map($penjualan): array
    {
        return [
            ++$this->rowNumber,
            \Carbon\Carbon::parse($penjualan->tanggal)->translatedFormat('d F Y'),
            $penjualan->jumlah,
        ];
    }
}