<?php

namespace App\Admin\Controllers;

use App\Admin\Actions\Chat\ChatAction;
use App\Admin\Actions\Chat\ChatLog;
use App\Models\ChatList;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class ChatListController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new ChatList());
        $grid->model()->with(['user','user_info','to_user','to_user_info'])->orderBy('id','desc');
        $grid->column('id')->sortable();
        $grid->combine('user', ['user_info.avatar', 'user_info.nickname'],'发消息用户');
        $grid->combine('to_user', ['to_user_info.avatar', 'to_user_info.nickname'],'接收消息用户');

        $grid->column('user_info.avatar','头像')->image('',50);
        $grid->column('user_info.nickname','昵称');

        $grid->column('to_user_info.avatar','头像')->image('',50);
        $grid->column('to_user_info.nickname','昵称');

//        $grid->column('last_msg');
//        $grid->column('last_time');
        $grid->column('atime');
//        $grid->column('un_read');
        $grid->disableCreateButton();
        $grid->filter(function (Grid\Filter $filter) {
            $filter->equal('id');
            $filter->like('user_info.nickname','昵称');

        });
        //批量操作
        $grid->batchActions(function (Grid\Tools\BatchActions $batch) {
            $batch->disableDelete();
        });
        //操作管理
        $grid->actions(function (Grid\Displayers\Actions $actions) {
            $actions->disableView();
            $actions->disableEdit();

            $actions->append(new ChatAction());
        });
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new ChatList(), function (Show $show) {
            $show->field('id');
            $show->field('user_id');
            $show->field('to_user_id');
            $show->field('last_msg');
            $show->field('last_time');
            $show->field('atime');
            $show->field('un_read');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new ChatList(), function (Form $form) {
            $form->display('id');
            $form->text('user_id');
            $form->text('to_user_id');
            $form->text('last_msg');
            $form->text('last_time');
            $form->text('atime');
            $form->text('un_read');
        });
    }
}
