<?php

if (!defined('ABSPATH')) { exit; }

$mtd_revenue    = floatval($mtd_summary->revenue ?? 0);
$last_rev       = floatval($last_month_revenue ?? 0);
$rev_diff       = $last_rev > 0 ? round(( ( $mtd_revenue - $last_rev ) / $last_rev ) * 100) : 0;
$noshow_total   = intval($mtd_summary->total_bookings ?? 0);
$noshow_count   = intval($mtd_summary->no_show_count ?? 0);
$noshow_rate    = $noshow_total > 0 ? round(( $noshow_count / $noshow_total ) * 100) : 0;
$is_pro         = class_exists('YATOON_License') && YATOON_License::is_pro();
$onboarding_milestones = get_option( 'yatoon_onboarding_milestones', array() );
$onboarding_milestones = is_array( $onboarding_milestones ) ? $onboarding_milestones : array();
$funnel_steps = array(
    'activated'           => __( 'Activated', 'yatoon-booking-system' ),
    'wizard_completed'    => __( 'Setup complete', 'yatoon-booking-system' ),
    'booking_page_opened' => __( 'Booking page opened', 'yatoon-booking-system' ),
    'first_test_booking'  => __( 'Test booking', 'yatoon-booking-system' ),
    'first_real_booking'  => __( 'First real booking', 'yatoon-booking-system' ),
);
global $wpdb;
$yd_services_table = esc_sql( $wpdb->prefix . 'yatoon_services' );
$yd_staff_table    = esc_sql( $wpdb->prefix . 'yatoon_staff' );
$yd_service_total  = (int) yatoon_db_get_var( "SELECT COUNT(*) FROM {$yd_services_table} WHERE is_active = 1" );
$yd_staff_total    = (int) yatoon_db_get_var( "SELECT COUNT(*) FROM {$yd_staff_table} WHERE is_active = 1 AND is_bookable = 1" );
$yd_service_images = (int) yatoon_db_get_var( "SELECT COUNT(*) FROM {$yd_services_table} WHERE image_url IS NOT NULL AND image_url != ''" );
$yd_staff_photos   = (int) yatoon_db_get_var( "SELECT COUNT(*) FROM {$yd_staff_table} WHERE image_url IS NOT NULL AND image_url != ''" );
$yd_policy_ready   = get_option( 'yatoon_policy_enabled', '0' ) === '1';
$yd_email_ready    = get_option( 'yatoon_send_confirmation_email', '1' ) === '1' || get_option( 'yatoon_email_notify_admin', '1' ) === '1';
$yd_staff_portal_page_id = absint( get_option( 'yatoon_staff_portal_page_id', 0 ) );
$yd_staff_portal_ready   = $yd_staff_portal_page_id > 0 && get_post_status( $yd_staff_portal_page_id ) === 'publish';
$yd_preview_url    = ! empty( $booking_preview_url ) ? (string) $booking_preview_url : '';
$yd_preview_ready  = $yd_preview_url !== '';
$yd_launch_items   = array(
    array(
        'ok'    => $yd_preview_ready,
        'title' => __( 'Create booking page', 'yatoon-booking-system' ),
        'meta'  => $yd_preview_ready ? __( 'Public page is ready', 'yatoon-booking-system' ) : __( 'Run the setup wizard or add [yatoon_booking]', 'yatoon-booking-system' ),
        'url'   => $yd_preview_ready ? $yd_preview_url : admin_url( 'admin.php?page=yatoon-setup-wizard' ),
    ),
    array(
        'ok'    => $yd_service_total >= 3,
        'title' => __( 'Build service menu', 'yatoon-booking-system' ),
        'meta'  => sprintf( _n( '%d active service', '%d active services', $yd_service_total, 'yatoon-booking-system' ), $yd_service_total ),
        'url'   => admin_url( 'admin.php?page=sbs-services' ),
    ),
    array(
        'ok'    => $yd_service_images >= min( 3, max( 1, $yd_service_total ) ),
        'title' => __( 'Add service photos', 'yatoon-booking-system' ),
        /* translators: %d: Number of services with an image. */
        'meta'  => sprintf( _n( '%d service has an image', '%d services have images', $yd_service_images, 'yatoon-booking-system' ), $yd_service_images ),
        'url'   => admin_url( 'admin.php?page=sbs-services' ),
    ),
    array(
        'ok'    => $yd_staff_total >= 1,
        'title' => __( 'Show bookable staff', 'yatoon-booking-system' ),
        /* translators: %d: Number of staff members available online. */
        'meta'  => sprintf( _n( '%d staff online', '%d staff online', $yd_staff_total, 'yatoon-booking-system' ), $yd_staff_total ),
        'url'   => admin_url( 'admin.php?page=sbs-staff' ),
    ),
    array(
        'ok'    => $yd_email_ready,
        'title' => __( 'Confirm notifications', 'yatoon-booking-system' ),
        'meta'  => $yd_email_ready ? __( 'Email confirmations are enabled', 'yatoon-booking-system' ) : __( 'Turn on customer or admin emails', 'yatoon-booking-system' ),
        'url'   => admin_url( 'admin.php?page=sbs-notifications' ),
    ),
    array(
        'ok'    => $yd_staff_portal_ready,
        'title' => __( 'Create staff portal', 'yatoon-booking-system' ),
        'meta'  => $yd_staff_portal_ready ? __( 'Staff app page is ready', 'yatoon-booking-system' ) : __( 'Optional, useful for phones on the salon floor', 'yatoon-booking-system' ),
        'url'   => admin_url( 'admin.php?page=yatoon-booking-system&tab=general' ),
    ),
    array(
        'ok'    => $yd_staff_total > 0 && $yd_staff_photos >= min( $yd_staff_total, 2 ),
        'title' => __( 'Add staff photos', 'yatoon-booking-system' ),
        /* translators: %d: Number of staff photos. */
        'meta'  => sprintf( _n( '%d staff photo', '%d staff photos', $yd_staff_photos, 'yatoon-booking-system' ), $yd_staff_photos ),
        'url'   => admin_url( 'admin.php?page=sbs-staff' ),
    ),
    array(
        'ok'    => $yd_policy_ready,
        'title' => __( 'Review booking policy', 'yatoon-booking-system' ),
        'meta'  => $yd_policy_ready ? __( 'Cancellation policy enabled', 'yatoon-booking-system' ) : __( 'Optional, but recommended before launch', 'yatoon-booking-system' ),
        'url'   => admin_url( 'admin.php?page=yatoon-booking-system' ),
    ),
    array(
        'ok'    => ! empty( $onboarding_milestones['first_test_booking'] ) || ! empty( $onboarding_milestones['first_real_booking'] ),
        'title' => __( 'Place a test booking', 'yatoon-booking-system' ),
        'meta'  => ! empty( $onboarding_milestones['first_test_booking'] ) || ! empty( $onboarding_milestones['first_real_booking'] ) ? __( 'Booking flow has been tested', 'yatoon-booking-system' ) : __( 'Open the customer page and book once', 'yatoon-booking-system' ),
        'url'   => $yd_preview_ready ? $yd_preview_url : admin_url( 'admin.php?page=yatoon-setup-wizard' ),
    ),
);
$yd_ready_count = 0;
foreach ( $yd_launch_items as $yd_launch_item ) {
    if ( ! empty( $yd_launch_item['ok'] ) ) {
        ++$yd_ready_count;
    }
}
?>
<div class="wrap yatoon-admin-page" id="yatoon-dashboard-wrap">
<h1><?php esc_html_e('Dashboard', 'yatoon-booking-system'); ?></h1>
<p class="yatoon-admin-subtitle"><?php esc_html_e("Today's schedule, this month's performance, and action items.", 'yatoon-booking-system'); ?></p>


