<div class="container">

    <br>
    <?php if ($this->session->flashdata('message')) { ?>
        <!-- Display Message -->
        <div class="alert-message error">
            <p><?php echo $this->session->flashdata('message'); ?></p>
        </div>
    <?php } ?>

    <h2><?php echo $page_title; ?></h2>
    <div class="row">
        <div>
            <?php $this->load->view('layout/messages'); ?>
            <div class="card">
                <div class="card-header">
                    <?= __("Contests") ?>
                </div>
                <div class="card-body">
                    <p class="card-text"><?= __("Here you can manage your contests, create new, edit or export them in various formats.") ?></p>
                    <button class="btn btn-primary btn-sm" onclick="create_modal();"><i class="fas fa-plus"></i> <?= __("Create New Contest") ?></button>
                    <a class="btn btn-primary btn-sm" href="<?php echo site_url('contesting/quickstart'); ?>" target="_blank"><i class="fas fa-play"></i> <?= __("Quick Start") ?></a>
                    <button class="btn btn-danger btn-sm" id="deleteSelectedSessions" data-bs-toggle="tooltip" title="<?= __("Delete selected contest sessions") ?>"><i class="fas fa-trash-alt"></i> <?= __("Delete Selected") ?></button>
                    <hr>
                    <div class="table-responsive" style="overflow: visible;">
                        <table id="user_contests_table" class="table-sm table table-hover table-striped table-condensed">
                            <thead>
                                <tr>
                                    <th><div class="form-check"><input class="form-check-input" type="checkbox" id="checkBoxAll" title="<?= __("Select all") ?>"></div></th>
                                    <th></th>
                                    <th scope="col"><?= __("Status") ?></th>
                                    <th scope="col"><?= __("Start") ?></th>
                                    <th scope="col"><?= __("End") ?></th>
                                    <th scope="col"><?= __("Contest") ?></th>
                                    <th scope="col"><?= __("Station") ?></th>
                                    <th scope="col"><?= __("Comment") ?></th>
                                    <th scope="col"><?= __("#QSO"); ?></th>
                                    <th scope="col"><?= __("Actions") ?></th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php foreach ($my_contests as $row) { ?>
                                    <?php
                                    $now = time();
                                    $start = !empty($row['time_start']) ? strtotime($row['time_start']) : null;
                                    $end   = !empty($row['time_end'])   ? strtotime($row['time_end'])   : null;

                                    if ($start && $start > $now) {
                                        $status = '<span class="badge text-bg-primary me-1">' . __("Coming Up") . '</span>';
                                    } elseif ($start && $end && $now >= $start && $now <= $end) {
                                        $status = '<span class="badge text-bg-warning me-1">' . __("In Progress") . '</span>';
                                    } elseif ($end && $end < $now) {
                                        $status = '<span class="badge text-bg-secondary me-1">' . __("Completed") . '</span>';
                                    } else {
                                        $status = "-";
                                    }
                                    ?>
                                    <tr>
                                        <td><div class="form-check"><input class="row-check form-check-input" type="checkbox" value="<?php echo $row['contest_session_id']; ?>"></div></td>
                                        <td><a target="_blank" href="<?php echo site_url('contesting/logging_engine') . "/" . $row['contest_session_id']; ?>" class="btn btn-success btn-sm"><i class="fas fa-play"></i> <?= __("START") ?></a></td>
                                        <td><?php echo $status; ?></td>
                                        <td><?php echo !empty($row['time_start']) ? date($custom_date_format . ' H:i', strtotime($row['time_start'])) : '-'; ?></td>
                                        <td><?php echo !empty($row['time_end']) ? date($custom_date_format . ' H:i', strtotime($row['time_end'])) : '-'; ?></td>
                                        <td><?php echo $row['display_name'] ?></td>
                                        <td class="callsign"><?php echo isset($row['station']) ? $row['station'] : '-'; ?></td>
                                        <td><?php echo isset($row['comment']) ? $row['comment'] : '-'; ?></td>
                                        <td><?php echo isset($row['qso_count']) ? $row['qso_count'] : '0'; ?></td>
                                        <td>
                                            <button onclick="edit_modal('<?php echo $row['contest_session_id']; ?>');"
                                                    class="btn btn-primary btn-sm"
                                                    data-bs-toggle="tooltip"
                                                    title="<?= __("Edit") ?>">
                                                <i class="fas fa-edit"></i>
                                            </button>

                                            <button onclick="delete_modal('<?php echo $row['contest_session_id']; ?>');"
                                                    class="btn btn-outline-danger btn-sm"
                                                    data-bs-toggle="tooltip"
                                                    title="<?= __("Delete") ?>">
                                                <i class="fas fa-trash-alt"></i>
                                            </button>

                                            <div class="dropdown d-inline-block">
                                                <a class="btn btn-secondary btn-sm dropdown-toggle contest-export-dropdown"
                                                    href="#"
                                                    role="button"
                                                    id="dropdownExportMenuLink_<?= $row['contest_session_id']; ?>"
                                                    data-bs-toggle="dropdown"
                                                    aria-expanded="false">
                                                        <i class="fas fa-file-export"></i>
                                                </a>

                                                <div class="dropdown-menu dropdown-menu-end" style="z-index: 99999;" aria-labelledby="dropdownExportMenuLink_<?= $row['contest_session_id']; ?>">
                                                    <a class="dropdown-item" href="<?php echo site_url('contesting/export_adif') . "/" . $row['contest_session_id']; ?>">
                                                        <i class="fas fa-file-export"></i> <?= __("Export ADIF"); ?>
                                                    </a>
                                                    <a class="dropdown-item" href="<?php echo site_url('contesting/export_cbr') . "/" . $row['contest_session_id'];  ?>">
                                                        <i class="fas fa-file-export"></i> <?= __("Export CBR"); ?>
                                                    </a>
                                                    <a class="dropdown-item" href="<?php echo site_url('contesting/export_edi') . "/" . $row['contest_session_id']; ?>">
                                                        <i class="fas fa-file-export"></i> <?= __("Export EDI"); ?>
                                                    </a>
                                                </div>
                                            </div>
                                        </td>
                                    </tr>
                                <?php } ?>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<div id="contestSessionModal-container"></div>

