<?php

namespace App\Http\Controllers\Frontend;

use App\Models\Product;
use App\Models\Attribute;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Http\Controllers\Controller;

class LandingPageController extends Controller
{
    


    public function landingProduct($slug){
        $product = Product::with('comments', 'reviews')->where('slug', $slug)->where('status', true)->firstOrFail();
        $product->reach += 1;
        $product->update();
        $colors_product = DB::table('color_product')
                        ->select('*')
                        ->join('colors', 'colors.id', '=', 'color_product.color_id')
                        ->where('color_product.product_id', $product->id)
                        ->get();
        $attributes = Attribute::all();


        $single_product_layout = setting('SINGLE_PRODUCT_LAYOUT'); // 1, 2, 3 like this, coming from setting and changin from dashboard/setting/layout
        $single_product_layout = ($single_product_layout) ? $single_product_layout : 1; // if null then set 1


        // $this->validate($request, [
        //     'id'    => 'required|integer',
        //     'qty'   => 'required|integer',
        //     'color' => 'nullable|string',
        //     'size'  => 'nullable|string'
        // ]);
        // $product = Product::find($request->id);
        // if($product->colors->count()>0 && $request->color=='blank'){
        //     notify()->warning("Please Choose a Colour", "Something Wrong");
        //     return back();
        // }
        // $attributes = Attribute::all();
        // foreach ($attributes as $attribute){
        //     $slug=$attribute->slug;
        //     $s =$request->$slug;
        //     if($s=='blank'){
        //         notify()->warning("Sorry,Please Fill All Attribute", "Something Wrong");
        //         return back();
        //     }
        // }
        
        // if (!$product) {
        //     notify()->warning("Sorry, please try again", "Something Wrong");
        //     return back();
        // }
        
        if($product->status_landing_page == 1){
            return view("frontend.landing_pages.page_1.landing_page_1_main", compact('product', 'colors_product', 'attributes'));
        }else{
            notify()->warning("Sorry, please try again", "Something Wrong");
            return back();
        }
    }



    public function updateLandingPageStatus(Request $request){
        
        $this->validate($request, [
            'id'    => 'required|integer',
            'status'   => 'required',
        ]);
        
        
        
        if (auth()->user() && auth()->user()->desig == 1 && auth()->user()->role_id == 1) {
            $status = ($request->status == 'null') ? null : $request->status;
            $product = Product::find($request->id);
            $product->status_landing_page = $status;
            $product->update();
            notify()->success("Product status successfully updated", "Update");
            return back();

        } elseif(auth()->user() && auth()->user()->role_id == 2) {
            $status = ($request->status == 'null') ? null : 0;
            $product = Product::find($request->id);
            $product->status_landing_page = $status;
            $product->update();
            notify()->success("Product status successfully updated", "Update");
            return back();
        }

        notify()->warning("You do not have permission to perform this action.", "Access Denied");
        return back();
        


    }


}
