<?php

namespace App\Nova\Actions;

use App\Http\Helpers\FileUploader;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Support\Collection;
use Laravel\Nova\Actions\Action;
use Laravel\Nova\Fields\ActionFields;
use Laravel\Nova\Fields\Image;
use Laravel\Nova\Fields\Text;
use Laravel\Nova\Http\Requests\NovaRequest;

class EditAboutImage extends Action
{
    use InteractsWithQueue, Queueable;

    public $name = 'Edit Images';
    /**
     * Perform the action on the given models.
     *
     * @param  \Laravel\Nova\Fields\ActionFields  $fields
     * @param  \Illuminate\Support\Collection  $models
     * @return mixed
     */
    public function handle(ActionFields $fields, Collection $models)
    {
        foreach ($models as $model) {
            if($fields->get('url')!=null){
                $model->url = FileUploader::store('/about/',$fields->get('url'));
            }
            if($fields->get('mission_icon')!=null){
                $model->mission_icon = FileUploader::store('/about/',$fields->get('mission_icon'));
            }
            if($fields->get('vision_icon')!=null){
                $model->vision_icon = FileUploader::store('/about/',$fields->get('vision_icon'));
            }
            $model->save();
        }
        return Action::message('Updated');
    }

    /**
     * Get the fields available on the action.
     *
     * @param  \Laravel\Nova\Http\Requests\NovaRequest  $request
     * @return array
     */
    public function fields(NovaRequest $request)
    {
        return [
            Image::make('Main Image','url'),
            Image::make('Vision Icon','mission_icon'),
            Image::make('Mission Icon','vision_icon'),
        ];
    }
}
