<?php
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

add_action( 'admin_post_elementsell_reminder_preview', 'elementsell_handle_reminder_preview' );
add_action( 'admin_post_elementsell_reminder_age_order', 'elementsell_handle_reminder_age_order' );
add_action( 'admin_post_elementsell_reminder_send_now', 'elementsell_handle_reminder_send_now' );

/**
 * @return void
 */
function elementsell_handle_reminder_preview() {
    if ( ! current_user_can( 'manage_options' ) ) {
        wp_die( 'Nemáte oprávnění.' );
    }

    $order_id = isset( $_GET['order_id'] ) ? (int) $_GET['order_id'] : 0;

    check_admin_referer( 'elementsell_reminder_preview_' . $order_id );

    $order = elementsell_get_order( $order_id );
    $send  = elementsell_send_order_reminder_preview( $order );

    $args = [ 'page' => 'elementsell' ];

    if ( is_wp_error( $send ) ) {
        $args['es_reminder_error'] = rawurlencode( $send->get_error_message() );
    } else {
        $args['es_reminder_preview'] = 1;
    }

    wp_safe_redirect( add_query_arg( $args, admin_url( 'admin.php' ) ) );
    exit;
}

/**
 * @return void
 */
function elementsell_handle_reminder_age_order() {
    if ( ! current_user_can( 'manage_options' ) ) {
        wp_die( 'Nemáte oprávnění.' );
    }

    $order_id = isset( $_POST['order_id'] ) ? (int) $_POST['order_id'] : 0;

    check_admin_referer( 'elementsell_reminder_age_' . $order_id );

    $days   = isset( $_POST['days'] ) ? (int) $_POST['days'] : 1;
    $result = elementsell_shift_order_created_date( $order_id, $days );

    $args = [ 'page' => 'elementsell' ];

    if ( is_wp_error( $result ) ) {
        $args['es_reminder_error'] = rawurlencode( $result->get_error_message() );
    } else {
        $args['es_reminder_aged'] = $days;
    }

    wp_safe_redirect( add_query_arg( $args, admin_url( 'admin.php' ) ) );
    exit;
}

/**
 * @return void
 */
function elementsell_handle_reminder_send_now() {
    if ( ! current_user_can( 'manage_options' ) ) {
        wp_die( 'Nemáte oprávnění.' );
    }

    $order_id = isset( $_POST['order_id'] ) ? (int) $_POST['order_id'] : 0;

    check_admin_referer( 'elementsell_reminder_send_' . $order_id );

    $order = elementsell_get_order( $order_id );
    $send  = elementsell_send_order_reminder( $order, true );

    $args = [ 'page' => 'elementsell' ];

    if ( is_wp_error( $send ) ) {
        $args['es_reminder_error'] = rawurlencode( $send->get_error_message() );
    } else {
        $args['es_reminder_sent'] = 1;
    }

    wp_safe_redirect( add_query_arg( $args, admin_url( 'admin.php' ) ) );
    exit;
}

/**
 * @return void
 */
function elementsell_render_order_reminder_notices() {
    if ( isset( $_GET['es_reminder_preview'] ) ) {
        echo '<div class="notice notice-success is-dismissible"><p>Náhled upomínky byl odeslán na váš e-mail správce (s předmětem <strong>[Náhled]</strong>). Nepočítá se do limitu 3 upomínek.</p></div>';
    }

    if ( isset( $_GET['es_reminder_sent'] ) ) {
        echo '<div class="notice notice-success is-dismissible"><p>Upomínka byla odeslána zákazníkovi.</p></div>';
    }

    if ( isset( $_GET['es_reminder_aged'] ) ) {
        echo '<div class="notice notice-info is-dismissible"><p>Datum objednávky bylo posunuto o ' . (int) $_GET['es_reminder_aged'] . ' den/dnů zpět. Teď můžete spustit upomínku nebo počkat na denní cron.</p></div>';
    }

    if ( isset( $_GET['es_reminder_error'] ) ) {
        echo '<div class="notice notice-error is-dismissible"><p>' . esc_html( wp_unslash( $_GET['es_reminder_error'] ) ) . '</p></div>';
    }
}

/**
 * @param object $order
 * @return void
 */
function elementsell_render_order_reminder_actions( $order ) {
    if ( ! function_exists( 'elementsell_is_order_payment_paid' ) || ! function_exists( 'elementsell_send_order_reminder_preview' ) ) {
        echo '<span style="color:#d63638;">Aktualizujte plugin</span>';
        return;
    }

    if ( elementsell_is_order_payment_paid( $order ) ) {
        echo '—';
        return;
    }

    if ( ! elementsell_order_is_unpaid( $order ) ) {
        echo '—';
        return;
    }

    $order_id = (int) $order->id;

    $preview_url = wp_nonce_url(
        add_query_arg(
            [
                'action'   => 'elementsell_reminder_preview',
                'order_id' => $order_id,
            ],
            admin_url( 'admin-post.php' )
        ),
        'elementsell_reminder_preview_' . $order_id
    );

    echo '<div class="elementsell-reminder-actions">';
    echo '<a class="button button-small" href="' . esc_url( $preview_url ) . '">Náhled</a>';

    echo '<form method="post" action="' . esc_url( admin_url( 'admin-post.php' ) ) . '">';
    wp_nonce_field( 'elementsell_reminder_age_' . $order_id );
    echo '<input type="hidden" name="action" value="elementsell_reminder_age_order">';
    echo '<input type="hidden" name="order_id" value="' . $order_id . '">';
    echo '<input type="hidden" name="days" value="1">';
    echo '<button type="submit" class="button button-small">−1 den</button>';
    echo '</form>';

    echo '<form method="post" action="' . esc_url( admin_url( 'admin-post.php' ) ) . '" onsubmit="return confirm(\'Opravdu odeslat upomínku zákazníkovi? Počítá se do limitu 3×.\');">';
    wp_nonce_field( 'elementsell_reminder_send_' . $order_id );
    echo '<input type="hidden" name="action" value="elementsell_reminder_send_now">';
    echo '<input type="hidden" name="order_id" value="' . $order_id . '">';
    echo '<button type="submit" class="button button-primary button-small">Odeslat</button>';
    echo '</form>';
    echo '</div>';
}
