<!DOCTYPE html>
<html lang="ru">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Редактирование опроса</title>
  <!-- Добавляем проверку на различные пути -->
  <script>
    // Определяем путь к статическим файлам с учетом prod и dev окружений
    function getStaticPath() {
      if (window.location.pathname.includes('/ms/questioneer')) {
        // Для продакшна
        return '/ms/questioneer/static';
      } else {
        // Для локальной разработки
        const basePath = window.location.pathname.split('/edit')[0];
        // Проверяем, заканчивается ли путь на слеш
        return basePath + (basePath.endsWith('/') ? 'static' : '/static');
      }
    }
    
    // Динамически добавляем CSS
    const cssLink = document.createElement('link');
    cssLink.rel = 'stylesheet';
    cssLink.href = getStaticPath() + '/css/style.css';
    document.head.appendChild(cssLink);
  </script>
  
  <!-- Добавляем jQuery -->
  <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/qrcode-generator/1.4.4/qrcode.min.js"></script>
  <script>
    document.addEventListener('DOMContentLoaded', function() {
      // Динамически добавляем скрипты
      const scriptPaths = [
        '/js/common.js',
        '/js/edit.js'
      ];
      
      const staticPath = getStaticPath();
      scriptPaths.forEach(path => {
        const script = document.createElement('script');
        script.src = staticPath + path;
        document.body.appendChild(script);
      });
    });
  </script>
</head>
<body>
  <!-- Навигационная шапка -->
  <header class="nav-header">
    <div class="nav-container">
      <a href="javascript:;" id="nav-home-link" class="nav-logo">Анонимные опросы</a>
      <nav class="nav-menu">
        <a href="javascript:;" id="nav-main-link" class="nav-link">Главная</a>
        <a href="javascript:;" id="nav-create-link" class="nav-link">Создать опрос</a>
      </nav>
    </div>
  </header>

  <div class="container">
    <h1>Редактирование опроса</h1>
    
    <div id="loading">Загрузка опроса...</div>
    
    <div class="form-container" id="edit-form-container" style="display: none;">
      <form id="edit-questionnaire-form">
        <div class="form-group">
          <label for="title">Название опроса:</label>
          <input type="text" id="title" name="title" required>
        </div>
        
        <div class="form-group">
          <label for="description">Описание:</label>
          <textarea id="description" name="description" rows="3"></textarea>
        </div>
        
        <div class="form-group">
          <label for="display-type">Тип отображения:</label>
          <select id="display-type" name="displayType">
            <option value="default">Обычный</option>
            <option value="tag_cloud">Облако тегов</option>
            <option value="voting">Голосование</option>
            <option value="poll">Опрос</option>
          </select>
        </div>
        
        <div class="questions-container">
          <h2>Вопросы</h2>
          <div id="questions-list"></div>
          
          <button type="button" id="add-question" class="btn btn-small">Добавить вопрос</button>
        </div>
        
        <div class="form-actions">
          <div class="link-group">
            <h3>Ссылки:</h3>
            <div class="link-input-group">
              <div>
                <label for="public-link">Ссылка для голосования:</label>
                <input type="text" id="public-link" readonly>
                <button type="button" class="btn btn-small" id="copy-public-link">Копировать</button>
                <button type="button" class="btn btn-small" id="show-qr-code">QR-код</button>
              </div>
              <div>
                <label for="admin-link">Административная ссылка:</label>
                <input type="text" id="admin-link" readonly>
                <button type="button" class="btn btn-small" id="copy-admin-link">Копировать</button>
              </div>
            </div>
          </div>
          
          <div class="form-buttons">
            <a href="#" id="back-to-admin" class="btn btn-secondary">Вернуться</a>
            <button type="submit" class="btn btn-primary">Сохранить изменения</button>
          </div>
        </div>
      </form>
    </div>
  </div>
  
  <!-- Шаблон для вопроса -->
  <template id="question-template">
    <div class="question-item" data-index="{{index}}">
      <div class="question-header">
        <h3>Вопрос {{number}}</h3>
        <button type="button" class="btn-icon delete-question">
          <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" viewBox="0 0 16 16">
            <path d="M4.646 4.646a.5.5 0 0 1 .708 0L8 7.293l2.646-2.647a.5.5 0 0 1 .708.708L8.707 8l2.647 2.646a.5.5 0 0 1-.708.708L8 8.707l-2.646 2.647a.5.5 0 0 1-.708-.708L7.293 8 4.646 5.354a.5.5 0 0 1 0-.708z"/>
          </svg>
        </button>
      </div>
      
      <div class="form-group">
        <label for="question-text-{{index}}">Текст вопроса:</label>
        <input type="text" id="question-text-{{index}}" name="questions[{{index}}][text]" required>
      </div>
      
      <div class="form-group">
        <label for="question-type-{{index}}">Тип вопроса:</label>
        <select id="question-type-{{index}}" name="questions[{{index}}][type]" class="question-type-select">
          <option value="single_choice">Один вариант</option>
          <option value="multiple_choice">Несколько вариантов</option>
          <option value="text">Текстовый ответ</option>
          <option value="rating">Оценка</option>
          <option value="scale">Шкала оценки</option>
          <option value="tag_cloud">Облако тегов</option>
        </select>
      </div>
      
      <div class="form-group">
        <label>
          <input type="checkbox" name="questions[{{index}}][required]" value="true">
          Обязательный вопрос
        </label>
      </div>
      
      <div class="options-container" id="options-container-{{index}}">
        <h4>Варианты ответа</h4>
        <div class="options-list" id="options-list-{{index}}"></div>
        <button type="button" class="btn btn-small add-option" data-question-index="{{index}}">Добавить вариант</button>
      </div>
      
      <div class="scale-container" id="scale-container-{{index}}" style="display: none;">
        <h4>Настройки шкалы</h4>
        <div class="form-group">
          <label for="scale-max-{{index}}">Максимальное значение:</label>
          <select id="scale-max-{{index}}" name="questions[{{index}}][scaleMax]">
            <option value="5">5</option>
            <option value="10" selected>10</option>
            <option value="20">20</option>
          </select>
        </div>
      </div>
    </div>
  </template>
  
  <!-- Шаблон для варианта ответа -->
  <template id="option-template">
    <div class="option-item" data-index="{{optionIndex}}">
      <input type="text" name="questions[{{questionIndex}}][options][{{optionIndex}}][text]" placeholder="Вариант ответа">
      <button type="button" class="btn-icon delete-option">
        <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" viewBox="0 0 16 16">
          <path d="M4.646 4.646a.5.5 0 0 1 .708 0L8 7.293l2.646-2.647a.5.5 0 0 1 .708.708L8.707 8l2.647 2.646a.5.5 0 0 1-.708.708L8 8.707l-2.646 2.647a.5.5 0 0 1-.708-.708L7.293 8 4.646 5.354a.5.5 0 0 1 0-.708z"/>
        </svg>
      </button>
    </div>
  </template>

  <script>
    // Добавляем корректные пути к ссылкам после загрузки страницы
    document.addEventListener('DOMContentLoaded', function() {
      // Определяем базовый путь с учетом /ms в продакшен-версии
      const isMsPath = window.location.pathname.includes('/ms/questioneer');
      const basePath = isMsPath ? '/ms/questioneer' : '/questioneer';
      
      // Устанавливаем правильные ссылки
      document.getElementById('nav-home-link').href = basePath;
      document.getElementById('nav-main-link').href = basePath;
      document.getElementById('nav-create-link').href = basePath + '/create';
    });
  </script>
</body>
</html>