<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use App\Models\Enquiry;
use Illuminate\Http\Request;

class EnquiryController extends Controller
{
    public function index(Request $request) {
        $query = Enquiry::with('product')->orderByDesc('created_at');
        if ($request->status) $query->where('status', $request->status);
        $enquiries = $query->paginate(20);
        return view('admin.pages.enquiries.index', compact('enquiries'));
    }
    public function show(Enquiry $enquiry) {
        if ($enquiry->status === 'new') $enquiry->update(['status' => 'read']);
        return view('admin.pages.enquiries.show', compact('enquiry'));
    }
    public function updateStatus(Request $request, Enquiry $enquiry) {
        $enquiry->update(['status' => $request->status]);
        return back()->with('success', 'Status updated.');
    }
    public function destroy(Enquiry $enquiry) {
        $enquiry->delete();
        return redirect()->route('admin.enquiries.index')->with('success', 'Enquiry deleted.');
    }
}
