<?php

use Illuminate\Support\Facades\Route;

Route::get('/', function () {
    return view('welcome');
});

Route::post('/upload', [\App\Http\Controllers\ImageController::class, 'upload']);


/**
 * 列表
 */
Route::get('/list', function () {
    $photoAlbum = \App\Models\PhotoAlbumModel::get();
//    $photoAlbum = \App\Models\PhotoAlbumModel::with('photos')->get();
    foreach ($photoAlbum as $album) {
        $album->url = \Illuminate\Support\Facades\Storage::url($album->url);
        $album->created_at = date('Y-m-d', $album->created_at);
        $album->photo_count = count($album['photos']);
//        foreach ($album['photos'] as &$photo) {
//            $photo->original = \Illuminate\Support\Facades\Storage::url($photo->original);
//            $photo->compressed = \Illuminate\Support\Facades\Storage::url($photo->compressed);
//            $photo->watermarked = \Illuminate\Support\Facades\Storage::url($photo->watermarked);
//        }
    }
//    exit(json_encode($photoAlbum));
    return json_encode([
        'code' => 0,
        'result' => $photoAlbum,
        'msg' => 'success',
    ]);
});
/**
 * 详情
 */
Route::get('/detail', function () {
    $id =  request()->get('id');
    $photoAlbum = \App\Models\PhotoAlbumModel::with('photos')->where('id', $id)->first();

    $photoAlbum->url = \Illuminate\Support\Facades\Storage::url($photoAlbum->url);
    $photoAlbum->created_at = date('Y-m-d', $photoAlbum->created_at);
    foreach ($photoAlbum['photos'] as $index => &$photo) {
        $photo->original = \Illuminate\Support\Facades\Storage::url($photo->original);
        $photo->compressed = \Illuminate\Support\Facades\Storage::url($photo->compressed);
        $photo->watermarked = \Illuminate\Support\Facades\Storage::url($photo->watermarked);
    }

    return json_encode([
        'code' => 0,
        'result' => $photoAlbum,
        'msg' => 'success',
    ]);
//    exit(json_encode($photoAlbum));
//    return view('detail', [
//        'photo_album' => $photoAlbum,
//    ]);
});


//Route::get('/o', function () {
//    $photoAlbum = \App\Models\PhotoAlbumModel::with('photos')->get();
//    foreach ($photoAlbum as $album) {
//        $album->url = \Illuminate\Support\Facades\Storage::url($album->url);
//        $album->created_at = date('Y-m-d', $album->created_at);
//        $album->count = count($album['photos']);
//    }
////    exit(json_encode($photoAlbum));
//    return view('o-index', [
//        'photo_album' => $photoAlbum,
//    ]);
//});
