<?php

namespace App\Orchid\Screens\Banner;

use App\Models\Banner;
use Orchid\Screen\Screen;
use Orchid\Screen\Actions\Link;
use Orchid\Support\Facades\Alert;
use Illuminate\Support\Facades\File;
use App\Orchid\Layouts\Banner\BannerListLayout;

class BannerListScreen extends Screen {
  public $banners;

  public function query(): iterable {
    return [
      'banners' => Banner::filters()->defaultSort('updated_at', 'desc')->paginate()
    ];
  }

  public function name(): ?string {
    return 'Banners';
  }

  public function description(): ?string {
    return 'You can create maximum 3 banners.';
  }

  public function commandBar(): iterable {
    return [
      Link::make('Create new')
        ->icon('bs.plus-circle')
        ->route('platform.banner.create')
        ->canSee($this->banners->count() < 3),
    ];
  }

  public function layout(): iterable {
    return [
      BannerListLayout::class
    ];
  }

  public function remove(Banner $banner) {
    $image_path = $banner->image;
    $image_path = str_replace(asset(''), '', $image_path);
    File::exists($image_path) && File::delete($image_path);

    $banner->delete();
    Alert::info('You have successfully deleted the banner.');
    return redirect()->route('platform.banner.list');
  }
}