<section class="yd-command">
    <div>
        <div class="yd-command-kicker"><?php esc_html_e( 'Today Command Center', 'yatoon-booking-system' ); ?></div>
        <h2 class="yd-command-title"><?php echo intval($dashboard_summary->today_active ?? 0); ?> confirmed visits, <?php echo intval($dashboard_summary->today_unpaid ?? 0); ?> unpaid</h2>
        <div class="yd-command-sub"><?php esc_html_e( 'Open the page you need, handle the day, and get back to the salon floor.', 'yatoon-booking-system' ); ?></div>
    </div>
    <div class="yd-command-actions">
        <a class="button button-primary" href="<?php echo esc_url(admin_url('admin.php?page=sbs-bookings')); ?>"><?php esc_html_e( 'Bookings', 'yatoon-booking-system' ); ?></a>
        <a class="button" href="<?php echo esc_url(admin_url('admin.php?page=yatoon-calendar')); ?>"><?php esc_html_e( 'Calendar', 'yatoon-booking-system' ); ?></a>
        <a class="button" href="<?php echo esc_url(admin_url('admin.php?page=yatoon-clients')); ?>"><?php esc_html_e( 'Clients', 'yatoon-booking-system' ); ?></a>
        <?php if (!empty($booking_preview_url)) : ?>
            <a class="button" href="<?php echo esc_url($booking_preview_url); ?>" target="_blank" rel="noopener noreferrer"><?php esc_html_e( 'Preview', 'yatoon-booking-system' ); ?></a>
        <?php endif; ?>
    </div>
