<?php


namespace App\Http\Controllers\V1;


use App\Models\Document;
use App\Models\EmailWeb;
use App\Services\Api\FeedsService;
use App\Services\Api\UserService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Validator;


class HomeController extends Controller
{
    public function __construct()
    {
        parent::__construct();
    }


    // 语言切换方法
    public function language(Request $request){
        $lang = $request->lang;
        App::setLocale($lang);  //配置默认语言
        file_put_contents('request.log',date('Y-m-d H:i:s').'---language--'.App::getLocale ().PHP_EOL,FILE_APPEND);
        $request->session()->put('language',$lang);  //存到session
        return App::getLocale();
    }

    //用户详情
    public function userDetail(Request $request){
        $userService = new UserService();
        $data = $userService->userDetail($request->post('user_id'));
        return $this->success($data);
    }

    //用户动态
    public function userFeeds(Request $request){
        $feedService = new FeedsService();
        $data = $feedService->userFeeds($request->post('user_id'));
        return $this->success($data);
    }

    //用户协议和隐私协议  关于我们
    public function document(Request $request){
        $lang = $request->header('lang','zh');
        if(isset($request->cont) && $request->cont==1){
            $data = Document::query()->where(['id'=>$request->id])->first();
            if($lang=='zh'){
                unset($data['en_title']);
                unset($data['en_content']);
            }else{
                $data['content'] = $data['en_content'];
                $data['title'] = $data['en_title'];
                unset($data['en_content']);
                unset($data['en_title']);
            }
            return $this->success($data);
        }else{
            $url  = "https://".$_SERVER['HTTP_HOST']."/xieyi/content.html?id=1&lang=".$lang;
            $url2 = "https://".$_SERVER['HTTP_HOST']."/xieyi/content.html?id=2&lang=".$lang;
            $url3 = "https://".$_SERVER['HTTP_HOST']."/xieyi/content.html?id=3&lang=".$lang;
            return $this->success(['url1'=>$url,'url2'=>$url2,'url3'=>$url3]);
        }
    }

    //分享信息
    public function shareInfo(Request $request){
        $lang = $request->header('lang','en');
        $type = $request->get('type',1);
        $id = $request->get('id',1);
        $url = '';
        if($type==1){
            //用户
            $url = 'https://'.$_SERVER['HTTP_HOST'].'/h5/index.html#/pages/personal/personal?user_id='.$id.'&id='.$id.'&lang='.$lang;
        }elseif ($type==2){
            //动态
            $url = 'https://'.$_SERVER['HTTP_HOST'].'/h5/index.html#/pages/dynamic/dynamic?feed_id='.$id.'&lang='.$lang;
        }elseif ($type==3){
            //球队
            $url = 'https://'.$_SERVER['HTTP_HOST'].'/h5/index.html#/pages/team/team?team_id='.$id.'&id='.$id.'&lang='.$lang;
        }elseif ($type==4){
            //比赛
            $url = 'https://'.$_SERVER['HTTP_HOST'].'/h5/index.html#/pages/CountGroup/CountGroup?game_id='.$id.'&lang='.$lang;
        }
        $data['url'] = $url;

        return $this->success($data);
    }

    //网站 email 提交
    public function submitEmail(Request $request)
    {
        $lang = $request->lang;

        if($lang == 'zh'){
            $emailError  = '邮箱不合法!';
            $hadExist    = '邮箱已存在!';
            $submitError = '提交失败，请重试~';
            $success     = '提交成功!';
        }else{
            $emailError  = 'Email is invalid!';
            $hadExist    = 'Sorry! this email had exist!';
            $submitError = 'Submit is error! please tyr again ~';
            $success     = 'Success!';
        }

        $validator = Validator::make($request->all(), [
            'email' => 'email',
        ],[
            'email.email' => $emailError
        ]);
        if($validator->fails()){
            return  $this->error($validator->errors()->first());
        }
        if(EmailWeb::query()->where('email', $request->email)->first()){
            return  $this->error($hadExist);
        }

        $emailWeb = EmailWeb::getInstance();
        $emailWeb->email = $request->email;
        if(!$emailWeb->save()){
            return  $this->error($submitError);
        }

        return  $this->success('','', $success);
    }


}
