﻿<?php

namespace App\Exports;

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

/**
 * ProductsExport
 */
class ProductsExport 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) {
            $r = [];
            $r[] = $sl;
            $r[] = $item->name;
            $r[] = $item->sku;
            $r[] = $item->category->name;
            $r[] = $item->price;
            $r[] = $item->stock == null ? '0' : $item->stock;
            $r[] = $item->is_variant == 0 ? 'No' : 'Yes';
            $r[] = $item->status == 'active' ? 'Active' : 'Inactive';

            $data[] = $r;

            $sl++;
        }

        return $data;
    }

    /**
     * headings
     *
     * @return array
     */
    public function headings(): array
    {
        return [
            '#',
            __('custom.product_name'),
            __('custom.sku'),
            __('custom.category'),
            __('custom.price'),
            __('custom.stock_quantity'),
            __('custom.variant'),
            __('custom.status')
        ];
    }