<?php

namespace App\Admin\Controllers\Share;

use App\Models\User;
use App\Models\UserShare;
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(['parent.info', 'info'])->withCount('child'), function (Grid $grid) {
            $grid->model()->where('is_share', 1)
                ->orderByDesc('become_share_at');
            $grid->column('id', '用户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 . '" onerror="this.src=\'https://fourtiao.oss-cn-beijing.aliyuncs.com/zhangsiye/images/6b40343b27263be34cf3212bf44f74c3.png\'" 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: 5px">' . $this->mobile . '</p>';
                $str .= '</div>';
                $str .= '</div>';

                return $str;
            });
            $grid->column('info.platform', '所属平台')
                ->using(config('global.platform'))
                ->label([1 => 'primary', 2 => 'success', 3 => 'info']);
            $grid->column('income');
            $grid->column('total_income');
            $grid->column('parent_id')->display(function () {
                return $this->parent ? $this->parent->nickname : '-';
            })->label('primary');
            $grid->column('child_count');
            $grid->column('become_share_at');

            $grid->filter(function (Grid\Filter $filter) {
                $filter->panel();
                $filter->equal('info.platform', '所属平台')->select(config('global.platform'))->width(3);
                $filter->like('nickname', '昵称')->width(3);
                $filter->equal('mobile', '手机号')->width(3);
            });

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

    /**
     * Make a show builder.
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new UserShare(), function (Show $show) {
            $show->field('id');
            $show->field('user_id');
            $show->field('child_id');
            $show->field('income');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

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

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