<?php

namespace app\admin\controller;

use app\BaseController;
use app\admin\model\Config;
use app\admin\model\CountryLanguages;
use think\facade\Lang;

class Home extends BaseController
{

    
    /**
     * @api {get} /country/list 国家|地区 列表
     * @apiGroup 公共
     * @apiVersion 1.0.0
     * @apiUse lang
     * @apiSuccess {int} id 国家ID
     * @apiSuccess {String} country_name 国家名称
     */
    public function countryList()
    {
        $list = CountryLanguages::where('language_code', $this->lang)
            ->order('id')
            ->select();
        if (count($list) < 1) {
            $list = CountryLanguages::where('language_code', config("app.fallback_locale"))
                ->order('id')
                ->select(); 
        }
        return $this->success($list);
    }
    /**
     * @api {get} /home/config 系统配置
     */
    function config()
    {
        $params = $this->request->param();
        $params['flag'] = $params['flag'] ?? '';
        $fields = !empty($params['fields']) ? $params['fields'] : [];
        $list = Config::getConfigData($fields, $params['flag']);
        return $this->success($list);
    }

    /**
     * @api {get} /home/getLanguageCode 语言列表
     * @apiGroup 首页
     * @apiVersion 1.0.0
     * @apiUse header
     *
     */
    function getLanguageCode()
    {
        $list = [
            [
                'code' => 'zh',
                'name' => '中文简体',
                'icon' => '',
            ], [
                'code' => 'en',
                'name' => '英语',
                'icon' => '',
            ], [
                'code' => 'zh-tw',
                'name' => '中文繁体',
                'icon' => '',
            ], [
                'code' => 'pt',
                'name' => '葡萄牙语',
                'icon' => '',
            ], [
                'code' => 'jp',
                'name' => '日语',
                'icon' => '',
            ], [
                'code' => 'ko',
                'name' => '韩语',
                'icon' => '',
            ], [
                'code' => 'th',
                'name' => '泰语',
                'icon' => '',
            ], [
                'code' => 'id',
                'name' => '印度尼西亚',
                'icon' => '',
            ], [
                'code' => 'vi',
                'name' => '越南语',
                'icon' => '',
            ], [
                'code' => 'pt-br',
                'name' => '巴西葡萄牙语',
                'icon' => '',
            ], [
                'code' => 'hi',
                'name' => '印地语',
                'icon' => '',
            ],[
                'code' => 'en-ph',
                'name' => '菲律宾语',
                'icon' => '',
            ],  [
                'code' => 'en-in',
                'name' => '印度英语',
                'icon' => '',
            ],[
                'code' => 'tl',
                'name' => '他加禄语',
                'icon' => '',
            ], 
        ];
        foreach ($list as &$item ) {
            $item['name'] = Lang::get($item['name']);
        }
        return $this->success($list);
    }

}