</section>

<section class="yd-funnel" aria-label="<?php esc_attr_e( 'Activation funnel', 'yatoon-booking-system' ); ?>">
    <div class="yd-funnel-head"><h2><?php esc_html_e( 'Activation funnel', 'yatoon-booking-system' ); ?></h2><span><?php esc_html_e( 'Stored locally; no customer data is included.', 'yatoon-booking-system' ); ?></span></div>
    <div class="yd-funnel-steps">
        <?php foreach ( $funnel_steps as $funnel_key => $funnel_label ) : ?>
            <?php $funnel_time = isset( $onboarding_milestones[ $funnel_key ] ) ? (string) $onboarding_milestones[ $funnel_key ] : ''; ?>
            <div class="yd-funnel-step <?php echo $funnel_time ? 'is-done' : ''; ?>">
                <div class="yd-funnel-label"><?php echo esc_html( $funnel_label ); ?></div>
                <div class="yd-funnel-time"><?php echo esc_html( $funnel_time ? $funnel_time : __( 'Not reached', 'yatoon-booking-system' ) ); ?></div>
            </div>
        <?php endforeach; ?>
    </div>
</section>

<section class="yd-readiness" aria-label="<?php esc_attr_e( 'Launch checklist', 'yatoon-booking-system' ); ?>">
    <div class="yd-readiness-head">
        <h2><?php esc_html_e( 'Launch checklist', 'yatoon-booking-system' ); ?></h2>
        <span class="yd-readiness-score"><?php echo esc_html( $yd_ready_count . ' / ' . count( $yd_launch_items ) ); ?></span>
    </div>
    <div class="yd-check-grid">
        <?php foreach ( $yd_launch_items as $yd_launch_item ) : ?>
            <a class="yd-check <?php echo ! empty( $yd_launch_item['ok'] ) ? 'is-ok' : ''; ?>" href="<?php echo esc_url( $yd_launch_item['url'] ); ?>" <?php echo $yd_preview_url !== '' && $yd_launch_item['url'] === $yd_preview_url ? 'target="_blank" rel="noopener noreferrer"' : ''; ?>>
                <span class="yd-check-mark"><?php echo ! empty( $yd_launch_item['ok'] ) ? '&#10003;' : '!'; ?></span>
                <span>
                    <span class="yd-check-title"><?php echo esc_html( $yd_launch_item['title'] ); ?></span>
                    <span class="yd-check-meta"><?php echo esc_html( $yd_launch_item['meta'] ); ?></span>
                </span>
            </a>
        <?php endforeach; ?>
    </div>
</section>

