<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\UserShare;
use App\Models\UserWithdraw;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class IncomeRecordController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new UserShare(), function (Grid $grid) {
            $grid->with(['user','child','user.parent']);
            $grid->column('id')->sortable();
            $grid->column('user_id','名称')->display(function () {
                return $this->user->nickname;
            });
            $grid->column('phone_num','手机号')->display(function () {
                return $this->user->phone_num;
            });
            $grid->column('total_income','累计佣金')->display(function () {
                return $this->user->income;
            });
            $grid->column('total_withdraw','已提现佣金')->display(function () {
                return UserWithdraw::where('user_id',$this->user_id)->where('status',1)->sum('price');
            });
            $grid->column('recommend','推荐人')->display(function () {
                return $this->user->parent ? $this->user->parent->nickname : '无';
            });
            $grid->column('child_id','下线用户')->display(function () {
                return $this->child->nickname;
            });

            $grid->column('income','收益');
            $grid->column('created_at');


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

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');

            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @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');
        });
    }
}
