<?php

namespace App\Http\Controllers\User;

use App\Http\Controllers\Controller;
use App\Models\Order;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class UserOrderController extends Controller
{
    public function orderList()
    {
        $user_id = Auth::id();
        $get_orders = Order::all();
        return view('user.order.index', compact('get_orders'));
    }


    public function orderDetail($id)
    {
        $order_detail = Order::where('id', $id)->with(['user', 'orderItems', 'orderItems.products'])->first();

        return view('user.order.detail', compact('order_detail'));
    }

    public function delete(Request $request)
    {
        $orderId = $request->id;
        $order = Order::find($orderId);
        if ($order) {

            $order->orderItems()->delete();
            $order->delete();
            return response()->json(['success' => 'Order and its items successfully deleted!']);
        } else {
            return response()->json(['error' => 'Order not found!'], 404);
        }
    }
}
