<?php
if( ! function_exists('get_admin_path'))
{
	function get_admin_path($link=""){
		$project = config('Config\\Project');
		return base_url(strtolower($project->admin_dir)."/".$link);
	}
} 

if( ! function_exists('front_url'))
{
	function front_url($link="",$language_code=""){
		$language_url = $language_code!="" ? base_url($language_code.'/'.$link) : base_url($link);
		return $language_url;
	}
}

if( ! function_exists('page_url'))
{
	function page_url($link=""){
		return base_url(strtolower("page/detail/".$link));
	}
}
if( ! function_exists('products_url'))
{
	function products_url($link=""){
		return base_url(strtolower("products/detail/".$link));
	}
}
if( ! function_exists('get_current_controller')){
	function get_current_controller(){
		// Router 
		$router = \Config\Services::router();
		$controller_path = explode("\\",$router->controllerName());		
		return strtolower($controller_path[count($controller_path)-1]);
	}
}

if( ! function_exists('get_date_formatted')){
	function get_date_formatted($date_str=""){
		if($date_str!=""){
			return date('d-m-Y',strtotime($date_str));
		}else{
			return date('d-m-Y');
		}
	}
}

if( ! function_exists('get_ampm_options')){
	function get_ampm_options($default=""){
		$opt_arr = ['AM','PM'];
		$opt_str = '';
		foreach($opt_arr as $opt_val){
			$selected = $default==$opt_val ? 'selected' : '';
			$opt_str .= '<option value="'.$opt_val.'">'.$opt_val.'</option>';
		}
		return $opt_str;
	}
}

if( ! function_exists('get_datetime_field')){
	function get_datetime_field($prefix="fld",$date="",$hr="",$min="",$ampm=""){
		$date = $date ?: date('d-m-Y');
		$hr = $hr ?: date('h');
		$min = $min ?: date('i');
		$ampm = $ampm ?: date('A');
		$html = '<table class="table-sm tbl_time_fld"><tr>';
		$html .= '<td width="38%"><input type="text" class="form-control c_datepicker" name="'.$prefix.'_date" id="'.$prefix.'_date" value="'.$date.'" readonly></td>';
		$html .= '<td width="20%"><input type="number" class="form-control" name="'.$prefix.'_hr" id="'.$prefix.'_hr" value="'.$hr.'"></td>';
		$html .= '<td class="td_time_fld_colon">:</td>';
		$html .= '<td width="20%"><input type="number" class="form-control" name="'.$prefix.'_min" id="'.$prefix.'_min" value="'.$min.'"></td>';
		$html .= '<td width="22%"><select class="form-control" name="'.$prefix.'_ampm" id="'.$prefix.'_ampm">'.get_ampm_options($ampm).'</select></td>';
		$html .= '</tr></table>';
		return $html;
	}
}

if( ! function_exists('get_amount_to_word')){
	function get_amount_to_word($number){
		$no = round($number);
		$point = round($number - $no, 2) * 100;
		$hundred = null;
		$digits_1 = strlen($no);
		$i = 0;
		$str = array();
		$words = array('0' => '', '1' => 'one', '2' => 'two',
			'3' => 'three', '4' => 'four', '5' => 'five', '6' => 'six',
			'7' => 'seven', '8' => 'eight', '9' => 'nine',
			'10' => 'ten', '11' => 'eleven', '12' => 'twelve',
			'13' => 'thirteen', '14' => 'fourteen',
			'15' => 'fifteen', '16' => 'sixteen', '17' => 'seventeen',
			'18' => 'eighteen', '19' =>'nineteen', '20' => 'twenty',
			'30' => 'thirty', '40' => 'forty', '50' => 'fifty',
			'60' => 'sixty', '70' => 'seventy',
			'80' => 'eighty', '90' => 'ninety');
		$digits = array('', 'hundred', 'thousand', 'lakh', 'crore');
		while ($i < $digits_1) {
			$divider = ($i == 2) ? 10 : 100;
			$number = floor($no % $divider);
			$no = floor($no / $divider);
			$i += ($divider == 10) ? 1 : 2;
			if ($number) {
				$plural = (($counter = count($str)) && $number > 9) ? 's' : null;
				$hundred = ($counter == 1 && $str[0]) ? ' and ' : null;
				$str [] = ($number < 21) ? $words[$number] .
				" " . $digits[$counter] . $plural . " " . $hundred
				:
				$words[floor($number / 10) * 10]
				. " " . $words[$number % 10] . " "
				. $digits[$counter] . $plural . " " . $hundred;
			} else $str[] = null;
		}
		$str = array_reverse($str);
		$result = implode('', $str);
		return ucwords($result . "Rupees  ");
    }
}
if( ! function_exists('set_spreadsheet_properties'))
{
	function set_spreadsheet_properties(&$spreadsheet,$title=""){
		$project = config('Config\\Project');
		
		$spreadsheet->getProperties()
			->setCreator($project->project_name)
			->setLastModifiedBy($project->project_name)
			->setTitle($title)
			->setSubject($title)
			->setDescription($title)
			->setKeywords($title)
			->setCategory($title);
	}
}

