<?php

namespace App\Http\Controllers\API\Internal;

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

class AppointmentController extends Controller
{

    //给第三的接口，查询预约记录
    public function GetAppointmentRecord()
    {
        date_default_timezone_set('PRC');
//        $res_sfz = request('id_card_num');
//        $org_sn = request('org_sn');
        $encrypted_data = request('encrypted_data');
        $iv = request('iv');
        $encrypt_iv = bin2hex(random_bytes(16 / 2));
        if (!isset($iv)) return \Yz::echoError1('加密时使用的iv不能为空');
        $encrypted_data = \App\Lib\Tools::AESDecrypt($encrypted_data, config('app.globals.AES_KEY'), $iv);
        if (!$encrypted_data) return \Yz::echoError1('encrypted_data解密失败');
        $encrypted_data = json_decode($encrypted_data, true);
        if (!$encrypted_data) return \Yz::echoError1('encrypted_data解密json串失败');
        if (!$encrypted_data['id_card_num']) return \Yz::echoError1('id_card_num不能为空');
        if (!$encrypted_data['org_sn']) return \Yz::echoError1('org_sn不能为空');
        $res_sfz = $encrypted_data['id_card_num'];
        $org_sn = $encrypted_data['org_sn'];
        $currentYear = date('Y');
        $firstDay = date('Y-01-01', strtotime($currentYear));
        $lastDay = date('Y-12-31', strtotime($currentYear));

        $HSM = \App\Lib\HSM::HsmEncrypt($res_sfz);
        if ($HSM['status'] != true) {
            return \Yz::echoError1('调用HSM加密失败');
        }
        $sfz = $HSM['data'];
        $info_data = [];
        if (isset($sfz)) {
            $info = DB::table('appointment_record as a')
                ->select('a.id', 'a.name', 'a.sex', 'a.tel', 'a.id_card_num', 'a.date', 'a.time', 'a.type', 'a.fee_type as free_type', 'a.doc_type_id', 'a.org_code as org_sn', 'a.org_name', 'a.doc_type_name', 'a.created_at', 'b.org_name')
                ->join('medical_institution as b', 'a.org_code', '=', 'b.sn')
                ->where(['a.id_card_num' => $sfz, 'a.is_del' => 0, 'a.org_code' => $org_sn])
                ->whereBetween('a.created_at', [$firstDay, $lastDay])->orderBy('a.id', 'desc')->first();

            if (!!$info) {
                //查询相关图片
                $imgs = [];
                $imgs = DB::table('appointment_img')
                    ->select('type', 'imgurl')
                    ->where(['appointment_record_id' => $info->id])->get();
                foreach ($imgs as $key => $item) {
                    //此处应该 把完整的url 拼接上
                    $imgs[$key]->imgurl =  env('ZHUAN_WANG').$imgs[$key]->imgurl;
                }
                $info->files = $imgs;
                $info->id_card_num = $sfz;
                // $infos= $info;
                $info_data = [
                    'type' => $info->type,
                    'name' => $info->name,
                    'id_card_num' => $info->id_card_num,
                    'sex' => $info->sex,
                    'tel' => $info->tel,
                    'date' => $info->date,
                    'time' => $info->time,
                    'free_type' => $info->free_type,
                    'doc_type_id' => $info->doc_type_id,
                    'doc_type_name' => $info->doc_type_name,
                    'org_sn' => $info->org_sn,
                    'org_name' => $info->org_name,
                    'files' =>  $info->files,
                ];
            }
            $info_data = json_encode($info_data, JSON_UNESCAPED_UNICODE);
            return \Yz::Return(true, '查完完成', ['info' =>$this->AESEncrypt($info_data, $encrypt_iv) , 'iv' => $encrypt_iv]);
        }
    }

    function AESEncrypt($data, $encrypt_iv)
    {
       // return $data;
        return \App\Lib\Tools::AESEncrypt($data, config('app.globals.AES_KEY'), $encrypt_iv);
    }
}
