<?php

namespace App\Http\Controllers\Service;

use App\Http\Controllers\Controller;
use App\Http\Resources\UserResource;
use App\Http\Resources\UserInfoResource;
use App\Http\Response\ResponseJson;
use App\Models\Order;
use App\Models\ServiceRole;
use App\Models\UserInfo;
use Illuminate\Http\Request;

class OrderController extends Controller
{
    use ResponseJson;
    public function orders(Request $request)
    {
        $user = auth()->user();
        $user_id = $user->id;
        $orders = Order::with('user', 'group')->whereHas('serviceRoleOrders', function ($sql) use($user_id) {
            $sql->where('user_id', $user_id);
        });
        $orders = $orders->where('pay_status', 'PAID')->simplePaginate();
        foreach ($orders as $order) {
            $order_user = $order->user;
            unset($order->user);
            $order->user = UserResource::make($order_user)->toArray($request);
        }
        return $this->success('ok', $orders);
    }

    public function order(Request $request, Order $order)
    {
        $roles = ServiceRole::with('roleUsers:service_users.id,service_users.user_id,service_users.name,service_users.mobile')->get();
        return $this->success('ok', compact('order','roles'));
    }
}