<!-- KPI cards -->
<div class="yd-kpi-grid">
    <div class="yd-kpi">
        <div class="yd-kpi-label"><?php esc_html_e( 'Today\'s Appointments', 'yatoon-booking-system' ); ?></div>
        <div class="yd-kpi-value"><?php echo intval($dashboard_summary->today_bookings ?? 0); ?></div>
        <div class="yd-kpi-sub"><?php echo intval($dashboard_summary->today_active ?? 0); ?> confirmed &middot; <?php echo intval($dashboard_summary->today_unpaid ?? 0); ?> unpaid</div>
    </div>
    <div class="yd-kpi">
        <div class="yd-kpi-label"><?php esc_html_e( 'Today\'s Revenue', 'yatoon-booking-system' ); ?></div>
        <div class="yd-kpi-value">$<?php echo number_format(floatval($dashboard_summary->today_revenue ?? 0), 0); ?></div>
        <div class="yd-kpi-sub"><?php esc_html_e( 'Scheduled', 'yatoon-booking-system' ); ?></div>
    </div>
    <div class="yd-kpi">
        <div class="yd-kpi-label"><?php esc_html_e( 'Month Revenue', 'yatoon-booking-system' ); ?></div>
        <div class="yd-kpi-value">$<?php echo number_format($mtd_revenue, 0); ?></div>
        <div class="yd-kpi-sub">
            <?php if ($rev_diff > 0) : ?>
                <span class="yd-trend-up">+<?php echo esc_html( $rev_diff ); ?>% vs last month</span>
            <?php elseif ($rev_diff < 0) : ?>
                <span class="yd-trend-down"><?php echo esc_html( $rev_diff ); ?>% vs last month</span>
            <?php else : ?>
                <span class="yd-trend-flat">vs last month $<?php echo number_format($last_rev, 0); ?></span>
            <?php endif; ?>
        </div>
    </div>
    <div class="yd-kpi">
        <div class="yd-kpi-label"><?php esc_html_e( 'No-show Rate', 'yatoon-booking-system' ); ?></div>
        <div class="yd-kpi-value <?php echo esc_attr($noshow_rate >= 15 ? 'yd-risk-high' : ( $noshow_rate >= 8 ? 'yd-risk-mid' : 'yd-risk-low' )); ?>"><?php echo esc_html( $noshow_rate ); ?>%</div>
        <div class="yd-kpi-sub"><?php echo esc_html( $noshow_count ); ?> of <?php echo esc_html( $noshow_total ); ?> this month</div>
    </div>
</div>

<?php if ($noshow_rate >= 15 && !$is_pro) : ?>
<div class="yd-noshow-warn">
    <strong>&#9888;</strong>
    <span><?php /* translators: %s: Monthly no-show percentage. */ printf(esc_html__('Your no-show rate is %s%% this month. Pro lets you require deposits from repeat offenders automatically.', 'yatoon-booking-system'), esc_html($noshow_rate)); ?> <a href="<?php echo esc_url(admin_url('admin.php?page=yatoon-booking-system-pricing')); ?>"><?php esc_html_e('Upgrade', 'yatoon-booking-system'); ?> &rarr;</a></span>
</div>
<?php endif; ?>

<!-- 7-day trend chart -->
<div class="yd-panel yd-panel-spaced">
    <div class="yd-panel-head">
        <h2><?php esc_html_e( 'Last 7 Days', 'yatoon-booking-system' ); ?></h2>
        <a href="<?php echo esc_url(admin_url('admin.php?page=yatoon-reports')); ?>"><?php esc_html_e( 'Full reports &rarr;', 'yatoon-booking-system' ); ?></a>
    </div>
    <div class="yd-chart-wrap">
        <?php
        // Build a day-indexed lookup
        $trend_by_day = array();
        foreach ((array) $weekly_trend as $row) {
            $trend_by_day[ $row->day ] = $row;
        }
        $max_rev = 1;
        foreach ($trend_by_day as $r) { if (floatval($r->revenue) > $max_rev) { $max_rev = floatval($r->revenue);
        } }
        ?>
        <div class="yd-bars">
            <?php for ($i = 6; $i >= 0; $i--) :
                $day_str  = gmdate('Y-m-d', strtotime("-{$i} days", current_time('timestamp')));
                $is_today = ( $i === 0 );
                $row      = $trend_by_day[ $day_str ] ?? null;
                $rev      = $row ? floatval($row->revenue) : 0;
                $bk       = $row ? intval($row->bookings) : 0;
                $pct      = $max_rev > 0 ? max(4, round(( $rev / $max_rev ) * 100)) : 4;
                $label    = gmdate('D', strtotime($day_str));
            ?>
            <div class="yd-bar-col">
                <div class="yd-bar-val"><?php echo $bk > 0 ? esc_html( $bk ) : ''; ?></div>
                <div class="yd-bar <?php echo $is_today ? 'today' : ''; ?>" style="height:<?php echo esc_html( $pct ); ?>%;min-height:4px;" title="<?php echo esc_attr($day_str . ': $' . number_format($rev, 0) . ', ' . $bk . ' bookings'); ?>"></div>
                <div class="yd-bar-day"><?php echo esc_html(substr($label, 0, 1)); ?></div>
            </div>
            <?php endfor; ?>
        </div>
        <p class="yd-sub yd-chart-note"><?php esc_html_e('Bar height = revenue. Dark bar = today.', 'yatoon-booking-system'); ?></p>
    </div>
</div>

