<?php

namespace Application\Service\Extension\Helper;
use Application\Service\Extension\ErrorHandler;
use Application\Service\Extension\Laminas;

/**
 * curl助手类
 * 写的都是封装的curl的接口
 */
class CurlHelper
{
    /**
     * 同步到arkEdc系统
     * @param string $xmlData xml数据
     * @return bool|string
     */
    public static function syncArkEdc(string $xmlData)
    {
        $curl = curl_init();

        curl_setopt_array($curl, array(
            CURLOPT_URL => 'http://42.192.180.144/arkedc/importODM',
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => '',
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 60,
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => 'POST',
            CURLOPT_POSTFIELDS => "{
    'fileData':$xmlData
}",
            CURLOPT_HTTPHEADER => array(
                'Content-Type: text/plain'
            ),
        ));

        $response = curl_exec($curl);

        $error = curl_error($curl);

        if ($error) {
            echo 'Curl error: ' . curl_error($curl);
        }

        curl_close($curl);

        return $response;
    }

    /**
     * 获取可回退的小程序版本
     * 调用本接口可以获取可回退的小程序版本（最多保存最近发布或回退的5个版本）。
     * @link https://developers.weixin.qq.com/doc/oplatform/Third-party_Platforms/2.0/api/code/get_history_version.html#%E8%AF%B7%E6%B1%82%E5%9C%B0%E5%9D%80
     * @param string $token
     * @return bool|string
     */
    public static function getMiniAppHistoryVersion(string $token)
    {
        $curl = curl_init();

        curl_setopt_array($curl, array(
            CURLOPT_URL => 'https://api.weixin.qq.com/wxa/revertcoderelease?action=get_history_version&access_token=' . $token,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => '',
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 0,
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => 'GET',
        ));

        $response = curl_exec($curl);

        curl_close($curl);
        return $response;
    }

    /**
     * @param $itemId
     * @param $patientId
     * @param $checktimeId
     * @param $formId
     * @param $isMulti
     * @param $contentId
     */
    public static function closeConfirm($itemId, $patientId, $checktimeId, $formId, $isMulti, $contentId)
    {
        $curl = curl_init();

        curl_setopt_array($curl, array(
            CURLOPT_URL => Laminas::$app->getConfig()['http_domain'] . '/expose/close-confirm?user=1',
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => '',
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 0,
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => 'POST',
            CURLOPT_POSTFIELDS => "item={$itemId}&patient_id={$patientId}&checktime_id={$checktimeId}&form_id={$formId}&is_multi={$isMulti}&content_id={$contentId}",
            CURLOPT_HTTPHEADER => array(
                "token: " . Laminas::$app->getMvcEvent()->getRequest()->getHeaders()->get('token')->getFieldValue(),
                'Content-Type: application/x-www-form-urlencoded'
            ),
        ));

        $response = curl_exec($curl);

        ErrorHandler::log2txt([
            'args' => func_get_args(),
            'response' => $response,
            'curl_error' => curl_error($curl),
            'post_fields' => "item={$itemId}&patient_id={$patientId}&checktime_id={$checktimeId}&form_id={$formId}&is_multi={$isMulti}&content_id={$contentId}",
            'http_headers' => array(
                "token: " . Laminas::$app->getMvcEvent()->getRequest()->getHeaders()->get('token')->getFieldValue(),
                'Content-Type: application/x-www-form-urlencoded'
            ),
        ]);

        curl_close($curl);
        return $response;

    }
}