<?php

namespace App\Http\Controllers\API;

use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Log;
use App\Services\Admin\YeWu\HealthCheckupService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;

class UpLoadController extends Controller
{
    public function UpFile(){
        $file=request('file');

        if ($file->isValid()) {
//            $s=app()->make(HealthCheckupService::class);
//            $save=$s->SaveFile(['file'=>$file]);
            $date = date("Ymd");
            $save = $file->store('public/H5Upload/'.$date);

            return \Yz::Return(true,'上传成功',$save);
        }else{
            return \Yz::echoError1('获取文件失败');
        }
    }
    public function UpFileBase64()
    {
        $base64Image =request('base64file'); // 获取 base64 图片字符串
//        Log::info($base64Image);
        // 将 base64 字符串转换为图片并保存
        $image = base64_decode(explode(',', $base64Image)[1]);
        $date = date("Ymd");
        $filename = 'image_' . time() . '.png'; // 或者使用其他方式生成文件名

        Storage::disk('public')->put('/H5Upload/'.$date.'/'.$filename, $image); // 保存图片到 public 目录
        return \Yz::Return(true,'上传成功',['fileurl' =>'/storage/H5Upload/'.$date.'/'.$filename]);

    }

    /**
     * 测试 network_drive 上传链路
     * 上传文件到 Go 服务，返回可访问的完整 URL
     * 入参: file (必填), disk_name (可选, 默认 network_drive_test)
     */
    public function testUploadNetworkDrive()
    {
        $file = request('file');
        $diskName = request('disk_name', 'network_drive_test');

        if (!$file || !$file->isValid()) {
            return \Yz::echoError1('获取文件失败');
        }

        // 验证文件类型
        $mime = $file->getMimeType();
        $allowed = ['application/pdf', 'image/png', 'image/jpeg', 'image/jpg'];
        if (!in_array($mime, $allowed)) {
            return \Yz::echoError1('不支持的文件格式，仅支持 PDF/PNG/JPG');
        }

        // 生成存储路径
        $date = date('Ymd');
        $filename = uniqid() . '_' . mt_rand(0, 999999) . '.' . $file->extension();
        $filePath = '/phpUpload/test/' . $date . '/' . $filename;

        // 根据传入的磁盘名称选择对应磁盘
        $disk = Storage::disk($diskName);
        $contents = file_get_contents($file->getRealPath());
        $savePath = $disk->put($filePath, $contents);
        $url = $disk->url($filePath);

        return \Yz::Return(true, '上传成功', [
            'disk' => $diskName,
            'path' => $savePath,
            'url'  => $url,
        ]);
    }
}
