<?php

namespace App\Http\Controllers\API\Admin\YeWu;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class PEISApiController extends Controller
{
    public static function Api($url_code, $code)
    {
        $url = 'https://dqgatjzx-wx.sixinyun.com';
        $api['套餐详情查询'] = "{$url}/PEISCommon/QueryComboDetail/{$code}";
        $api['自选项目查询'] = "{$url}/PEISCommon/QueryGroups/{$code}";
        $api['套餐查询'] = "{$url}/PEISCommon/QueryCombos/{$code}";
        return $api["{$url_code}"] ?? $url_code;
    }
    public static function Post($url_code, $hospital_id, $data, $print = false)
    {

        $hospital = DB::table('hospital')->where(['id'=>$hospital_id,'status'=>1,'is_del'=>0])->first();
        if (!$hospital) return \Yz::echoError1('医院不存在或不可用');
        $code = $hospital->code;
        $url = self::Api($url_code, $code);
//        self::RequestLog($url, $data, $code, $url_code);
        $data_string = json_encode($data, JSON_UNESCAPED_UNICODE);
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HTTPHEADER, [
            'Content-Type: application/json; charset=utf-8',
            'Content-Length: ' . strlen($data_string)
        ]);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
        $res_string = curl_exec($ch);
        curl_close($ch);


        if (!json_decode($res_string, true)) {
            return \Yz::Return(false,'获取失败', [
                'url' => $url,
                'data' => $data,
                'res' => $res_string
            ]);
        }
        $res = json_decode($res_string, true);


        return [
            'code' => $res['ResultCode'],
            'message' => $res['ResultContent'],
            'data' => $res['Records']
        ];
    }
}
