﻿<?php

namespace App\Exports;

use Maatwebsite\Excel\Concerns\FromArray;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;

/**
 * SaleReturnListsExport
 */
class SaleReturnListsExport implements FromArray, WithHeadings, ShouldAutoSize
{
    protected $items;

    /**
     * __construct
     *
     * @param  mixed $items
     * @return void
     */
    public function __construct($items)
    {
        $this->items = $items;
    }

    /**
     * array
     *
     * @return array
     */
    public function array(): array
    {
        $data = [];

        $sl = 1;
        foreach ($this->items as $item) {
            $customerName = 'Walk-in Customer';

            if (!empty($item->invoice->customer['first_name']) && !empty($item->invoice->customer['last_name'])) {
                $customerName = $item->invoice->customer['first_name'] . ' ' . $item->invoice->customer['last_name'];
            } elseif (!empty($item->invoice->customer['full_name'])) {
                $customerName = $item->invoice->customer['full_name'];
            }

            $r = [];
            $r[] = $sl;
            $r[] = $item->invoice_id;
            $r[] = $customerName;
            $r[] = $item->return_date;
            $r[] = $item->saleReturnItems->count();

            $data[] = $r;

            $sl++;
        }

        return $data;
    }

    /**
     * headings
     *
     * @return array
     */
    public function headings(): array
    {
        return [
            '#',
            __('Invoice Number'),
            __('Customer Name'),
            __('Date'),
            __('Total Product'),
        ];
    }