<?php

namespace App\Admin\Controllers;

use App\Models\Order;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class OrderController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Order(), function (Grid $grid) {
            $grid->model()->with(['userData','orderData'])->orderByDesc('id');
            $grid->column('id')->sortable();
            $grid->column('userData.name','昵称');
            $grid->column('userData.mobile','手机号');
            $grid->column('orderData.title','商品名称');
            $grid->column('state')->using([0 => '待支付', 1 => '已支付',2 => '支付失败'])
                ->dot(
                    [
                        0 => 'danger',
                        1 => 'success',
                        2 => 'primary',
                    ],
                    'danger' // 第二个参数为默认值
                );
            $grid->column('order');
            $grid->column('wx_order');
            $grid->column('amount');
            $grid->column('diamond');
            $grid->column('pay_at','支付时间');
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();
            $grid->quickSearch(['order', 'wx_order'])->placeholder('搜索...');
            $grid->filter(function (Grid\Filter $filter) {
                $filter->panel();
                $filter->between('created_at')->datetime();
                $filter->equal('state')->select([0 => '待支付', 1 => '已支付',2 => '支付失败']);
                $filter->where('mobile', function ($query) {
                    $query->whereHas('userData', function ($query) {
                        $query->where('mobile', 'like', "%{$this->input}%");
                    });
                }, '用户手机号');
            });
            $grid->disableViewButton();
            $grid->disableCreateButton();
            $grid->disableEditButton();
            $grid->disableDeleteButton();
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Order(), function (Show $show) {
            $show->field('id');
            $show->field('user_id');
            $show->field('state');
            $show->field('order');
            $show->field('wx_order');
            $show->field('amount');
            $show->field('diamond');
            $show->field('pay_at');
            $show->field('pay');
            $show->field('isPost');
            $show->field('config_id');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Order(), function (Form $form) {
            $form->display('id');
            $form->text('user_id');
            $form->text('state');
            $form->text('order');
            $form->text('wx_order');
            $form->text('amount');
            $form->text('diamond');
            $form->text('pay_at');
            $form->text('pay');
            $form->text('isPost');
            $form->text('config_id');

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