<?php

namespace App\Admin\Actions\backstage\Docters;

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

class DefaultOrganazation extends RowAction
{
    public $name = '修改默认机构';

    public function handle(Model $model, Request $request)
    {
        // $model ...
        $org_id = $request->get('org_id');
        //经度
        $longitude = Organization::where('id',$org_id)->value('longitude');
        //纬度
        $latitude = Organization::where('id',$org_id)->value('latitude');
        Docter::where('id',$this->row->id)->update(['latitude'=>$latitude,'longitude'=>$longitude]);
        return $this->response()->success('修改默认机构成功')->refresh();
    }

    public function form(){
        $id = $this->row->id;
        $org_id = DocterOrganization::where('docter_id',$id)->where('state',1)->pluck('organization_id')->toArray();
        $all_org = Organization::whereIn('id',$org_id)->pluck('name','id')->toArray();

        $this->select('org_id','机构名')->options($all_org);
    }
}