<?php
/**
 * Maho
 *
 * @category    design
 * @package     default_default
 * @copyright   Copyright (c) 2006-2020 Magento, Inc. (https://magento.com)
 * @copyright   Copyright (c) 2021-2024 The OpenMage Contributors (https://openmage.org)
 * @copyright   Copyright (c) 2024 Maho (https://mahocommerce.com)
 * @license     https://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
 */

/**
 * Install database configuration template
 *
 * @see Mage_Install_Block_Config
 * @var Mage_Install_Block_Config $this
 */
?>
<script type="text/javascript">
    let dbTypeIds = [];
    function showContent(select) {
        dbTypeIds.forEach(function (name) {
            document.getElementById(name + '_conn_form').style.display = 'none';
        });
        document.getElementById(select.value + '_conn_form').style.display = 'block';
    }
</script>

<fieldset class="group-select">
    <legend><?= $this->__('Connection') ?></legend>
    <h4 class="legend"><?= $this->__('Database Connection') ?></h4>
    <ul>
        <?php $dbTypeOptions = $this->getDatabaseBlocks(); ?>
        <?php if (count($dbTypeOptions) > 0): ?>
            <li>
                <div class="input-box">
                    <label for="prefix"><?= $this->__('Database Type') ?></label><br />
                    <select name="config[db_model]" onchange="showContent(this)">
                        <?php foreach($dbTypeOptions as $block): ?>
                            <option value="<?= $block->getIdPrefix()?>" <?php if($this->getFormData()->getDbModel() == $block->getIdPrefix()):?>selected="selected"<?php endif ?>><?= $block->getTitle() ?></option>
                        <?php endforeach ?>
                    </select>
                </div>
            </li>
        <?php endif ?>

        <?php foreach ($dbTypeOptions as $block): ?>
            <script type="text/javascript">
                dbTypeIds.push("<?= $block->getIdPrefix() ?>");
            </script>
        <?php $block = $this->getDatabaseBlock($block->getIdPrefix()) ?>

            <div id="<?= $block->getIdPrefix() ?>_conn_form" <?php if($this->getFormData()->getDbModel() != $block->getIdPrefix()):?>style="display:none;"<?php endif ?>>
                <ul>
                    <?= $block->toHtml() ?>
                </ul>
            </div>
        <?php endforeach ?>
    </ul>
</fieldset>