<?php 

function email_msg_template($to, $subject, $content, $signed = '') {
	$user = new user;
	$user_meta = [];
	if($to == 'admins') {
		$user_meta['role'] = '';
		$user_meta['surname'] = 'School Admins<br>'.get_option('company_name');
		$user_meta['address'] = get_option('company_address');
	} else {
		$user_meta = $user->get_user_meta($to);
	}
	if($signed != "") {
		$sign_meta = $user->get_user_meta($signed);
	} else {
		$sign_meta = $user->get_user_meta(get_option('school_principal'));
	}
	$msg = '<style type="text/css">.m_wrap {max-width: 640px; margin: auto; position: relative;}
			.m_header, .m_footer {
				background-color: #efefef;
			}
			.m_header, .m_footer, .m_body {
				padding-top: 50px;
				padding-bottom: 50px;
				padding-right: 15px;
				padding-left: 15px;
			}
			.m_logo img {
				margin: auto;
				display: block;
			}
			.text-center {
				text-align: center;
			}
			.text-right {
				right: 10px;
				position: absolute;
			}
			.m_copytext, .m_copytext a {
				color: #efefef;
			}
			.m_address {
				width: 50%;
			}
			.m_subject h3 {
				text-align: center;
				text-transform: uppercase;
				letter-spacing: 0.4em;
				font-size: medium;
				text-decoration: underline;
			}
			.m_table {
				width: 100%;
			}
			.m_table thead tr th {
				border-bottom: 3px solid #056990;
				color: #056990;
			}
			.m_table td, .m_table th {
				padding-top: 10px;
				padding-bottom: 5px;
				border-right: 1px solid #056990;
				padding-left: 10px;
				padding-right: 10px;
			}
			tfoot tr td {
				border-top: 1px solid #056990;
			}
			tfoot tr td:last-child {
				border-bottom: 3px #056990 double;
			}
			.m_text {
				padding-bottom: 120px;
			}
			.m_signature {
				width: 50%;
				position: absolute;
				right: 20px;
				text-align: center;
				bottom: 140px;
				display: block;
			}
		</style>';
	$msg .= '<div class="m_container"><div class="m_wrap"><div class="m_header"><div class="m_logo"><img src="';
	$msg .= get_option('home_url').get_option('company-logo');
	$msg .= '"></div></div>';
	$msg .= '<div class="m_body"><div class="m_address"><p>';
	$msg .= date(_DATE_FORMAT_, time());
	$msg .= '</p><p>';
	if($user_meta['role'] == 'student') {
		$msg .= $user_meta['n1_fullname'];
		$msg .= '<br>C/o ';
		$msg .= $user_meta['surname'].' '.$user_meta['other_names'];
	} else {
		$msg .= $user_meta['surname'].' '.$user_meta['other_names'];
	}
	$msg .= '<br>'.$user_meta['address'];
	$msg .= '</p><p>Dear Sir/Madam,</p></div><div class="m_subject"><h3>';
	$msg .= $subject;
	$msg .= '</h3><div class="m_text">';
	$msg .= $content;
	$msg .= '<div class="m_signature"><p>';
	$msg .= '<img src="'.get_option('home_url').$sign_meta['signature'].'">';
	$msg .= '<p class="m_designation">'.$sign_meta['surname'].' '.$sign_meta['other_names'].' <br>'.$sign_meta['designation'].'</p></div>';
	$msg .= '</div>
				</div>
			</div>
			<div class="m_footer text-center">
				This message was sent from ';
	$msg .= get_option('company_name');
	$msg .= '</div>
			<div class="m_copytext text-right">&copy; ';
	$msg .= date('Y', time()).' ';
	$msg .= '<a href="https://schoolskonnect.com" target="_blank">schoolskonnect app</a>';
	$msg .= '</div></div></div>';
	return $msg;
}

function receipt_template($to, $subject, $items, $signed = '') {

	$content = '<table class="m_table" cellspacing="0"><thead><tr><th>SN</th><th>Description</th><th>Amount</th></tr></thead><tbody>';
	$total = 0;
	if(_count($items) > 0) {
		$i = 1;
		foreach ($items as $item => $price) {
			$content .= '<tr><td>';
			$content .= $i;
			$content .= '</td><td>';
			$content .= ucwords($item);
			$content .= '</td><td>';
			$content .= toMoney($price);
			$content .= '</td></tr>';
			$i++; $total += $price;
		}
	}
	$content .= '</tbody><tfoot><tr><td></td><td>Total</td><td>';
	$content .= toMoney($total);
	$content .= '</td></tr></tfoot></table>';

	return email_msg_template($to, $subject, $content, $signed);
}