<?php

namespace App\Http\Controllers;

use App\Models\Order;
use Illuminate\Http\Request;

class AdminOrdersManagement extends Controller
{
    public function OrdersManagement()
    {
        $orders = Order::with(['product', 'user'])->latest()->paginate(10);
        return view('adminDashboard.orders-management', compact('orders'));
    }

    public function OrderInfo(Request $request)
    {
        $request->validate([
            'id' => 'required'
        ]);
        $order = Order::where(['id' => $request->id])->with(['product', 'user'])->first();
        return response()->json(['status' => 'success', 'order' => $order]);
    }

    public function UpdateOrder(Request $request)
    {
        $request->validate([
            'id' => 'required',
            'status' => 'required'
        ]);
        Order::where(['id' => $request->id])->update(['status' => $request->status]);
        return response()->json(['status' => 'success']);
    }

    public function DeleteOrder($id)
    {
        Order::where(['id' => $id])->delete();
        return redirect()->back();
    }
}
