<?php defined('ABSPATH') || exit; ?>

<div id="iv1wpt-settings-schedule-result" style="margin:15px 0;"></div>
<div id="iv1wpt-settings-ftp-result" style="margin:15px 0;"></div>
<div id="iv1wpt-settings-email-result" style="margin:15px 0;"></div>

<?php
// Sub-Tab aus URL lesen, Standard: schedule
$backup_sub = sanitize_text_field($_GET['sub'] ?? 'schedule');
if (!in_array($backup_sub, ['schedule', 'ftp', 'email'], true)) {
    $backup_sub = 'schedule';
}

$sub_tabs = [
    'schedule' => __('Schedule', 'iv1-wp-tools'),
    'ftp'      => __('FTP / FTPS', 'iv1-wp-tools'),
    'email'    => __('Email', 'iv1-wp-tools'),
];
?>

<div style="margin-bottom:0px; border-bottom:1px solid #ddd; padding-bottom:0;">
    <?php foreach ($sub_tabs as $key => $label) : ?>
        <a href="?page=iv1-wp-tools&tab=backup-settings&sub=<?php echo esc_attr($key); ?>"
           style="display:inline-block; padding:8px 16px; margin-right:4px; margin-bottom:-1px; font-weight:<?php echo $backup_sub === $key ? '600' : '400'; ?>; border:1px solid <?php echo $backup_sub === $key ? '#ddd' : 'transparent'; ?>; border-bottom-color:<?php echo $backup_sub === $key ? '#fff' : 'transparent'; ?>; background:<?php echo $backup_sub === $key ? '#fff' : 'transparent'; ?>; color:#1d2327; text-decoration:none; border-radius:3px 3px 0 0;">
            <?php echo (in_array($key, ['ftp', 'email'], true) && !$iv1wpt_has_pro) ? '🔒 ' : ''; echo esc_html($label); ?>
        </a>
    <?php endforeach; ?>
</div>

<div style="background:#fff; border:1px solid #ddd; border-top:none; padding:20px; border-radius:0 0 4px 4px;">

<?php
$iv1wpt_has_pro = iv1wpt_has_pro();
?>
<?php if ($backup_sub === 'schedule') : ?>

    <h2 style="margin-top:0;"><?php esc_html_e('Automatic Backups – Schedule', 'iv1-wp-tools'); ?></h2>
    <?php wp_nonce_field('iv1wpt_schedule_nonce'); ?>
    <?php
    // Stunden-Array 0-23 aufbauen
    $hours = [];
    for ($h = 0; $h <= 23; $h++) {
        $hours[$h] = sprintf('%02d:00', $h);
    }
    $files_hour = (int) ($schedule_opts['files_hour'] ?? 3);
    $db_hour    = (int) ($schedule_opts['db_hour']    ?? 4);
    ?>
    <table class="form-table">
        <tr>
            <th><label for="files_schedule"><?php esc_html_e('File backup', 'iv1-wp-tools'); ?></label></th>
            <td style="display:flex; gap:10px; align-items:center; flex-wrap:wrap;">
                <select name="files_schedule" id="files_schedule">
                    <?php foreach ($schedules_available as $key => $label): ?>
                        <option value="<?php echo esc_attr($key); ?>" <?php selected($schedule_opts['files_schedule'], $key); ?>><?php echo esc_html($label); ?></option>
                    <?php endforeach; ?>
                </select>
                <label style="display:flex; align-items:center; gap:6px;">
                    <?php esc_html_e('at', 'iv1-wp-tools'); ?>
                    <select name="files_hour" id="files_hour">
                        <?php foreach ($hours as $h => $label): ?>
                            <option value="<?php echo esc_attr($h); ?>" <?php selected($files_hour, $h); ?>><?php echo esc_html($label); ?></option>
                        <?php endforeach; ?>
                    </select>
                </label>
                <span class="description"><?php esc_html_e('(server time)', 'iv1-wp-tools'); ?></span>
            </td>
        </tr>
        <tr>
            <th><label for="db_schedule"><?php esc_html_e('Database backup', 'iv1-wp-tools'); ?></label></th>
            <td style="display:flex; gap:10px; align-items:center; flex-wrap:wrap;">
                <select name="db_schedule" id="db_schedule">
                    <?php foreach ($schedules_available as $key => $label): ?>
                        <option value="<?php echo esc_attr($key); ?>" <?php selected($schedule_opts['db_schedule'], $key); ?>><?php echo esc_html($label); ?></option>
                    <?php endforeach; ?>
                </select>
                <label style="display:flex; align-items:center; gap:6px;">
                    <?php esc_html_e('at', 'iv1-wp-tools'); ?>
                    <select name="db_hour" id="db_hour">
                        <?php foreach ($hours as $h => $label): ?>
                            <option value="<?php echo esc_attr($h); ?>" <?php selected($db_hour, $h); ?>><?php echo esc_html($label); ?></option>
                        <?php endforeach; ?>
                    </select>
                </label>
                <span class="description"><?php esc_html_e('(server time)', 'iv1-wp-tools'); ?></span>
            </td>
        </tr>
        <tr>
            <th><label for="retention_files"><?php esc_html_e('Retention files', 'iv1-wp-tools'); ?></label></th>
            <td>
                <input type="number" name="retention_files" id="retention_files" value="<?php echo esc_attr($schedule_opts['retention_files']); ?>" min="1" max="90" style="width:80px;">
                <span class="description"><?php esc_html_e('backups to keep', 'iv1-wp-tools'); ?></span>
            </td>
        </tr>
        <tr>
            <th><label for="retention_db"><?php esc_html_e('Retention database', 'iv1-wp-tools'); ?></label></th>
            <td>
                <input type="number" name="retention_db" id="retention_db" value="<?php echo esc_attr($schedule_opts['retention_db']); ?>" min="1" max="90" style="width:80px;">
                <span class="description"><?php esc_html_e('backups to keep', 'iv1-wp-tools'); ?></span>
            </td>
        </tr>
        <tr>
            <th><label for="exclude_patterns"><?php esc_html_e('Exclude patterns', 'iv1-wp-tools'); ?></label></th>
            <td>
                <textarea name="exclude_patterns" id="exclude_patterns" rows="5" class="large-text code" style="max-width:400px;"><?php echo esc_textarea($schedule_opts['exclude_patterns']); ?></textarea>
                <p class="description"><?php esc_html_e('One pattern per line. Paths containing this pattern will be skipped.', 'iv1-wp-tools'); ?></p>
            </td>
        </tr>
    </table>
    <p class="submit" style="margin-top:8px;">
        <button type="button" id="iv1wpt_schedule_save" class="button button-primary"><?php esc_attr_e('Save schedule', 'iv1-wp-tools'); ?></button>
    </p>

