<?php

namespace App\Admin\Actions\backstage;
use App\Models\Docter;
use App\Models\DocterOrganization;
use Encore\Admin\Actions\RowAction;
use Illuminate\Database\Eloquent\Model;

class Revoke extends RowAction
{
    public $name = '撤销';

    public function handle(Model $model)
    {

        //获得通过认证的机构数量
        $is_pass_num = 0;
        $docter_approve_num = DocterOrganization::where('docter_id',$model->docter_id)->get();
        $docter_approve_num_array = $docter_approve_num->toarray();
        foreach ($docter_approve_num_array as $value)
        {
            if ($value['state'] == 1)
            {
                $is_pass_num=$is_pass_num+1;
            }
        }
        //如果同时有多个机构认证
        if($is_pass_num > 1)
        {
            //撤销到社区端
            DocterOrganization::where('id',$model->id)->update(['state'=>0,'authentication_time'=>'','authentication_end_time'=>0]);
        }
        if ($is_pass_num ==1)
        {
            DocterOrganization::where('id',$model->id)->update(['state'=>0,'authentication_time'=>0,'authentication_end_time'=>0]);
            Docter::where('id',$model->docter_id)->update(['is_then'=>0]);
        }
        return $this->response()->success('审核撤销成功')->refresh();
    }

}