﻿<?php

namespace App\Exports;

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

/**
 * InvoicesExport
 */
class InvoicesExport 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->customer['first_name']) && !empty($item->customer['last_name'])) {
                $customerName = $item->customer['first_name'] . ' ' . $item->customer['last_name'];
            } elseif (!empty($item->customer['full_name'])) {
                $customerName = $item->customer['full_name'];
            }

            $r = [];
            $r[] = $sl;
            $r[] = $item->id;
            $r[] = $item->warehouse->name;
            $r[] = $item->date;
            $r[] = $customerName;
            $r[] = currencySymbol() . $item->total;
            $r[] = currencySymbol() . $item->total_paid;
            $r[] = strtoupper($item->payment_type);
            $r[] = ucwords(str_replace('_', ' ', $item->status));
            $r[] = ucwords(str_replace('_', ' ', $item->delivery_status));

            $data[] = $r;

            $sl++;
        }

        return $data;
    }

    /**
     * headings
     *
     * @return array
     */
    public function headings(): array
    {
        return [
            '#',
            __('Invoice ID'),
            __('Warehouse'),
            __('Date'),
            __('Customer'),
            __('Total'),
            __('Total Paid'),
            __('Payment Type'),
            __('custom.status'),
            __('Delivery Status'),
        ];
    }