<div class="container px-3 px-lg-4 mt-3 mb-3">
    <h2><?= __("Callsign statistics"); ?></h2>
	<div class="card">
		<div class="card-header">
			<?= __("View statistics for worked callsigns"); ?>
		</div>
		<div class="card-body">

			<form class="form" action="<?php echo site_url('callstats'); ?>" method="post" enctype="multipart/form-data">
				<!-- Select Basic -->
			<div class="mb-3 row">
					<label class="col-md-1 control-label w-auto" for="band"><?= __("Band"); ?></label>
					<div class="col-md-3 w-auto">
						<select id="band" name="band" class="form-select form-select-sm">
							<option value="All" <?php if ($bandselect == "All") echo ' selected'; ?>><?= __("All"); ?></option>
							<?php
							foreach ($worked_bands as $band) {
								echo '<option value="' . html_escape($band) . '"';
								if ($bandselect == $band) echo ' selected';
								echo '>' . html_escape($band) . '</option>' . "\n";
							}
							?>
						</select>
					</div>

					<label id="satlabel" class="col-md-1 control-label w-auto" for="sat" style="display: none;"><?= __("Satellite"); ?></label>
					<div id="satselect" class="col-sm-2 w-auto" style="display: none;">
						<select class="form-select form-select-sm w-auto" name="sat" id="sat">
							<option value="All"><?= __("All") ?></option>
							<?php
							foreach ($sats as $sat) {
								echo '<option value="' . html_escape($sat) . '"';
								if ($satselect == $sat) echo ' selected';
								echo '>' . html_escape($sat) . '</option>' . "\n";
							}
							?>
						</select>
					</div>

					<label id="orbitlabel" class="col-md-1 control-label w-auto" for="orbit" style="display: none;"><?= __("Orbit"); ?></label>
					<div id="orbitselect" class="col-md-3 w-auto" style="display: none;">
						<select id="orbit" name="orbit" class="form-select form-select-sm">
							<option value="All"><?= __("All") ?></option>
							<?php
							foreach ($orbits as $orbitval) {
								echo '<option value="' . html_escape($orbitval) . '"';
								if ($orbit == $orbitval) echo ' selected';
								echo '>' . html_escape(strtoupper($orbitval)) . '</option>' . "\n";
							}
							?>
						</select>
					</div>

					<label class="col-md-1 control-label w-auto" for="mode"><?= __("Mode"); ?></label>
					<div class="col-sm-2 w-auto">
						<select class="form-select form-select-sm w-auto" name="mode" id="mode">
							<option value="All"><?= __("All") ?></option>
							<?php
							foreach ($worked_modes as $mode) {
								if ($mode->submode ?? '' == '') {
									echo '<option value="' . html_escape($mode) . '"';
									if ($modeselect == $mode) echo ' selected';
									echo '>' . html_escape(strtoupper($mode)) . '</option>' . "\n";
								}
							}
							?>
						</select>
					</div>

					<label class="col-md-1 controll-label w-auto" for="propagation"><?= __("Propagation"); ?></label>
					<div class="col-sm-4 w-auto">
						<select class="form-select form-select-sm w-auto" name="propagation" id="propagation">
							<option value="" <?php if ($propagationselect == '') echo ' selected'; ?>><?= __("All"); ?></option>
							<option value="None" <?php if ($propagationselect == 'None') echo ' selected'; ?>><?= __("None/Empty"); ?></option>
							<option value="NoSAT" <?php if ($propagationselect == 'NoSAT') echo ' selected'; ?>><?= __("All except SAT") ?></option>
							<?php foreach ($adif_propmodes as $mode => $desc) {
								echo "<option value=\"$mode\" ".((($propagationselect ?? '') == "$mode") ? "selected=\"selected\"" : "").">".htmlspecialchars_decode($desc)."</option>\n";
							} ?>
						</select>
					</div>

					<label class="col-md-1 control-label w-auto" for="mincount"><?= __("Minimum Count"); ?></label>
					<div class="col-md-3 w-auto">
						<select id="mincount" name="mincount" class="form-select form-select-sm w-auto">
							<?php
							$i = 2;
							do {
								echo '<option value="' . $i . '"';
								if ($mincount == $i) echo ' selected';
								echo '>' . $i . '</option>' . "\n";
								$i++;
							} while ($i <= $maxactivatedgrids);
							?>
						</select>
					</div>

						<button id="button1id" type="submit" name="button1id" class="btn btn-sm btn-primary w-auto"><?= __("Show"); ?></button>
				</div>

			</form>

			<?php
			// Get Date format
			if ($this->session->userdata('user_date_format')) {
				// If Logged in and session exists
				$custom_date_format = $this->session->userdata('user_date_format');
			} else {
				// Get Default date format from /config/wavelog.php
				$custom_date_format = $this->config->item('qso_date_format');
			}
			?>
			<?php

			if ($activators_array) {

				$result = write_activators($activators_array, $bandselect, $modeselect, $satselect, $orbit, $propagationselect, $custom_date_format);
			} else {
				echo '<div class="alert alert-danger" role="alert">' . __("Nothing found!") . '</div>';
			}
			?>

			</div>
		</div>
	</div>
</div>

<?php

function write_activators($activators_array, $band, $mode, $sat, $orbit, $propagation, $custom_date_format)
{
    if ($band == '') {
        $band = 'All';
    }
    $i = 1;
    echo '<table style="width:100%" class="table table-sm callstatstable table-bordered table-hover table-striped table-condensed text-center">
			<thead>
				<tr>
					<td>#</td>
					<td>' . __("Callsign") . '</td>
					<td>' . __("#QSOs") . '</td>
					<td>' . __("First QSO") . '</td>
					<td>' . __("Last QSO") . '</td>
					<td>' . __("Show QSOs") . '</td>
				</tr>
			</thead>
			<tbody>';

    $activators = array();

    foreach ($activators_array as $line) {
        $call = $line->call;
        $count = $line->count;
		$timestamp = strtotime($line->first_qso);
		$first_qso = date($custom_date_format, $timestamp);
		$timestamp = strtotime($line->last_qso);
		$last_qso = date($custom_date_format, $timestamp);
        array_push($activators, array($count, $call, $first_qso, $last_qso));
    }
    arsort($activators);
    foreach ($activators as $line) {
        echo '<tr>
                <td>' . $i++ . '</td>
                <td class="callsign">' . html_escape($line[1]) . '</td>
                <td>' . $line[0] . '</td>
				<td>' . $line[2] . '</td>
				<td>' . $line[3] . '</td>
                <td><a href=\'javascript:displayCallstatsContacts(' . js_escape($line[1]) . ',' . js_escape($band) . ',' . js_escape($mode) . ',' . js_escape($sat) . ',' . js_escape($orbit) . ',' . js_escape($propagation) . ')\'><i class="fas fa-list"></i></a></td>
			</tr>';
    }
    echo '</tfoot></table></div>';
}
