<?php

/**
 * @var \Illuminate\Database\Eloquent\Collection $menu
 */

use Itguild\EloquentTable\ViewEloquentTable;
use Itguild\EloquentTable\ViewJsonTableEloquentModel;
use kernel\models\Menu;
use kernel\widgets\IconBtn\IconBtnDeleteWidget;
use kernel\widgets\IconBtn\IconBtnEditWidget;
use kernel\widgets\IconBtn\IconBtnListWidget;

$table = new ViewEloquentTable(new ViewJsonTableEloquentModel($menu, [
    'params' => ["class" => "table table-bordered", "border" => "2"],
    'baseUrl' => "/admin/settings/menu",
]));

$table->beforePrint(function () use ($menu) {
    $btn = IconBtnListWidget::create(['url' => '/admin/settings/menu'])->run();
    $btn .= IconBtnEditWidget::create(['url' => '/admin/settings/menu/update/' . $menu->id])->run();
    $btn .= IconBtnDeleteWidget::create(['url' => '/admin/settings/menu/delete/' . $menu->id])->run();
    return $btn;
});
$table->rows([
    'parent_id' => (function ($data) {
        if ($data == 0) return null;
        return Menu::find($data)->label;
    }),
    'icon_file' => function ($data) {
        return $data ? "<img src='$data' width='300px'>" : "";
    }
]);
$table->create();
$table->render();