<?php
$partitionOptions = array('', 'HASH', 'LINEAR HASH', 'KEY', 'LINEAR KEY', 'RANGE', 'LIST');
$subPartitionOptions = array('', 'HASH', 'LINEAR HASH', 'KEY', 'LINEAR KEY');
$valueTypeOptions = array('', 'LESS THAN', 'LESS THAN MAXVALUE', 'IN');
?>

<table id="partition_table">
    <tr class="vmiddle">
        <td><label for="partition_by"><?= __('Partition by:'); ?></label></td>
        <td>
            <select name="partition_by" id="partition_by">
                <?php foreach ($partitionOptions as $option): ?>
                    <option value="<?= $option?>"
                        <?php if ($partitionDetails['partition_by'] == $option): ?>
                            selected="selected"
                        <?php endif; ?>
                    >
                        <?= $option; ?>
                    </option>
                <?php endforeach; ?>
            </select>
        </td>
        <td>(
            <input name="partition_expr" type="text"
                   placeholder="<?= __('Expression or column list'); ?>"
                   value="<?= htmlspecialchars($partitionDetails['partition_expr']); ?>"/>
        )</td>
    </tr>
    <tr class="vmiddle">
        <td><label for="partition_count"><?= __('Partitions:'); ?></label></td>
        <td>
            <input name="partition_count" type="number" min="2"
                   value="<?= htmlspecialchars($partitionDetails['partition_count']); ?>" />
        </td>
    </tr>
    <?php if ($partitionDetails['can_have_subpartitions']): ?>
        <tr class="vmiddle">
            <td><label for="subpartition_by"><?= __('Subpartition by:'); ?></label></td>
            <td>
                <select name="subpartition_by" id="subpartition_by">
                    <?php foreach ($subPartitionOptions as $option): ?>
                    <option value="<?= $option?>"
                        <?php if ($partitionDetails['subpartition_by'] == $option): ?>
                            selected="selected"
                        <?php endif; ?>
                    >
                        <?= $option; ?>
                    </option>
                <?php endforeach; ?>
                </select>
            </td>
            <td>(
                <input name="subpartition_expr" type="text"
                       placeholder="<?= __('Expression or column list'); ?>"
                       value="<?= htmlspecialchars($partitionDetails['subpartition_expr']); ?>"/>
            )</td>
        </tr>
        <tr class="vmiddle">
            <td><label for="subpartition_count"><?= __('Subpartitions:'); ?></label></td>
            <td>
                <input name="subpartition_count" type="number" min="2"
                       value="<?= htmlspecialchars($partitionDetails['subpartition_count']); ?>" />
            </td>
        </tr>
    <?php endif; ?>
</table>

<?php if ($partitionDetails['partition_count'] > 1): ?>
    <table id="partition_definition_table">
        <tr>
            <th><?= __('Partition'); ?></th>
            <?php if ($partitionDetails['value_enabled']): ?>
                <th><?= __('Values'); ?></th>
            <?php endif; ?>
            <?php if ($partitionDetails['can_have_subpartitions'] && $partitionDetails['subpartition_count'] > 1): ?>
                <th><?= __('Subpartition'); ?></th>
            <?php endif; ?>
            <th><?= __('Engine'); ?></th>
            <th><?= __('Comment'); ?></th>
            <th><?= __('Data directory'); ?></th>
            <th><?= __('Index directory'); ?></th>
            <th><?= __('Max rows'); ?></th>
            <th><?= __('Min rows'); ?></th>
            <th><?= __('Table space'); ?></th>
            <th><?= __('Node group'); ?></th>
        </tr>
        <?php $odd = true; ?>
        <?php foreach ($partitionDetails['partitions'] as $partition): ?>
            <?php $rowspan = ! empty($partition['subpartition_count']) ? ($partition['subpartition_count'] + 1) : 2; ?>
            <tr class="<?= ($odd ? 'odd' : 'even'); ?>">
                <th rowspan="<?= $rowspan; ?>" class="vmiddle">
                    <?= htmlspecialchars($partition['name']); ?>
                </th>
                <?php if ($partitionDetails['value_enabled']): ?>
                    <td rowspan="<?= $rowspan; ?>" class="vmiddle">
                        <select class="partition_value"
                                name="<?= $partition['prefix']; ?>[value_type]">
                            <?php foreach ($valueTypeOptions as $option): ?>
                                <option value="<?= $option?>"
                                    <?php if ($partition['value_type'] == $option): ?>
                                        selected="selected"
                                    <?php endif; ?>
                                >
                                    <?= $option; ?>
                                </option>
                            <?php endforeach; ?>
                        </select>
                        <input type="text" class="partition_value"
                               name="<?= $partition['prefix']; ?>[value]"
                               value="<?= $partition['value']; ?>" />
                    </td>
                <?php endif; ?>
            </tr>

            <?php
            if (isset($partition['subpartitions'])):
                $subpartitions = $partition['subpartitions'];
            else:
                $subpartitions = array($partition);
            endif;
            ?>

            <?php foreach ($subpartitions as $subpartition): ?>
                <tr class="<?= ($odd ? 'odd' : 'even'); ?>">
                    <?php if ($partitionDetails['can_have_subpartitions'] && $partitionDetails['subpartition_count'] > 1): ?>
                        <th class="vmiddle"><?= htmlspecialchars($subpartition['name']); ?></th>
                    <?php endif; ?>
                    <td>
                        <?= PMA\libraries\StorageEngine::getHtmlSelect(
                            $subpartition['prefix'] . '[engine]',
                            null,
                            $subpartition['engine'],
                            false,
                            true
                        ); ?>
                    </td>
                    <td>
                        <textarea name="<?= $subpartition['prefix']; ?>[comment]">
                            <?= htmlspecialchars($subpartition['comment']); ?>
                        </textarea>
                    </td>
                    <td>
                        <input type="text" name="<?= $subpartition['prefix']; ?>[data_directory]"
                               value="<?= htmlspecialchars($subpartition['data_directory']); ?>" />
                    </td>
                    <td>
                        <input type="text" name="<?= $subpartition['prefix']; ?>[index_directory]"
                               value="<?= htmlspecialchars($subpartition['index_directory']); ?>" />
                    </td>
                    <td>
                        <input type="number" name="<?= $subpartition['prefix']; ?>[max_rows]"
                               value="<?= htmlspecialchars($subpartition['max_rows']); ?>" />
                    </td>
                    <td>
                        <input type="number" min="0" name="<?= $subpartition['prefix']; ?>[min_rows]"
                               value="<?= htmlspecialchars($subpartition['min_rows']); ?>" />
                    </td>
                    <td>
                        <input type="text" min="0" name="<?= $subpartition['prefix']; ?>[tablespace]"
                               value="<?= htmlspecialchars($subpartition['tablespace']); ?>" />
                    </td>
                    <td>
                        <input type="text" name="<?= $subpartition['prefix']; ?>[node_group]"
                               value="<?= htmlspecialchars($subpartition['node_group']); ?>" />
                    </td>
                </tr>
            <?php endforeach; ?>
            <?php $odd = !$odd; ?>
        <?php endforeach; ?>
    </table>
<?php endif; ?>