<?php

namespace App\Admin\Controllers;

use App\Admin\Metrics\User\NewDevices;
use App\Admin\Metrics\User\NewUsers;
use App\Admin\Metrics\User\TotalUsers;
use App\Models\PaymentLog;
use App\Models\PaymentLogModel;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Layout\Row;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Illuminate\Database\Eloquent\Model;
use Dcat\Admin\Layout\Content;
class PaymentLogController extends AdminController
{


//    public function index(Content $content)
//    {
//        return $content
//            ->header('购买记录')
//            ->description('购买记录列表')
//            ->body(function (Row $row) {
//                $row->column(4, new TotalUsers());
////                $row->column(4, new NewUsers());
////                $row->column(4, new NewDevices());
//            })
//            ->body($this->grid());
//    }


    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new PaymentLogModel());
        $grid->model()->leftJoin('users_info','payment_log.user_id','=','users_info.user_id')->select(['payment_log.*','users_info.nickname','users_info.avatar','users_info.area'])->orderBy('id','desc');

        $grid->column('id')->sortable();
        $grid->column('avatar','头像')->image("",50);
        $grid->column('nickname','昵称');
        $grid->column('order_no');
        $grid->column('price');
        $grid->column('status')->using([0=>'待支付',1=>'已支付'])->label(['gray','green']);
        $grid->column('content')->display(function ($res){
            $res = json_decode($res,true);
            return $res['title'];
        });
        $grid->column('payment')->using([1=>'微信',2=>'支付宝'])->label(['gray','green','blue']);
        $grid->column('created_at');
        $grid->column('updated_at')->sortable();

        $grid->filter(function (Grid\Filter $filter) {
            $filter->equal('id');
            $filter->equal('user_id','用户ID');
            $filter->like('nickname','昵称');
            $filter->equal('status','支付状态')->select(['0'=>'待支付','1'=>'已支付']);
            $filter->equal('payment','支付方式')->select(['1'=>'微信','2'=>'支付宝']);
        });
//        $grid->actions(function (Grid\Displayers\Actions $actions) {
//            $actions->disableView();
//            $actions->disableEdit();
//            $actions->disableDelete();
//        });
        $grid->disableCreateButton();
        $grid->disableActions();
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new PaymentLogModel(), function (Show $show) {
            $show->field('id');
            $show->field('order_no');
            $show->field('price');
            $show->field('status');
            $show->field('content');
            $show->field('payment');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new PaymentLogModel(), function (Form $form) {
            $form->display('id');
            $form->text('order_no');
            $form->text('price');
            $form->text('status');
            $form->text('content');
            $form->text('payment');

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