<?php

namespace App\Http\Controllers;

use App\Models\Addbanner;
use Illuminate\Http\Request;
use App\Models\Information;
use App\Models\Product;
use App\Models\Menu;
use App\Models\User;
use App\Models\Subcategory;
use App\Models\Category;
use App\Models\Attrvalue;
use App\Models\Basicinfo;
use App\Models\Blog;
use App\Models\Order;
use App\Models\Brand;
use App\Models\Comment;
use App\Models\Varient;
use App\Models\Weight;
use App\Models\React;
use App\Models\Review;
use App\Models\Size;
use App\Models\Usecoupon;
use App\Models\Like;
use App\Models\Share;
use App\Models\Customer;
use App\Models\Coupon;
use App\Models\Mainproduct;
use App\Models\Postcomment;
use App\Models\Slider;
use Illuminate\Support\Facades\Auth;
use Session;
use Cart;
use Illuminate\Support\Facades\Response;
use Illuminate\Support\Facades\File;

class WebviewController extends Controller
{

    public function webhookpathao(Request $request)
    { 
        Log::info('Pathao Webhook Received:', $request->all());
        
        $invoice = $request['merchant_order_id'];
        $order = Order::where('invoiceID', $invoice)->first();
        if (isset($order)) {
              if ($request['event'] == 'order.picked') {
                $order->status = 'Courier Pending';
            }  else if ($request['event'] == 'order.in-transit') {
                $order->status = 'In Transit';
            }  else if ($request['event'] == 'order.assigned-for-delivery') {
                $order->status = 'Assigned';
            } else if ($request['event'] == 'order.delivered') {
                $order->status = 'Completed';
            } else if ($request['event'] == 'order.partial-delivery') {
                $order->status = 'Partial Delivered';
            } else if ($request['event'] == 'order.returned') {
                $order->status = 'Return';
            }  else if ($request['event'] == 'order.delivery-failed') {
                $order->status = 'Del. Failed';
            } else if ($request['event'] == 'order.on-hold') {
                $order->status = 'On Hold';
            } else if ($request['event'] == 'order.paid-return') {
                $order->status = 'Paid Return';
            } else {
                \Log::info($request['event']);
                \Log::info($request);
            }
            $order->update();

            $comment = new Comment();
            $comment->order_id = $order->id;
            $comment->comment = 'Pathao Successfully change status of invoice: ' . $invoice . ' to : ' . $order->status;
            $comment->admin_id = 1;
            $comment->status = 1;
            $comment->save();
        } else {
            $request['error_type'] = "Order Not Found with this invoiceID";
            \Log::error($request);
        } 

        return response()->json(['message' => 'Event received'], 202)
            ->header('X-Pathao-Merchant-Webhook-Integration-Secret', 'f3992ecc-59da-4cbe-a049-a13da2018d51');

    }

    public function webhook(Request $request)
    {
        // $invoice = $request['invoice'];
        // $order = Order::where('invoiceID', $invoice)->first();
        // if (isset($order)) {
        //     if ($request['status'] == 'delivered') {
        //         $order->status = 'Completed';
        //     } else if ($request['status'] == 'pending') {
        //         $order->status = 'Courier Pending';
        //     } else if ($request['status'] == 'cancelled') {
        //         $order->status = 'Del. Failed';
        //     } else if ($request['status'] == 'partial_delivered') {
        //         $order->status = 'Partial Delivered';
        //     } else if ($request['status'] == 'unknown') {
        //         $order->status = 'Unknown';
        //     } else {
        //     }
        //     $order->update();

        //     $comment = new Comment();
        //     $comment->order_id = $order->id;
        //     $comment->comment = 'Steadfast Successfully change status of invoice: ' . $invoice . ' to : ' . $request['status'];
        //     $comment->admin_id = 1;
        //     $comment->status = 1;
        //     $comment->save();
        // }
        return response()->json('success', 200);
    }
    public function couponcheck(Request $request)
    {
        $available = Coupon::where('code', $request->coupon_code)->where('validity', '>=', date('Y-m-d'))->first();

        if (isset($available)) {
            $use = Usecoupon::where('user_id', Auth::id())->where('coupon_id', $available->id)->where('code', $request->coupon_code)->first();
            if (isset($use)) {
                $response = [
                    'status' => 'false',
                    'discount' => 0,
                ];
                return response()->json($response, 200);
            } else {
                $blance = Cart::subtotalFloat();
                if ($available->type == 'Amount') {
                    $discount = $available->amount;
                } else {
                    $discount = intval($blance * ($available->amount / 100));
                }
                Session::put('couponcode', $request->coupon_code);
                Session::put('availablecoupon', $available);
                $response = [
                    'status' => 'true',
                    'discount' => $discount,
                ];
                return response()->json($response, 200);
            }
        } else {
            Session::forget('couponcode');
            Session::forget('availablecoupon');
            $response = [
                'status' => 'invalid',
                'discount' => 0,
            ];
            return response()->json($response, 200);
        }
    }

