﻿<?php

namespace App\Exports;

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

/**
 * CouponsExport
 */
class CouponsExport 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) {
            $discount = '';
            if ($item->discount !== null) {
                if ($item->discount_type == 'percent') {
                    $discount = $item->discount . '%';
                } elseif ($item->discount_type == 'fixed') {
                    $discount = currencySymbol() . $item->discount;
                } else {
                    $discount = $item->discount;
                }
            }

            $validity = date('Y-m-d', strtotime($item->start_date)) . ' - ' . 
                ($item->end_date ? date('Y-m-d', strtotime($item->end_date)) : date('Y-m-d', strtotime($item->start_date)));

            $r = [];
            $r[] = $sl;
            $r[] = $item->title;
            $r[] = $item->code;
            $r[] = $discount;
            $r[] = $validity;
            $r[] = $item->minimum_shopping;
            $r[] = $item->status ? 'Active' : 'Inactive';

            $data[] = $r;

            $sl++;
        }

        return $data;
    }

    /**
     * headings
     *
     * @return array
     */
    public function headings(): array
    {
        return [
            '#',
            __('Title'),
            __('Coupon Code'),
            __('Discount'),
            __('Validity'),
            __('Minimum Quantity'),
            __('Status'),
        ];
    }