<?php

namespace App\Http\Controllers\H5;

use App\Http\Controllers\Controller;
use App\Http\Response\ResponseJson;
use App\Models\Food;
use App\Models\S2RealObject;
use App\Models\S2ShopSku;
use App\Models\Shop;
use Illuminate\Http\Request;

class FoodController extends Controller
{
    use ResponseJson;

    public function foodList()
    {
        //        $foods = Food::keyword()->type()->orderByDesc('id')->get();
//        return $this->success('ok', $foods);
        $foods = Food::with("shopSpu")->whereHas('shopSpu', function ($sql) {
            $sql->keyword();
        })->hasStock()->type()->orderByDesc('id')->get();
        foreach ($foods as $food) {
            $sku = S2ShopSku::where('id', $food->shopSpu->skuid)->first();
            $object = S2RealObject::where('id', $sku->real_id)->first();
            // $food->name = $food->shopSpu->title;
            $food->icon = $food->shopSpu->icon;
            $food->price = ($sku->price ?? 0) / 100;
            $food->stock = $object->stock ?? 0;
            $food->have_goods = 1;
            unset($food->shop);
        }
        return $this->success('ok', $foods);
    }
}
