<?php

namespace App\Admin\Actions\User;

use Encore\Admin\Actions\RowAction;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;

class Profit extends RowAction
{
    public $name = '设置分润';

    public function handle(Model $model, Request $request)
    {
        $datas = $request->code;

        $codes = $model->code;
        foreach ($codes as $key => $code) {
            $code->profit = round($datas[$code->id], 2);
            $code->save();
        }
        return $this->response()->success('操作成功')->refresh();
    }

    public function form(Model $model)
    {
        $codes = $model->code;
        foreach ($codes as $key => $code) {
            $this->text("code[{$code->id}]", $code->name)->value($code->profit)->required();
        }

    }
}
