<?php

namespace App\Admin\Actions\Community\Docter;

use App\Models\Docter;
use App\Models\DocterOrganization;
use Encore\Admin\Actions\Action;
use Encore\Admin\Actions\RowAction;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Mockery\Exception;

class Failed extends RowAction
{
    protected $selector = '.failed';

    public $name = '驳回';
    public function handle()
    {
        $openid = $this->row->docter->openid;

        $data = [
            $openid,
            '',
            '很抱歉，你的医生身份认证失败！',
            $this->row->docter->name,
            $this->row->organization->name,
            $this->row->qualification->name,
            $this->row->office->name,
            '点击进入小程序查看详情，有疑问请联系管理员！'
        ];

        DB::beginTransaction();
        try {

            DocterOrganization::where('id',$this->row->id)->update(['state'=>2]);
            if(!empty($openid)){
                admin_send_docter_message('then_result',$data);
            }
            DB::commit();
        } catch (Exception $e){
            dd($e->getMessage());
            DB::rollBack();
            return $this->response()->error('操作失败');
        }
        return $this->response()->success('操作成功')->refresh();
    }
}
