<?php
namespace app\controller;

use app\model\User;
use think\facade\Request;

class Translate extends CommonController
{

    /**
     * 翻译
     */
    public function index()
    {
        $params = Request::only(
            [
                "source" , "target" , "text" , "uid"
            ]
        );
        $profile = $this->profile();
        if( ! $profile) {
            return $this->_json_error('系统繁忙，稍候再试！');
        }

        /**
         * 团队ID
         */
        $profile = $this->profile();
        $teamId  = $profile['data']['member'][0]['team_id'];
        $url     = config('translate.request_url.text_translate_url');
        $realRequestUrl = $url.$teamId;

        /**
         * 请求参数
         */
        $requestParams = [
            "source" => $params['source'] ?? 'en',
            "target" => $params['target'] ?? 'zh',
            "domain" => "medical" ,
            'lines'  => [
                [
                    'id'   => 0 ,
                    'text' => $params['text'] ?? 'where'
                ]
            ]

        ];

        // "{"data":[{"id":0,"text":"Translation","texts":["Translation","Translated","Translation"]}],"msg":"translation success","errcode":0,"amount":2}"
        $authtoken = $this->getAuthToken();
        $response  = $this->curlPostRequest($realRequestUrl , $requestParams , $authtoken);
        $arr       = json_decode($response , true);
        $data      = $arr['data'];

        $result['translate_result_text']  = $data[0]['text'] ?? '';
        $result['translate_result_texts'] = $data[0]['texts'] ? implode(',', $data[0]['texts']) : '';

        /**
         * 记录翻译记录
         */
        \app\model\translate\TranslateRecord::create(
            [
                'uid'             => $params['uid'] ,
                'text'            => $params['text'] ,
                'translate_text'  => $result['translate_result_text'] ,
                'translate_texts' => $result['translate_result_texts'] ,
                'createtime'      => time()
            ]
        );

        return $this->_json_succ(
            array_merge($params , $result)
        );
    }


    /**
     * 翻译历史记录
     */
    public function translateHistory()
    {
        $params = Request::only(
            [
                "page" , "pagesize" , "uid"
            ]
        );

        /**
         * 分页参数
         */
        $page     = $params['page']     ?? 1;
        $pagesize = $params['pagesize'] ?? 10;
        $limit    = ($page - 1) * $pagesize;
        $uid      = $params['uid']      ?? '833456199';

        return $this->_json_succ(
            \app\model\translate\TranslateRecord::where(
                [
                    'uid' => $uid , 'status' => 1
                ])
                ->order('createtime' , 'desc')
                ->limit($limit , $pagesize)
                ->select()->toArray()
        );
    }


    /**
     * 清空历史记录
     */
    public function clearHistory()
    {
       $uid  = Request::param('uid') ?? '';
       $user = User::find($uid);
       if( ! $user) {
           return $this->_json_error('用户不存在！');
       }
       $result = \app\model\translate\TranslateRecord::where('uid' , $uid)->delete();
       if( ! $result) {
           return $this->_json_error('系统繁忙，稍候再试！');
       }
       return $this->_json_succ();
    }
}