<?php

namespace App\Admin\Controllers;

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

class UserFriendController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     * @throws \Dcat\Admin\Exception\InvalidArgumentException
     */
    protected function grid()
    {
        $grid = new Grid(new UserFollow());
        $grid->model()->with(['user:id,avatar,name','target_user:id,avatar,name'])->orderBy('id','desc');

        $grid->column('id')->sortable();

        $grid->combine('user', ['user.id','user.avatar', 'user.name'],trans('user-friend.fields.messaging_user'));
        $grid->combine('target_user', ['target_user.id','target_user.avatar', 'target_user.name'],trans('user-friend.fields.receiving_message_user'));

        $grid->column('user.id');
        $grid->column('user.avatar')->image('',50);
        $grid->column('user.name');

        $grid->column('target_user.id');
        $grid->column('target_user.avatar')->image('',50);
        $grid->column('target_user.name');
//        $grid->column('status');
//        $grid->column('created_at');
        $grid->column('updated_at')->sortable();

        $grid->disableCreateButton();
        $grid->filter(function (Grid\Filter $filter) {
            $filter->equal('id');
            $filter->like('user.name');

        });
        $grid->disableRowSelector();
        //批量操作
        $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 UserFriend(), function (Show $show) {
            $show->field('id');
            $show->field('user_id');
            $show->field('to_uid');
            $show->field('status');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new UserFriend(), function (Form $form) {
            $form->display('id');
            $form->text('user_id');
            $form->text('to_uid');
            $form->text('status');

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