<?php
declare (strict_types = 1);

namespace app\index\controller;

use app\index\model\ArticleModel;
use app\index\model\FaqModel;
use app\index\model\ProductModel;
use app\index\model\SysBannerModel;
use think\facade\View;

/**
 * 首页控制器
 */
class Index extends Common
{
    /**
     * 首页
     */
    public function index()
    {
        // 获取banner数据
        $banner = $this->getBannerData();
        View::assign('focus_images', $banner['focus_images']);
        View::assign('product_categorys', $banner['product_categorys']);
        View::assign('featured_topics', $banner['featured_topics']);
        View::assign('video', array_shift($banner['video']));
        View::assign('scenes', $banner['scenes']);
        View::assign('brand_story', array_map(function($item) {
            $item['year'] = '';
            $arr = explode('-', $item['title']);
            if (count($arr) > 1) {
                $item['year'] = $arr[0];
            }
            return $item;
        }, $banner['brand_story']??null));
        View::assign('data_statistics', $banner['data_statistics']);

        // 获取明星产品/热点产品
        View::assign('featured_products', $this->getFeaturedProducts());

        // 获取推荐文章
        View::assign('recommend_articles', $this->getRecommendArticles());

        // 获取常见问题
        View::assign('recommend_faq', $this->getRecommendFAQ());

        return View::fetch('index');
    }

    // 获取banner数据
    private function getBannerData()
    {
        $banners = SysBannerModel::with(['items' => function($query) {
                $query->where('type', 'IN', ['image', 'video'])->where('status', '=', 1)->order(['sort' => 'asc', 'id' => 'desc']);
            }])
            ->atPlatform(request()->from)
            ->uniqueLabel([
                'BANNER_67f61cd70e8e1',
                'BANNER_67f633023a5b3',
                'BANNER_67f63f8ab5029',
                'BANNER_67f724ed81b1e',
                'BANNER_67f7392b4d83a',
                'BANNER_67f7410e244fb',
                'BANNER_67f76a96545f9',
            ])
            ->language($this->lang_id)
            ->recommend(true)
            ->enabled(true)
            ->select();
        $banner_map = [];
        foreach ($banners as $v) {
            $banner_map[$v->unique_label] = $v;
        }

        // 处理焦点轮播图和产品分类
        $data['focus_images']      = [];  // 焦点轮播图
        $data['product_categorys'] = [];  // 产品分类信息
        $data['featured_topics']   = [];  // 特色专题及公司实力
        $data['video']             = [];  // 视频
        $data['scenes']            = [];  // 场景介绍
        $data['brand_story']       = [];  // 品牌故事
        $data['data_statistics']   = [];  // 数据统计
        if (!empty($banner_map)) {
            $data['focus_images']      = data_get($banner_map, 'BANNER_67f61cd70e8e1')?->items->toArray();
            $data['product_categorys'] = data_get($banner_map, 'BANNER_67f633023a5b3')?->items->toArray();
            $data['product_categorys'] = data_get($banner_map, 'BANNER_67f633023a5b3')?->items->toArray();
            $data['featured_topics']   = data_get($banner_map, 'BANNER_67f63f8ab5029')?->items->toArray();
            $data['video']             = data_get($banner_map, 'BANNER_67f724ed81b1e')?->items->toArray();
            $data['scenes']            = data_get($banner_map, 'BANNER_67f7392b4d83a')?->items->toArray();
            $data['brand_story']       = data_get($banner_map, 'BANNER_67f7410e244fb')?->items->toArray();
            $data['data_statistics']   = data_get($banner_map, 'BANNER_67f76a96545f9')?->items->toArray();
        }

        return $data;
    }

    // 获取明星/热点产品
    private function getFeaturedProducts()
    {
        $products = ProductModel::field([
                'id',
                'name',
                'short_name',
                'cover_image',
            ])
            ->language($this->lang_id)
            ->enabled(true)
            ->onSale(true)
            ->onShelves(true)
            ->hot(true)
            ->order(['sort' => 'asc', 'id' => 'desc'])
            ->select();
        if ($products->isEmpty()) {
            return [];
        }

        return $products->toArray();
    }

    // 获取推荐文章
    private function getRecommendArticles()
    {
        $articles = ArticleModel::field([
                'id',
                'title',
                'desc',
                'image'
            ])
            ->language($this->lang_id)
            ->recommend(true)
            ->order(['sort' => 'asc', 'id' => 'desc'])
            ->limit(6)
            ->select();
        if ($articles->isEmpty()) {
            return [];
        }

        return $articles->toArray();
    }

    // 获取推荐问题
    private function getRecommendFAQ()
    {
        $faqs = FaqModel::field([
                'id',
                'question',
                'image',
                'answer'
            ])
            ->language($this->lang_id)
            ->recommend(true)
            ->order(['sort' => 'asc', 'id' => 'desc'])
            ->limit(6)
            ->select();
        if ($faqs->isEmpty()) {
            return [];
        }
        return $faqs->toArray();
    }
}
