<?php

namespace App\Classes\Modules\Exports\Services;

use Carbon\Carbon;
use Illuminate\Support\Arr;
use Illuminate\Http\Request;
use App\Models\TransactionMappingLog;
use Maatwebsite\Excel\Concerns\FromQuery;
use Maatwebsite\Excel\Concerns\Exportable;
use Maatwebsite\Excel\Concerns\WithMapping;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
use Maatwebsite\Excel\Concerns\WithHeadingRow;

class ExportsImportedInvoiceMappeds implements FromQuery, WithHeadings, WithHeadingRow, WithMapping, ShouldAutoSize
{
    use Exportable;

    private $dateTime;
    private $count;
    private $counter = 1;

    public function __construct(Request $request)
    {
        $this->dateTime = $request->input('date').' '.$request->input('time');
        $this->count = 0;
    }

    public function headings(): array
    {
        return [
            'No',
            'Doc No',
            'Date',
            'Debtor Code',
            'Debtor Name',
            'Shipping Info',
            'Net Total',
            'Cancelled',
            'Mapped Status',
            'Mapped Reference No'
        ];
    }

    /**
     * @return \Illuminate\Support\Collection|mixed
     */
    public function query()
    {
        return TransactionMappingLog::where('imported_date', $this->dateTime);
    }

    /**
     * @param Transaction $transaction
     *
     * @return array
     */
    public function map($transaction): array
    {
        // dd($transaction);
        $this->count += 1;
        $data = $transaction->data;
        return [
            $this->count,
            Arr::get($data,'doc_no'),
            Arr::get($data,'date'),
            Arr::get($data,'debtor_code'),
            Arr::get($data,'debtor_name'),
            Arr::get($data,'shipping_info'),
            Arr::get($data,'net_total'),
            Arr::get($data,'cancelled'),
            Arr::get($data,'mapped_status'),
            Arr::get($data,'mapped_result_reference'),
        ];
    
    }
}
