MembershipController.php 5.46 KB
<?php

namespace App\Admin\Controllers;

use App\Admin\Renderable\MembershipGoodsTable;
use App\Admin\Repositories\Membership;
use App\Models\MembershipGood;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Form\NestedForm;

class MembershipController extends AdminController
{
    protected $title = '会员';
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Membership(), function (Grid $grid) {

            // 设置自定义视图
            $grid->setActionClass(Grid\Displayers\Actions::class);

            $grid->column('id',__('ID'))->sortable();

            $grid->column('name');
            $grid->column('price');
            $grid->column('line_price');
            $grid->column('limited_days');
            $grid->column('limit_unit');
            $grid->column('intro');
            $grid->column('state');
            $grid->column('sn');

            $grid->column('video_url');
            $grid->column('video_cover');
            $grid->column('bg_images');
            $grid->column('visits');
            $grid->column('virtual_sales');
            $grid->column('sales');

            $grid->column('created_at');
            $grid->column('updated_at')->sortable();
        
            $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 Membership(), function (Show $show) {
            $show->field('id');
            $show->field('name');
            $show->field('price');
            $show->field('line_price');
            $show->field('limited_days');
            $show->field('limit_unit');
            $show->field('intro');
            $show->field('state');
            $show->field('sn');

            $show->field('is_video');
            $show->field('video_url');
            $show->field('video_cover');
            $show->field('bg_images');
            $show->field('visits');
            $show->field('virtual_sales');
            $show->field('sales');
            $show->field('created_at');
            $show->field('updated_at');
            
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Membership(), function (Form $form) {
            $form->display('id');
            $form->block(12, function (Form\BlockForm $form) {
                // 设置标题
                $form->title('基本设置');
                // 显示底部提交按钮
                $form->showFooter();
                // 设置字段宽度
                $form->width(8, 2);


                $form->radio('terminal')->addElementClass('terminal')
                    ->options([1 => 'Android', 2 => 'IOS'])->default(2);

                $form->text('title')->addElementClass('name');
                $form->textarea('intro')->addElementClass('intro');

                $form->radio('bg_type')->addElementClass('bg_type')
                    ->options([1 => '单图', 2 => '轮播图', 3 => '视频'])->default(1)
                    ->when([1,2],function (Form\BlockForm $form){
                        $form->multipleImage('bg_images')
                            ->limit(5)
                            ->uniqueName()
                            ->addElementClass('bg_images');
                    })
                    ->when(3,function (Form\BlockForm $form){
                        $form->file('video_url')
                            ->accept('mp4')
                            ->autoUpload()
                            ->uniqueName()
                            ->addElementClass('video_url');

                        $form->image('video_cover')
                            ->uniqueName()
                            ->addElementClass('video_cover');
                    });

                $form->radio('state')->options(['不显示', '显示'])->default(0);


                $form->radio('is_bind_old')->options(['新增会员商品', '选择已有商品'])->default(0)
                    ->when(0,function (Form\BlockForm $form){
                        $form->table('membership_goods','新赠会员商品', function (NestedForm $table) {
                            $table->currency('price')->symbol('¥')->addElementClass('price');
                            $table->currency('line_price')->symbol('¥')->addElementClass('line_price');
                            $table->text('limited_days')->addElementClass('limited_days');
                            $table->text('limit_unit')->addElementClass('limit_unit');

                        });
                    })
                    ->when(1,function (Form\BlockForm $form){
                        $form->selectTable('membership_goods')
                            ->title('会员商品')
                            ->from(MembershipGoodsTable::make())
                            ->model(MembershipGood::class, 'id', 'price');
                    });
            });

//            $form->block(4, function (Form\BlockForm $form) {
//                $form->width(9, 1);
//                $form->html(view('admin.form.phone'));
//            });

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