<?php

use Config\Services;

if (! function_exists('logged_in'))
{
	/**
	 * Checks to see if the user is logged in.
	 *
	 * @return bool
	 */
	function logged_in()
	{
		return Services::authentication()->check();
	}
}

if (! function_exists('user'))
{
	/**
	 * Returns the User instance for the current logged in user.
	 *
	 * @return \Myth\Auth\Entities\User|null
	 */
	function user()
	{
		$authenticate = Services::authentication();
		$authenticate->check();
		return $authenticate->user();
	}
}

if (! function_exists('user_id'))
{
	/**
	 * Returns the User ID for the current logged in user.
	 *
	 * @return int|null
	 */
	function user_id()
	{
		$authenticate = Services::authentication();
		$authenticate->check();
		return $authenticate->id();
	}
}

if (! function_exists('in_groups'))
{
	/**
	 * Ensures that the current user is in at least one of the passed in
	 * groups. The groups can be passed in as either ID's or group names.
	 * You can pass either a single item or an array of items.
	 *
	 * Example:
	 *  in_groups([1, 2, 3]);
	 *  in_groups(14);
	 *  in_groups('admins');
	 *  in_groups( ['admins', 'moderators'] );
	 *
	 * @param mixed  $groups
	 *
	 * @return bool
	 */
	function in_groups($groups): bool
	{
		$authenticate = Services::authentication();
        $authorize    = Services::authorization();

        if ($authenticate->check())
        {
            return $authorize->inGroup($groups, $authenticate->id());
        }

        return false;
	}
}

if (! function_exists('has_permission'))
{
	/**
	 * Ensures that the current user has the passed in permission.
	 * The permission can be passed in either as an ID or name.
	 *
	 * @param int|string $permission
	 *
	 * @return bool
	 */
	function has_permission($permission): bool
	{
		$authenticate = Services::authentication();
        $authorize    = Services::authorization();

        if ($authenticate->check())
        {
            return $authorize->hasPermission($permission, $authenticate->id()) ?? false;
        }

        return false;
	}
}


function hlp_userdata()
{
	$user_id = user_id();
	$db = \Config\Database::connect();

	$q = "select a.user_id,a.username,a.realname,a.nip,e.user_photo,e.user_phone,b.group_id,c.name as role from users a
	left join users_profile e on a.user_id = e.user_id 
	left join auth_groups_users b on a.user_id=b.user_id
	left join auth_groups c on b.group_id=c.id
	where a.user_id=:user_id:";
	$query = $db->query($q, ['user_id'     => $user_id]);
	$res = $query->getRowArray();

	return $res;
}

function cekrw($vid,$uid,$role)
{
	//if vid <> versi_id -> 0
	//if role !== pegawai -> 0 
	//if role == pegawai  -> status 1 -> 1, others 0

	$cekrw = array();

	$session = \Config\Services::session();	
	$db = \Config\Database::connect();
		
	$u = $session->get('user_data');
	$v = $session->get('versi_data');
	$versi_id = intval($v['id']);

	//update per 17JULI 2022
	//pergantian periode tidak akan serta merta mengunci
	//menjadi readonly jika tidak sesuai dengan periode aktif

	//if($vid !== $versi_id) $cekrw = array(5,0,0,0,0,0); 

	// 5=Final  0= file ev readonly  0 = ubah status di hide;
	//else 
	//{
		$q = "select a.status,a.statusp1,statusp2, @name := b.$role as stsd from rb_status a left join rb_mststatus b on a.status=b.status_id where a.versi_id='$vid' and a.user_id='$uid'";
		$query = $db->query($q);
		$row = $query->getRow();

		if(isset($row))
		{
			$sts = $row->status;
			$stsp1 = $row->statusp1;
			$stsp2 = $row->statusp2;
			$stsd = $row->stsd;

		}
		else 
		{
			$db->query("insert into rb_status (versi_id,user_id,status,status_tgl) values ('$vid','$uid','1',curdate())");
			$sts = 1;
			$stsp1 = 0;
			$stsp2 = 0;
			$stsd = 100;
		}

		if($sts == 1) $cekrw1 = 1; else $cekrw1 = 0;
		if($role !== 'pegawai') $cekrw1=0;
		$cekrw2 = 1; // dropdown update status ditampakkan

		$cekrw = array($sts,$cekrw1,$cekrw2,$stsd,$stsp1,$stsp2);
	//}
	return $cekrw;
}

function tgl_simpan2($tgl)
{
    $tanggal = substr($tgl, 0, 2);
    $bulan = substr($tgl, 3, 2);
    $tahun = substr($tgl, 6, 4);

    $tglsimpan = $tahun . '-' . $bulan . '-' . $tanggal;
    if ($tglsimpan == '--') $tglsimpan = NULL;
    return $tglsimpan;
}



