<?php
$languages = CE_Lib::getEnabledLanguages(true);
?>

<?php if (count($languages) > 1) { ?>
<ul class="nav nav-pills mb-3" id="langTabs" role="tablist">
  <?php $active = true; foreach ($languages as $key => $lang) { ?>
    <li class="nav-item" role="presentation">
      <button
        type="button"
        class="nav-link<?php if ($active) { echo ' active'; $active = false; } ?>"
        data-lang="<?php echo $key; ?>">
        <?php echo $lang; ?>
      </button>
    </li>
  <?php } ?>
</ul>
<?php } ?>

<div class="row g-3">

  <!-- Name -->
  <div class="col-md-8">
    <label class="form-label fw-bold">
      <?php echo $this->user->lang('Name'); ?>
    </label>

    <?php
    if (count($languages) > 1) {
      $active = true;
      foreach ($languages as $languageKey => $language) { ?>
        <span class="language <?php echo $languageKey; ?>" <?php if (!$active) echo 'style="display:none"'; ?>>
          <input
            class="form-control required"
            type="text"
            name="title<?php echo $languageKey; ?>"
            value="<?php echo $this->nameLanguages[$languageKey]; ?>"
            <?php if ($active) { echo 'id="mainlanguagetitle"'; $active = false; } ?>
          >
        </span>
    <?php }} else { ?>
      <input
        class="form-control required"
        type="text"
        id="mainlanguagetitle"
        name="title"
        value="<?php echo $this->name; ?>"
      >
    <?php } ?>
  </div>

  <!-- Order -->
  <div class="col-md-4">
    <label class="form-label fw-bold">
      <?php echo $this->user->lang('Order'); ?>
    </label>
    <input
      class="form-control required"
      type="number"
      id="my_order"
      name="my_order"
      value="<?php echo $this->my_order; ?>"
    >
  </div>

  <!-- Description -->
  <div class="col-12">
    <label class="form-label fw-bold">
      <?php echo $this->user->lang('Description'); ?>
    </label>

    <?php
    if (count($languages) > 1) {
      $active = true;
      foreach ($languages as $languageKey => $language) { ?>
        <span class="language <?php echo $languageKey; ?>" <?php if (!$active) echo 'style="display:none"'; ?>>
          <textarea
            class="form-control required"
            rows="8"
            name="description<?php echo $languageKey; ?>"
            <?php if ($active) { echo 'id="mainlanguagedescription"'; $active = false; } ?>
          ><?php echo $this->descriptionLanguages[$languageKey]; ?></textarea>
        </span>
    <?php }} else { ?>
      <textarea
        class="form-control required"
        rows="8"
        name="description"
        id="mainlanguagedescription"
      ><?php echo $this->description; ?></textarea>
    <?php } ?>
  </div>

  <!-- Checkboxes -->
  <div class="col-12">
    <div class="form-check form-check-inline">
      <input
        class="form-check-input"
        type="checkbox"
        name="staffonly"
        id="staffonly"
        value="1"
        <?php if ($this->staffonly == 1) echo 'checked'; ?>
      >
      <label class="form-check-label" for="staffonly">
        <?php echo $this->user->lang("Staff Only"); ?>
      </label>
    </div>

    <div class="form-check form-check-inline">
      <input
        class="form-check-input"
        type="checkbox"
        id="is_series"
        name="is_series"
        value="1"
        <?php if ($this->is_series == 1) echo 'checked'; ?>
      >
      <label class="form-check-label" for="is_series">
        <span
          data-bs-toggle="tooltip"
          data-bs-html="true"
          title="<?php echo $this->user->lang('Articles in this category will contain footer links to the next article in the series.'); ?>">
          <?php echo $this->user->lang("Treat as a series"); ?>
        </span>
      </label>
    </div>

    <div class="form-check form-check-inline" id="is_global_series" style="display:none;">
      <input
        class="form-check-input"
        type="checkbox"
        name="is_global_series"
        id="is_global_series_input"
        value="1"
        <?php if ($this->is_global_series == 1) echo 'checked'; ?>
      >
      <label class="form-check-label" for="is_global_series_input">
        <span
          data-bs-toggle="tooltip"
          data-bs-html="true"
          title="<?php echo $this->user->lang('If checked this category will be listed with all other global series in a sidebar navigation UI.'); ?>">
          <?php echo $this->user->lang("Global series?"); ?>
        </span>
      </label>
    </div>
  </div>

</div>

<script data-cfasync="false">
$(function () {

  // Bootstrap 5 tooltips
  document.querySelectorAll('[data-bs-toggle="tooltip"]').forEach(el => {
    new bootstrap.Tooltip(el);
  });

  // Language switching
  $('#langTabs button').on('click', function () {
    const lang = $(this).data('lang');
    $('#langTabs button').removeClass('active');
    $(this).addClass('active');
    $('.language').hide();
    $('.language.' + lang).show();
  });

  // Series toggle
  function toggleGlobalSeries() {
    $('#is_global_series').toggle($('#is_series').is(':checked'));
  }

  toggleGlobalSeries();
  $('#is_series').on('change', toggleGlobalSeries);

});
</script>
