<?php
// If this file is called directly, abort.
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}
?>
<div class="wrap">

    <?php settings_errors(); ?>

    <form method="post" action="options.php" id="tabs">

        <?php
            settings_fields( 'YTAM-logs' );
            $options = wp_parse_args( get_option( 'YTAM_logs' ), $this->default_options_logs_settings( TRUE ) );

            // Get the log file
            $current_file = $this->log_file( true );



            if( false !== $current_file ) {

                // Get upload folder
                $upload_dir 		= wp_upload_dir();
                $upload_base_dir 	= $upload_dir['basedir'];
                $dir_logs			= $upload_base_dir . '/youtube-automate/logs/';

                $log_file     = $dir_logs . $current_file;
                $file_content = file_get_contents( $log_file );
            } else {
                $file_content = esc_html__( 'No log yet', 'easy-youtube-automate' );;
            }

        ?>

        <table id="general-options" class="form-table logs">
                <tbody>
                    <tr valign="top">
                        <th scope="row">
                                <?php esc_html_e( 'Enable Logs', 'easy-youtube-automate' ); ?>
                        </th>
                        <td>
                                <input data-switch="true" type="checkbox" name="YTAM_logs[logs_enable]" id="enable_logs" value="true" <?php echo( !empty( $options['logs_enable']) && esc_html( $options['logs_enable'] ) == 'true' )? 'checked': ''; ?> />
                        </td>
                    </tr>

                    <tr valign="top" class="show_logs" <?php echo((esc_html($options['logs_enable']) != 'true') ? 'style="display:none;"': ''); ?>>
                        <th scope="row">
                                <?php esc_html_e( 'Logs history', 'easy-youtube-automate' ); ?>
                        </th>

                        <td>
                            <pre id="logs-block" class="text-white bg-dark"><?php echo wp_kses_post( $file_content ); ?></pre>
                            <?php
                                if( false !== $current_file ) {
                                    $download_file_URL = esc_url( wp_nonce_url( add_query_arg( array( 'action' => 'downloadlog' ), admin_url( 'admin.php?page=youtube-automate&module=logs' ) ), 'download_log' ) );
                                    $delete_file       = esc_url( wp_nonce_url( add_query_arg( array( 'action' => 'deletelog' ), admin_url( 'admin.php?page=youtube-automate&module=logs' ) ), 'delete_log' ) );
                            ?>

                                    <a href="<?php echo esc_url( $download_file_URL ); ?>" class="button button-primary"><?php esc_html_e( 'Download Log', 'easy-youtube-automate' ); ?></a>

                                    <a href="<?php echo esc_url( $delete_file ); ?>" class="delete-logs"><?php esc_html_e( 'Delete all logs', 'easy-youtube-automate' ); ?></a>
                            <?php } ?>

                        </td>

                    </tr>

                            

                </tbody>
        </table>

        <?php submit_button(); ?>

    </form>

</div>