<?php

namespace App\Http\Controllers;
use App\Path;
use App\Car;
use GuzzleHttp\Client;
use Illuminate\Http\Request;

class AttendanceController extends Controller
{
    private $docker_ip="http://192.168.5.150:8086";

    public function atten(Request $request)
    {

        //一開始顯示所有資料
        $query = Path::all();
        $main=Car::all();
        $client = new \GuzzleHttp\Client();
        $res = $client->get($this->docker_ip."/query?db=test&q=select%20*%20from%20people2");
        $response = json_decode($res->getBody()->__toString(), true);
 


        $people_count=count($response['results'][0]['series'][0]['values']);
        $result_content=array();
        // dd($response[0]['Ports']);
  

        for($i=0;$i<$people_count;$i++){
            $dbname=$response['results'][0]['series'][0]['name'];
            $time=$response['results'][0]['series'][0]['values'][$i][6];
            $time=date('Y-m-d H:i:s',$time);
            $come_back=$response['results'][0]['series'][0]['values'][$i][1];
            $go_out=$response['results'][0]['series'][0]['values'][$i][2];
            $name=$response['results'][0]['series'][0]['values'][$i][3];
            $off_work=$response['results'][0]['series'][0]['values'][$i][4];
            $on_work=$response['results'][0]['series'][0]['values'][$i][5];
            // $array = Arr::add('Id',$response[$i]['Id']);
            array_push($result_content,['Names'=>$dbname,'time'=>$time,'come_back'=>$come_back,'go_out'=>$go_out,
            'name'=>$name,'off_work'=>$off_work,
            'on_work'=>$on_work]);
        }

        return View('index',['page' => 'attendance','result'=>$query,'iresult'=>$main,'content'=>$result_content,'navbar_li'=>'layouts.navbar_li','title'=>'tt']);
 
    }
    public function attend(Request $request)
    {
            //時間戳記轉換及接收姓名欄位
            $start=$request->input('start_time');
            $end=$request->input('end_time');
            $people=$request->input('people');
            $start=strtotime($start);
            $end=strtotime($end."now");

                  

            //搜尋姓名及開始結束時間，有資料
            $query = Path::all();
            $main=Car::all();
            $client = new \GuzzleHttp\Client();
            $res = $client->get($this->docker_ip."/query?db=test&q=select%20*%20from%20people2%20WHERE \"name\" ='$people' AND timestamp>=$start AND  timestamp<=$end ");
            $response = json_decode($res->getBody()->__toString(), true);
            if(count($response['results'][0])>1){
                $people_count=count($response['results'][0]['series'][0]['values']);
                $result_content=array();

                for($i=0;$i<$people_count;$i++){
                    $dbname=$response['results'][0]['series'][0]['name'];
                    $time=$response['results'][0]['series'][0]['values'][$i][6];
                    $time=date('Y-m-d H:i:s',$time);
                    $come_back=$response['results'][0]['series'][0]['values'][$i][1];
                    $go_out=$response['results'][0]['series'][0]['values'][$i][2];
                    $name=$response['results'][0]['series'][0]['values'][$i][3];
                    $off_work=$response['results'][0]['series'][0]['values'][$i][4];
                    $on_work=$response['results'][0]['series'][0]['values'][$i][5];
                    // $array = Arr::add('Id',$response[$i]['Id']);
                    array_push($result_content,['Names'=>$dbname,'time'=>$time,'come_back'=>$come_back,'go_out'=>$go_out,
                    'name'=>$name,'off_work'=>$off_work,
                    'on_work'=>$on_work]);
                }
                  return View('index',['page' => 'attendance','result'=>$query,'iresult'=>$main,'content'=>$result_content,'navbar_li'=>'layouts.navbar_li','title'=>'tt']);

            }
            else {
                //查無資料，顯示全部資料列
                    $query = Path::all();
                    $main=Car::all();
                    $client = new \GuzzleHttp\Client();
                    $res = $client->get($this->docker_ip."/query?db=test&q=select%20*%20from%20people2");
                    $response = json_decode($res->getBody()->__toString(), true);
            


                    $people_count=count($response['results'][0]['series'][0]['values']);
                    $result_content=array();
                    // dd($response[0]['Ports']);
            

                    for($i=0;$i<$people_count;$i++){
                        $dbname=$response['results'][0]['series'][0]['name'];
                        $time=$response['results'][0]['series'][0]['values'][$i][6];
                        $time=date('Y-m-d H:i:s',$time);
                        $come_back=$response['results'][0]['series'][0]['values'][$i][1];
                        $go_out=$response['results'][0]['series'][0]['values'][$i][2];
                        $name=$response['results'][0]['series'][0]['values'][$i][3];
                        $off_work=$response['results'][0]['series'][0]['values'][$i][4];
                        $on_work=$response['results'][0]['series'][0]['values'][$i][5];
                        // $array = Arr::add('Id',$response[$i]['Id']);
                        array_push($result_content,['Names'=>$dbname,'time'=>$time,'come_back'=>$come_back,'go_out'=>$go_out,
                        'name'=>$name,'off_work'=>$off_work,
                        'on_work'=>$on_work]);
                    }
            echo "<script>alert('查無此資料');</script>";
            
            return View('index',['page' => 'attendance','result'=>$query,'iresult'=>$main,'content'=>$result_content,'navbar_li'=>'layouts.navbar_li','title'=>'tt']);
            }
               
                
                
                // dd($result_content);
    }
    public function user(Request $request)
    {
        $query = Path::all();
            $main=Car::all();
            $client = new \GuzzleHttp\Client();
            $res = $client->get($this->docker_ip."/query?db=test&q=select%20*%20from%20people2%20 ");
            $response = json_decode($res->getBody()->__toString(), true);
            $people_count=count($response['results'][0]['series'][0]['values']);
            $result_content=array();
            for($i=0;$i<$people_count;$i++){
                $name=$response['results'][0]['series'][0]['values'][$i][3];
                array_push($result_content,['name'=>$name]);
            return View('index',['page' => 'attendance','result'=>$query,'iresult'=>$main,'content'=>$result_content,'navbar_li'=>'layouts.navbar_li','title'=>'tt']);

    }
}
}