<?php elseif ($backup_sub === 'ftp') : ?>
    <?php $dis = !$iv1wpt_has_pro ? 'disabled' : ''; ?>
    <h2 style="margin-top:0;"><?php esc_html_e('FTP/FTPS Upload after Backup', 'iv1-wp-tools'); ?></h2>
    <?php wp_nonce_field('iv1wpt_ftp_nonce'); ?>
    <table class="form-table">
        <tr>
            <th><label for="ftp_enabled"><?php esc_html_e('Enable upload', 'iv1-wp-tools'); ?></label></th>
            <td><input type="checkbox" name="ftp_enabled" id="ftp_enabled" value="1" <?php checked($ftp_settings['enabled']); ?> <?php echo $dis; ?>></td>
        </tr>
        <tr>
            <th><label for="ftp_host"><?php esc_html_e('Server / Host', 'iv1-wp-tools'); ?></label></th>
            <td><input type="text" name="ftp_host" id="ftp_host" value="<?php echo esc_attr($ftp_settings['host']); ?>" class="regular-text" <?php echo $dis; ?>></td>
        </tr>
        <tr>
            <th><label for="ftp_port"><?php esc_html_e('Port', 'iv1-wp-tools'); ?></label></th>
            <td><input type="number" name="ftp_port" id="ftp_port" value="<?php echo esc_attr($ftp_settings['port'] ?: 21); ?>" style="width:120px;" <?php echo $dis; ?>></td>
        </tr>
        <tr>
            <th><label for="ftp_username"><?php esc_html_e('Username', 'iv1-wp-tools'); ?></label></th>
            <td><input type="text" name="ftp_username" id="ftp_username" value="<?php echo esc_attr($ftp_settings['username']); ?>" class="regular-text" <?php echo $dis; ?>></td>
        </tr>
        <tr>
            <th><label for="ftp_password"><?php esc_html_e('Password', 'iv1-wp-tools'); ?></label></th>
            <td>
                <input type="password" name="ftp_password" id="ftp_password" class="regular-text" autocomplete="new-password" <?php echo $dis; ?>>
                <p class="description"><?php esc_html_e('Only enter if you want to change it.', 'iv1-wp-tools'); ?></p>
            </td>
        </tr>
        <tr>
            <th><label for="use_ftps"><?php esc_html_e('Use FTPS (TLS)', 'iv1-wp-tools'); ?></label></th>
            <td>
                <input type="checkbox" name="use_ftps" id="use_ftps" value="1" <?php checked($ftp_settings['use_ftps']); ?> <?php echo $dis; ?>>
                <span class="description"><?php esc_html_e('(recommended)', 'iv1-wp-tools'); ?></span>
            </td>
        </tr>
        <tr>
            <th><label for="ftp_passive"><?php esc_html_e('Passive mode', 'iv1-wp-tools'); ?></label></th>
            <td><input type="checkbox" name="ftp_passive" id="ftp_passive" value="1" <?php checked($ftp_settings['passive'], true, true); ?> <?php echo $dis; ?>></td>
        </tr>
        <tr>
            <th><label for="ftp_remote_dir"><?php esc_html_e('Remote directory', 'iv1-wp-tools'); ?></label></th>
            <td><input type="text" name="ftp_remote_dir" id="ftp_remote_dir" value="<?php echo esc_attr($ftp_settings['remote_dir']); ?>" class="regular-text" <?php echo $dis; ?>></td>
        </tr>
    </table>
    <p class="submit" style="margin-top:8px;">
        <?php if ($iv1wpt_has_pro) : ?>
        <button type="button" id="iv1wpt_ftp_save" class="button button-primary"><?php esc_attr_e('Save FTP settings', 'iv1-wp-tools'); ?></button>
        <?php else : ?>
        <button type="button" id="iv1wpt_ftp_save" class="button button-primary" disabled> 🔒 <?php esc_attr_e('Save FTP settings', 'iv1-wp-tools'); ?></button>
        <?php endif; ?>
    </p>