if( ! function_exists('set_spreadsheet_heading_style'))
{
	function set_spreadsheet_heading_style(&$sheet,$cell_range){		
		// Style Headings
		$headingStyleArray = [
			'font' => [
				'bold' => true,
				'size' => 12,
			],
			'alignment' => [
				'horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER,
			],
			'borders' => [
				'allBorders' => [
					'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN,
				],
			],
			'fill' => [
				'fillType' => \PhpOffice\PhpSpreadsheet\Style\Fill::FILL_GRADIENT_LINEAR,
				'rotation' => 90,
				'startColor' => [
					'argb' => 'FFA0A0A0',
				],
				'endColor' => [
					'argb' => 'FFFFFFFF',
				],
			],
		];
		
		$sheet->getStyle($cell_range)->applyFromArray($headingStyleArray);
	}
}

if( ! function_exists('output_spreadsheet'))
{
	function output_spreadsheet(&$writer,$filename){			
		header('Content-Type: application/vnd.ms-excel');
		header('Content-Disposition: attachment;filename="'. $filename .'.xlsx"'); 
		header('Cache-Control: max-age=0');

		$writer->save('php://output'); // download file 
	}
}

if( ! function_exists('is_valid_datetime_range'))
{
	function is_valid_datetime_range($start_datetime,$end_datetime){
		$start_ts = strtotime($start_datetime);
		$end_ts = strtotime($end_datetime);
		if($start_ts < $end_ts){
			return true;
		}
		return false;
	}
}

if( ! function_exists('get_lang_links')){
	function get_lang_links($lang_arr,$curr_lang_code){
		$code_arr = [];
		foreach($lang_arr as $code => $name){
			array_push($code_arr,$code);
		}
		$curr_url = current_url();
		$uri = current_url(true);
		$query_str = $uri->getQuery();
		$base_url = base_url();
		$path = str_replace($base_url,"",$curr_url);		
		$html = '';
		foreach($lang_arr as $code => $name){
			if($code==$curr_lang_code){
				$html .= '<li><span>'.$name.'</span></li>';
			}else{
				$lang_url = $base_url.$code."/home";
				if($uri->getSegment(1)==$curr_lang_code){
					$lang_url = str_replace('/'.$uri->getSegment(1),'/'.$code,$curr_url);
				}else if($path){
					$lang_url = $base_url.$code."/".$path;
				}
				if($query_str){
					$lang_url .= "?".$query_str;
				}
				$html .= '<li><a href="'.$lang_url.'">'.$name.'</a></li>';
			}
		}
		return $html;
	}
}
if( ! function_exists('get_lang_options')){
	function get_lang_options($lang_arr,$curr_lang_code){
		$code_arr = [];
		foreach($lang_arr as $code => $name){
			array_push($code_arr,$code);
		}
		$curr_url = current_url();
		$uri = current_url(true);
		$query_str = $uri->getQuery();
		$base_url = base_url();
		$path = str_replace($base_url,"",$curr_url);
		$html = '';
		foreach($lang_arr as $code => $name){
			if($code==$curr_lang_code){
				$html .= '<option value="" selected>'.$name.'</option>';
			}else{
				$lang_url = $base_url.$code."/home";
				if($uri->getSegment(1)==$curr_lang_code){
					$lang_url = str_replace('/'.$uri->getSegment(1),'/'.$code,$curr_url);
				}else if($path){
					$lang_url = $base_url.$code."/".$path;
				}
				if($query_str){
					$lang_url .= "?".$query_str;
				}
				$html .= '<option value="'.$lang_url.'">'.$name.'</option>';
			}
		}
		return $html;
	}
}