<?php

namespace App\Admin\Controllers\Share;

use App\Admin\Actions\Grid\WithdrawReview;
use App\Models\UserWithdraw;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class UserWithdrawController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(UserWithdraw::with('user'), function (Grid $grid) {
            $grid->model()->orderBy('status')->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->user->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->user->nickname . '</p>';
                $str .= '</div>';
                $str .= '</div>';

                return $str;
            });
            $grid->column('name');
            $grid->column('phone_num');
            $grid->column('type')->using(config('global.withdraw_type'))
                ->label(['default', 'primary', 'success', 'info']);
            $grid->column('account');
            $grid->column('bank_name');
            $grid->column('price')->label('primary');
            $grid->column('discount')->label('warning');
            $grid->column('real', '实际打款金额')->display(function () {
                return number_format($this->price - $this->discount, 2);
            })->label('success');
            $grid->column('status', '状态')
                ->using(config('global.withdraw_status'))
                ->label(['default', 'primary', 'success', 'danger'])
            ;
            $grid->column('created_at', '申请时间');
            $grid->column('review_at', '审核时间');
            $grid->column('withdraw_at', '打款时间');
            $grid->actions(function (Grid\Displayers\Actions $actions) {
                if (2 != $actions->row->status) {
                    $actions->append(new WithdrawReview(UserWithdraw::class, $actions->row->id, $actions->row->status));
                }
            });

            $grid->filter(function (Grid\Filter $filter) {
                $filter->panel();
                $filter->between('created_at', '申请时间')->date()->width(3);
                $filter->equal('status', '状态')->select(config('global.withdraw_status'))->width(3);
                $filter->like('nickname', '昵称')->width(2);
                $filter->like('name', '姓名')->width(2);
                $filter->equal('mobile', '手机号')->width(2);
            });

            $grid->disableCreateButton();
            $grid->disableEditButton();
            $grid->disableViewButton();
        });
    }

    /**
     * Make a show builder.
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new UserWithdraw(), function (Show $show) {
            $show->field('id');
            $show->field('user_id');
            $show->field('name');
            $show->field('type');
            $show->field('account');
            $show->field('price');
            $show->field('desc');
            $show->field('status');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new UserWithdraw(), function (Form $form) {
            $form->display('id');
            $form->text('user_id');
            $form->text('name');
            $form->text('type');
            $form->text('account');
            $form->text('price');
            $form->text('desc');
            $form->text('status');

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