<?php elseif ($backup_sub === 'email') : ?>
    <?php $dis = !$iv1wpt_has_pro ? 'disabled' : ''; ?>
    <h2 style="margin-top:0;"><?php esc_html_e('Email Notification', 'iv1-wp-tools'); ?></h2>
    <?php wp_nonce_field('iv1wpt_email_nonce'); ?>
    <table class="form-table">
        <tr>
            <th><label for="email_enabled"><?php esc_html_e('Enable email', 'iv1-wp-tools'); ?></label></th>
            <td><input type="checkbox" name="email_enabled" id="email_enabled" value="1" <?php checked($email_settings['enabled']); ?> <?php echo $dis; ?>></td>
        </tr>
        <tr>
            <th><label for="email_recipient"><?php esc_html_e('Recipient', 'iv1-wp-tools'); ?></label></th>
            <td>
                <input type="email" name="email_recipient" id="email_recipient" value="<?php echo esc_attr($email_settings['recipient']); ?>" class="regular-text" placeholder="<?php echo esc_attr(get_option('admin_email')); ?>" <?php echo $dis; ?>>
                <p class="description"><?php
                    /* translators: %s: WordPress admin email address */
                    printf(esc_html__('Leave empty = WordPress admin email (%s)', 'iv1-wp-tools'), esc_html(get_option('admin_email')));
                ?></p>
            </td>
        </tr>
        <tr>
            <th><?php esc_html_e('Notify on', 'iv1-wp-tools'); ?></th>
            <td>
                <label><input type="checkbox" name="email_on_success" value="1" <?php checked($email_settings['on_success']); ?> <?php echo $dis; ?>> <?php esc_html_e('Success', 'iv1-wp-tools'); ?></label><br>
                <label><input type="checkbox" name="email_on_error" value="1" <?php checked($email_settings['on_error']); ?> <?php echo $dis; ?>> <?php esc_html_e('Error', 'iv1-wp-tools'); ?></label>
            </td>
        </tr>
    </table>
    <p class="submit" style="margin-top:8px;">
        <?php if ($iv1wpt_has_pro) : ?>
        <button type="button" id="iv1wpt_email_save" class="button button-primary"><?php esc_attr_e('Save email settings', 'iv1-wp-tools'); ?></button>
        <?php else : ?>
        <button type="button" id="iv1wpt_email_save" class="button button-primary" disabled> 🔒 <?php esc_attr_e('Save email settings', 'iv1-wp-tools'); ?></button>
        <?php endif; ?>
    </p>

<?php endif; ?>

</div>
