<form method="post" action="tbl_relation.php">
    <?= PMA\libraries\URL::getHiddenInputs($db, $table); ?>
    <!-- InnoDB  -->
    <?php if (PMA\libraries\Util::isForeignKeySupported($tbl_storage_engine)): ?>
        <fieldset>
            <legend><?= __('Foreign key constraints'); ?></legend>
            <table id="foreign_keys" class="relationalTable">
                <tr>
                    <th><?= __('Actions'); ?></th>
                    <th><?= __('Constraint properties'); ?></th>
                    <?php if (strtoupper($tbl_storage_engine) == 'INNODB'): ?>
                        <th><?= __('Column') , PMA\libraries\Util::showHint(__(
                                'Creating a foreign key over a non-indexed column would automatically create'
                                . ' an index on it. Alternatively, you can define an index below, before creating'
                                . ' the foreign key.'
                            )) ?>
                        </th>
                    <?php else: ?>
                        <th><?= __('Column') , PMA\libraries\Util::showHint(__(
                                'Only columns with index will be displayed. You can define an'
                                . ' index below.'
                            )) ?>
                        </th>
                    <?php endif; ?>
                    <th colspan="3">
                        <?= __('Foreign key constraint'); ?> (<?= $tbl_storage_engine; ?>)
                    </th>
                </tr>
                <tr>
                    <th></th>
                    <th></th>
                    <th></th>
                    <th><?= __('Database'); ?></th>
                    <th><?= __('Table'); ?></th>
                    <th><?= __('Column'); ?></th>
                </tr>
                <?php $i = 0; ?>
                <?php if (! empty($existrel_foreign)): ?>
                    <?php foreach ($existrel_foreign as $key => $one_key): ?>
                        <?= PMA\libraries\Template::get('table/relation/foreign_key_row')->render(
                            array(
                                'one_key' => $one_key,
                                'columns' => $columns,
                                'i' => $i++,
                                'options_array' => $options_array,
                                'tbl_storage_engine' => $tbl_storage_engine,
                                'db' => $db
                            )
                        );
                        ?>
                    <?php endforeach; ?>
                <?php endif; ?>
                <?= PMA\libraries\Template::get('table/relation/foreign_key_row')->render(
                    array(
                        'one_key' => array(),
                        'columns' => $columns,
                        'i' => $i++,
                        'options_array' => $options_array,
                        'tbl_storage_engine' => $tbl_storage_engine,
                        'db' => $db
                    )
                ); ?>
                <tr>
                    <th colspan="6">
                        <a class="formelement clearfloat add_foreign_key" href="">
                            <?= __('+ Add constraint'); ?>
                    </td>
                </tr>
            </table>
        </fieldset>
    <?php endif; ?>

    <?php if ($cfgRelation['relwork']): ?>
        <?php if (PMA\libraries\Util::isForeignKeySupported($tbl_storage_engine)): ?>
            <br/>
            <?= PMA\libraries\Util::getDivForSliderEffect('ir_div', __('Internal relationships')) ?>
        <?php endif; ?>

        <?php $save_row = array_values($columns);
        $saved_row_cnt  = count($save_row);?>
        <fieldset>
            <legend><?= __('Internal relationships') , PMA\libraries\Util::showDocu('config', 'cfg_Servers_relation'); ?></legend>
            <table id="internal_relations" class="relationalTable">
                <tr>
                    <th><?= __('Column'); ?></th>
                    <th><?= __('Internal relation'); ?>
                        <?php if (PMA\libraries\Util::isForeignKeySupported($tbl_storage_engine)): ?>
                            <?= PMA\libraries\Util::showHint(__(
                                'An internal relation is not necessary when a corresponding'
                                . ' FOREIGN KEY relation exists.')); ?>
                        <?php endif; ?>
                    </th>
                    <?php for ($i = 0; $i < $saved_row_cnt; $i++): ?>
                        <?= PMA\libraries\Template::get('table/relation/internal_relational_row')->render(
                            array(
                                'save_row' => $save_row,
                                'i' => $i,
                                'existrel' => $existrel,
                                'db' => $db
                            )
                        );
                        ?>
                    <?php endfor; ?>
            </table>
        </fieldset>
        <?php if (PMA\libraries\Util::isForeignKeySupported($tbl_storage_engine)): ?>
            </div>
        <?php endif; ?>
    <?php endif; ?>

    <?php if ($cfgRelation['displaywork']): ?>
        <?php $save_row = array_values($columns);?>
        <?php $disp = PMA_getDisplayField($db, $table); ?>
        <fieldset>
            <label><?= __('Choose column to display:'); ?></label>
            <select name="display_field">
                <option value="">---</option>
                <?php foreach ($save_row as $row): ?>
                    <option value="<?= htmlspecialchars($row['Field']); ?>"
                        <?php if (isset($disp) && $row['Field'] == $disp): ?>
                            selected="selected"
                        <?php endif; ?>>
                        <?= htmlspecialchars($row['Field']); ?>
                    </option>
                <?php endforeach; ?>
            </select>
        </fieldset>
    <?php endif; ?>

    <fieldset class="tblFooters">
        <input type="button" class="preview_sql" value="<?= __('Preview SQL'); ?>" />
        <input type="submit" value="<?= __('Save'); ?>" />
    </fieldset>
</form>
