<?php

include_once "admin.inc.php";

$UI_CONFIG = new $uiconfig_name('admin', 'form_generator');

$module_object = new DashaMail($module_login, $module_password);

$connection_test = $module_object->test_connection();

if (!$connection_test) {
  header("Location: admin.php");
  die();
}

$UI_CONFIG->actionButtons[] = array(
  "id" => "submit",
  "caption" => "Сгенерировать формы",
  "action" => "mainView.submitIframeForm('GenerateForm')",
  "align" => "left"
);

$bases_list = $module_object->get_bases_list();

BeginHtml();
?>
  <link href="css/style.css" rel="stylesheet"/>
  <div class="generator">
    <?
    if (!$bases_list || !count($bases_list)) {
      ?>
      <div class="nc-alert nc--red">
        <i class="nc-icon-l nc--status-error"></i>
        Для создания форм подписки требуется создать хотя бы одну адресную базу.
      </div>
      <?
    }elseif (count($bases_list)) {
      if (count($_POST)) {
        ob_start();
        ?>
        <form action="http://<? echo $DOMAIN_NAME . $SUB_FOLDER . $HTTP_ROOT_PATH; ?>modules/dashamail/subscribe/" method="post">
          <input type="hidden" value="<? echo $_POST["base_id"]; ?>" name="base_id"/>
          Подпишитесь на нашу рассылку:
          <input type="text" name="email" value="" placeholder="E-mail"/><br/><?
          for ($counter = 1; $counter <= 5; $counter++)
            if ($_POST["merge_" . $counter]) {
              ?>

              <input type="text" name="merge_<? echo $counter; ?>" value="" placeholder="<? echo $_POST["merge_" . $counter]; ?>"/>
              <br/><?
            }
          ?>
          <input type="submit" name="submit" value="Отправить"/>
        </form>
        <?
        $netcat_form = ob_get_contents();
        ob_clean();

        ob_start();
        ?>
        <div id='mc_embed_signup'>
          <form action='https://lk.dashamail.com/subscribe/' method='post' id='mc-embedded-subscribe-form' name='mc-embedded-subscribe-form' class='validate' target='_blank'>
            <input type='hidden' name='list_id' value='<? echo $_POST["base_id"]; ?>'/>
            <input type='hidden' name='no_conf' value=''/>
            <input type='hidden' name='notify' value=''/>
            <h2>Подпишитесь на нашу рассылку</h2>
            <div class='indicates-required'>
              <span class='asterisk'>*</span> обязательные поля
            </div>
            <div class='mc-field-group'>
              <label for='mce-EMAIL'>Email-адрес <span class='asterisk'>*</span></label>
              <input type='email' value='' name='email' class='required email' id='mce-EMAIL'/>
            </div><?
            for ($counter = 1; $counter <= 5; $counter++)
              if ($_POST["merge_" . $counter]) {
                ?>

                <div class='mc-field-group'>
                <label for='mce-MERGE<? echo $counter; ?>'><? echo $_POST["merge_" . $counter]; ?></label>
                <input type='text' value='' class='required' name='merge_<? echo $counter; ?>' id='mce-MERGE<? echo $counter; ?>'/>
                </div><?
              }
            ?>
            <div id='mce-responses' class='clear'>
              <div class='response' id='mce-error-response' style='display:none'></div>
              <div class='response' id='mce-success-response' style='display:none'></div>
            </div>
            <div class='clear'>
              <input dojotype='dijit.form.Button' type='submit' value='Подписаться' name='subscribe' id='mc-embedded-subscribe' class='button'/>
            </div>
          </form>
        </div>
        <?
        $dashamail_form = ob_get_contents();
        ob_clean();
      }

      ?>
      <form id="GenerateForm" method="post">
        <div class="generator-table">
          <div class="generator-table-field">
            <div class="generator-table-field-label">
              База пользователей
            </div>
            <div class="generator-table-field-value">
              <select name="base_id">
                <?
                foreach ($bases_list as $bases_list_row) {
                  ?>
                  <option value="<? echo $bases_list_row->id; ?>"><? echo $bases_list_row->name; ?></option>
                  <?
                }
                ?>
              </select>
            </div>
          </div>
          <div class="generator-table-field">
            <div class="generator-table-field-label">
              Дополнительные поля
            </div>
            <div class="generator-table-field-value">
              <?
              for ($counter = 1; $counter <= 5; $counter++) {
                ?>
                <div class="generator-table-field-value-row">
                  <input value="<? echo(isset($_POST["merge_" . $counter]) ? $_POST["merge_" . $counter] : NULL); ?>" name="merge_<? echo $counter; ?>" placeholder="Дополнительное поле <? echo $counter; ?>"/>
                </div>
                <?
              }
              ?>
            </div>
          </div>
        </div>
        <?
        if ($netcat_form) {
          ?>
          <div class="generated-form">
            <div class="generated-form-label">
              Форма для подписки в Netcat
            </div>
            <textarea class="generated-form-netcat"><? echo $netcat_form; ?></textarea>
          </div>
          <?
        }
        if ($dashamail_form) {
          ?>
          <div class="generated-form">
            <div class="generated-form-label">
              Форма для подписки в сервис DashaMail
            </div>
            <textarea class="generated-form-netcat"><? echo $dashamail_form; ?></textarea>
          </div>
          <?
        }
        ?>
      </form>
      <?
    }
    ?>
  </div>
<?
EndHtml();
?>
