<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\Msg;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class MsgController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Msg(), function (Grid $grid) {
            $grid->model()->orderByDesc("id");
            $grid->column('id')->sortable();
//            $grid->column('type');
//            $grid->column('user_id');
//            $grid->column('to_user_id');
            $grid->column('title');
            $grid->column('content');
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();
            $grid->actions(function (Grid\Displayers\Actions $actions) {
                $actions->disableEdit();
                $actions->disableView();
            });
            $grid->filter(function (Grid\Filter $filter) {
                $filter->panel();
                $filter->like('title')->width(4);
                $filter->like('content')->width(4);
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Msg(), function (Show $show) {
            $show->field('id');
            $show->field('type');
            $show->field('user_id');
            $show->field('to_user_id');
            $show->field('title');
            $show->field('content');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Msg(), function (Form $form) {
            $form->display('id');
            $form->hidden('type')->default(6);
            $form->hidden('user_id')->default(0);
            $form->hidden('to_user_id')->default(0);
            $form->hidden('title')->default("系统通知");
            $form->textarea('content');

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
