<?php

namespace App\Admin\Actions\Device;

use App\Model\UserDevice;
use App\Model\UserInfo;
use Encore\Admin\Actions\BatchAction;
use Illuminate\Database\Eloquent\Collection;

class BatchDel extends BatchAction
{
    public $name = '批量解绑';

    public function handle(Collection $collection)
    {
        $ids =[];
        $uid =session('list_uid');
        $user = UserInfo::find($uid);
        if ($user->role == 2){
            return $this->response()->error('请在设备中解绑')->refresh();
        }

        foreach ($collection as $model) {
            $ids[]=$model->id;
        }
        $res = UserDevice::where('user_id',$uid)->whereIn('device_id',$ids)->delete();
        if ($res)
            return $this->response()->info('解除绑定成功')->refresh();
        return $this->response()->error('操作失败，请重试')->refresh();
    }

    public function dialog()
    {
        $this->confirm('确定把该用户的所有设备解绑？');
    }

}