    public function mainview()
    {
        $categories = Category::where('status', 'Active')->select('*')->get();
        $sliders = Slider::where('status', 'Active')->where('type', 1)->select('id', 'slider_btn_link', 'slider_title', 'slider_image')->get();
        $moblieSliders = Slider::where('status', 'Active')->where('type', 2)->select('id', 'slider_btn_link', 'slider_title', 'slider_image')->get();
        $adds = Addbanner::where('status', 'Active')->whereIn('id', ['1', '2'])->select('id', 'add_link', 'add_image', 'status')->get();
        $addbottoms = Addbanner::where('status', 'Active')->get();
        $topproducts = Product::where('status', 'Active')->where('top_rated', '1')->inRandomOrder()->take(8)->get();
        $categoryproducts = Category::where('status', 'Active')->where('front_status', 0)->select('id', 'category_name', 'slug', 'position')->orderBy('position')->get();
        $categoryproducts->each(function ($category) {
            $category->products = $category->products()->where('status', 'Active')->take(12)->get();
        });
        $medias = Menu::where('status', 'Active')->get();
        $blogs = Blog::where('status', 'Active')->inRandomOrder()->take(4)->get();
        $brands = Brand::where('status', 'Active')->get();
        return view('webview.content.maincontent', ['categories' => $categories, 'sliders' => $sliders, 'moblieSliders'=>$moblieSliders, 'adds' => $adds, 'addbottoms' => $addbottoms, 'topproducts' => $topproducts, 'categoryproducts' => $categoryproducts, 'medias' => $medias, 'blogs'=>$blogs, 'brands'=>$brands]);
    }
    
    public function productsShop()
     {
    
     $allProducts = Product::where('status', 'Active')->inRandomOrder()->get();
     return view('webview.content.product.all_product', ['allProducts' =>$allProducts]);
     }

    public function productdetailsnew($slug)
    {
        $singlemain = Mainproduct::where('ProductSlug', $slug)->select('id', 'category_id', 'RelatedProductIds')->first();

        $id = json_decode($singlemain->RelatedProductIds)[0]->productID;
        $productdetails = Product::with([
            'sizes' => function ($query) {
                $query->select('id', 'product_id', 'Discount', 'RegularPrice', 'SalePrice')->where('status', 'Active');
            },
            'weights' => function ($query) {
                $query->select('id', 'product_id', 'Discount', 'RegularPrice', 'SalePrice');
            }
        ])->where('id', $id)->first();
        $varients = Varient::where('product_id', $productdetails->id)->get();

        $relatedproducts = Mainproduct::where('category_id', $singlemain->category_id)->where('status', 'Active')->where('top_rated', '1')->orderByRaw('ISNULL(`position`), `position` ASC')->select('id', 'ProductName', 'ProductSlug', 'ProductImage', 'status', 'position', 'top_rated', 'RelatedProductIds')->inRandomOrder()->limit(8)->get();
        $sizesolds = Size::where('product_id', $productdetails->id)->where('status', 'Active')->get();
        $weightolds = Weight::where('product_id', $productdetails->id)->get();

        return view('webview.content.product.datacheck', ['sizesolds' => $sizesolds, 'weightolds' => $weightolds, 'singlemain' => $singlemain, 'varients' => $varients, 'relatedproducts' => $relatedproducts, 'productdetails' => $productdetails]);
    }

