<?php
/*
    * Created by VahabDev in Aparat Live
    * Website: https://pourfi.ir
    * Aparat Live: https://www.aparat.com/vahabdev/live
    * Aparat Channel: https://www.aparat.com/vahabdev
    * Rubika Channel: https://rubika.ir/vahabdev
    * Donate: https://reymit.ir/vahabdev
    * Telegram: https://t.me/vahabdev
*/


namespace App\Http\Controllers;

use App\Models\Blog;
use App\Models\Comment;
use App\Models\Product;
use App\Models\productCategories;
use Illuminate\Http\Request;

class productController extends Controller
{
    public function allProducts(Request $request){
        $q = Product::where('status', 'active');
        $search = $request->input('search', '');
        if($search){
            $q->where('name', 'like', '%'.$search.'%')
            ->orWhere('description', 'like', '%'.$search.'%');
        }

        $orderBy = $request->input('sort', 'news');
        if($orderBy){
            if($request->input('sort') == 'lowest'){
                $q->orderBy('price', 'asc');
            }
            if($request->input('sort') == 'highest'){
                $q->orderBy('price', 'desc');
            }
            if($request->input('sort') == 'news'){
                $q->orderBy('id', 'desc');
            }
        }

        $countProduct  = Product::where('status', 'active')->count();
        $listProducts = $q->paginate(12);
        return view('site.products.all', compact(
            'listProducts',
            'countProduct',
            'search',
            'orderBy',
        ));
    }


    public function single($slug){
        if(!Product::where('slug', $slug)
            ->where('status', 'active')
            ->exists()){
            abort(404);
        }

        $data = Product::where('slug', $slug)
            ->where('status', 'active')
            ->with('category')
            ->first();
        $mizanTakhfif = 0;

        $totalPayment = $data->price;
        if($data->discount > 0){
            $mizanTakhfif = $data->price * ($data->discount/100);
            $totalPayment = $totalPayment - $mizanTakhfif;
        }


        $Comments = Comment::where('post_type', 'product')
            ->where('relid', $data->id)
            ->where('status', 'active')
            ->get();

        return view('site.products.view', compact(
            'data',
            'totalPayment',
            'mizanTakhfif',
            'Comments'
        ));
    }


    public function productCategorys(Request $request, $slug){

        if(!productCategories::where('slug', $slug)->exists()){
            abort(404);
        }

        $catDetails = productCategories::where('slug', $slug)->first();

        productCategories::where('id', $catDetails->id)->update(['count_view' => $catDetails->count_view + 1]);

        $q= Product::where('category_id', $catDetails->id);
        $orderBy = $request->input('sort', 'news');
        $search = $request->input('search', '');
        if($orderBy){
             if($request->input('sort') == 'lowest'){
                 $q->orderBy('price', 'asc');
             }
             if($request->input('sort') == 'highest'){
                 $q->orderBy('price', 'desc');
             }
             if($request->input('sort') == 'news'){
                 $q->orderBy('id', 'desc');
             }
        }
        if($search){
            $q->where('name', 'like', '%'.$search.'%')
            ->orWhere('description', 'like', '%'.$search.'%');
        }
        $q->where('status', 'active');
        $listProducts = $q->paginate(12);
        $countProduct = Product::where('category_id', $catDetails->id)->count();


        return view('site.products.list', compact(
            'catDetails',
            'listProducts',
            'countProduct',
            'orderBy',
            'search',
        ));
    }
}
