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

<?= $this->section('content') ?>
<?php
$mailgroupid = 0;
$mailgroupname = '';
$mailgrouptext = '';
if(isset($mailgroups)) { $data = $mailgroups[0]; }
if(isset($new_value)) { $data = $new_value; }
if(isset($data)) {
	if(isset($data['mailgroupid'])) { $mailgroupid = $data['mailgroupid']; }
	$mailgroupname = $data['mailgroupname'];
	$mailgrouptext = $data['mailgrouptext'];
}
?>
<div class="form-body">
	<h3 class="card-title">Mail Group Editor</h3>
	<hr>
	<?php 
	if(isset($validation)) {
	?>
		<div class='alert alert-danger alert-dismissible'>
			<?= $validation->listErrors(); ?>
			<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"> <span aria-hidden="true"></span> </button>
		</div>
	<?php
	}
	?>
	<form method='post'>
		<input type='hidden' name='mailgroupid' value='<?=$mailgroupid;?>'/>
		<div class="mb-3">
			<label class="form-label">Mail Group Name</label>
			<input class="form-control" type="text" name='mailgroupname' value="<?=$mailgroupname;?>" />
		</div>
		<div class="mb-3">
			<label class="form-label">Mail Group Text</label>
			<select multiple class='form-select form-select-sm select2' name='mailgrouptext[]'>
				<?php
					$mailgroups = explode(",", $mailgrouptext);
					foreach($emails as $email) {
						if(in_array($email,$mailgroups)) { echo "<option value='$email' selected>$email</option>\r\n"; }
						else { echo "<option value='$email'>$email</option>\r\n"; }
					}
				?>
			</select>
		</div>
		<button type="button" class="btn btn-dark float-start" onclick="window.close()">Cancel</button>
		<button type="submit" class="btn btn-success text-white float-end"> <i class="fa fa-check"></i> Submit</button>
	</form>
</div>
<?= $this->endSection() ?>

<?= $this->section('script') ?>
<script>
$(document).ready(function() {
	$('.select2').select2({
		width: '100%',
		tags: true,
		tokenSeparators: [',', ' '],
		closeOnSelect: false,
		allowClear: true,
		placeholder: '',
	});    	
});
</script>
<?= $this->endSection() ?>