<?php

function dDate($date=NULL, $format='d M Y', $default='N/A'){

	if(empty($format)) $format='d M Y';

	if(empty($date)) return $default;
	return date($format, strtotime($date));

}


function cn($object, $string, $if_false="N/A"){

    $properties=explode('.', $string);

    foreach($properties as $property){

        if(empty($object->$property)) return $if_false;
        else{
            $object=$object->$property;
        }

    }

    if(empty($object)) return $if_false;
    return $object;

}

function no_img($url='assets/img/placeholder.png'){
	return asset($url);
}


function model($name){

	return "\\App\\Models\\{$name}";

}

function default_avatar($path="assets/img/avatar-2-64.png"){

	return asset($path);

}



function nf($value, $decimal=2){
    if(empty($value)) $value=0.00;
    return number_format($value, $decimal);
}


function die_log($value){
	die(\Log::info($value));
}

function fDate($date, $format='d-m-Y'){
	if(empty($date)) return '';
	return \Carbon\Carbon::parse($date)->format($format);
}

// function auth_user(){
// 	return \Auth::user();
// }

function auth_user($guard='teacher'){
	return \Auth::guard($guard)->user();
}


if(!function_exists('teacherRefNoGenerate')){

	function teacherRefNoGenerate(){
	    $currentId = \DB::table('users')->where('user_type',2)->count();

	    do{
	    	++$currentId;
	    	$currentIdExist =\DB::table('users')->where('user_type',2)->where('id',$currentId)->exists();
	    }while($currentIdExist);

	    return date('y').intval(date('m')).str_pad($currentId, 3, "0", STR_PAD_LEFT).'R';
	}

}


if(!function_exists('studentRefNoGenerate')){

	function studentRefNoGenerate(){
	    $currentId = \DB::table('student_proposals')->count();

	    do{
	    	++$currentId;
	    	$currentIdExist =\DB::table('student_proposals')->where('id',$currentId)->exists();
	    }while($currentIdExist);

	    return date('y').intval(date('m')).str_pad($currentId, 2, "0", STR_PAD_LEFT);
	}

}

if(!function_exists('proposalRefNoGenerate')){

	function proposalRefNoGenerate($student_id){
	    $currentId = \DB::table('proposal')->where('student_id',$student_id)->count();

	    do{
	    	++$currentId;
	    	$currentIdExist =\DB::table('proposal')->where('id',$currentId)->exists();
	    }while($currentIdExist);

	    return str_pad($currentId, 3, "10", STR_PAD_LEFT);
	}

}



if(!function_exists('name_initials')){
	function name_initials($firstName,$lastName){
		return strtoupper(mb_substr($firstName, 0, 1) . mb_substr($lastName, 0, 1));
	}
}


if(!function_exists('student_proposal')){
	function student_proposal($std_id,$pro_id){

		$apply_checked = \App\StudentProposal::where('student_id',$std_id)->where('proposal_id',$pro_id)->exists();

		if($apply_checked){
			return FALSE;
		}

		return TRUE;

	}
}

if(!function_exists('profile_permission')){
	function profile_permission($std_id){

		$apply_checked = \App\StudentProposal::where('student_id',$std_id)->exists();

		if($apply_checked){
			return FALSE;
		}

		return TRUE;

	}
}


function local_tz($user=NULL, $default='UTC'){

    if(empty($user)) $user=\Auth::user();

    if($user && $user->last_logged_timezone){
        return $user->last_logged_timezone;
    }

    return $default;

}

function utc_to_ltz($time_str, $format='d M Y g:i A', $tz=NULL){

    if(empty($tz)) $tz=local_tz();

    return \Carbon\Carbon::parse(
        $time_str
    )->setTimezone($tz)->format($format);

}

function lt_to_utc($time_str, $tz='Asia/Dhaka'){

    return \Carbon\Carbon::parse(
        $time_str, $tz
    )->setTimezone('UTC');

}


if(!function_exists('age_calculator')){
	function age_calculator($birthday=NULL){
		return \Carbon\Carbon::parse($birthday)->diff(\Carbon\Carbon::now())->format('%y years, %m months');
	}
	// %y years, %m months and %d days
}

if(!function_exists('work_experience')){
	function work_experience($start_date =NULL,$end_date =NULL){
		$start_date = new DateTime($start_date);
		$end_date = new DateTime($end_date);
		$interval = $start_date->diff($end_date);
		return $interval->format('%y years');
	}
}

if(!function_exists('diff_in_days')){

	function diff_in_days($sd, $ed){
		$c=new \Carbon\Carbon;

		if(is_string($sd)) $sd=$c->parse($sd);

		if(is_string($ed)) $ed=$c->parse($ed);

		return $sd->diffInDays($ed, FALSE);

	}

}

if(!function_exists('res_msg')){

	function res_msg(String $msg, Int $code=200, Array $data=NULL){

		return response(['msg'=>$msg, 'data'=>$data], $code);
	}
}

function active($url, $active_class="active"){

	$current_url=url()->current();

	if(is_string($url) && $current_url==$url){

		return $active_class;

	}elseif(is_array($url) && in_array($current_url, $url)){

		return $active_class;

	}

}


 


