<?php

namespace App\Http\Controllers;

use App\Handlers\ImageHandler;
use Carbon\Carbon;
use GuzzleHttp\Client;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;

class HumanreviewController extends  Controller{


    public function get_info(Request $request){
        $user=DB::table('user')->where('phone',$request->phone)->select('nickname')->first();
        if (!$user){
            return $this->error(300);
        }
        return $this->success($user);
    }

    public function review(Request $request,ImageHandler $imageHandler){
        $season=DB::table('basic')->where('keys','=','season')->value('value');
        $name=Auth::user()->name;
        $num=DB::table('inte_review')->where('number',$request->number)->first();
        $nums=DB::table('fail')->where('number',$request->number)->where('type','<>',2)->first();
        $uid=DB::table('user')->where('phone',$request->phone)->value('id');
        $phone=DB::table('user')->where('id',$uid)->value('phone');
//        if (!$uid){
//            return $this->error(100);
//        }
        if ($num||$nums){
            return $this->error(400);
        }


        $count=$request->hard+$request->simple+$request->old;
        $inte=$count*10;
        $data=[
            'type'=>$request->type,
            'type_one'=>$request->hard,
            'type_two'=>$request->simple,
            'type_three'=>$request->old,
            'number'=>$request->number,
            'figure'=>$request->figure,
            'season'=>$season,
            'integral'=>$inte,
            'uid'=>$uid,
            'status'=>$name,
            'imgurl'=>$request->imgurl,
            'time'=>strtotime($request->time),
            'created_at'=>Carbon::now()->toDateTimeString()
        ];
        $re=DB::table('inte_review')->insert($data);
        DB::table('inte_log')->insert([
            'uid'=>$uid,
            'season'=>$season,
            'integral'=>$inte,
            'created_at'=>Carbon::now()->toDateTimeString(),
            'updated_at'=>Carbon::now()->toDateTimeString(),
            'type'=>1,
            'time'=>strtotime($request->time),
        ]);
        if ($re){
            DB::table('user')->where('id',$uid)->increment('integral',$inte);
            DB::table('user')->where('id',$uid)->increment('total',$count);
            DB::table('user')->where('id',$uid)->increment('money',$request->figure);
            return $this->success([0]);
        }
        return $this->error(600);
    }

    public function upload_img(Request $request ,ImageHandler $imageHandler){
        if ($request->has('img')){
            $imgurl=$imageHandler->save($request->img,'img',Auth::user()->id,1024);
            if (!$imgurl){
                return $this->error(300);
            }else{
                $client=new Client();
                $url='https://api.jiuweiyun.cn/api/human/upload';
                $array=[
                    'query' => [
//                        'path'=>$imgurl['path'],
                        'verify_key'=>$request->verify_key,
                        'verify_code'=>$request->verify_code
                    ],
                    'multipart' => [
                        [
                            'name'     => 'img',
                            'contents' => fopen(public_path().$imgurl['file'], 'r')
                        ],
                    ],
                ];
                $data=$client->request('POST',$url,$array);
//                dd($data);
                $ad = json_decode($data->getBody()->getContents());
//                dd($ad);
                if ($ad->code==200){
                    $res=$ad->data;
                    $re=[
                        'number'=>$res->number,
                        'time'=>$res->time,
                        'imgurl'=>$imgurl['path']
                    ];
                    return $this->success($re);
                }

                return $this->error(400,['imgurl'=>$imgurl['path']]);
            }
        }
        return $this->error(600);
    }
}