<?php

namespace App\Admin\Actions\backstage\Docters;

use App\Models\Docter;
use App\Models\DocterLabel;
use Encore\Admin\Actions\RowAction;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;

class LabelManagement extends RowAction
{
    public $name = '标签管理';

    public function handle(Model $model,Request $request)
    {
        // $model ...
        $label_name = $request->get('name');
        if ($label_name == null)
        {
            $label_name = [];
        }
        $label_num = count($label_name);
        if($label_num > 2)
        {
            return $this->response()->error('不能选择超过两个标签')->refresh();
        }
        if ($label_num == 0)
        {
            Docter::where('id',$model->id)->update(['label'=>$label_name]);
            return $this->response()->success('修改标签成功')->refresh();
        }else
        {
            Docter::where('id',$model->id)->update(['label'=>$label_name]);
            return $this->response()->success('修改标签成功')->refresh();
        }

    }
    public function form()
    {
        $label = Docter::where('id',$this->row->id)->pluck('label');
        $result = [];
        array_walk_recursive($label, function($value) use (&$result) {
            array_push($result, $value);
        });
        $this->multipleSelect('name', '请选择标签')
            ->options(DocterLabel::where('status','1')->get()->pluck('label_name','id'))
            ->value($result);
    }
}