<?php
/** @var \Dotdigitalgroup\Email\Block\Customer\Account\Books $block */
// @codingStandardsIgnoreFile
?>
<form id="connector-newsletter-form-validate" method="post" action="<?= $block->escapeHtml($block->getSaveUrl()) ?>">
    <?= $block->getBlockHtml('formkey') ?>
    <fieldset class="fieldset">
        <legend class="legend"><span><?= $block->escapeHtml(__('General Subscription')) ?></span></legend>
        <div class="field choice">
            <input type="checkbox" name="is_subscribed" id="subscription" value="1"
                   title="General Subscription"
                <?php if ($block->isSubscribed()): ?> checked="checked"<?php endif; ?> class="checkbox">
            <label for="subscription" class="label">
                    <span>
                        <?php if (strlen($block->getCustomerConsentText())) : ?>
                            <?= $block->escapeHtml($block->getCustomerConsentText()) ?>
                        <?php else : ?>
                            <?= $block->escapeHtml(__('General Subscription')) ?>
                        <?php endif; ?>
                    </span>
            </label>
        </div>
        <?php /* Extensions placeholder */ ?>
        <?= $block->getChildHtml('customer.form.newsletter.extra') ?>
    </fieldset>
    <?php if ($block->canShowAnything()) : ?>
        <?php $additionalBooks = $block->getAdditionalBooksToShow();
            $dataFields = $block->getDataFieldsToShow();
            $canShowAddressBook = $block->getCanShowAdditionalBooks();
            $canShowDataFields = $block->getCanShowDataFields(); ?>

            <?php if ($block->getCanShowPreferences()) : ?>
                <fieldset class="fieldset">
                    <legend class="legend"><span><?= $block->escapeHtml(__('Preferences')) ?></span></legend>
                    <ul class="form-list" style="padding-left: 0">
                        <?php foreach ($block->getPreferencesToShow() as $prefId => $preferences) : ?>
                            <?php $uniqId = uniqid('pr-'); ?>
                            <?php if (! $preferences['isPreference']) : ?>
                                <h5 style="margin-bottom: 10px"><?= $block->escapeHtml($preferences['catLabel']) ?></h5>
                                <?php foreach ($preferences['catPreferences'] as $catPrefId => $catPref) : ?>
                                    <li class="control" style="display: block; margin: 5px 0 5px 10px">
                                        <input type="checkbox" name="preferences[]"
                                           id="<?= $block->escapeHtml($uniqId . $catPrefId); ?>"
                                           value="<?= $block->escapeHtml($prefId . ',' . $catPrefId) ?>"
                                           title="<?= $block->escapeHtml($catPref["label"]) ?>"
                                            <?php if ($catPref["isOptedIn"]) : ?>
                                                checked="checked"
                                            <?php endif; ?> class="checkbox"
                                        />
                                        <label for="<?= $block->escapeHtml($uniqId . $catPrefId); ?>">
                                            <?= $block->escapeHtml($catPref["label"]) ?>
                                        </label>
                                    </li>
                                <?php endforeach; ?>
                            <?php else: ?>
                                <li class="control" style="display: block; margin: 17px 0 17px 10px">
                                    <input type="checkbox" name="preferences[]"
                                       id="<?= $block->escapeHtml($uniqId . $prefId); ?>"
                                       value="<?= $block->escapeHtml($prefId) ?>"
                                       title="<?= $block->escapeHtml($preferences["label"]) ?>"
                                        <?php if ($preferences["isOptedIn"]) : ?>
                                            checked="checked"
                                        <?php endif; ?> class="checkbox"
                                    />
                                    <label for="<?= $block->escapeHtml($uniqId . $prefId); ?>">
                                        <?= $block->escapeHtml($preferences["label"]) ?>
                                    </label>
                                </li>
                            <?php endif; ?>
                        <?php endforeach; ?>
                    </ul>
                </fieldset>
            <?php endif; ?>
            <?php if (!empty($additionalBooks) && $canShowAddressBook) : ?>
                <fieldset class="fieldset">
                    <legend class="legend">
                        <span><?= $block->escapeHtml(__('Subscriptions')) ?></span>
                    </legend>
                    <ul class="form-list" style="padding-left: 0">
                        <?php foreach ($additionalBooks as $books) : ?>
                            <li class="control" style="display: block; margin: 5px 0 5px 10px">
                                <input type="checkbox" name="additional_subscriptions[]"
                                   id="<?= $block->escapeHtml($books["name"]); ?>"
                                   value="<?= $block->escapeHtml($books["value"]) ?>"
                                   title="<?= $block->escapeHtml($books["name"]) ?>"
                                    <?php if ($books["subscribed"]) : ?>
                                        checked="checked"
                                    <?php endif; ?> class="checkbox"
                                />
                                <label for="<?= $block->escapeHtml($books["name"]); ?>">
                                    <?= $block->escapeHtml($books["name"]) ?>
                                </label>
                            </li>
                        <?php endforeach; ?>
                    </ul>
                </fieldset>
            <?php endif; ?>
            <?php if (!empty($dataFields) && $canShowDataFields) : ?>
                <fieldset class="fieldset">
                    <legend class="legend">
                        <span><?= $block->escapeHtml(__('Information')) ?></span>
                    </legend>
                    <ul class="form-list" style="padding-left: 0">
                        <?php foreach ($dataFields as $dataField) : ?>
                            <li class="fields" style="display: block">
                                <div class="field" style="margin-bottom: 10px;">
                                    <label for="<?= $block->escapeHtml($dataField['name']) ?>">
                                        <?= $block->escapeHtml($dataField['name']) ?>
                                    </label>
                                    <?php if ($dataField['type'] == "Boolean") : ?>
                                    <div class="control">
                                        <select style="width: 50%" name="data_fields[<?= $block->escapeHtml($dataField['name']) ?>]" id="<?= $block->escapeHtml($dataField['name']) ?>" title="<?= $block->escapeHtml($dataField['name']) ?>">
                                            <option value="1"<?php if ($block->escapeHtml($dataField['value'])) : ?> selected="selected"<?php endif; ?>><?= __('Yes') ?></option>
                                            <option value="0"<?php if (!$block->escapeHtml($dataField['value'])) : ?> selected="selected"<?php endif; ?>><?= __('No') ?></option>
                                        </select>
                                    </div>
                                    <?php elseif ($dataField['type'] == "Numeric") : ?>
                                    <div class="input-box">
                                        <input style="width: 50%" type="number"
                                           title="<?= $block->escapeHtml($dataField['name']) ?>"
                                           value="<?= $block->escapeHtml($dataField['value']) ?>"
                                           name="data_fields[<?= $block->escapeHtml($dataField['name']) ?>]"
                                           id="<?= $block->escapeHtml($dataField['name']) ?>"
                                        />
                                    </div>
                                    <?php else: ?>
                                    <div class="input-box">
                                        <input style="width: 50%" type="text"
                                           class="input-text <?php if ($dataField['type'] == "Date") : ?>
                                               date-field <?php endif; ?>"
                                           title="<?= $block->escapeHtml($dataField['name']) ?>"
                                           value="<?= $block->escapeHtml($dataField['value']) ?>"
                                           name="data_fields[<?= $block->escapeHtml($dataField['name']) ?>]"
                                           id="<?= $block->escapeHtml($dataField['name']) ?>"
                                        />
                                    </div>
                                    <?php endif; ?>
                                </div>
                            </li>
                        <?php endforeach; ?>
                    </ul>
                </fieldset>
            <?php endif; ?>
    <?php endif; ?>
    <div class="actions-toolbar">
        <div class="primary">
            <button type="submit" title="Save" class="action save primary">
                <span>Save</span>
            </button>
        </div>
    </div>
</form>
