<?php 

use App\Models\UserModel;
use App\Models\AccessMenuModel;
use App\Models\LogModel;
use App\Models\ConnectionModel;


function titleApp()
{
	return "LDU Digital Network";
}
	
function titleDev()
{
	return "Binco";
}
	
function addressApp()
{
	return "Jl. Ir. Soekarno No.19, Medokan Semampir, Surabaya";
}
	
function nameBrand()
{
	return "Lil Bruh";
}
	
function lateBrand()
{
	return "-6.812928, 111.870370";
}

function phoneBrand()
{
	return "0897465736521";
}


function isSuperUser($roleId)
{

	if(intval($roleId)==1||intval($roleId)==2){
		return true;
	}

	return false;
}



function isAdmin($roleId)
{

	if(intval($roleId)==1||intval($roleId)==2||intval($roleId)==3){
		return true;
	}

	return false;
}


function getAccessMenu($menuId,$access, $roleId = 0)
{
	
	if(intval($roleId)==1||intval($roleId)==2){
		return true;
	}

	try {
		
		$AccessMenuModel = new AccessMenuModel();
		$AccessMenuModel->select('id');
		$AccessMenuModel->where('tbl_access_menu.menu_id',$menuId);
		$AccessMenuModel->where('tbl_access_menu.role_id',$roleId);
		$AccessMenuModel->where($access,1);
		
		$menu = $AccessMenuModel->findAll();
		if(!empty((array) $menu)){
			return true;
		}else{
			return false;
		}
	} catch (Exception $e) {
		return false;
	}
}



function haveAccess($menuId,$access, $roleId = 0)
{

	$has = getAccessMenu($menuId, $access, $roleId);
	if(!$has){
		echo "<script>window.location.href = '#';</script>";
		exit;
	}

}


function getNameUser()
{
	$UserModel = new UserModel();

	$row = $UserModel->first(session()->get('id'));


	if(!empty((array) $row )){
		return $row->first_name.' '.$row->last_name;
	}

	return "User";

}


function getProfileUser()
{
	$UserModel = new UserModel();

	$row = $UserModel->first(session()->get('id'));


	if(!empty((array) $row )){
		return $row->image;
	}

	return "default-profile.png";

}


function getDashboardPrefixName($roleName)
{
	if(strtoupper($roleName)=="TEKNISI"){
		return 'technichal';
	}else if(strtoupper($roleName)=="LEADER TEKNISI"){
		return 'leader-technichal';
	}else if(strtoupper($roleName)=="ADMIN"){
		return 'admin';
	}else{
		return 'main';
	}
}


function getIp()
{
	$ConnectionModel = new ConnectionModel();
	$row = $ConnectionModel->first();
	
	if(empty($row->ip)){
		return [];
	}

	return $row->ip;

}

function formatDate($datetime) {
    date_default_timezone_set('Asia/Jakarta');
    
    return date('j F Y', strtotime($datetime));
}

function formatTime($datetime) {
    date_default_timezone_set('Asia/Jakarta');
    
    return date('H:i', strtotime($datetime)) . ' WIB';
}

function getNameIndoMonth($tanggal)
{
	// Tanggal asli
	$tanggal = date('d-m-Y',strtotime($tanggal));

	// Ubah ke format DateTime
	$dateObj = DateTime::createFromFormat('d-m-Y', $tanggal);

	// Format ulang ke '25 Agu 2025'
	$tanggalFormatted = $dateObj->format('d M Y');

	// Untuk nama bulan dalam bahasa Indonesia
	$bulanIndonesia = [
	    'Jan' => 'Jan', 
	    'Feb' => 'Feb',
	    'Mar' => 'Mar',
	    'Apr' => 'Apr',
	    'May' => 'Mei',
	    'Jun' => 'Jun',
	    'Jul' => 'Jul',
	    'Aug' => 'Agu',
	    'Sep' => 'Sep',
	    'Oct' => 'Okt',
	    'Nov' => 'Nov',
	    'Dec' => 'Des'
	];

	// Ganti nama bulan Inggris ke Indonesia
	$bagian = explode(' ', $tanggalFormatted);
	$bagian[1] = $bulanIndonesia[$bagian[1]];
	return implode(' ', $bagian);

}

function getBadgeTypeApp()
{
	return TYPE=="TRIAL" ? '<span class="badge bg-warning mt-1">Trial</span>' : '<span class="badge bg-success mt-1">Premium</span>';
}


function getDescriptionTypeApp()
{
	return TYPE=="TRIAL" ? 'Versi percobaan atau trial merupakan versi gratis dengan masa aktif (30 hari), untuk evaluasi sebelum membeli versi lengkap. Data tetap tersimpan saat upgrade ke premium.' : 'Versi berbayar dengan semua fitur tanpa batas, dukungan teknis penuh, dan masa aktif (1 tahun).';
}

function getExpairedApp()
{

	$now = new DateTime();
	$expaireDate = new DateTime(EXPAIRED); 

	$range = $now->diff($expaireDate);

	if ($expaireDate < $now) {
	    
	    return '<span class="text-danger mt-1">'.getNameIndoMonth(EXPAIRED).'</span>';
	} else {
	    
	    $rangeMonth = ($range->y * 12) + $range->m;
	    
	    if ($rangeMonth <= 1) {
	    	return '<span class="text-danger mt-1">'.getNameIndoMonth(EXPAIRED).'</span>';
	    } elseif ($rangeMonth <= 3) {
	    	return '<span class="text-warning mt-1">'.getNameIndoMonth(EXPAIRED).'</span>';
	    } else {
	    	return '<span class="text-success mt-1">'.getNameIndoMonth(EXPAIRED).'</span>';
	    }
	}
}




function insertLog($menu,$action,$note,$userId)
{	

	$logModel = new LogModel();
	
	$logModel->insert([
		'menu'			=> strtoupper($menu),
		'type'			=> strtoupper($action),
		'note'			=> $note,
		'created_by'	=> $userId,
		'created_at'	=> date('Y-m-d H:i:s'),
	]);

}

function getAlertDashboard()
{

	
	$tanggalHariIni = new DateTime();
	$tanggalAkhir  = new DateTime(EXPAIRED);

	$selisihHari = (int)$tanggalHariIni->diff($tanggalAkhir)->format('%r%a');

	if ($selisihHari < 0) {
	    
	    return (object)[
	        'safe'   => false,
	        'month'  => 0
	    ];
	}

	$bulan = floor($selisihHari / 30);
	if ($bulan <= 1) {
	    return (object)[
	        'safe'   => false,
	        'month'  => $bulan
	    ];
	} elseif ($bulan >= 2 && $bulan <= 3) {
	    return (object)[
	        'safe'   => false,
	        'month'  => $bulan
	    ];
	} else {
	    return (object)[
	        'safe'   => true,
	        'month'  => $bulan
	    ];
	}



}