<?php

namespace App\Admin\Actions\backstage\Notice;

use App\Models\CommunityNotice;
use App\Models\Organization;
use Encore\Admin\Actions\Action;
use Encore\Admin\Form;
use Illuminate\Http\Request;
use Encore\Admin\Facades\Admin;

class MassDistribution extends Action
{
    public $name = '群发社区通告';
    protected $selector = '.mass-distribution';

    public function handle(Request $request)
    {
        // $request ...
        $title = request('title');
        $organization_id = request('organization_id');
        $type = request('type');
        $content = request('content');
        $admin_id = Admin::user()->id;
        $arr = [
            'title' =>$title,
            'organization_id' =>0,
            'type' =>$type,
            'content' =>$content,
            'admin_id' =>$admin_id,
        ];
        foreach ($organization_id as $value)
        {
            $arr['organization_id'] = $value;
            CommunityNotice::create($arr);
        }
        return $this->response()->success('群发社区通告成功')->refresh();
    }
    public function form(){
        $this->text('title','标题')->required();
        $this->multipleSelect('organization_id','机构')->options(Organization::pluck('name','id')->toArray())->required();
        $this->select('type','类型')->options([1=>'社区通告'])->required();
        $this->textarea('content','内容')->required();
    }
    public function html()
    {
        return <<<HTML
        <a class="btn btn-sm btn-default mass-distribution">群发社区通告</a>
HTML;
    }
}