<?php

namespace App\Admin\Actions\Area;

use App\Models\Area;
use Encore\Admin\Actions\RowAction;
use Illuminate\Http\Request;

class SubStock extends RowAction
{

    public $name = '减少库存';

    public function handle(Area $area, Request $request)
    {
        try {
            $number = $request->number;

            $res = Area::addStock($area, -$number, false);

            if ($res === true) {
                return $this->response()->success('增加库存成功')->refresh();
            } else {
                return $this->response()->error('增加库存成功失败,'.$res)->refresh();
            }
        } catch (\Exception $exception) {
            return $this->response()->error('增加库存成功失败,'.$exception->getMessage())->refresh();
        }

    }

    public function form(Area $area)
    {
        $this->integer('number', '减少库存数')
            ->default(0)
            ->help('1份=2箱')
            ->required();
    }

}
