<?= $this->extend('layouts/main.php') ?>

<?= $this->section('content') ?>
<?php

$data = $userinfo;
$username = $data['firstname'].' '.$data['lastname'];
$offaddress = $data['offaddress'];
$offphone = $data['offphone'];
$email_1 = $data['email_1'];

$content .= "<br/>
<p>
Terimakasih<br/>
Salam,<br/>
$username<br/>
<br/>
PT. SUMBERMITRA AGUNGJAYA<br/>
$offaddress<br/>
Phone : $offphone<br/>
Email : $email_1<br/>
<br/>
</p>
";
$emailoption = '';
foreach ($mailgroups as $data) { 
	$qmailgroupname = $data['mailgroupname'];
	$qmailgrouptext = $data['mailgrouptext'];
	$emailoption .= "<option value='$qmailgrouptext'>$qmailgroupname</option>"; 
}
foreach ($emails as $data) {
	$emailoption .= "<option value='$data'>$data</option>";
}
?>
<script src="<?=base_url();?>/assets/tinymce/tinymce.min.js"></script>
<div class="page-wrapper">
	<div class="container-fluid">
		<div class="row page-titles">
			<div class="col-md-5 align-self-center">
				<h4 class="text-themecolor">Compose Email</h4>
			</div>
		</div>
		<div class="row">
			<div class="col-12">
				<form method="post" enctype="multipart/form-data">
				<div class="card">
					<div class="card-body">
						<div class="row mb-2">
							<label for="from" class="col-sm-2 col-form-label">Reply to</label>
							<div class="col-sm-10">
							<input type="text" class="form-control form-control-sm" name="replyto" id="from" value="<?=$replyto;?>" >
							</div>
						</div>
						<div class="row mb-2">
							<label for="to" class="col-sm-2 col-form-label">To</label>
							<div class="col-sm-10">
							<select class="form-control form-control-sm select2" id="to" name="to[]" multiple required pattern="/^[a-zA-Z0-9.!#$%&’*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/">
								<?php 
									if(isset($sitecontact[0])) {
										foreach($sitecontact[0] as $email) {
											if ($email !== ""){
												echo "<option value='$email' selected>$email</selected>\r\n";
											}
										}
									}
									echo $emailoption; 
								?>
							</select>
							</div>
						</div>
						<div class="row mb-2">
							<label for="cc" class="col-sm-2 col-form-label">Cc</label>
							<div class="col-sm-10">
							<select class="form-control form-control-sm select2" id="cc" name="cc[]" multiple pattern="/^[a-zA-Z0-9.!#$%&’*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/">
								<?php echo $emailoption; ?>
							</select>
							</div>
						</div>
						<div class="row mb-2">
							<label for="bcc" class="col-sm-2 col-form-label">Bcc</label>
							<div class="col-sm-10">
							<select class="form-control form-control-sm select2" id="bcc" name="bcc[]" multiple pattern="/^[a-zA-Z0-9.!#$%&’*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/">
								<option value='<?=$replyto;?>' selected><?=$replyto;?></option>
								<?php echo $emailoption; ?>
							</select>
							</div>
						</div>
						<div class="row mb-2">
							<label for="subject" class="col-sm-2 col-form-label">Subject</label>
							<div class="col-sm-10">
							<input type="text" class="form-control form-control-sm" name="subject" id="subject" value="<?=$subject;?>" >
							</div>
						</div>
						<div class="row mb-2">
							<label for="message" class="col-sm-2 col-form-label">Message</label>
							<div class="col-sm-10">
							<textarea class="form-control form-control-sm" name="message" id="message"><?=$content;?></textarea>
							</div>
						</div>
						<div class="row mb-2">
							<label for="attachment" class="col-sm-2 col-form-label">Attachment list</label>
							<div class="col-sm-10">
							<input type="text" class="form-control form-control-sm" name="attachment" id="attachment" value="<?=$activities[0]['attachment']?>" >
							</div>
						</div>
					</div>
					<div class="card-footer">
						<button type="submit" name="submit" class="btn btn-info float-end">Send</button>
					</div>
				</div>
				</form>
			</div>
		</div>		
	</div>
</div>

<?= $this->endSection() ?>

<?= $this->section('script') ?>

<!-- SCRIPT TEXT EDITOR TINYMCE -->
<script>
	// config
	// config = {
	// 	selector:'textarea',
	// 	promotion:false,
	// 	menubar:false,
	// 	content_style: "p { margin: 0; }",
	// };
	// tinymce.init(config);

	// config
	config = {
		selector:'textarea',
		height:'350',
		promotion:false,
		// forced_root_block : 'pre',
		paste_data_images:false,
		content_style: "p { margin: 0; }",
		plugins: 'preview importcss searchreplace autolink autosave save code visualblocks visualchars fullscreen link template codesample table charmap pagebreak nonbreaking anchor insertdatetime advlist lists wordcount charmap emoticons',
		menubar: 'file edit view insert format tools table',
		toolbar: 'undo redo | bold italic underline strikethrough | fontfamily fontsize blocks | alignleft aligncenter alignright alignjustify | outdent indent |  numlist bullist | forecolor backcolor removeformat | pagebreak | charmap emoticons | fullscreen  preview save print | template link anchor codesample',
		autosave_ask_before_unload: true,
		autosave_interval: '30s',
		automatic_uploads: false,
	};
	tinymce.init(config);


</script>

<!-- SCRIPT SELECT2 UNTUK INPUT TO, CC, BCC -->
<script>
	$(document).ready(function () {
		var AllowClear = $.fn.select2.amd.require('select2/selection/allowClear');
		var _handleKeyboardClearOriginal = AllowClear.prototype._handleKeyboardClear;

		AllowClear.prototype._handleKeyboardClear = function(_, evt, container) {
			if (this.$element.prop('multiple')) { return; }
			_handleKeyboardClearOriginal.call(this, _, evt, container);
		};
		
		$( ' #to , #cc , #bcc ' ).select2( {
			tags: true,
			tokenSeparators: [',', ' '],
			closeOnSelect: true,
			allowClear: true,
			placeholder: '',
		} );
	});
</script>

<?= $this->endSection() ?>
