<?php
// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- Local template scope variables.
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

$this->render_page_header(
    __( 'nnax User Files - Licenses', 'nnax-frontend-user-files' ),
    __( 'Manage addon licenses centrally.', 'nnax-frontend-user-files' ),
    'licenses'
);
?>
<div class="nnax-grid nnax-grid-1">
    <?php if ( empty( $products ) ) : ?>
        <section class="nnax-card">
            <p><?php esc_html_e( 'There are currently no licensed addons registered.', 'nnax-frontend-user-files' ); ?></p>
        </section>
    <?php endif; ?>

    <?php foreach ( $products as $plugin_slug => $product ) : ?>
        <?php
        $data = $license_manager->get_product_data( $plugin_slug );
        $status_labels = array(
            'active'    => __( 'active', 'nnax-frontend-user-files' ),
            'inactive'  => __( 'inactive', 'nnax-frontend-user-files' ),
            'expired'   => __( 'expired', 'nnax-frontend-user-files' ),
            'paused'    => __( 'paused', 'nnax-frontend-user-files' ),
            'cancelled' => __( 'cancelled', 'nnax-frontend-user-files' ),
            'revoked'   => __( 'revoked', 'nnax-frontend-user-files' ),
            'invalid'   => __( 'invalid', 'nnax-frontend-user-files' ),
            ''          => __( 'unknown', 'nnax-frontend-user-files' ),
        );
        $display_status = isset( $status_labels[ $data['status'] ] ) ? $status_labels[ $data['status'] ] : $status_labels['invalid'];
        $max_activations_display = ( isset( $data['max_activations'] ) && (int) $data['max_activations'] === 0 )
            ? '∞'
            : (string) $data['max_activations'];
        ?>
        <section class="nnax-card nnax-card--primary">
            <h2><?php echo esc_html( isset( $product['label'] ) ? (string) $product['label'] : $plugin_slug ); ?></h2>
            <p><code><?php echo esc_html( $plugin_slug ); ?></code></p>

            <form method="post" class="nnaxadmin-form">
                <?php wp_nonce_field( 'nnaxfuf_license_action', 'nnaxfuf_license_nonce' ); ?>
                <input type="hidden" name="nnaxfuf_license_plugin_slug" value="<?php echo esc_attr( $plugin_slug ); ?>">

                <p>
                    <label class="nnax-label" for="nnaxfuf_license_key_<?php echo esc_attr( md5( $plugin_slug ) ); ?>"><?php esc_html_e( 'License key', 'nnax-frontend-user-files' ); ?></label>
                    <input
                        id="nnaxfuf_license_key_<?php echo esc_attr( md5( $plugin_slug ) ); ?>"
                        name="nnaxfuf_license_key"
                        type="text"
                        class="regular-text"
                        value=""
                        placeholder="<?php esc_attr_e( 'Enter new license key', 'nnax-frontend-user-files' ); ?>"
                    >
                </p>

                <p>
                    <button type="submit" class="nnax-btn nnax-btn--primary nnax-btn--small" name="nnaxfuf_license_action" value="activate"><?php esc_html_e( 'Activate license', 'nnax-frontend-user-files' ); ?></button>
                </p>
            </form>

            <table class="widefat striped">
                <tbody>
                <tr>
                    <th><?php esc_html_e( 'Status', 'nnax-frontend-user-files' ); ?></th>
                    <td><?php echo esc_html( $display_status ); ?></td>
                </tr>
                <?php if ( ! empty( $data['license_key'] ) && $data['status'] !== 'inactive' ) : ?>
                    <tr>
                        <th><?php esc_html_e( 'Stored license key', 'nnax-frontend-user-files' ); ?></th>
                        <td><code><?php echo esc_html( $license_manager->get_masked_license_key( (string) $data['license_key'] ) ); ?></code></td>
                    </tr>
                <?php endif; ?>
                <tr>
                    <th><?php esc_html_e( 'Expiry date', 'nnax-frontend-user-files' ); ?></th>
                    <td><?php echo esc_html( $data['expires_at'] !== '' ? $data['expires_at'] : '-' ); ?></td>
                </tr>
                <tr>
                    <th><?php esc_html_e( 'Activations', 'nnax-frontend-user-files' ); ?></th>
                    <td><?php echo esc_html( (string) $data['activations_used'] . ' / ' . $max_activations_display ); ?></td>
                </tr>
                <tr>
                    <th><?php esc_html_e( 'Last check', 'nnax-frontend-user-files' ); ?></th>
                    <td><?php echo esc_html( $data['last_check'] !== '' ? $data['last_check'] : '-' ); ?></td>
                </tr>
                <?php if ( ! empty( $data['last_successful_check'] ) ) : ?>
                    <tr>
                        <th><?php esc_html_e( 'Last successful check', 'nnax-frontend-user-files' ); ?></th>
                        <td><?php echo esc_html( (string) $data['last_successful_check'] ); ?></td>
                    </tr>
                <?php endif; ?>
                <?php if ( ! empty( $data['last_failed_check'] ) ) : ?>
                    <tr>
                        <th><?php esc_html_e( 'Last failed check attempt', 'nnax-frontend-user-files' ); ?></th>
                        <td><?php echo esc_html( (string) $data['last_failed_check'] ); ?></td>
                    </tr>
                <?php endif; ?>
                <?php if ( ! empty( $data['last_error_code'] ) || ! empty( $data['last_error_message'] ) ) : ?>
                    <tr>
                        <th><?php esc_html_e( 'Technical status', 'nnax-frontend-user-files' ); ?></th>
                        <td>
                            <?php if ( ! empty( $data['last_error_code'] ) ) : ?>
                                <code><?php echo esc_html( (string) $data['last_error_code'] ); ?></code>
                            <?php endif; ?>
                            <?php if ( ! empty( $data['last_error_message'] ) ) : ?>
                                <br><?php echo esc_html( (string) $data['last_error_message'] ); ?>
                            <?php endif; ?>
                        </td>
                    </tr>
                <?php endif; ?>
                <tr>
                    <th><?php esc_html_e( 'Last error message', 'nnax-frontend-user-files' ); ?></th>
                    <td><?php echo esc_html( $data['error_message'] !== '' ? $data['error_message'] : ( $data['error_code'] !== '' ? $data['error_code'] : '-' ) ); ?></td>
                </tr>
                </tbody>
            </table>

            <?php if ( ! empty( $data['license_key'] ) ) : ?>
                <form method="post" class="nnaxadmin-form" style="margin-top: 16px;">
                    <?php wp_nonce_field( 'nnaxfuf_license_action', 'nnaxfuf_license_nonce' ); ?>
                    <input type="hidden" name="nnaxfuf_license_plugin_slug" value="<?php echo esc_attr( $plugin_slug ); ?>">
                    <p>
                        <button type="submit" class="nnax-btn nnax-btn--secondary nnax-btn--small" name="nnaxfuf_license_action" value="check"><?php esc_html_e( 'Check license status again', 'nnax-frontend-user-files' ); ?></button>
                        <button type="submit" class="nnax-btn nnax-btn--secondary nnax-btn--small" name="nnaxfuf_license_action" value="deactivate"><?php esc_html_e( 'Deactivate stored license', 'nnax-frontend-user-files' ); ?></button>
                    </p>
                </form>
            <?php endif; ?>
        </section>
    <?php endforeach; ?>
</div>
<?php
$this->render_page_footer();
