<?php
declare (strict_types = 1);

namespace app\admin\controller\v1;

use app\admin\model\v1\ArticleCategoryModel;
use app\admin\model\v1\ArticleModel;
use app\admin\model\v1\ProductCategoryModel;
use app\admin\model\v1\ProductModel;
use think\facade\Db;

class System
{
    // 系统信息
    public function info()
    {
        $mysql_version = Db::query('select version() as version');
        $info = [
            [
                'name'  => '操作系统',
                'value' => PHP_OS
            ],
            [
                'name'  => '运行环境',
                'value' => $_SERVER["SERVER_SOFTWARE"]
            ],
            [
                'name'  => 'PHP版本',
                'value' => PHP_VERSION
            ],
            [
                'name'  => '上传附件限制',
                'value' => ini_get('upload_max_filesize')
            ],
            [
                'name'  => 'MySQL版本',
                'value' => !empty($mysql_version) ? $mysql_version[0]['version'] : '未知'
            ],
            [
                'name'  => '最大执行时间',
                'value' => ini_get('max_execution_time') . 's'
            ],
            [
                'name'  => 'GD版本',
                'value' => function_exists('gd_info') ? (gd_info()['GD Version']) : '未知'
            ],
            [
                'name'  => '最大占用内存',
                'value' => ini_get('memory_limit')
            ],
            [
                'name'  => '当前时间',
                'value' => date('Y-m-d H:i:s')
            ],
            [
                'name'  => '服务器时区',
                'value' => function_exists("date_default_timezone_get") ? date_default_timezone_get() : '未知'
            ],
            [
                'name'  => '是否开启安全模式',
                'value' => ini_get('safe_mode') ? 'YES' : 'NO'
            ],
            [
                'name'  => '允许打开远程连接',
                'value' => ini_get("allow_url_fopen") ? 'YES' : 'NO'
            ],
            [
                'name'  => 'CURL支持',
                'value' => function_exists('curl_init') ? 'YES' : 'NO'
            ],
            [
                'name'  => 'Zlib支持',
                'value' => function_exists('gzclose') ? 'YES' : 'NO'
            ],
            [
                'name'  => '自身版本',
                'value' => '1.0.0'
            ],
            [
                'name'  => '服务器域名/IP',
                'value' => $_SERVER['HTTP_HOST'] . '[' . gethostbyname($_SERVER['SERVER_NAME']) . ']'
            ]
        ];

        return success('获取成功', $info);
    }

    // 组装系统内页面URL
    public function urls()
    {
        $lang_id = request()->lang_id;

        if (request()->has('link_to')) {
            $param = request()->get([
                'link_to',
                'id'
            ]);

            $urls = [];
            switch ($param['link_to']) {
                case 'article':
                    $articles = $this->getArticleByCategory($lang_id, $param['id']);
                    $urls     = array_map(function($item) {
                        $item['url'] = (string)url('yy/zz', ['id' => $item['id']]);
                        return $item;
                    }, $articles);
                    break;
                case 'product':
                    $products = $this->getProductByCategory($lang_id, $param['id']);
                    $urls     = array_map(function($item) {
                        $item['url'] = (string)url('yy/zz', ['id' => $item['id']]);
                        return $item;
                    }, $products);
                    break;
                default:
                    return error('请确认link_to参数');
                    break;
            }
            return success('获取成功', $urls);
        } else {
            // 获取文章分类
            $article_category = $this->getArticleCategory($lang_id);
            // 获取产品分类
            $product_category = $this->getProductCategory($lang_id);

            $urls = [
                [
                    'name'    => '自定义',
                    'link_to' => 'custom',
                    'data'    => []
                ],
                [
                    'name'    => '文章分类',
                    'link_to' => 'article_category',
                    'data'    => array_to_tree(array_map(function($item) {
                        $item['url'] = (string)url('yy/zz', ['id' => $item['id']]);
                        return $item;
                    }, $article_category), 0, 'pid', false, false)
                ],
                [
                    'name'    => '文章管理',
                    'link_to' => 'article',
                    'data'    => array_to_tree($article_category, 0, 'pid', false, false)
                ],
                [
                    'name'    => '产品分类',
                    'link_to' => 'product_category',
                    'data'    => array_to_tree(array_map(function($item) {
                        $item['url'] = (string)url('yy/zz', ['id' => $item['id']]);
                        return $item;
                    }, $product_category), 0, 'pid', false, false)
                ],
                [
                    'name'    => '产品',
                    'link_to' => 'product',
                    'data'    => array_to_tree($product_category, 0, 'pid', false, false)
                ]
            ];

            return success('获取成功', $urls);
        }
    }
    // 获取文章分类数据
    private function getArticleCategory($lang_id)
    {
        $data = ArticleCategoryModel::field([
                'id',
                'pid',
                'name'
            ])
            ->language($lang_id)
            ->isShow(true)
            ->order(['sort' => 'asc', 'id' => 'desc'])
            ->select();

        return $data->toArray();
    }
    // 获取产品分类数据
    private function getProductCategory($lang_id)
    {
        $data = ProductCategoryModel::field([
                'id',
                'pid',
                'name'
            ])
            ->language($lang_id)
            ->isShow(true)
            ->order(['sort' => 'asc', 'id' => 'desc'])
            ->select();

        return $data->toArray();
    }
    // 根据文章分类获取文章
    private function getArticleByCategory($lang_id, $category_id)
    {
        $data = ArticleModel::field([
                'id',
                'title' => 'name'
            ])
            ->language($lang_id)
            ->category($category_id)
            ->enabled()
            ->select();

        return $data->toArray();
    }
    // 根据产品分类获取产品
    private function getProductByCategory($lang_id, $category_id)
    {
        $data = ProductModel::field([
                'id',
                'name'
            ])
            ->language($lang_id)
            ->category($category_id)
            ->enabled()
            ->isShow(true)
            ->select();

        return $data->toArray();
    }

    // 根据系统页面url获取回显数据项
    static public function getEchoDataBySystemPageUrl($link_to, $link)
    {
        if ('custom' == $link_to) return [];

        $data = [];
        $url  = parse_url($link, PHP_URL_QUERY);
        if (empty($url)) return [];
        
        parse_str($url, $params);
        switch ($link_to) {
            case 'article':
                $data = ArticleModel::field(['id', 'title' => 'name'])->bypk($params['id'])->find();
                break;
            case 'product':
                $data = ProductModel::field(['id', 'name'])->bypk($params['id'])->find();
                break;
            default:
                return [];
                break;
        }
        return [
            'id'   => $data['id'],
            'name' => $data['name'],
            'link' => $link
        ];
    }
}