<?php
use BLR\Backline\Core\Access;
use BLR\Backline\Modules\Contacts\Contacts;
use BLR\Backline\Modules\Contacts\NewsletterDoubleOptIn;
use BLR\Backline\Modules\Contacts\Pages as ContactsPages;

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

$__blr_ba_data = isset($ctx['contact_form']) && is_array($ctx['contact_form']) ? $ctx['contact_form'] : ContactsPages::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_contact')) { echo '<div class="container-fluid p-0"><div class="alert alert-danger">' . esc_html__('Contact not found.', 'blr-backline') . '</div></div>'; return; }
if ($is_edit && !Access::can_edit_post($post_id, 'blr_contact')) { echo '<div class="container-fluid p-0"><div class="alert alert-danger">' . esc_html__('You do not have permission to edit this contact.', 'blr-backline') . '</div></div>'; return; }
if (!$is_edit && !Access::can_create_post_type('blr_contact')) { echo '<div class="container-fluid p-0"><div class="alert alert-danger">' . esc_html__('You do not have permission to create contacts.', 'blr-backline') . '</div></div>'; return; }

$getm = static function(string $key) use ($post_id, $failed_post, $posted_values) {
  return ContactsPages::contact_meta_value($post_id, $key, $failed_post, $posted_values);
};
?>
<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 Contact', 'blr-backline') : esc_html__('New Contact', 'blr-backline'); ?></h1>
    <a class="btn btn-outline-secondary" href="<?php echo esc_url($base . 'contacts/'); ?>"><?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 $e): ?>
    <div class="alert alert-danger"><?php echo esc_html($e); ?></div>
  <?php endforeach; ?>

  <div class="card">
    <div class="card-body">
      <form method="post">
        <?php wp_nonce_field('blr_ba_save_contact'); ?>

        <div class="row">
          <div class="col-12 col-lg-8">
            <div class="row">
              <div class="col-12 col-md-8">
                <div class="mb-3">
                  <label class="form-label"><?php echo esc_html__('Full Name', 'blr-backline'); ?></label>
                  <input class="form-control" type="text" name="title" value="<?php echo esc_attr($title_v); ?>" required>
                </div>
              </div>
              <div class="col-12 col-md-4">
                <div class="mb-3">
                  <label class="form-label"><?php echo esc_html__('First Name', 'blr-backline'); ?></label>
                  <input class="form-control" type="text" name="blr_contact_first_name" value="<?php echo esc_attr($getm('blr_contact_first_name')); ?>" placeholder="<?php echo esc_attr__('For merge tags', 'blr-backline'); ?>">
                </div>
              </div>
            </div>

            <div class="mb-3">
              <label class="form-label"><?php echo esc_html__('Notes', 'blr-backline'); ?></label>
              <textarea class="form-control" name="notes" rows="6"><?php echo esc_textarea($notes_v); ?></textarea>
            </div>

            <div class="mb-3">
              <label class="form-label"><?php echo esc_html__('Internal Notes', 'blr-backline'); ?></label>
              <textarea class="form-control" name="blr_contact_notes" rows="4"><?php echo esc_textarea($getm('blr_contact_notes')); ?></textarea>
            </div>
          </div>

          <div class="col-12 col-lg-4">
            <div class="mb-3">
              <label class="form-label"><?php echo esc_html__('Contact Type', 'blr-backline'); ?></label>
              <select class="form-select" name="blr_contact_type">
                <option value=""><?php echo esc_html__('— Select —', 'blr-backline'); ?></option>
                <?php foreach ($type_options as $type): ?>
                  <option value="<?php echo esc_attr($type); ?>" <?php selected($getm('blr_contact_type'), $type); ?>><?php echo esc_html(Contacts::type_label($type)); ?></option>
                <?php endforeach; ?>
              </select>
            </div>

            <div class="mb-3">
              <label class="form-label"><?php echo esc_html__('Role', 'blr-backline'); ?></label>
              <input class="form-control" type="text" name="blr_contact_role" value="<?php echo esc_attr($getm('blr_contact_role')); ?>" placeholder="<?php echo esc_attr__('Booker / Promoter', 'blr-backline'); ?>">
            </div>

            <div class="mb-3">
              <label class="form-label"><?php echo esc_html__('Company', 'blr-backline'); ?></label>
              <input class="form-control" type="text" name="blr_contact_company" value="<?php echo esc_attr($getm('blr_contact_company')); ?>">
            </div>

            <div class="mb-3">
              <label class="form-label"><?php echo esc_html__('Email', 'blr-backline'); ?></label>
              <input class="form-control" type="email" name="blr_contact_email" value="<?php echo esc_attr($getm('blr_contact_email')); ?>">
            </div>

            <div class="mb-3">
              <label class="form-label"><?php echo esc_html__('Phone', 'blr-backline'); ?></label>
              <input class="form-control" type="text" name="blr_contact_phone" value="<?php echo esc_attr($getm('blr_contact_phone')); ?>">
            </div>

            <div class="mb-3">
              <label class="form-label"><?php echo esc_html__('Website', 'blr-backline'); ?></label>
              <input class="form-control" type="url" name="blr_contact_website" value="<?php echo esc_attr($getm('blr_contact_website')); ?>">
            </div>

            <div class="row">
              <div class="col-8">
                <div class="mb-3">
                  <label class="form-label"><?php echo esc_html__('City', 'blr-backline'); ?></label>
                  <input class="form-control" type="text" name="blr_contact_city" value="<?php echo esc_attr($getm('blr_contact_city')); ?>">
                </div>
              </div>
              <div class="col-4">
                <div class="mb-3">
                  <label class="form-label"><?php echo esc_html__('State', 'blr-backline'); ?></label>
                  <input class="form-control" type="text" name="blr_contact_state" value="<?php echo esc_attr($getm('blr_contact_state')); ?>">
                </div>
              </div>
            </div>

            <div class="mb-3">
              <label class="form-label" for="blr_contact_newsletter_status"><?php echo esc_html__('Newsletter Status', 'blr-backline'); ?></label>
              <select class="form-select" id="blr_contact_newsletter_status" name="blr_contact_newsletter_status">
                <?php foreach (NewsletterDoubleOptIn::status_options() as $status_option): ?>
                  <option value="<?php echo esc_attr($status_option); ?>" <?php selected($newsletter_status, $status_option); ?>><?php echo esc_html(NewsletterDoubleOptIn::status_label($status_option)); ?></option>
                <?php endforeach; ?>
              </select>
              <div class="form-text"><?php echo esc_html__('Pending is mainly for confirmation-based fan signups. Subscribed contacts are the only ones included in newsletter sends.', 'blr-backline'); ?></div>
            </div>
          </div>
        </div>

        <div class="d-flex gap-2">
          <button class="btn btn-primary" type="submit"><?php echo esc_html__('Save', 'blr-backline'); ?></button>
          <a class="btn btn-outline-secondary" href="<?php echo esc_url($base . 'contacts/'); ?>"><?php echo esc_html__('Cancel', 'blr-backline'); ?></a>
        </div>
      </form>
    </div>
  </div>
</div>
