<?php

namespace App\Orchid\Layouts\Banner;

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

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

class BannerListLayout extends Table {

  protected $target = 'banners';

  protected function columns(): iterable {
    return [
      TD::make('image', 'Image')
        ->cantHide()
        ->render(function (Banner $banner) {
          return '<img src="' . $banner->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 (Banner $banner) {
          $title1 = ucwords($banner->title_line_1);
          $title2 = ucwords($banner->title_line_2);
          return Link::make($title1 . ' ' . $title2)->route('platform.banner.edit', $banner->id);
        }),

      TD::make('button_text', 'Button Text')
        ->cantHide()
        ->sort()
        ->filter(Input::make())
        ->render(function (Banner $banner) {
          return strtoupper($banner->button_text);
        }),

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

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

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

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

          ])),
    ];
  }
}
