<?php

namespace App\Orchid\Layouts\Carousel;

use Orchid\Screen\Layouts\Table;
use Orchid\Screen\TD;

use App\Models\Slide;
use Orchid\Screen\Fields\Input;
use Orchid\Screen\Actions\Link;
use Orchid\Screen\Actions\Button;
use Orchid\Screen\Actions\DropDown;

class SlideListLayout extends Table {

  protected $target = 'slides';

  protected function columns(): iterable {
    return [
      TD::make('image', 'Image')
        ->cantHide()
        ->render(function (Slide $slide) {
          return '<img src="' . $slide->image . '" width="50" height="50" style="object-fit: cover; object-position: center;">';
        }),

      TD::make('title_line_1', 'Description')
        ->cantHide()
        ->sort()
        ->filter(Input::make())
        ->render(function (Slide $slide) {
          return Link::make(ucwords($slide->title_line_1) . ' ' . ucwords($slide->title_line_2))->route('platform.slide.edit', $slide->id);
        }),

      TD::make('button_text', 'Button Text')
        ->sort()
        ->cantHide()
        ->filter(Input::make())
        ->render(function (Slide $slide) {
          return '<div style="overflow: hidden; text-overflow: ellipsis; white-space: nowrap; text-transform: uppercase;">' . $slide->button_text . '</div>';
        }),

      TD::make('updated_at', __('Last edit'))
        ->cantHide()
        ->sort()
        ->filter(Input::make())
        ->render(function (Slide $slide) {
          return $slide->updated_at->format('M j, Y');
        }),

      TD::make('id', 'ID')
        ->cantHide()
        ->sort()
        ->filter(Input::make())
        ->align(TD::ALIGN_CENTER)
        ->render(function (Slide $slide) {
          return '#' . $slide->id;
        }),

      TD::make(__('Actions'))
        ->cantHide()
        ->align(TD::ALIGN_CENTER)
        ->width('100px')
        ->render(fn (Slide $slide) => DropDown::make()
          ->icon('bs.three-dots-vertical')
          ->list([
            Link::make(__('Edit'))
              ->route('platform.slide.edit', $slide->id)
              ->icon('bs.pencil'),

            Button::make('Delete')
              ->icon('bs.trash3')
              ->confirm('Are you sure you want to delete this slide ?')
              ->method('remove')
              ->parameters([
                'slide' => $slide->id,
              ]),
          ])),
    ];
  }
}
