<?php

namespace App\Http\Controllers\Frontend;

use App\Models\Product;
use App\Models\Setting;
use App\Models\ProductCate;
use App\Models\ProductAttribute;
use App\Models\Page;
use App\Models\ProductIntroduction;

class ProductController extends FrontendController
{
    use \App\Traits\ControllerTrait;

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $title = self::title('product', 'web');
        $seo   = self::meta($title);

        return view('theme::product', compact('seo'));
    }

    /**
     * Display a listing of the resource.
     *
     * @param $cid
     * @return \Illuminate\Http\Response
     */
    public function lists($cid)
    {
        $cate = ProductCate::with('description')->flag()->where('slug', $cid)->first();

        if (!$cate) {
            return response()->view('theme::404', ['message' => 'Page not found !'], 404);
        }
        if (!$cate->isRoot()) {
            $root   = $cate->getRoot();
            $rootId = $root->id;
        } else {
            $rootId = $cate->id;
        }
        $pageNum  = Setting::get('product_paginate') ?: 6;
        $idArr    = $cate->getDescendantsAndSelf(['id'])->pluck('id')->toArray();
        $products = Product::with(['cates', 'description', 'attrs'])->flag()->whereIn('pid', $idArr)->orderBy('orders', 'DESC')->paginate($pageNum);

        $tags   = $cate->tags()->with('description')->get();

        $productSeo = Setting::get('product_seo');
        $company    = Setting::get('title');
        if ($productSeo) {
            $seo = self::productCateMeta($tags, $cate->description);
        } else {
            $seo['title']       = str_replace(['{1}', '{2}', '{3}', '{4}'], [$tags[0]->description->name ?? '', $tags[1]->description->name ?? '', $tags[2]->description->name ?? '', $company], Setting::seo('cate_title'));
            $seo['keywords']    = str_replace(['{1}', '{2}', '{3}'], [$tags[0]->description->name ?? '', $tags[1]->description->name ?? '', $tags[2]->description->name ?? ''], Setting::seo('cate_keyword'));
            $seo['description'] = str_replace(['{4}', '{1}', '{2}', '{3}'], [$company, $tags[0]->description->name ?? '', $tags[1]->description->name ?? '', $tags[2]->description->name ?? ''], Setting::seo('cate_description'));
        }
        $seo['resumo'] = "";

        return view('theme::product_list', compact('products', 'seo', 'cate', 'rootId'));
        
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function details($id)
    {
        $product = Product::with(['cates.description', 'description', 'downloads'])->flag()->where('slug', $id)->first();

        if (!$product) {
            return response()->view('theme::404', ['message' => 'Page not found !'], 404);
        }

        if (!$product->cates->isRoot()) {
            $root   = $product->cates->getRoot();
            $rootId = $root->id;
        } else {
            $rootId = $product->cates->id;
        }

        $pages = Page::with('description')->where('flag', 1)->whereIn('id', $product->page_ids)->get();

        $attrs  = ProductAttribute::leftJoin('attributes', 'attributes.id', '=', 'product_attributes.attribute_id')
                ->leftJoin('attribute_descriptions', 'attribute_descriptions.attribute_id', '=', 'product_attributes.attribute_id')
                ->select('product_attributes.*', 'attribute_descriptions.title')
                ->where('attribute_descriptions.lang', config('lang'))
                ->where('product_attributes.lang', config('lang'))
                ->where('product_attributes.product_id', $product->id)
                ->oldest('attributes.orders')->get();

        $intros = ProductIntroduction::where('lang', config('lang'))->where('product_id', $product->id)->oldest('id')->get();

        $tags   = $product->tags()->with('description')->get();
        $result = Product::find($product->id)->update(['hits' => $product->hits + 1]);
        
        $title  = '';
        if ($product->description) {
            $title = $product->description->seo_title ?: $product->description->title;
        }

        $productSeo = Setting::get('product_seo');
        $company    = Setting::get('title');
        if ($productSeo) {
            $seo = self::productMeta($title, $product->description);
        } else {
            $seo['title']       = str_replace(['{1}', '{2}'], [$title, $company], Setting::seo('product_title'));
            $seo['keywords']    = str_replace('{1}', $title, Setting::seo('product_keyword'));
            $seo['description'] = str_replace('{1}', $title, Setting::seo('product_description'));
        }
        
        return view('theme::product_detail', compact('product', 'seo', 'tags', 'attrs', 'rootId', 'pages', 'intros'));
    }

   
}
