<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\UserMemberOrder;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class UserMemberOrderController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new UserMemberOrder(), function (Grid $grid) {
            $grid->model()->with('user:id,name,company_name,avatar')->orderByDesc('id');
            $grid->column('id')->sortable();
            $grid->column('user_id')->display(function (){
                $str = '';
                if(!empty($this->user)){
                    $str .= '<div>';
                    $str .= '<p style="margin-bottom: 5px">'. admin_trans('user.fields.name').'：' . $this->user->name . '</p>';
                    $str .= '<p style="margin-bottom: 5px">' . admin_trans('user.fields.company_name').'：' . $this->user->company_name . '</p>';
                    $str .= "</div>";
                }
                return $str;
            });
            $grid->column('pay_type')->display(function (){
                return $this->pay_type_text;
            })->label();
            $grid->column('days');
            $grid->column('money');
            $grid->column('order_no');
            $grid->column('status')->display(function (){
                return $this->status_text;
            })->label(['gray', 'success']);
            $grid->column('created_at');
            $grid->disableCreateButton();
            // 禁用删除按钮
            // 也可以通过以下方式启用或禁用按钮
            $grid->disableDeleteButton();
            $grid->disableEditButton();
            $grid->disableQuickEditButton();
            $grid->disableViewButton();
            $grid->disableActions();
            $grid->filter(function (Grid\Filter $filter) {
                $filter->panel();
                $filter->equal('id')->width(4);

            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new UserMemberOrder(), function (Show $show) {
            $show->model()->with('user:id,name,company_name,avatar');
            $show->field('id')->width(4);
            $show->field('user_id')->width(4)->unescape()->as(function (){
                $str = '';
                if(!empty($this->user)){
                    $str .= '<div>';
                    $str .= '<p style="margin-bottom: 5px">'. admin_trans('user.fields.name').'：' . $this->user->name . '</p>';
                    $str .= '<p style="margin-bottom: 5px">' . admin_trans('user.fields.company_name').'：' . $this->user->company_name . '</p>';
                    $str .= "</div>";
                }
                return $str;
            });
            $show->field('pay_type_text',admin_trans_field('pay_type'))->width(4);
            $show->field('status_text',admin_trans_field('status'))->width(4);
            $show->field('days')->width(4);
            $show->field('money')->width(4);
            $show->field('created_at')->width(4);
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new UserMemberOrder(), function (Form $form) {
            $form->display('id');
            $form->text('user_id');
            $form->text('pay_type');
            $form->text('status');
            $form->number('days');

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