<?php

namespace Modules\Mall\Http\Controllers\Admin;

use App\Admin\Traits\WithUploads;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Illuminate\Routing\Controller;
use Modules\Mall\Models\Goods;
use Modules\Mall\Models\GoodsSku;

class SkuController extends Controller
{

    use WithUploads;

    public function index(Content $content, Goods $good): Content
    {
        return $content
            ->header($good->name)
            ->description('价格表')
            ->body($this->grid($good));
    }

    public function grid($goods): Grid
    {
        $grid = new Grid(new GoodsSku());

        $grid->model()->where('goods_id', $goods->id);

        $grid->actions(function (Grid\Displayers\Actions $actions) {
            $actions->disableView();
            $actions->disableDelete();
        });
        $grid->disableCreateButton();

        $grid->column('cover', '封面图')->image('', 80);
        $grid->column('unit', '产品单元');
        $grid->column('price', '售价');
        $grid->column('score', '积分/原石');
        $grid->column('assets', '资产');
        $grid->column('stock', '库存');
        $grid->column('weight', '重量');
        $grid->column('created_at', '创建时间');

        return $grid;
    }

    public function edit(Content $content, Goods $good, GoodsSku $sku)
    {
        return $content
            ->header($good->name)
            ->description('编辑价格')
            ->body($this->form($good)->edit($sku->id));
    }

    public function form($good): Form
    {
        $form = new Form(new GoodsSku());

        $this->cover($form);
        $form->currency('price', '销售价格');
        $form->currency('score', '积分/原石');
        $form->currency('assets', '资产');
        $form->number('stock', '库存');
        $form->text('weight', '重量')->setWidth(2);

        return $form;
    }

    public function update(Goods $good, GoodsSku $sku)
    {
        return $this->form($good)->update($sku->id);
    }

}