<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;

class IntegralController extends Controller{

    public function all(Request $request){
        $data=DB::table('inte_log')->join('user','user.id','=','inte_log.uid')
            ->where('inte_log.season',$request->season)->select('user.level_name','user.nickname','user.phone','inte_log.*','user.avatar','inte_log.remarks')
            ->orderByDesc('inte_log.updated_at')
            ->paginate(15);
        $all=[];
//        $data=$data->items();
        if ($data->items()){
            foreach ($data->items() as $k=>$v){
                $all[$k]['nickname']=$v->nickname;
                $all[$k]['phone']=$v->phone;
                $all[$k]['score']=$v->integral;
                $all[$k]['type']=$v->type;
                $all[$k]['avatar']=$v->avatar;
                $all[$k]['level_name']=$v->level_name;
                $all[$k]['remark']=$v->remarks;
                $all[$k]['time']=strtotime($v->updated_at)*1000;
            }
            $total=$data->total();
        }else{
            $total=0;
        }
        return $this->success_list($all,'ok',$total);

    }

    public function search(Request $request){
        $string=$request->phone;
        if (is_numeric($string)){
            $s='phone';
        }else{
            $s='nickname';
        }
        $user=DB::table('user')->where($s,'like','%'.$string.'%')->first();
        if (!$user){
            return $this->success([]);
        }
        $data=DB::table('inte_log')->where('uid',$user->id)->where('season',$request->season)->orderByDesc('created_at')
            ->paginate(15);
//        $data=$data->items();
        $all=[];
        if ($data->items()){
            foreach ($data->items() as $k=>$v){
                $all[$k]['nickname']=$user->nickname;
                $all[$k]['phone']=$user->phone;
                $all[$k]['score']=$v->integral;
                $all[$k]['type']=$v->type;
                $all[$k]['avatar']=$user->avatar;
                $all[$k]['remark']=$v->remarks;
                $all[$k]['level_name']=$user->level_name;
                $all[$k]['time']=strtotime($v->updated_at)*1000;
            }
            $total=$data->total();
        }else{
            $total=0;
        }
        return $this->success_list($all,'ok',$total);
    }

    public function change_inte(Request $request){
        $user=DB::table('user')->where('phone',$request->phone)->select('id')->first();
        if (!$user){
            return $this->error(300);
        }
//        return date('Y-m-d H:i:s',$request->time);
        DB::table('inte_log')->insert([
            'uid'=>$user->id,
            'created_at'=>date('Y-m-d H:i:s',$request->time),
            'season'=>$request->season,
            'integral'=>$request->score,
            'type'=>3,
            'remarks'=>Auth::user()->name
        ]);
        DB::table('user')->where('id',$user->id)->increment('integral',$request->score);
        return $this->success([]);
    }

    public function search_pic(Request $request){

    }
}