<?php

namespace App\Admin\Controllers;

use App\Admin\Actions\Grid\UserConsumeRecord as UserConsumeRecordGrid;
use App\Admin\Actions\Grid\UserEpisodeRecord  as UserEpisodeRecordGrid;
use App\Admin\Actions\Grid\UserRechargeRecord as UserRechargeRecordGrid;
use App\Admin\Actions\Grid\UserRechargeRecord;
use App\Models\User;
use App\Models\UserConsumeRecord;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Widgets\Modal;

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 . '" 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 .= "</div>";
                $str .= "</div>";
                return $str;
            });
            $grid->column('mobile','手机号');
            $grid->column('info.is_vip','是否会员')->using([0 => '否', 1 => '是'])->label(['gray', 'primary']);
            $grid->column('info.integral','订单数量');
            $grid->column('episode','消费记录')->display('消费记录')->modal(new UserEpisodeRecordGrid());
            $grid->column('recharge','充值记录')->display('充值记录')->modal(new UserRechargeRecordGrid());
            $grid->column('info.integral','现有金币数')->label('success');
            $grid->column('consumes','金币交易记录')->display('金币交易记录')->modal(new UserConsumeRecordGrid());
            $grid->column('info.platform','所属平台')->using(config('global.platform'))->label([1=>'primary',2=>'success',3=>'info']);
            $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(config('global.platform'))->width(3);

            });

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

    /**
     * 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(User::with(['info']), function (Form $form) {
            $form->display('id');
            $form->text('nickname')->disable();
            $form->image('avatar')->width(200)->disable();
            $form->text('open_id','openid')->disable();
            $form->radio('status','是否拉黑')
                ->options(config('global.bool_status'))
                ->default(0);

            $form->radio('info.is_vip','是否会员')
                ->options(config('global.bool_status'))
                ->default(0)
                ->when(1,function (Form $form){
                $form->date('info.start_at','会员开始时间')->default(0);
                $form->date('info.end_at','会员结束时间')->default(0);
            });

            $form->number('info.integral','金币');

            $form->display('created_at');

            $form->disableViewButton();
            $form->disableDeleteButton();
            $form->disableListButton();
            $form->disableEditingCheck();
            $form->disableViewCheck();
            $form->disableCreatingCheck();
        });
    }
}