<!-- Batch delete confirmation modal -->
<div class="modal fade bg-black bg-opacity-50" id="contestBatchDeleteModal" tabindex="-1" aria-labelledby="contestBatchDeleteLabel" aria-hidden="true" data-bs-backdrop="static" data-bs-keyboard="false">
    <div class="modal-dialog modal-dialog-centered">
        <div class="modal-content">
            <div class="modal-header border-bottom d-flex justify-content-between align-items-center">
                <h5 class="modal-title" id="contestBatchDeleteLabel"><i class="fas fa-trash-alt me-2"></i><?= __("Delete Contest Sessions"); ?></h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="<?= __("Close"); ?>" style="flex-shrink: 0;"></button>
            </div>
            <form action="<?= site_url('contesting/batch_delete_sessions'); ?>" method="post" id="contestBatchDeleteForm">
                <div class="modal-body">
                    <p class="text-muted"><?= __("Do you really want to delete the selected contest sessions?"); ?></p>
                    <p><strong><?= __("Selected sessions: "); ?></strong><span id="batchDeleteCount"></span></p>
                    <div class="form-check mt-3">
                        <input class="form-check-input" type="checkbox" name="delete_qsos" id="batchDeleteQsosCheck" value="1">
                        <label class="form-check-label text-danger" for="batchDeleteQsosCheck">
                            <?= __("Also delete all QSOs logged in these sessions from the logbook"); ?>
                        </label>
                    </div>
                </div>
                <div class="modal-footer border-top">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal"><?= __("Cancel"); ?></button>
                    <button type="submit" class="btn btn-danger">
                        <i class="fas fa-trash-alt me-2"></i><?= __("Delete Sessions"); ?>
                    </button>
                </div>
                <input type="hidden" name="ids" id="batchDeleteIds" value="">
            </form>
        </div>
    </div>
</div>

<script>
    var custom_date_format = "<?php echo $custom_date_format ?>";
    var lang_admin_contest_add_contest = '<?= __("Add a Contest"); ?>';
    var lang_error = "<?= __("Error") ?>";
    var lang_contesting_no_selection = "<?= __("Please select at least one contest session.") ?>";

    document.addEventListener('DOMContentLoaded', function () {
        document.querySelectorAll('.contest-export-dropdown').forEach(function (dropdownToggle) {
            new bootstrap.Dropdown(dropdownToggle, {
                boundary: document.body,
                popperConfig: function (defaultBsPopperConfig) {
                    defaultBsPopperConfig.strategy = 'fixed';
                    return defaultBsPopperConfig;
                }
            });
        });
    });
</script>
<?php 
/**
 * The following code block is for the legacy import feature. It allows users to import historical contest data from their logbook into the contesting module.
 * 
 * This can be disabled by setting 'contest_legacy_import' to false in the configuration. Access to this feature is restricted to users with club access level 9 or higher.
 * 
 * This feature will get removed in the future. For a complete removal delete the following code block beside the following files:
 * - application/controllers/Contesting_import.php
 * - application/models/Contesting_import_model.php
 * - application/views/contesting/manager/import.php
 * 
 * Vy 73 de HB9HIL
 */
if (($this->config->item('contest_legacy_import') ?? true) && clubaccess_check(9)): ?>
<div class="container mt-2 mb-4">
    <p class="text-muted small mb-0">
        <a href="<?= site_url('contesting_import') ?>"><?= __("Import historical contests from logbook") ?></a>
        <?php if ($this->user_model->authorize(99)): ?>
            |
            <a href="<?= site_url('contesting_import/all') ?>"><?= __("Import for all users of this instance. You can do that because you are an administrator.") ?></a>
        <?php endif; ?>
    </p>
</div>
<?php endif; 
// END of legacy import block
?>