<?php

namespace App\Http\Controllers\Web;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\{Product, ProductCategory, Contact, About};

class DiscountController extends Controller
{
    public function index(Request $request){
        $q_name = $request->q_name;
        $q_category_id = $request->q_category_id;
        $category_name = "";
        $about = About::first();

        $product = Product::where('is_discount', 1)->orderby('id', 'asc');
        $product_category = ProductCategory::limit(5)->get();
        $contact = Contact::first();

        if (!empty($q_name)) {
            $product->where('name', 'like', '%'.$q_name.'%');
        }

        if (!empty($q_category_id)) {
            $product->where('product_category_id', $q_category_id);
            $category_name = ProductCategory::findOrFail($q_category_id);
        }

        $product = $product->get();

        return view('apps.web.product')->with('product', $product)
                                       ->with('category_name' ,$category_name)
                                       ->with('about' ,$about)
                                       ->with('q_name', $q_name)
                                       ->with('q_category_id', $q_category_id)
                                       ->with('product_category', $product_category)
                                       ->with('contact', $contact);
    }
}
