<?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.222:8086";

    public function atten(Request $request)
    {
   
       

        $query = Path::all();
        $main=Car::all();
        $client = new \GuzzleHttp\Client();
        //$client_influxDB = new InfluxDB\Client($host, $port);

        //$database = $client_influxDB->selectDB('people');

        // dd($response_test);

        $res_emp = $client->get($this->docker_ip."/query?db=people&q=select%20*%20from%20employee");
        $res_on_work = $client->get($this->docker_ip."/query?db=people&q=select%20*%20from%20on_work");
        $res_off_work = $client->get($this->docker_ip."/query?db=people&q=select%20*%20from%20off_work");
        $res_go_out= $client->get($this->docker_ip."/query?db=people&q=select%20*%20from%20go_out");
        $res_come_back= $client->get($this->docker_ip."/query?db=people&q=select%20*%20from%20come_back");
       

        $response_emp = json_decode($res_emp->getBody()->__toString(), true);
        $response_on = json_decode($res_on_work->getBody()->__toString(), true);
        $response_off = json_decode($res_off_work->getBody()->__toString(), true);
        $response_go = json_decode($res_go_out->getBody()->__toString(), true);
        $response_back = json_decode($res_come_back->getBody()->__toString(), true);
dd($response_off);
        // dd($response_back);
        $maxcount=max(count($response_emp['results'][0]['series'][0]['values']),count($response_on['results'][0]['series'][0]['values']),count($response_off['results'][0]['series'][0]['values']),
        count($response_go['results'][0]['series'][0]['values']),count($response_back['results'][0]['series'][0]['values']));

        // for($z=0;$z<$maxcount;$z++){
            $emp_count=count($response_emp['results'][0]['series'][0]['values']);
            $result_content=array();

        for($j=0;$j<$emp_count;$j++){
            $id=$response_emp['results'][0]['series'][0]['values'][$j][1];         

            $name0=$response_emp['results'][0]['series'][0]['values'][$j][3]; 

            // array_push($result_content,['id'=>$id,'name'=>$name0]);
            // $array = Arr::add('Id',$response[$i]['Id']);
            $result_content[$id]=$name0;
           
        }
        // dd($result_content);
       
        $on_count=count($response_on['results'][0]['series'][0]['values']);

            // array_push($result_content,['on_work'=>"null"]);

        // dd($response[0]['Ports']);

        // Allen teach
        $contents=array();
       
        for($i=0;$i<$on_count;$i++){ 
           
            $name=$response_on['results'][0]['series'][0]['values'][$i][1];
            $date=$response_on['results'][0]['series'][0]['values'][$i][0];

            $on_work=$response_on['results'][0]['series'][0]['values'][$i][0];

            $replace_dateon = explode("T",$date);
            // dd($replace_dateon);
            $replace_onwork0 = explode("T",$on_work);
            
            $replace_onwork = explode(".",$replace_onwork0[1]);
            
             
            // dd($time);
            // if($replace_date==$$on_work) {
        //    return $replace_example;
            // if($id==$name){
            //     array_push($result_content,['dateon'=>$replace_dateon[0],'on_work0'=>$replace_onwork0[0],'on_work'=>$replace_onwork[0]]); 
            //     $content=array(["$replace_dateon[0]"=>$replace_dateon[0],["name"=>$name0],['dateon'=>$replace_dateon[0],'on_work0'=>$replace_onwork0[0],'on_work'=>$replace_onwork[0]]]);

             
            // }
            if (array_key_exists($replace_dateon[0], $contents)) {
                $contents [$replace_dateon[0]][$result_content[$name]]=array("dateon" =>$replace_onwork[1]);

            }
            else{
                $contents[$replace_dateon[0]]=array($result_content[$name] => array("dateon" =>$replace_onwork[1]));
            }
        }
            




        // ivy did
        for($i=0;$i<$on_count;$i++){ 
           
            $name=$response_on['results'][0]['series'][0]['values'][$i][1];
            $date=$response_on['results'][0]['series'][0]['values'][$i][0];

            $on_work=$response_on['results'][0]['series'][0]['values'][$i][0];
      

          
            $replace_dateon = explode("T",$date);
            $replace_onwork0 = explode("T",$on_work);
            
            $replace_onwork = explode(".",$on_work);
            
            // if($replace_date==$$on_work) {
        //    return $replace_example;
            if($id==$name){
                array_push($result_content,['dateon'=>$replace_dateon[0],'on_work0'=>$replace_onwork0[0],'on_work'=>$replace_onwork[0]]); 
                // $content=array(["$replace_dateon[0]"=>$replace_dateon[0],["name"=>$name0],['dateon'=>$replace_dateon[0],'on_work0'=>$replace_onwork0[0],'on_work'=>$replace_onwork[0]]]);

             
            }
            

            // $array = Arr::add('Id',$response[$i]['Id']);
       
 
        //  return( $result_content);
        // if(isset($response_select_on['results'][0]['series']))
        
        //     array_push($result_content,['on_work'=>null]);  


        $off_count=count($response_off['results'][0]['series'][0]['values']);
       
        for($i=0;$i<$off_count;$i++){
            $name=$response_off['results'][0]['series'][0]['values'][$i][1];         
            $off_work=$response_off['results'][0]['series'][0]['values'][$i][0];
            $date=$response_off['results'][0]['series'][0]['values'][$i][0];
          
            $replace_dateoff = explode("T",$date);
            $replace_offwork0 = explode("T",$off_work);
            $replace_offwork = explode(".",$off_work);

            if (array_key_exists($replace_dateoff[0], $contents)) {
                $contents[$replace_dateoff[0]][$result_content[$name]]["dateoff"]=$replace_offwork[1];
            }
            else{
                
                $contents[$replace_dateoff[0]]=array($result_content[$name] => array("dateoff" =>$replace_offwork[1]));
            }
        }

        // dd($contents);
        
        if(isset($response_select_off['results'][0]['series']))
        array_push($result_content,['off_work'=>null]);  


        $go_count=count($response_go['results'][0]['series'][0]['values']);
        // dd($response[0]['Ports']);
        for($i=0;$i<$go_count;$i++){
            $name=$response_go['results'][0]['series'][0]['values'][$i][1];         
            $go_out=$response_go['results'][0]['series'][0]['values'][$i][0];
            $date=$response_go['results'][0]['series'][0]['values'][$i][0];
            $replace_datego = explode("T",$date);
            $replace_goout0 = explode("T",$go_out);
            $replace_goout = explode(".",$go_out);
            if (array_key_exists($replace_datego[0], $contents)) {
                $contents[$replace_datego[0]][$result_content[$name]]["goout"]=$replace_goout[1];
            }
            else{
                
                $contents[$replace_datego[0]]=array($result_content[$name] => array("goout" =>$replace_goout[1]));
            }
        }
dd($contents);
        $back_count=count($response_back['results'][0]['series'][0]['values']);
        // dd($response[0]['Ports']);
        for($i=0;$i<$back_count;$i++){
            $name=$response_back['results'][0]['series'][0]['values'][$i][1];         
            $come_back=$response_back['results'][0]['series'][0]['values'][$i][0];
            $date=$response_back['results'][0]['series'][0]['values'][$i][0];
            $replace_datecome = explode("T",$date);
            $replace_come_back0 = explode("T",$come_back);
            $replace_come_back = explode(".",$come_back);
            if (array_key_exists($replace_datecome[0], $contents)) {
                $contents[$replace_datecome[0]][$result_content[$name]]["comeback"]=$replace_come_back[1];
            }
            else{
                $contents[$replace_datecome[0]]=array($result_content[$name] => array("comeback" =>$replace_come_back[1]));
            }
        }  
                     return View('index',['page' => 'attendance','result'=>$query,'iresult'=>$main,'contents'=>$contents,'navbar_li'=>'layouts.navbar_li','title'=>'tt']);

    }  
  
    


        // return View('index',['page' => 'attendance','result'=>$query,'iresult'=>$main,'on_content'=>$on_result_content,'off_content'=>$off_result_content,
        // 'go_content'=>$go_result_content,'back_content'=>$back_result_content,'navbar_li'=>'layouts.navbar_li','title'=>'tt']);
        
    }
        public function attend(Request $request)
                {
                    $start=$request->input('start_time');
                    $end=$request->input('end_time');

                    $start=strtotime("$start,now");
                    $end=strtotime("$end,now");

                    $response_emp = json_decode($res_emp->getBody()->__toString(), true);
                    $response_on = json_decode($res_on_work->getBody()->__toString(), true);

                    // $replace_date = explode("T",$date);
                    dd($start);
                    return $start.$end;

                    echo $time;

                
                
                // dd($result_content);
                }

}