<?php

namespace App\Http\Controllers;

use Log;
use Illuminate\Http\Request;
use App\Models\Presence;
use App\Models\Activity;
use App\Models\MapMonitoring;
use Carbon\Carbon;
use Illuminate\Support\Facades\DB;

class MapMonitoringController extends Controller
{
    // default map monitoring shows today's presence lat lon in the map

    public function search(Request $request)
    { 
        $dateNow = Carbon::today()->addHour(7)->format('Y-m-d');
        // get distinct human assign project
        $hr_assign_project = DB::table('assign_hr_to_proyek')
                                ->select('user_id')
                                ->whereIn('proyek_id', $request->project_id)
                                ->distinct() 
                                ->get();
        // get position hr in presensi 
        $tmp = [];
        foreach($hr_assign_project as $key){
            $presensi = DB::table('t_clock_in_out as tcio')
                            ->select('tcio.id as clock_in_out_id','mu.id as user_id', 'mu.name as fullname', 'tcio.clock_in', 'tcio.clock_out', 'tcio.clock_in_lat', 'tcio.clock_in_lng', 
                                     'tcio.clock_out_lat', 'tcio.clock_out_lng', 'tcio.clock_in_loc', 'tcio.clock_out_loc', 'tcio.clock_in_boundary',
                                     'tcio.clock_out_boundary', 'mu.username', 'tcio.date_presence', 'tcio.created_at')
                            ->join('m_users as mu', 'mu.id', '=', 'tcio.user_id')
                            ->where('mu.id', $key->user_id)
                            ->orderBy('tcio.id', 'DESC')
                            ->first();
            $project =  DB::table('assign_hr_to_proyek as ahtp')
                            ->select('ahtp.proyek_id as id', 'mp.nama as project_name')
                            ->join('m_proyek as mp', 'mp.id', '=', 'ahtp.proyek_id')
                            ->whereIn('ahtp.proyek_id', $request->project_id)
                            ->where('ahtp.user_id', $key->user_id) 
                            ->get();
            if($presensi && isset($presensi->user_id)){
                $image = DB::table('m_image')->select('image')->where('category', 'presensi')->where('ref_id', $presensi->clock_in_out_id)->first();
                $tmp[] = array(
                    'user_id'               => $presensi->user_id,
                    'clock_in'              => $presensi->clock_in,
                    'clock_out'             => $presensi->clock_out,
                    'date_presence'         => $presensi->date_presence,
                    'clock_in_lat'          => $presensi->clock_in_lat,
                    'clock_in_lng'          => $presensi->clock_in_lng,
                    'clock_out_lat'         => $presensi->clock_out_lat,
                    'clock_out_lng'         => $presensi->clock_out_lng,
                    'clock_in_loc'          => $presensi->clock_in_loc,
                    'clock_out_loc'         => $presensi->clock_out_loc,
                    'clock_in_boundary'     => $presensi->clock_in_boundary,
                    'clock_out_boundary'    => $presensi->clock_out_boundary,
                    'username'              => $presensi->username,
                    'name'                  => $presensi->fullname,
                    'image_selfie'          => isset($image->image) ? $image->image : '-',
                    'created_at'            => $presensi->created_at,
                    'presence_status'       => $presensi->date_presence == $dateNow ? true : false,//true, //status date_presence,
                    'projects'              =>  $project
                );
            }
        }
        return response()->json(['status'=>'success','code'=>200, 'data' => $tmp, 'totalRecord'=>count($tmp)], 200);
    }

    public function list()
    {
        $data = Presence::all();
        $countData = $data->count();

        if($data){
            return response()->json(['status'=>'success','code'=>200,'data'=>$data, 'totalRecord'=>$countData], 200);
        }else{
            return response()->json(['status'=>'failed','message'=>'failed get list presence, please try again later!','code'=>400], 400);
        }
    }

    
}