<div class="yd-actions-row">
    <a class="button button-primary" href="<?php echo esc_url(admin_url('admin.php?page=sbs-bookings')); ?>"><?php esc_html_e( 'Open Bookings', 'yatoon-booking-system' ); ?></a>
    <a class="button" href="<?php echo esc_url(admin_url('admin.php?page=yatoon-reports')); ?>"><?php esc_html_e( 'Reports', 'yatoon-booking-system' ); ?></a>
    <a class="button" href="<?php echo esc_url(admin_url('admin.php?page=yatoon-clients')); ?>"><?php esc_html_e( 'Clients', 'yatoon-booking-system' ); ?></a>
    <a class="button" href="<?php echo esc_url(admin_url('admin.php?page=sbs-notifications')); ?>"><?php esc_html_e( 'Notifications', 'yatoon-booking-system' ); ?></a>
    <a class="button" href="<?php echo esc_url(admin_url('admin.php?page=sbs-waitlist')); ?>"><?php esc_html_e( 'Waitlist', 'yatoon-booking-system' ); ?></a>
</div>

<div class="yd-section-grid">
    <!-- Today's timeline -->
    <section class="yd-panel">
        <div class="yd-panel-head">
            <h2><?php esc_html_e( 'Today\'s Timeline', 'yatoon-booking-system' ); ?></h2>
            <a href="<?php echo esc_url(admin_url('admin.php?page=sbs-bookings')); ?>"><?php esc_html_e( 'All bookings &rarr;', 'yatoon-booking-system' ); ?></a>
        </div>
        <table>
            <thead><tr><th><?php esc_html_e( 'Time', 'yatoon-booking-system' ); ?></th><th><?php esc_html_e( 'Client', 'yatoon-booking-system' ); ?></th><th><?php esc_html_e( 'Service / Staff', 'yatoon-booking-system' ); ?></th><th><?php esc_html_e( 'Status', 'yatoon-booking-system' ); ?></th></tr></thead>
            <tbody>
                <?php if (empty($today_bookings)) : ?>
                    <tr><td colspan="4" class="yd-sub yd-table-empty"><?php esc_html_e('No appointments today.', 'yatoon-booking-system'); ?></td></tr>
                <?php else : ?>
                    <?php foreach ($today_bookings as $row) : ?>
                        <tr>
                            <td><strong><?php echo esc_html(yatoon_format_local('g:i A', $row->start_time)); ?></strong></td>
                            <td>
                                <?php echo esc_html($row->customer_name ?: 'Guest'); ?>
                                <div class="yd-sub"><?php echo esc_html($row->customer_phone ?: $row->customer_email ?: ''); ?></div>
                            </td>
                            <td>
                                <?php echo esc_html($row->service_name ?: 'Service'); ?>
                                <div class="yd-sub"><?php echo esc_html($row->staff_name ?: 'Any'); ?></div>
                            </td>
                            <td class="yd-status-<?php echo esc_attr($row->status); ?>"><?php echo esc_html(ucfirst(str_replace('_', ' ', $row->status))); ?></td>
                        </tr>
                    <?php endforeach; ?>
                <?php endif; ?>
            </tbody>
        </table>
    </section>

    <!-- Needs attention -->
    <section class="yd-panel">
        <div class="yd-panel-head"><h2><?php esc_html_e( 'Needs Attention', 'yatoon-booking-system' ); ?></h2></div>
        <table>
            <thead><tr><th><?php esc_html_e( 'Type', 'yatoon-booking-system' ); ?></th><th><?php esc_html_e( 'Client', 'yatoon-booking-system' ); ?></th><th><?php esc_html_e( 'Action', 'yatoon-booking-system' ); ?></th></tr></thead>
            <tbody>
                <?php if (empty($todo_unpaid) && empty($todo_recall)) : ?>
                    <tr><td colspan="3" class="yd-sub yd-table-empty"><?php esc_html_e('All clear! Nothing urgent right now.', 'yatoon-booking-system'); ?></td></tr>
                <?php endif; ?>
                <?php foreach ((array) $todo_unpaid as $row) : ?>
                    <tr>
                        <td><strong class="yd-attention-unpaid"><?php esc_html_e('Unpaid', 'yatoon-booking-system'); ?></strong><div class="yd-sub">$<?php echo number_format(floatval($row->total_price), 0); ?></div></td>
                        <td><?php echo esc_html($row->customer_name ?: 'Guest'); ?><div class="yd-sub"><?php echo esc_html($row->service_name ?: ''); ?></div></td>
                        <td><button type="button" class="button button-small yatoon-send-payment-request" data-id="<?php echo esc_attr($row->id); ?>"><?php esc_html_e( 'Send request', 'yatoon-booking-system' ); ?></button></td>
                    </tr>
                <?php endforeach; ?>
                <?php foreach ((array) $todo_recall as $row) : ?>
                    <tr>
                        <td><strong class="yd-attention-recall"><?php esc_html_e('Recall', 'yatoon-booking-system'); ?></strong><div class="yd-sub"><?php echo esc_html(yatoon_format_local('M j', $row->last_visit)); ?></div></td>
                        <td><?php echo esc_html($row->customer_name ?: 'Client'); ?><div class="yd-sub"><?php echo intval($row->visit_count); ?> visits</div></td>
                        <td><button type="button" class="button button-small yatoon-send-recall" data-name="<?php echo esc_attr($row->customer_name); ?>" data-email="<?php echo esc_attr($row->customer_email); ?>" data-phone="<?php echo esc_attr($row->customer_phone); ?>"><?php esc_html_e( 'Send recall', 'yatoon-booking-system' ); ?></button></td>
                    </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </section>
