<?php

namespace App\Http\Controllers\Admin;

use App\Models\Setting;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
use App\Traits\ViewLangTrait;

class SeoController extends AdminController
{
    use ViewLangTrait;
    /**
     * Display a listing of the resource.
     *
     * @param $type
     * @param Request $request
     * @return \Illuminate\Http\Response
     */
    public function index($type, Request $request)
    {
        if (config('managerLang') != config('setting.lang')) {
            return response()->json(['status' => 'error', 'message' => '非主语言下，无法智能生成TDK！']);
        }

        $action = Str::studly($type);
        return call_user_func([$this, $action], $request);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @param Request $request
     * @return \Illuminate\Http\Response
     */
    public function home(Request $request)
    {
        if (!$request->input('keywords')) {
            return response()->json(['status' => 'error', 'data' => '请填写首页关键词！']);
        }

        $title = Setting::where('lang', config('setting.lang'))->where('name', 'title')->value('value');
        $keywords = explode(',', str_replace('，', ',', $request->input('keywords')));
        $description = self::fileArray('system', config('setting.lang'))['home_description'];
        $description = str_replace(['{g}', '{k1}', '{k2}', '{k3}'], [$title, $keywords[0] ?? '', $keywords[1] ?? '', $keywords[2] ?? ''], $description);

        return response()->json(['status' => 'success', 'data' => $description]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @param Request $request
     * @return \Illuminate\Http\Response
     */
    public function productCate(Request $request)
    {
        $title = Setting::where('lang', config('setting.lang'))->where('name', 'title')->value('value');
        $tagsArr = $request->get('tags', []);
        $seo['title'] = str_replace(['{1}', '{2}', '{3}', '{4}'], [$tagsArr[0] ?? '', $tagsArr[1] ?? '', $tagsArr[2] ?? '', $title], Setting::seo('cate_title'));
        $seo['keywords'] = str_replace(['{1}', '{2}', '{3}'], [$tagsArr[0] ?? '', $tagsArr[1] ?? '', $tagsArr[2] ?? ''], Setting::seo('cate_keyword'));
        $seo['description'] = str_replace(['{4}', '{1}', '{2}', '{3}'], [$title, $tagsArr[0] ?? '', $tagsArr[1] ?? '', $tagsArr[2] ?? ''], Setting::seo('cate_description'));

        return response()->json(['status' => 'success', 'data' => $seo]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @param Request $request
     * @return \Illuminate\Http\Response
     */
    public function product(Request $request)
    {
        $title = Setting::where('lang', config('setting.lang'))->where('name', 'title')->value('value');
        $seo['title'] = str_replace(['{1}', '{2}'], [$request->input('title'), $title], Setting::seo('product_title'));
        $seo['keywords'] = str_replace('{1}', $request->input('title'), Setting::seo('product_keyword'));
        $seo['description'] = str_replace('{1}', $request->input('title'), Setting::seo('product_description'));

        return response()->json(['status' => 'success', 'data' => $seo]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @param Request $request
     * @return \Illuminate\Http\Response
     */
    public function newsCate(Request $request)
    {
        $title = Setting::where('lang', config('setting.lang'))->where('name', 'title')->value('value');
        $keywords = Setting::where('lang', config('setting.lang'))->where('name', 'meta_keywords')->value('value');
        $description = Setting::where('lang', config('setting.lang'))->where('name', 'meta_description')->value('value');
        $tagsArr = $request->get('tags', []);
        $seo['title'] = $request->input('title') . ' - ' . $title;
        $seo['keywords'] = $tagsArr ? implode(',', $tagsArr) : $keywords;
        $seo['description'] = $description;

        return response()->json(['status' => 'success', 'data' => $seo]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @param Request $request
     * @return \Illuminate\Http\Response
     */
    public function news(Request $request)
    {
        $title = Setting::where('lang', config('setting.lang'))->where('name', 'title')->value('value');
        $keywords = Setting::where('lang', config('setting.lang'))->where('name', 'meta_keywords')->value('value');
        $description = Setting::where('lang', config('setting.lang'))->where('name', 'meta_description')->value('value');
        $tagsArr = $request->get('tags', []);
        if ($request->input('content')) {
            $description = trim(str_replace(['&nbsp;', PHP_EOL, '　'], '', strip_tags($request->input('content'))));

            if (mb_strlen($description, 'utf-8') > 200) {
                $description = mb_substr($description, 0, 200, 'utf-8') . '...';
            }
        }
        $seo['title'] = $request->input('title') . ' - ' . $title;
        $seo['keywords'] = $tagsArr ? implode(',', $tagsArr) : $keywords;
        $seo['description'] = $description;

        return response()->json(['status' => 'success', 'data' => $seo]);
    }
}
