<?php

use App\Constants\DoctorTitleCode;
use App\Constants\HttpEnumCode;


/**
 * @method:打印使用，格式化打印数据
 * @param mixed ...$vars
 */
function dump(...$vars)
{
    ob_start();
    var_dump(...$vars);

    $output = ob_get_clean();
    $output = preg_replace('/\]\=\>\n(\s+)/m', '] => ', $output);

    if (PHP_SAPI == 'cli') {
        $output = PHP_EOL . $output . PHP_EOL;
    } else {
        if (!extension_loaded('xdebug')) {
            $output = htmlspecialchars($output, ENT_SUBSTITUTE);
        }
        $output = '<pre>' . $output . '</pre>';
    }

    echo $output;
}

/**
 * 成功返回
 * @param array $data 需返回数据
 * @param int $code code枚举码
 * @param string $message 返回提示信息
 * @return array
 */
function success(array|string $data = [], int $code = HttpEnumCode::HTTP_SUCCESS, string $message = ""): array
{
    if (empty($message)) {
        $message = HttpEnumCode::getMessage($code);
    }

    return [
        'code' => $code,
        'data' => $data,
        'message' => $message
    ];
}

/**
 * 失败返回
 * @param int $code code枚举码
 * @param string $message 返回提示信息
 * @param array $data 需返回数据
 * @return array
 */
function fail(int $code = HttpEnumCode::HTTP_ERROR, string $message = "", array $data = []): array
{
    if (empty($message)) {
        $message = HttpEnumCode::getMessage($code);
    }
    return [
        'code' => $code,
        'data' => $data,
        'message' => $message
    ];
}

/**
 * 转换用户类型-字符串
 * @param int|string $user_type 用户类型（1:患者 2:医生 3:药师）
 * @return string
 */
function UserTypeToString(int|string $user_type): string
{
    if ($user_type == 1) {
        $result = "患者端";
    } elseif ($user_type == 2) {
        $result = "医生端";
    } elseif ($user_type == 3) {
        $result = "药师端";
    } else {
        $result = "未知";
    }
    return $result;
}

/**
 * 获取身份证年龄
 * @param $idCard
 * @return false|int|mixed|string
 */
function getIdCardAge(string $idCard): mixed
{
    $age_time = strtotime(substr($idCard, 6, 8));
    if ($age_time === false) {
        return false;
    }

    list($y1, $m1, $d1) = explode("-", date("Y-m-d", $age_time));

    $now_time = strtotime("now");

    list($y2, $m2, $d2) = explode("-", date("Y-m-d", $now_time));
    $age = $y2 - $y1;
    if ((int)($m2 . $d2) < (int)($m1 . $d1)) {
        $age -= 1;
    }
    return $age;
}

/**
 * 获取身份证性别
 * @param string $idCard
 * @return int
 */
function getIdCardSex(string $idCard): int
{
    if(empty($idCard)) {
        return 0;
    }

    $sexint = (int) substr($idCard, 16, 1);
    return $sexint % 2 === 0 ? 2 : 1;
}

/**
 * 添加阿里云oss前缀网址
 * @param $path
 * @return string
 */
function addAliyunOssWebsite($path): string
{
    if (empty($path)){
        return "";
    }
    return config('alibaba.oss.custom_domain_name') . '/' . $path;
}

/**
 * 转换性别至im识别的性别
 * @param string|int $sex
 * @return string
 */
function sexToImSex(string|int $sex): string
{
    $result = "Gender_Type_Unknown";
    if ($sex == 1){
        $result = "Gender_Type_Male";
    }elseif ($sex == 2){
        $result = "Gender_Type_Female";
    }

    return $result;
}

/**
 * 转换订单类型-字符串
 * @param int|string $inquiry_type 订单类型（1:专家问诊 2:快速问诊 3:公益问诊 4:问诊购药）
 * @return string
 */
function inquiryTypeToString(int|string $inquiry_type): string
{
    if ($inquiry_type == 1) {
        $result = "专家问诊";
    } elseif ($inquiry_type == 2) {
        $result = "快速问诊";
    } elseif ($inquiry_type == 3) {
        $result = "公益问诊";
    } elseif ($inquiry_type == 4) {
        $result = "问诊购药";
    } else {
        $result = "未知";
    }
    return $result;
}

/**
 * 转换问诊订单取消原因-字符串
 * @param int|string $cancel_reason 取消订单原因（1:医生未接诊 2:主动取消 3:无可分配医生 4:客服取消 5:支付超时）
 * @return string
 */
function inquiryCancelReasonToString(int|string $cancel_reason): string
{
    if ($cancel_reason == 1) {
        $result = "医生未接诊";
    } elseif ($cancel_reason == 2) {
        $result = "主动取消";
    } elseif ($cancel_reason == 3) {
        $result = "无可分配医生";
    } elseif ($cancel_reason == 4) {
        $result = "客服取消";
    } elseif ($cancel_reason == 5) {
        $result = "支付超时";
    } else {
        $result = "未知";
    }
    return $result;
}

/**
 * 转换药品订单取消原因-字符串
 * @param int|string $cancel_reason 订单取消原因（1:主动取消 2:复核失败/库存不足 3:支付超时 4:客服取消）
 * @return string
 */
function productCancelReasonToString(int|string $cancel_reason): string
{
    if ($cancel_reason == 1) {
        $result = "主动取消";
    } elseif ($cancel_reason == 2) {
        $result = "复核失败/库存不足";
    } elseif ($cancel_reason == 3) {
        $result = "支付超时";
    } elseif ($cancel_reason == 4) {
        $result = "客服取消";
    } else {
        $result = "未知";
    }
    return $result;
}

/**
 * 转换性别至字符串
 * @param string|int $sex
 * @return string
 */
function sexToStringSex(string|int $sex): string
{
    $result = "未知";
    if ($sex == 1){
        $result = "男";
    }elseif ($sex == 2){
        $result = "女";
    }

    return $result;
}