<?php

namespace App\Admin\Controllers;

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

class UserController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(User::with(['info']), function (Grid $grid) {
            $grid->model()->orderByDesc('id');
            $grid->column('id')->sortable();
            $grid->column('avatar')->display(function () {
                $str  = "";
                $str .= "<div style='margin-right:10px;display: flex;align-items: center'>";
                $str .= '<img data-action="preview-img" src="' . $this->avatar . '" style="height:50px;width:50px;cursor:pointer;margin-right:10px;" class="img img-thumbnail">';
                $str .= '<div>';
                $str .= '<p style="margin-bottom: 5px">' . $this->nickname . '</p>';
                $str .= '<p style="margin-bottom: 0px">' . $this->mobile . '</p>';
                $str .= "</div>";
                $str .= "</div>";
                return $str;
            });
            $grid->column('info.is_vip','是否会员')->using([0 => '否', 1 => '是'])->label(['gray', 'primary']);
            $grid->column('info.integral','金币余额');
            $grid->column('info.platform','注册平台')->using([1 => '抖音', 2 => '快手'])->label(['gray', 'primary']);;
            $grid->column('created_at','注册时间');

            $grid->filter(function (Grid\Filter $filter) {
                $filter->panel();
                $filter->equal('id','ID')->width(3);
                $filter->like('nickname')->width(3);
                $filter->equal('mobile')->width(3);
                $filter->equal('info.is_vip','是否会员')->select(function (){
                    return ['否','是'];
                })->width(3);
                $filter->equal('info.platform','是否会员')->select(function (){
                    return [1 => '抖音', 2 => '快手'];
                })->width(3);

            });

            $grid->disableCreateButton();
            $grid->disableDeleteButton();
            $grid->disableRowSelector();
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new User(), function (Show $show) {
            $show->field('id');
            $show->field('nickname');
            $show->field('avatar');
            $show->field('password');
            $show->field('email');
            $show->field('mobile');
            $show->field('open_id');
            $show->field('union_id');
            $show->field('status');
            $show->field('email_verified_at');
            $show->field('remember_token');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new User(), function (Form $form) {
            $form->display('id');
            $form->text('nickname');
            $form->text('avatar');
            $form->text('password');
            $form->text('email');
            $form->text('mobile');
            $form->text('open_id');
            $form->text('union_id');
            $form->text('status');
            $form->text('email_verified_at');
            $form->text('remember_token');

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