<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;
use Validator;
use Hidehalo\Nanoid\Client as Nanoid;
use Hidehalo\Nanoid\GeneratorInterface as NanoidInterface;
use App\Responses;
use App\Helper;
use App\Models\ConfRates;
use App\Models\ConfTruckTypes;
use App\Models\Vehicles;
use App\Models\UserLogs;
use Auth;
use App\Models\Users;

class UserLogsController extends Controller
{
    /**
     * View
     */

    public function view_user_logs(Request $req)
    {
		$users = Users::listUsers();
		$data = [
			"users" => $users,
		];

		$log = [
			"module" => "User Logs",
			"action" => "View",
			"desc" => "Open User Logs menu",
		];
		UserLogs::insert(Auth::user()->id, $log);
        return view("menu_v1.userLogs", $data);
    }
    public function api_user_logs(Request $req)
    {
        try {
			$tgl0 = $req->tgl0;
			$tgl1 = $req->tgl1;
			$userId = $req->userId;

			$d = [$tgl0, $tgl1, $userId, $userId];
			$list = DB::select("SELECT
					a.*, b.email
				FROM t_user_log a
					join t_users b on a.userId = b.id
				WHERE 
					a.crt BETWEEN ? AND ?
					and if(? , a.userId = ? , 1=1)
				order by a.crt desc
			", $d);

			$apiResp = Responses::success("success user logs");
			$apiResp["data"] = $list;
			return new Response($apiResp, $apiResp["meta"]["code"]);
		} catch (\Exception $e) {
            DB::rollBack();
            $apiResp = Responses::error($e->getMessage());
            return new Response($apiResp, $apiResp["meta"]["code"]);
        }
    }

}
