<?php

namespace App\Orchid\Screens\Category;

use App\Models\Category;
use Orchid\Screen\Screen;
use Orchid\Screen\Actions\Link;
use Orchid\Support\Facades\Alert;
use Illuminate\Support\Facades\File;
use App\Orchid\Layouts\Category\CategoryListLayout;

class CategoryListScreen extends Screen {
  public function query(): iterable {
    return [
      'categories' => Category::filters()->defaultSort('updated_at', 'desc')->paginate(10)
    ];
  }

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

  public function commandBar(): iterable {
    return [
      Link::make('Create new')
        ->icon('bs.plus-circle')
        ->route('platform.category.create')
    ];
  }


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

  public function remove(Category $category) {

    $image_path = $category->image;
    $image_path = str_replace(asset(''), '', $image_path);
    File::exists($image_path) && File::delete($image_path);

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