<?php
use BLR\Backline\Core\Access;
use BLR\Backline\Core\Media;
use BLR\Backline\Modules\Events\Module as EventsModule;
use BLR\Backline\Modules\Events\Pages as EventsPages;

if (!defined('ABSPATH')) {
    exit;
}

$__blr_ba_data = isset($ctx['event_form']) && is_array($ctx['event_form']) ? $ctx['event_form'] : EventsPages::form_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 ($is_edit && (!$post || $post->post_type !== 'blr_event')) { echo '<div class="container-fluid p-0"><div class="alert alert-danger">' . esc_html__('Event not found.', 'blr-backline') . '</div></div>'; return; }
if ($is_edit && !Access::can_edit_post($post_id, 'blr_event')) { echo '<div class="container-fluid p-0"><div class="alert alert-danger">' . esc_html__('You do not have permission to edit this event.', 'blr-backline') . '</div></div>'; return; }
if (!$is_edit && !Access::can_create_post_type('blr_event')) { echo '<div class="container-fluid p-0"><div class="alert alert-danger">' . esc_html__('You do not have permission to create events.', 'blr-backline') . '</div></div>'; return; }

$getm = static function (string $key) use ($post_id): string {
    return $post_id ? (string) get_post_meta($post_id, $key, true) : '';
};
$show_frontend_raw = $getm('blr_show_frontend');
$show_frontend = ($show_frontend_raw === '1');
?>
<div class="container-fluid p-0">
  <div class="d-flex align-items-center justify-content-between mb-3"><h1 class="h3 mb-0"><?php echo $is_edit ? esc_html__('Edit Event', 'blr-backline') : esc_html__('New Event', 'blr-backline'); ?></h1><a class="btn btn-outline-secondary" href="<?php echo esc_url($base . 'events/'); ?>"><?php echo esc_html__('Back', 'blr-backline'); ?></a></div>
  <?php if ($saved_notice): ?><div class="alert alert-success"><?php echo esc_html__('Saved.', 'blr-backline'); ?></div><?php endif; ?>
  <?php foreach ($errors as $error): ?><div class="alert alert-danger"><?php echo esc_html((string) $error); ?></div><?php endforeach; ?>
  <div class="card"><div class="card-body"><form method="post" enctype="multipart/form-data"><?php wp_nonce_field('blr_ba_save_event'); ?>
    <div class="row g-3">
      <div class="col-12 col-lg-8">
        <div class="mb-3"><label class="form-label"><?php echo esc_html__('Title', 'blr-backline'); ?></label><input class="form-control" type="text" name="title" value="<?php echo esc_attr($title_v); ?>" required></div>
        <div class="mb-3"><label class="form-label"><?php echo esc_html__('Description / Notes', 'blr-backline'); ?></label><textarea class="form-control" name="content" rows="5"><?php echo esc_textarea($content_v); ?></textarea></div>
        <div class="row g-3">
          <div class="col-md-6"><label class="form-label"><?php echo esc_html__('Start', 'blr-backline'); ?></label><input class="form-control blr-ba-datetime" type="text" name="blr_event_start" value="<?php echo esc_attr($getm('blr_event_start')); ?>" placeholder="<?php echo esc_attr__('YYYY-MM-DD HH:MM', 'blr-backline'); ?>"></div>
          <div class="col-md-6"><label class="form-label"><?php echo esc_html__('End', 'blr-backline'); ?></label><input class="form-control blr-ba-datetime" type="text" name="blr_event_end" value="<?php echo esc_attr($getm('blr_event_end')); ?>" placeholder="<?php echo esc_attr__('YYYY-MM-DD HH:MM', 'blr-backline'); ?>"></div>
          <div class="col-md-6"><label class="form-label"><?php echo esc_html__('Timezone', 'blr-backline'); ?></label><input class="form-control" type="text" name="blr_timezone" value="<?php echo esc_attr($getm('blr_timezone')); ?>"></div>
          <div class="col-md-6"><label class="form-label"><?php echo esc_html__('Show Status', 'blr-backline'); ?></label><select class="form-select" name="blr_event_status"><option value=""><?php echo esc_html__('— Select —', 'blr-backline'); ?></option><?php foreach ($event_status_options as $value => $label): ?><option value="<?php echo esc_attr($value); ?>" <?php selected($getm('blr_event_status'), $value); ?>><?php echo esc_html($label); ?></option><?php endforeach; ?></select></div>
          <div class="col-md-6"><label class="form-label"><?php echo esc_html__('Venue Name', 'blr-backline'); ?></label><input class="form-control" type="text" name="blr_venue_name" value="<?php echo esc_attr($getm('blr_venue_name')); ?>"></div>
          <div class="col-md-6"><label class="form-label"><?php echo esc_html__('Venue Website', 'blr-backline'); ?></label><input class="form-control" type="url" name="blr_venue_website" value="<?php echo esc_attr($getm('blr_venue_website')); ?>"></div>
          <div class="col-12"><label class="form-label"><?php echo esc_html__('Address', 'blr-backline'); ?></label><input class="form-control" type="text" name="blr_venue_address" value="<?php echo esc_attr($getm('blr_venue_address')); ?>"></div>
          <div class="col-md-4"><label class="form-label"><?php echo esc_html__('City', 'blr-backline'); ?></label><input class="form-control" type="text" name="blr_venue_city" value="<?php echo esc_attr($getm('blr_venue_city')); ?>"></div>
          <div class="col-md-4"><label class="form-label"><?php echo esc_html__('State', 'blr-backline'); ?></label><input class="form-control" type="text" name="blr_venue_state" value="<?php echo esc_attr($getm('blr_venue_state')); ?>"></div>
          <div class="col-md-4"><label class="form-label"><?php echo esc_html__('Postal', 'blr-backline'); ?></label><input class="form-control" type="text" name="blr_venue_postal" value="<?php echo esc_attr($getm('blr_venue_postal')); ?>"></div>
          <div class="col-md-6"><label class="form-label"><?php echo esc_html__('Country', 'blr-backline'); ?></label><input class="form-control" type="text" name="blr_venue_country" value="<?php echo esc_attr($getm('blr_venue_country')); ?>"></div>
          <div class="col-md-6"><label class="form-label"><?php echo esc_html__('Age Restriction', 'blr-backline'); ?></label><input class="form-control" type="text" name="blr_age_restriction" value="<?php echo esc_attr($getm('blr_age_restriction')); ?>"></div>
          <div class="col-md-6"><label class="form-label"><?php echo esc_html__('Ticket URL', 'blr-backline'); ?></label><input class="form-control" type="url" name="blr_ticket_url" value="<?php echo esc_attr($getm('blr_ticket_url')); ?>"></div>
          <div class="col-md-6"><label class="form-label"><?php echo esc_html__('Event URL', 'blr-backline'); ?></label><input class="form-control" type="url" name="blr_event_url" value="<?php echo esc_attr($getm('blr_event_url')); ?>"></div>
          <div class="col-md-3"><label class="form-label"><?php echo esc_html__('Doors', 'blr-backline'); ?></label><input class="form-control blr-ba-time" type="text" name="blr_doors" value="<?php echo esc_attr($getm('blr_doors')); ?>"></div>
          <div class="col-md-3"><label class="form-label"><?php echo esc_html__('Set Time', 'blr-backline'); ?></label><input class="form-control blr-ba-time" type="text" name="blr_set_time" value="<?php echo esc_attr($getm('blr_set_time')); ?>"></div>
          <div class="col-md-3"><label class="form-label"><?php echo esc_html__('Load In', 'blr-backline'); ?></label><input class="form-control blr-ba-time" type="text" name="blr_load_in" value="<?php echo esc_attr($getm('blr_load_in')); ?>"></div>
          <div class="col-md-3"><label class="form-label"><?php echo esc_html__('Soundcheck', 'blr-backline'); ?></label><input class="form-control blr-ba-time" type="text" name="blr_soundcheck" value="<?php echo esc_attr($getm('blr_soundcheck')); ?>"></div>
          <div class="col-md-6"><label class="form-label"><?php echo esc_html__('Payout', 'blr-backline'); ?></label><input class="form-control" type="text" name="blr_payout" value="<?php echo esc_attr($getm('blr_payout')); ?>"></div>
          <div class="col-12"><label class="form-label"><?php echo esc_html__('Internal Notes', 'blr-backline'); ?></label><textarea class="form-control" name="blr_notes" rows="4"><?php echo esc_textarea($getm('blr_notes')); ?></textarea></div>
        
          <div class="col-12">
            <div class="form-check form-switch mb-0">
              <input type="hidden" name="blr_show_frontend" value="0">
              <input
                class="form-check-input"
                type="checkbox"
                id="blr_show_frontend"
                name="blr_show_frontend"
                value="1"
                <?php checked($show_frontend); ?>
              >
              <label class="form-check-label" for="blr_show_frontend">
                <?php echo esc_html__('Show on public website', 'blr-backline'); ?>
              </label>
            </div>
          </div>
        
        </div>
      </div>
      <div class="col-12 col-lg-4">
        <h5 class="mb-3"><?php echo esc_html__('Flyer / Featured Image', 'blr-backline'); ?></h5>
          <div id="blr-ba-event-image-preview" class="mb-3 blr-ba-visual-preview blr-ba-visual-preview--fixed"><?php echo wp_kses_post(Media::preview_markup($current_image_id, 'large')); ?></div>