</div>

<?php if (!$is_pro) : ?>
<!-- Pro feature gates -->
<div class="yd-pro-grid">
    <div class="yd-pro-gate">
        <span class="yd-pro-badge">PRO</span>
        <span class="yd-pro-text"><strong><?php esc_html_e( 'Deposit collection', 'yatoon-booking-system' ); ?></strong> <?php esc_html_e( '- Require upfront payment for high-value or repeat no-show clients.', 'yatoon-booking-system' ); ?></span>
        <a class="yd-pro-link" href="<?php echo esc_url(admin_url('admin.php?page=yatoon-booking-system-pricing')); ?>"><?php esc_html_e( 'Upgrade &rarr;', 'yatoon-booking-system' ); ?></a>
    </div>
    <div class="yd-pro-gate">
        <span class="yd-pro-badge">PRO</span>
        <span class="yd-pro-text"><strong><?php esc_html_e( 'SMS reminders', 'yatoon-booking-system' ); ?></strong> <?php esc_html_e( '- Automatic text reminders slash no-show rates by up to 50%.', 'yatoon-booking-system' ); ?></span>
        <a class="yd-pro-link" href="<?php echo esc_url(admin_url('admin.php?page=yatoon-booking-system-pricing')); ?>"><?php esc_html_e( 'Upgrade &rarr;', 'yatoon-booking-system' ); ?></a>
    </div>
    <div class="yd-pro-gate">
        <span class="yd-pro-badge">PRO</span>
        <span class="yd-pro-text"><strong><?php esc_html_e( 'Waitlist auto-notify', 'yatoon-booking-system' ); ?></strong> <?php esc_html_e( '- When a cancellation opens up, the next person in line is emailed instantly.', 'yatoon-booking-system' ); ?></span>
        <a class="yd-pro-link" href="<?php echo esc_url(admin_url('admin.php?page=yatoon-booking-system-pricing')); ?>"><?php esc_html_e( 'Upgrade &rarr;', 'yatoon-booking-system' ); ?></a>
    </div>
</div>
<?php endif; ?>

</div><!-- /wrap -->

<script>
jQuery(function($){
    const nonce = '<?php echo esc_js(wp_create_nonce('yatoon_admin_nonce')); ?>';
    $(document).on('click', '.yatoon-send-payment-request', function(){
        const $btn = $(this).prop('disabled', true).text('Sending...');
        $.post(ajaxurl, {action:'yatoon_send_payment_request', nonce, booking_id:$btn.data('id')}, function(resp){
            $btn.text(resp.success ? 'Sent' : 'Failed');
        }).fail(function(){ $btn.text('Failed'); });
    });
    $(document).on('click', '.yatoon-send-recall', function(){
        const $btn = $(this).prop('disabled', true).text('Sending...');
        $.post(ajaxurl, {action:'yatoon_send_recall_email', nonce, name:$btn.data('name'), email:$btn.data('email'), phone:$btn.data('phone')}, function(resp){
            $btn.text(resp.success ? 'Sent' : 'Failed');
        }).fail(function(){ $btn.text('Failed'); });
    });
});
</script>
