<?php

namespace App\Admin\Controllers;

use App\Models\Product;
use App\Models\ProductSpec;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Http\Controllers\AdminController;

class ProductSpecController extends AdminController
{

    protected $product_id;
    protected $product;

    public function __construct()
    {
        $route = \request()->route();
        $this->product_id = $route->parameters['id'];
        $this->product = Product::find($this->product_id);

    }
    protected function content(Content $content)
    {
        return $content
            ->translation($this->translation())
            ->breadcrumb(
                ['text' => $this->product->name,'url' => '#'],
                ['text' => '规格管理'],
            )
            ->title($this->title())
            ->description('规格管理');
    }

    public function index(Content $content, $productId = 0)
    {
        return $this->content($content)
            ->body($this->grid());
    }


    public function create(Content $content,$productId = 0)
    {
        return $this->content($content)->body($this->form());
    }


    public function update($productId, $id = 0)
    {
        return $this->form()->update($id);
    }

    public function store($episodeId = 0)
    {
        return $this->form()->store();
    }

    public function edit($productId, Content $content, $id = 0)
    {
        return $this->content($content)->body($this->form()->edit($id));
    }

    public function destroy($productId, $id = 0)
    {
        parent::destroy($id);
        return $this->form()->destroy($id);
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(ProductSpec::where('product_id', $this->product_id), function (Grid $grid) {
            $grid->model()->orderByDesc('sort');
            $grid->column('id')->sortable();
            $grid->column('name')->label('primary');
            $grid->column('is_opened')->switch();
            $grid->column('sort')->editable();
            $grid->column('specs')->display(function (){
                $html = '';
                foreach ($this->specs as $key => $specs){
                    $br = $key % 3 == 0 && $key != 0 ? '<br>': '';
                    $html .= "<span class='label' style='background:#586cb1;margin: 0 5px 5px 0;display: inline-block;'> {$specs['name']}(&yen;{$specs['price']})</span>{$br}";
                }
                return $html;
            });

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

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new ProductSpec(), function (Form $form) {
            $form->display('id');

            $form->hidden('product_id')->value($this->product_id);
            $form->text('name')->required();
            $form->radio('is_opened')->options(config('global.bool_status'))->default(1);
            $form->number('sort');

            $form->table('specs', '规格', function (Form\NestedForm $table){
                $table->text('name','规格名称')->required();
                $table->decimal('price','规格价格')->required();
            });


            $form->disableViewButton();
            $form->disableDeleteButton();
            $form->disableListButton();
            $form->disableEditingCheck();
            $form->disableViewCheck();
            $form->disableCreatingCheck();

        });
    }
}
