<?php

namespace App\Admin\Controllers;

use App\Models\ProductCategory;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class ProductCategoryController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new ProductCategory(), function (Grid $grid) {
            $grid->model()->orderByDesc('sort');
            $grid->column('id')->sortable();
            $grid->column('name');
            $grid->column('level')->using(config('global.cat_level'));
            $grid->column('is_opened')->switch();
            $grid->column('sort')->editable();
            $grid->column('created_at');
            $grid->disableViewButton();
        });
    }


    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new ProductCategory(), function (Form $form) {
            $form->display('id');
            $form->radio('level')
                ->when(2, function (Form $form){
                    $options = ProductCategory::select(['id','name'])
                        ->where('level',1)
                        ->get()
                        ->pluck('name', 'id');
                    $form->select('pid')->options($options);
                })
                ->options(config('global.cat_level'))
                ->default(1);
            $form->text('name')->required();
            $form->radio('is_opened')
                ->options(config('global.bool_status'))
                ->default(1);
            $form->number('sort');

            $form->disableViewButton();
            $form->disableDeleteButton();
            $form->disableListButton();
            $form->disableEditingCheck();
            $form->disableViewCheck();
            $form->disableCreatingCheck();
        });
    }
}