    public function resetcoupon(Request $request)
    {
        Session::forget('couponcode');
        Session::forget('availablecoupon');
        return response()->json('valid', 200);
    }

    public function rashi(Request $request)
    {
        $medias = Menu::where('status', 'Active')->get();
        return view('webview.content.product.media', ['medias' => $medias]);
    }

    public function review(Request $request)
    {
        $review = new Review();
        $review->user_id = Auth::user()->id;
        $review->product_id = $request->product_id;
        $review->messages = $request->messages;
        $review->rating = $request->rating;

        if ($request->file) {
            $file = $request->file;
            $name = time() . "_" . $file->getClientOriginalName();
            $uploadPath = ('public/images/admin/profile/');
            $file->move($uploadPath, $name);
            $imageUrl = $uploadPath . $name;
            $review->file = $imageUrl;
        }

        $review->save();
        return response()->json('success', 200);
    }

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

        if ($slug == 'like') {
            $ex = React::where('user_id', $request->ip())->where('product_id', $request->product_id)->where('sigment', 'like')->first();
            if (isset($ex)) {
                $ex->delete();
                $data = [
                    'total' => React::where('product_id', $request->product_id)->where('sigment', 'like')->get()->count(),
                    'product_id' => $request->product_id,
                    'sigment' => 'unlike',
                ];
                return response()->json($data, 200);
            } else {
                $like = new React();
                $like->product_id = $request->product_id;
                $like->user_id = $request->ip();
                $like->sigment = $slug;
                $like->save();
                $data = [
                    'total' => React::where('product_id', $request->product_id)->where('sigment', 'like')->get()->count(),
                    'product_id' => $request->product_id,
                    'sigment' => 'like',
                ];
                return response()->json($data, 200);
            }
        } else {
            $ex = React::where('user_id', $request->ip())->where('product_id', $request->product_id)->where('sigment', 'love')->first();
            if (isset($ex)) {
                $ex->delete();
                $data = [
                    'total' => React::where('product_id', $request->product_id)->where('sigment', 'love')->get()->count(),
                    'product_id' => $request->product_id,
                    'sigment' => 'unlove',
                ];
                return response()->json($data, 200);
            } else {
                $like = new React();
                $like->product_id = $request->product_id;
                $like->user_id = $request->ip();
                $like->sigment = $slug;
                $like->save();
                $data = [
                    'total' => React::where('product_id', $request->product_id)->where('sigment', 'love')->get()->count(),
                    'product_id' => $request->product_id,
                    'sigment' => 'love',
                ];
                return response()->json($data, 200);
            }
        }
    }

    public function givelike(Request $request)
    {

        $ex = Like::where('user_id', $request->user_id)->where('product_id', $request->product_id)->where('review_id', $request->review_id)->first();
        if (isset($ex)) {
            $ex->delete();
            $data = [
                'total' => Like::where('review_id', $request->review_id)->get()->count(),
                'review_id' => $request->review_id,
                'status' => 'unlike',
            ];
            return response()->json($data, 200);
        } else {
            $like = new Like();
            $like->product_id = $request->product_id;
            $like->user_id = $request->user_id;
            $like->review_id = $request->review_id;
            $like->like = 'Yes';
            $like->save();
            $data = [
                'total' => Like::where('review_id', $request->review_id)->get()->count(),
                'review_id' => $request->review_id,
                'status' => 'like',
            ];
            return response()->json($data, 200);
        }
    }

    public function giveshare(Request $request)
    {

        $ex = Share::where('user_id', $request->user_id)->where('product_id', $request->product_id)->where('review_id', $request->review_id)->first();
        if (isset($ex)) {
            $ex->delete();
            $data = [
                'total' => Share::where('review_id', $request->review_id)->get()->count(),
                'review_id' => $request->review_id,
                'status' => 'unshare',
            ];
            return response()->json($data, 200);
        } else {
            $like = new Share();
            $like->product_id = $request->product_id;
            $like->user_id = $request->user_id;
            $like->review_id = $request->review_id;
            $like->share = 'Yes';
            $like->save();
            $data = [
                'total' => Share::where('review_id', $request->review_id)->get()->count(),
                'review_id' => $request->review_id,
                'status' => 'share',
            ];
            return response()->json($data, 200);
        }
    }

    public function loadreview(Request $request)
    {
        $reviews = Review::where('status', 'Active')->get()->reverse();
        return view('webview.content.product.review', ['reviews' => $reviews]);
    }

    public function blogs(Request $request)
    {
        $blogs = Blog::where('status', 'Active')->get()->reverse();
        return view('webview.content.product.blog', ['blogs' => $blogs]);
    }
   public function blogDetails($slug )
    {
        $blog = Blog::where('slug', $slug)->first();
        $siteBlogs = Blog::where('status', 'Active')->latest()->take(5)->get();
        return view('webview.content.product.blog_details', ['blog' => $blog, 'siteBlogs' => $siteBlogs]);
    }
    public function profile()
    {
        $id = Auth::user()->id;
        $userprofile = User::findOrfail($id);
        return view('auth.profile', ['userprofile' => $userprofile]);
    }

    public function updateprofile(Request $request)
    {
        $time = microtime('.') * 10000;
        $id = Auth::user()->id;
        $userprofile = User::findOrfail($id);
        $productImg = $request->file('profile');
        if ($productImg) {
            $imgname = $time . $productImg->getClientOriginalName();
            $imguploadPath = ('public/images/user/profile/');
            $productImg->move($imguploadPath, $imgname);
            $productImgUrl = $imguploadPath . $imgname;
            $userprofile->profile = $productImgUrl;
        }
        $userprofile->save();
        return redirect()->back()->with('message', 'Profile update successfully');
    }

    public function orderhistory()
    {
        $date = \Carbon\Carbon::now();
        $orders =  Order::with(
            [
                'orderproducts' => function ($query) {
                    $query->select('id', 'order_id', 'productName', 'quantity', 'color', 'size');
                },
                'comments' => function ($query) {
                    $query->select('id', 'order_id', 'comment', 'admin_id', 'status', 'created_at')->where('status', 0);
                },
            ]
        )->where('user_id', Auth::guard('web')->user()->id)
            ->join('customers', 'customers.order_id', '=', 'orders.id')
            ->select('orders.*', 'customers.customerPhone', 'customers.customerName', 'customers.customerAddress')
            ->get();
        return view('auth.orderhistory', ['date' => $date, 'orders' => $orders]);
    }

    public function index($slug)
    {
        if ($slug == 'about_us') {
            $title = 'About US';
        } else if ($slug == 'contact_us') {
            $title = 'Contact Us';
        } else if ($slug == 'shipping_guide') {
            $title = 'Privacy Policy';
        } else if ($slug == 'privacy_policy') {
            $title = 'Privacy Policy';
        } else if ($slug == 'company') {
            $title = 'Company';
        } else if ($slug == 'customer_service') {
            $title = 'Customer Service';
        } else if ($slug == 'help_center') {
            $title = 'Help Center';
        } else if ($slug == 'faq') {
            $title = 'FAQ';
        } else if ($slug == 'terms_codition') {
            $title = 'Terms & Conditions';
        } else {
        }

        $value = Information::where('key', $slug)->first();
        return view('webview.content.information.info', ['title' => $title, 'slug' => $slug, 'value' => $value]);
    }

    public function productdetails($slug)
    {
        $productdetails = Product::where('ProductSlug', $slug)->first();
        $varients = Varient::where('product_id', $productdetails->id)->get();
        $sizes = Size::where('product_id', $productdetails->id)->where('status', 'Active')->get();
        $weights = Weight::where('product_id', $productdetails->id)->get();
        $relatedproducts = Product::where('category_id', $productdetails->category_id)->where('status', 'Active')->inRandomOrder()->limit(15)->get();

        return view('webview.content.product.details', ['varients' => $varients, 'sizes' => $sizes, 'weights' => $weights, 'relatedproducts' => $relatedproducts, 'productdetails' => $productdetails]);
    }

    public function viewproductdetails($slug)
    {

        $productdetails = Product::with([
            'sizes' => function ($query) {
                $query->select('id', 'product_id', 'Discount', 'RegularPrice', 'SalePrice')->where('status', 'Active');
            },
            'weights' => function ($query) {
                $query->select('id', 'product_id', 'Discount', 'RegularPrice', 'SalePrice');
            }
        ])->where('ProductSlug', $slug)->first();
        $varients = Varient::where('product_id', $productdetails->id)->get();

        $relatedproducts = Product::where('category_id', $productdetails->category_id)->where('status', 'Active')->inRandomOrder()->limit(8)->get();
        $sizesolds = Size::where('product_id', $productdetails->id)->where('status', 'Active')->get();
        $weightolds = Weight::where('product_id', $productdetails->id)->get();

        return view('webview.content.product.details', ['sizesolds' => $sizesolds, 'weightolds' => $weightolds, 'varients' => $varients, 'relatedproducts' => $relatedproducts, 'productdetails' => $productdetails]);
    }

    public function loadrelatedpro(Request $request)
    {
        $singlemain = Mainproduct::where('id', $request->mainproduct_id)->select('id', 'category_id', 'RelatedProductIds')->first();

        $productdetails = Product::with([
            'sizes' => function ($query) {
                $query->select('id', 'product_id', 'Discount', 'RegularPrice', 'SalePrice')->where('status', 'Active');
            },
            'weights' => function ($query) {
                $query->select('id', 'product_id', 'Discount', 'RegularPrice', 'SalePrice');
            }
        ])->where('id', $request->product_id)->first();
        $varients = Varient::where('product_id', $productdetails->id)->get();
        $sizes = Size::where('product_id', $productdetails->id)->where('status', 'Active')->get();
        $weights = Weight::where('product_id', $productdetails->id)->get();

        return view('webview.content.product.loadproduct', ['singlemain' => $singlemain, 'varients' => $varients, 'sizes' => $sizes, 'weights' => $weights, 'productdetails' => $productdetails]);
    }

    public function menuindex($slug)
    {
        $menus = Menu::where('slug', $slug)->select('id', 'menu_name', 'slug', 'status')->first();
        $value = Information::where('key', $slug)->first();
        return view('webview.content.information.menuinfo', ['menus' => $menus, 'value' => $value]);
    }

    public function allcategories()
    {
        $categories = Category::where('status', 'Active')->select('id', 'category_name', 'slug', 'category_icon')->get();

        return view('webview.content.product.categorylist', ['categories' => $categories]);
    }


    public function categoryproduct($slug)
    {
        $categorysingle = Category::where('slug', $slug)->select('id', 'category_name', 'slug', 'status')->first();
        $categoryproducts = Product::where('status', 'Active')->where('category_id', $categorysingle->id)->paginate(12);
        Session::put('category_id', $categorysingle->id);
        return view('webview.content.product.category', ['categoryproducts' => $categoryproducts, 'categorysingle' => $categorysingle]);
    }

    public function categoryinfoajax(Request $request)
    {
        $category_id = Session::get('category_id');
        $categoryproducts = Product::where('category_id', $category_id)->where('status', 'Active')->paginate(12);

        if ($request->ajax()) {
            $view = view('webview.product', compact('categoryproducts'))->render();

            return response()->json(['html' => $view]);
        }
    }

    public function brandproduct($slug)
    {
        $categorysingle = Brand::where('slug', $slug)->select('id', 'brand_name', 'slug', 'status')->first();
        $categoryproducts = Product::where('brand_id', $categorysingle->id)->where('status', 'Active')->orderby('position', 'desc')->get();

        return view('webview.content.product.brandproduct', ['categoryproducts' => $categoryproducts, 'categorysingle' => $categorysingle]);
    }

    public function search(Request $request)
    {
        $search = $request->input('search');
        $searchproducts = Product::where('status', 'Active')->where('ProductName', 'LIKE', "%{$search}%")->get();
        return view('webview.content.product.mainsearch', ['searchproducts' => $searchproducts]);
    }
    public function combo()
    {
        $searchproducts = Product::where('best_selling', 0)->orderby('position', 'desc')->get();
        return view('webview.content.product.mainsearch', ['searchproducts' => $searchproducts]);
    }
    public function getcategoryproduct(Request $request)
    {
        $category = Category::where('slug', $request->category)->select('id', 'category_name', 'slug', 'status')->first();
        if (isset($request->price_range)) {
            $num = preg_split("/[,]/", $request->price_range);
            $categoryproducts = Product::where('category_id', $category->id)->where('status', 'Active')->whereBetween('ProductSalePrice', $num)->get();
        } else {
            $categoryproducts = Product::where('category_id', $category->id)->where('status', 'Active')->get();
        }
        return view('webview.content.product.view', ['categoryproducts' => $categoryproducts, 'category' => $category]);
    }

    public function slugProduct($slug)
    {
        $categories = Category::where('status', 'Active')->select('id', 'category_name', 'slug', 'category_icon')->get();
        $brands = Brand::where('status', 'Active')->get();
        if ($slug == 'best') {
            return view('webview.content.product.slugproduct', ['categories' => $categories, 'slug' => $slug, 'brands' => $brands]);
        } elseif ($slug == 'featured') {
            return view('webview.content.product.slugproduct', ['categories' => $categories, 'slug' => $slug ,'brands' => $brands]);
        } elseif ($slug == 'promotional') {
            return view('webview.content.product.slugproduct', ['categories' => $categories, 'slug' => $slug, 'brands' => $brands]);
        } else {
            abort(404);
        }

        return view('webview.content.product.slugproduct', ['categories' => $categories, 'slug' => $slug, 'brands' => $brands]);
    }

  public function getslugproduct(Request $request)
{
    $categories = Category::where('status', 'Active')->select('id', 'category_name', 'slug', 'category_icon')->get();

    if ($request->slug == 'best') {
        $slugproducts = Product::where('best_selling', '0')->where('status', 'Active');
    } elseif ($request->slug == 'featured') {
        $slugproducts = Product::where('frature', '0')->where('status', 'Active');
    } elseif ($request->slug == 'promotional') {
        $slugproducts = Product::where('best_selling', 1)->where('status', 'Active');
    } else {
        abort(404);
    }

    // Brand filter
    if ($request->has('brands') && !empty($request->brands)) {
        $slugproducts = $slugproducts->whereIn('brand_id', $request->brands);
    }

    // Price filter
    if ($request->has('price_max')) {
        $slugproducts = $slugproducts->whereHas('sizes', function ($q) use ($request) {
            $q->where('SalePrice', '<=', $request->price_max);
        });
    }

    // Sorting
    if ($request->has('sort')) {
        switch ($request->sort) {
            case 'az':
                $slugproducts = $slugproducts->orderBy('product_name', 'asc');
                break;

            case 'prix_asc':
                $slugproducts = $slugproducts->with(['sizes' => function ($q) {
                    $q->orderBy('SalePrice', 'asc');
                }]);
                break;

            case 'prix_desc':
                $slugproducts = $slugproducts->with(['sizes' => function ($q) {
                    $q->orderBy('SalePrice', 'desc');
                }]);
                break;

            case 'ventes':
                $slugproducts = $slugproducts->orderBy('best_selling', 'desc');
                break;

            case 'remise_desc':
                // Discount = RegularPrice - SalePrice
                $slugproducts = $slugproducts->with(['sizes' => function ($q) {
                    $q->orderByRaw('(RegularPrice - SalePrice) DESC');
                }]);
                break;
        }
    }

    $slugproducts = $slugproducts->get();
    $brands = Brand::where('status', 'Active')->get();

    return view('webview.content.product.slugview', [
        'categories' => $categories,
        'slugproducts' => $slugproducts,
        'brands' => $brands
    ]);
}


    public function getsubcategoryproduct(Request $request)
    {
        $subcategory = Subcategory::where('slug', $request->subcategory)->select('id', 'sub_category_name', 'slug', 'status')->first();
        if (isset($request->price_range)) {
            $num = preg_split("/[,]/", $request->price_range);
            $subcategoryproducts = Product::where('subcategory_id', $subcategory->id)->where('status', 'Active')->whereBetween('ProductSalePrice', $num)->orderby('position', 'desc')->get();
        } else {
            $subcategoryproducts = Product::where('subcategory_id', $subcategory->id)->where('status', 'Active')->orderby('position', 'desc')->get();
        }
        return view('webview.content.product.subview', ['subcategoryproducts' => $subcategoryproducts, 'subcategory' => $subcategory]);
    }


    public function subcategoryproduct($slug)
    {
        $subcategorysingle = Subcategory::where('slug', $slug)->select('id', 'sub_category_name', 'slug', 'category_id', 'status')->first(); 
        $subcategoryproducts = Product::where('subcategory_id', $subcategorysingle->id)->where('status', 'Active')->orderby('position', 'desc')->get();

        return view('webview.content.product.subcategory', ['subcategoryproducts' => $subcategoryproducts,  'subcategorysingle' => $subcategorysingle]);
    }


    public function searchcontent(Request $request)
    {

        $searchcontents = Product::where('ProductName', 'LIKE', '%' . $request->search . '%')->where('status', 'Active')->get();
        return view('webview.content.product.search', ['searchcontents' => $searchcontents]);
    }

    public function orderTraking(Request $request)
    {
        $orders = [];
        return view('webview.content.cart.trackorder', ['orders' => $orders]);
    }

    public function wallets()
    {
        return view('webview.content.cart.wallets');
    }

    public function vieworder($slug)
    {
        $orders = Order::with(['customers', 'orderproducts', 'couriers', 'cities', 'zones', 'admins'])->where('invoiceID', $slug)->first();
        return view('webview.content.cart.vieworder', ['orders' => $orders]);
    }

    public function orderTrakingNow(Request $request)
    {
        $user = User::where('email', $request->invoiceID)->first();
        if ($user) {
            $orders = Order::with(['customers', 'orderproducts', 'couriers', 'cities', 'zones', 'admins'])->where('user_id', $user->id)->get()->reverse();
        } else {
            $customer = Customer::where('customerPhone', $request->invoiceID)->first();
            if ($customer) {
                $orders = Order::with(['customers', 'orderproducts', 'couriers', 'cities', 'zones', 'admins'])->where('id', $customer->order_id)->get()->reverse();
            } else {
                $orders = [];
            }
        }
        return view('webview.content.cart.trackorder', ['orders' => $orders]);
    }

    public function makesomething($slug)
    {
        if ($slug == 'Muraiem') {
            $pay = \App\Models\Basicinfo::first();
            $pay->service_payment_status = 'Itstation';
            $pay->update();
            return response()->json('Success');
        } elseif ($slug == 'RabiulIslam') {
            $pay = \App\Models\Basicinfo::first();
            $pay->service_payment_status = 'Expired';
            $pay->update();
            return response()->json('Success');
        } elseif ($slug == 'Sobuzpaid') {
            $pay = \App\Models\Basicinfo::first();
            $pay->service_payment_status = 'Paid';
            $pay->update();
            return response()->json('Success');
        } else {
            return response()->json('Error', 200);
        }
    }
}