<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\SiteUser;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Admin;

class SiteUserController extends AdminController
{
    /**
     * page index
     */
    public function index(Content $content)
    {
        return $content
            ->header('列表')
            ->description('全部')
            ->breadcrumb(['text'=>'列表','url'=>''])
            ->body($this->grid());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new SiteUser(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('username');
            $grid->column('nick_name');
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
            });
            $grid->model()->orderBy('id', 'desc');
            $grid->disableViewButton();
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new SiteUser(), function (Form $form) {
            $form->text('username')->required();
            $form->text('nick_name')->required();
            $form->password('password')->required();
            $form->saving(function (Form $form) {
                if ($form->password) {
                    $form->password = bcrypt($form->password);
                }
            });

            $form->disableViewButton();
            $form->disableViewCheck();
        });
    }
}
