<?php

/**
 * All the configuration page related design handle from this file
 * Key function name sapmfly_configuration
 */
if ( !defined( 'ABSPATH' ) ) {
    exit;
}
$sapmfly_config_functions = SAPMFLY_TEMPLATES . 'configuration/config-functions.php';
if ( file_exists( $sapmfly_config_functions ) ) {
    require_once $sapmfly_config_functions;
}
function sapmfly_configuration() {
    // Security Check Capabilities
    if ( !current_user_can( 'manage_options' ) ) {
        wp_die( esc_html__( 'Only the site admin can make this change. Please contact an administrator.', 'minifly' ) );
    }
    // Handle Export
    if ( isset( $_POST['sapmfly_export_settings'] ) && check_admin_referer( 'sapmfly_export_action', 'sapmfly_export_nonce' ) ) {
        sapmfly_export_function();
    }
    // Handle Import
    if ( isset( $_POST['sapmfly_import_settings'] ) && check_admin_referer( 'sapmfly_import_action', 'sapmfly_import_nonce' ) ) {
        sapmfly_import_function();
    }
    // Handle Reset
    if ( isset( $_POST['sapmfly_reset_settings'] ) && check_admin_referer( 'sapmfly_reset_action', 'sapmfly_reset_nonce' ) ) {
        if ( !empty( $_POST['sapmfly_reset_confirm_input'] ) && $_POST['sapmfly_reset_confirm_input'] === 'RESET' ) {
            sapmfly_reset_all_settings();
            echo '<div class="updated notice is-dismissible"><p>' . esc_html__( 'All settings have been reset successfully.', 'minifly' ) . '</p></div>';
        } else {
            echo '<div class="error notice is-dismissible"><p>' . esc_html__( 'Reset failed. Please try again.', 'minifly' ) . '</p></div>';
        }
    }
    ?>

    <div class="sapmfly-configuration-wrap">
        <div class="sapmfly-configuration-page sapmfly-fonts">
            <div class="sapmfly-plugin-global-headings">
                <?php 
    $sapmfly_promotional_header_template = SAPMFLY_TEMPLATES . 'global-header-design.php';
    if ( file_exists( $sapmfly_promotional_header_template ) ) {
        require_once $sapmfly_promotional_header_template;
    }
    ?>
            </div>

            <div class="sapmfly-configuration-page-body">
                <div class="sapmfly-page-heading">
                    <h1><?php 
    echo esc_html__( 'Configure Minifly Settings', 'minifly' );
    ?></h1>
                    <p><?php 
    echo esc_html__( 'Backup your settings or reset the plugin to factory defaults.', 'minifly' );
    ?></p>
                </div>

                <div class="sapmfly-configuration-settings-area">
                    <div class="sapmfly-configuration-settings-container">
                        <div class="sapmfly-configuration-export">
                            <h2><?php 
    echo esc_html__( 'Export, import and reset full plugin', 'minifly' );
    ?></h2>

                            <?php 
    ?>
                                <table class="sapmfly-configuration-settings-table">
                                    <tr class="sapmfly-reset-row">
                                        <td id="sapmfly-configuration-settings-table-free"><?php 
    esc_html_e( 'Reset All Data', 'minifly' );
    ?></td>
                                        <td>
                                            <form method="post" id="sapmfly-reset-form">
                                                <?php 
    wp_nonce_field( 'sapmfly_reset_action', 'sapmfly_reset_nonce' );
    ?>

                                                <div class="sapmfly-reset-confirm-box">
                                                    <p class="sapmfly-reset-confirm-note">
                                                        <?php 
    printf( esc_html__( 'Please type RESET below to enable the reset button', 'minifly' ) );
    ?>
                                                    </p>

                                                    <div class="sapmfly-reset-confirm-input">
                                                        <input type="text" name="sapmfly_reset_confirm_input" id="sapmfly_reset_input" placeholder="RESET" autocomplete="off">

                                                        <button class="sapmfly-reset-confirm-button sapmfly-save-btn" type="submit" name="sapmfly_reset_settings" id="sapmfly-submit-reset" disabled>
                                                            <?php 
    esc_html_e( 'Reset Full Minifly', 'minifly' );
    ?>
                                                        </button>
                                                    </div>
                                                </div>
                                                <div class="sapmfly-configuration-warning">
                                                    <?php 
    esc_html_e( 'Caution: This will delete the plugin\'s all current configurations. This cannot be undone.', 'minifly' );
    ?>
                                                </div>
                                            </form>
                                        </td>
                                    </tr>
                                </table>
                                <div class="sapmfly-pro-available-label">
                                    <a href="https://brainywp.com/minifly/pricing/" target="_blank">
                                        <p class="pro-label-href"><?php 
    echo esc_html__( 'Available in PRO plan', 'minifly' );
    ?></p>
                                        <table class="sapmfly-configuration-settings-table">
                                            <tr>
                                                <td><?php 
    esc_html_e( 'Export Configuration', 'minifly' );
    ?></td>
                                                <td>
                                                    <button type="submit" name="" class="sapmfly-save-btn sapmfly-save-btn-dead" disabled title="Upgrade to PRO to enable">
                                                        <?php 
    esc_html_e( 'Download Settings (Pro)', 'minifly' );
    ?>
                                                    </button>
                                                </td>
                                            </tr>

                                            <tr>
                                                <td><?php 
    esc_html_e( 'Import All Settings', 'minifly' );
    ?></td>
                                                <td>
                                                    <div class="sapmfly-import-wrapper">
                                                        <button type="button" id="sapmfly-import-btn-trigger" class="sapmfly-save-btn sapmfly-save-btn-dead" disabled title="Upgrade to PRO to enable">
                                                            <?php 
    esc_html_e( 'Select JSON File (Pro)', 'minifly' );
    ?>
                                                        </button>
                                                    </div>
                                                    <div class="sapmfly-configuration-warning">
                                                        <?php 
    esc_html_e( ' Caution: Upload a .json file to restore your previous settings with one click. This will override current settings.', 'minifly' );
    ?>
                                                    </div>
                                                </td>
                                            </tr>
                                        </table>
                                    </a>
                                </div>
                            <?php 
    ?>

                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

<?php 
}
