<?php

namespace App\Livewire\Datatable\Master;

use App\Models\Warehouse;
use App\Traits\Datatable\WithAppendColumn;
use App\Traits\Datatable\WithConfiguration;
use App\Traits\Datatable\WithPrependColumn;
use Illuminate\Database\Eloquent\Builder;
use Rappasoft\LaravelLivewireTables\DataTableComponent;
use Rappasoft\LaravelLivewireTables\Views\Column;

class WarehousesTable extends DataTableComponent
{
    use WithAppendColumn, WithConfiguration, WithPrependColumn;

    protected $model = Warehouse::class;

    public function columns(): array
    {
        return [
            Column::make('Nama', 'name')->searchable()->sortable(),

            Column::make('Aksi')
                ->label(function ($row) {
                    $actions = '';

                    if (auth()->user()->can('show warehouse')) {
                        $actions .= view('components.actions.table.show', [
                            'showRoute' => route('studio.master.warehouse.show', $row->hash),
                        ])->render();
                    }

                    if (auth()->user()->can('update warehouse')) {
                        $actions .= view('components.actions.table.edit-modal', [
                            'id' => $row->hash,
                            'method' => 'update',
                            'modalTitle' => 'Ubah Gudang',
                        ])->render();
                    }

                    if (auth()->user()->can('delete warehouse')) {
                        $actions .= view('components.actions.table.delete', [
                            'id' => $row->hash,
                        ])->render();
                    }

                    return $actions;
                })
                ->html()
                ->hideIf(auth()->user()->cannot('view warehouse') && auth()->user()->cannot('update warehouse') && auth()->user()->cannot('delete warehouse')),
        ];
    }

    public function builder(): Builder
    {
        return Warehouse::select('id', 'name');
    }
}
