<?php
if (!defined('ABSPATH')) {
    exit;
}

use BLR\Backline\Core\Media;
use BLR\Backline\Modules\Events\Module as EventsModule;
use BLR\Backline\Modules\Events\Pages as EventsPages;

$__blr_ba_data = isset($ctx['event_view']) && is_array($ctx['event_view']) ? $ctx['event_view'] : EventsPages::view_data($ctx);
foreach ($__blr_ba_data as $__blr_ba_key => $__blr_ba_value) {
    ${$__blr_ba_key} = $__blr_ba_value;
}
unset($__blr_ba_data, $__blr_ba_key, $__blr_ba_value);

if (!empty($not_found)) {
    echo '<div class="container-fluid p-0"><div class="alert alert-danger">' .
        esc_html__('Event not found.', 'blr-backline') .
        '</div></div>';
    return;
}

$delete_item_label = sprintf(
    /* translators: %s: event title. */
    __('event “%s”', 'blr-backline'),
    get_the_title($post)
);
?>

<div class="container-fluid p-0">
    <div class="d-flex align-items-center justify-content-between mb-3">
        <div>
            <h1 class="h3 mb-1">
                <?php echo esc_html(get_the_title($post)); ?>
            </h1>
            <div class="text-muted">
                <?php echo esc_html((string) get_post_meta($id, 'blr_event_start', true) ?: '—'); ?>
            </div>
        </div>

        <div class="d-flex gap-2">
            <a class="btn btn-outline-secondary" href="<?php echo esc_url($base . 'events/'); ?>">
                <?php echo esc_html__('Back', 'blr-backline'); ?>
            </a>

            <a class="btn btn-primary" href="<?php echo esc_url($edit_url); ?>">
                <?php echo esc_html__('Edit', 'blr-backline'); ?>
            </a>

            <form
                method="post"
                action="<?php echo esc_url($delete_url); ?>"
                class="blr-ba-delete-form d-inline"
                data-item-label="<?php echo esc_attr($delete_item_label); ?>"
            >
                <?php wp_nonce_field('blr_ba_delete_blr_event_' . (int) $id); ?>

                <button type="submit" class="btn btn-outline-danger">
                    <?php echo esc_html__('Delete', 'blr-backline'); ?>
                </button>
            </form>
        </div>
    </div>

    <div class="row g-3">
        <div class="col-12 col-lg-5">
            <div class="card mb-3">
                <div class="card-body">
                    <?php echo wp_kses_post(Media::preview_markup(absint(get_post_thumbnail_id($id)), 'large')); ?>
                </div>
            </div>

            <div class="card">
                <div class="card-header">
                    <h5 class="card-title mb-0">
                        <?php echo esc_html__('Venue', 'blr-backline'); ?>
                    </h5>
                </div>

                <div class="card-body">
                    <div>
                        <strong>
                            <?php echo esc_html((string) get_post_meta($id, 'blr_venue_name', true) ?: '—'); ?>
                        </strong>
                    </div>

                    <div class="text-muted">
                        <?php
                        echo esc_html(
                            trim(
                                (string) get_post_meta($id, 'blr_venue_city', true) .
                                (
                                    (string) get_post_meta($id, 'blr_venue_state', true)
                                        ? ', ' . (string) get_post_meta($id, 'blr_venue_state', true)
                                        : ''
                                )
                            )
                        );
                        ?>
                    </div>

                    <?php if (($address = (string) get_post_meta($id, 'blr_venue_address', true)) !== '') : ?>
                        <div class="mt-2">
                            <?php echo esc_html($address); ?>
                        </div>
                    <?php endif; ?>
                </div>
            </div>
        </div>

        <div class="col-12 col-lg-7">
            <div class="card">
                <div class="card-header">
                    <h5 class="card-title mb-0">
                        <?php echo esc_html__('Details', 'blr-backline'); ?>
                    </h5>
                </div>

                <div class="card-body">
                    <div class="row row-cols-1 row-cols-md-2 g-3 mb-4">
                        <div class="col">
                            <div class="border rounded p-3 h-100">
                                <div class="text-muted small mb-1">
                                    <?php echo esc_html__('Status', 'blr-backline'); ?>
                                </div>
                                <div class="fw-semibold">
                                    <?php
                                    echo esc_html(
                                        EventsModule::status_label(
                                            (string) get_post_meta($id, 'blr_event_status', true)
                                        )
                                    );
                                    ?>
                                </div>
                            </div>
                        </div>

                        <div class="col">
                            <div class="border rounded p-3 h-100">
                                <div class="text-muted small mb-1">
                                    <?php echo esc_html__('Age Restriction', 'blr-backline'); ?>
                                </div>
                                <div class="fw-semibold">
                                    <?php echo esc_html((string) get_post_meta($id, 'blr_age_restriction', true) ?: '—'); ?>
                                </div>
                            </div>
                        </div>

                        <div class="col">
                            <div class="border rounded p-3 h-100">
                                <div class="text-muted small mb-1">
                                    <?php echo esc_html__('Doors', 'blr-backline'); ?>
                                </div>
                                <div class="fw-semibold">
                                    <?php echo esc_html((string) get_post_meta($id, 'blr_doors', true) ?: '—'); ?>
                                </div>
                            </div>
                        </div>

                        <div class="col">
                            <div class="border rounded p-3 h-100">
                                <div class="text-muted small mb-1">
                                    <?php echo esc_html__('Set Time', 'blr-backline'); ?>
                                </div>
                                <div class="fw-semibold">
                                    <?php echo esc_html((string) get_post_meta($id, 'blr_set_time', true) ?: '—'); ?>
                                </div>
                            </div>
                        </div>
                    </div>

                    <?php
                    echo trim((string) $post->post_content) !== ''
                        ? wp_kses_post(wpautop($post->post_content))
                        : '<div class="text-muted">' . esc_html__('No description yet.', 'blr-backline') . '</div>';
                    ?>
                </div>
            </div>
        </div>
    </div>
</div>
<p class="small text-muted mb-0"><?php printf(/* translators: 1: modified date, 2: last editor display name */ esc_html__('Last edited %1$s by %2$s', 'blr-backline'), esc_html(get_the_modified_date(get_option('date_format') . ' ' . get_option('time_format'))), esc_html(($r = wp_get_post_revisions(get_the_ID(), ['numberposts' => 1])) ? get_the_author_meta('display_name', (int) array_shift($r)->post_author) : get_the_author_meta('display_name', (int) get_post_field('post_author', get_the_ID())))); ?></p>