<?php

namespace App\Http\Controllers;

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

class BasicController extends Controller
{

    public function repair(Request $request){
//        dd($request->all());
        DB::table('basic')->where('keys','=','repair_start')->update(['value'=>$request->start]);
        DB::table('basic')->where('keys','=','repair_end')->update(['value'=>$request->end]);
        return $this->success([]);
    }

    public function jude(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'title' => 'required|unique:posts|max:255',
            'body' => 'required',
        ]);
        if ($validator->fails()) {
            return $this->error(300);
        }
//        auth()->logout();
        return Auth::user()->id;
        $time=Carbon::now()->timestamp;
//        return Carbon::now()
        $time_end = DB::table('basic')->whereIn('keys', ['season', 'gift_end_time'])->select('value')->get();
        if ($time_end[0]->value < $time) {
            return $this->response()->array([
                'code' => 200,
                'data' => [
                    'set' => true,
                    'season' => (int)$time_end[1]->value
                ]
            ]);
        } else {
            return $this->response()->array([
                'code' => 200,
                'data' => [
                    'set' => true,
//                    'set'=>false,
                    'season' => (int)$time_end[1]->value
                ]
            ]);
        }

    }

    /*
     * 当前时间能否设置赛季信息
     */

    public function status(){
        $time=Carbon::now()->timestamp;
        $data=DB::table('basic')->get();
        foreach ($data as $k=>$v){
            $dat[$v->keys]=$v->value;
        }
        $time_end=DB::table('basic')->where('keys','=','round_end_time')->value('value');
        $time_start=DB::table('basic')->where('keys','=','round_start_time')->value('value');
        $season=DB::table('basic')->where('keys','=','season')->value('value');
        if ($time<$time_end&&$time>$time_start){
            $data=[
                'set'=>true,
                'season'=>$dat['season'],
                'sign_start_time' => $dat['sign_start_time']*1000,
                'sign_end_time' => $dat['sign_end_time']*1000,
                'round_start_time' => $dat['round_start_time']*1000,
                'round_end_time' => $dat['round_end_time']*1000,
                'gift_start_time' => $dat['gift_start_time']*1000,
                'gift_end_time' => $dat['gift_end_time']*1000,
                'cost'=>$dat['cost'],

            ];
        }else{
            $data=[
                'set'=>true,
                'season'=>$dat['season'],
                'sign_start_time' => $dat['sign_start_time']*1000,
                'sign_end_time' => $dat['sign_end_time']*1000,
                'round_start_time' => $dat['round_start_time']*1000,
                'round_end_time' => $dat['round_end_time']*1000,
                'gift_start_time' => $dat['gift_start_time']*1000,
                'gift_end_time' => $dat['gift_end_time']*1000,
                'cost'=>$dat['cost'],

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

    public function season_time(Request $request)
    {
//        return 's';
//        $sign_start_time = $request->start_time;
//        $sign_end_time = $sign_start_time + $request->sign_day * 86400;
//        $round_start_time = $sign_end_time+$request->bet_sign_round_day*86400;
//        $round_end_time = $round_start_time + 86400 * $request->round_day;
//        $gift_start_time = $round_end_time+$request->bet_round_gift_day*86400;
//        $gift_end_time = $gift_start_time + 86400 * $request->gift_day;
        $data = [
            'sign_start_time' => $request->sign_start_time,
            'sign_end_time' => $request->sign_end_time,
            'round_start_time' => $request->round_start_time,
            'round_end_time' => $request->round_end_time,
            'gift_start_time' => $request->gift_start_time,
            'gift_end_time' => $request->gift_end_time,
            'season'=>$request->season,
            'cost'=>$request->cost,

        ];
        $season=DB::table('season')->where('season',$request->season)->first();
        if (!$season){
            DB::table('season')->insert($data);
        }else{
            DB::table('season')->where('id',$season->id)->update($data);
        }
        foreach ($data as $k => $v) {
            $s[] = DB::table('basic')->where('keys', '=', $k)->update(['value' => $v, 'updated_at' =>Carbon::now()->toDateTimeString()]);
        }
        if ($s) {
            return $this->response()->array([
                'code' => 200,
                'data' => []
            ]);
        }

    }

    public function user_all(Request $request)
    {
        $user_all = DB::table('user')->whereIn('level_name', ['销售经理', '销售主管'])->paginate(20);
//        return $this->response()->paginator($user_all);
//        $user=DB::table('user')->whereIn('level_name',['销售经理','销售主管'])->count();
//        $user_all=$user_all->items();
        return $this->response()->array([
            'code' => 200,
            'data' => [
                'total' => $user_all->total(),
                'list' => $user_all->items(),
            ]
        ]);
    }

    public function bannerset(Request $request,ImageHandler $imageHandler){
        $keys=array_keys($request->all());
        $s=$keys['0'];
        if ($request->has($keys[0])){
            $url=$imageHandler->save($request->$s,'banner',1,1024);
        }

        $re=DB::table('basic')->where('keys',$keys[0])->update([
            'value'=>$url['path']
        ]);
        if ($re){
            return $this->success([]);
        }
        return $this->error(300);
    }

    public function bannerlist(){
        $banner_1=DB::table('basic')->where('keys','=','banner_1')->value('value');
        $banner_2=DB::table('basic')->where('keys','=','banner_2')->value('value');
        $banner_3=DB::table('basic')->where('keys','=','banner_3')->value('value');
        $banner_4=DB::table('basic')->where('keys','=','banner_4')->value('value');
        $data=[$banner_1,$banner_2,$banner_3,$banner_4];
        return $this->success_list($data,'ok',0);
    }

    public function verify(){
        $array=[1,2,3,4,5,6,7,8,9,0,'A','B','C','D','E','F','G','H','J','K','P','M','N','X','Y','Z','W','T','R'];
        for ($i=0;$i<6;$i++){
            $ver[$i]=$array[rand(0,28)];
        }
        $ver=implode('',$ver);
        $key =time(). str_random(15);
        $data=['verify_code'=>$ver,'verify_key'=>$key];
        DB::table('verify')->insert($data);
        return $this->success($data);
    }

    public function send(Request $request){
        $client=new Client();
        $input=$request->all();
        $url='https://api.jiuweiyun.cn/api/pay/refunds';
        $array=[
            'query' => $input
        ];
        $data=$client->request('POST',$url,$array);

        $ad = json_decode($data->getBody()->getContents());
        return $this->error($ad->code);
    }

    public function insert_demo(Request $request){
        foreach ($request->list as $k=>$v){
            try{
                $data=$v;
                DB::table('user_demo')->insert($data);
            }catch (\Exception $exception){
                Log::error('{-------这里出错了-----}'.$k);
            }

        }
        return $this->error(200);
    }
}