<div class="mb-3">
  <label class="form-label"><?php echo esc_html__('Existing Images', 'blr-backline'); ?></label>
  <div>
    <button
      class="btn btn-outline-secondary blr-ch-img-btn"
      type="button"
      data-bs-toggle="modal"
      data-bs-target="#blr-ba-event-image-modal"
    >
      <?php echo esc_html__('Choose Existing Image', 'blr-backline'); ?>
    </button>
  </div>
</div>



<div
  class="modal fade"
  id="blr-ba-event-image-modal"
  tabindex="-1"
  aria-labelledby="blr-ba-event-image-modal-label"
  aria-hidden="true"
>
  <div class="modal-dialog modal-xl modal-dialog-scrollable">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="blr-ba-event-image-modal-label">
          <?php echo esc_html__('Choose Existing Image', 'blr-backline'); ?>
        </h5>
        <button
          type="button"
          class="btn-close"
          data-bs-dismiss="modal"
          aria-label="<?php echo esc_attr__('Close', 'blr-backline'); ?>"
        ></button>
      </div>

      <div class="modal-body">
        <?php
        Media::render_visual_picker(
            'event_image_id',
            [
                'selected'           => $current_image_id,
                'allow_none'         => true,
                'none_label'         => __('No Event Image', 'blr-backline'),
                'search_placeholder' => __('Search images...', 'blr-backline'),
                'preview_target'     => '#blr-ba-event-image-preview',
            ]
        );
        ?>
      </div>

      <div class="modal-footer">
        <button type="button" class="btn btn-outline-secondary" data-bs-dismiss="modal">
          <?php echo esc_html__('Cancel', 'blr-backline'); ?>
        </button>
      </div>
    </div>
  </div>
</div>

          <div class="mb-3">
            <label class="form-label"><?php echo esc_html__('Upload New Image', 'blr-backline'); ?></label>
            <input class="form-control blr-ba-image-input" type="file" name="event_image_file" accept="image/*" data-preview="#blr-ba-event-image-preview">
          </div>
      </div>
    </div>
    <div class="mt-4"><button class="btn btn-primary" type="submit"><?php echo esc_html__('Save Event', 'blr-backline'); ?></button></div>
  </form></div></div>
</div>
