<?php
// phpcs:ignoreFile
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' ),
        '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' ),
        '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' ),
        '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>

<style>
#yatoon-dashboard-wrap *{box-sizing:border-box;}
#yatoon-dashboard-wrap .yd-kpi-grid{display:grid;grid-template-columns:repeat(auto-fit,minmax(150px,1fr));gap:12px;margin:16px 0;}
#yatoon-dashboard-wrap .yd-kpi{background:#fff;border:1px solid #e5e7eb;border-radius:10px;padding:16px 18px;}
#yatoon-dashboard-wrap .yd-kpi-label{font-size:11px;font-weight:600;text-transform:uppercase;letter-spacing:.05em;color:#9ca3af;margin-bottom:6px;}
#yatoon-dashboard-wrap .yd-kpi-value{font-size:28px;font-weight:700;line-height:1.1;color:#111827;}
#yatoon-dashboard-wrap .yd-kpi-sub{font-size:12px;margin-top:4px;color:#6b7280;}
#yatoon-dashboard-wrap .yd-trend-up{color:#16a34a;font-weight:600;}
#yatoon-dashboard-wrap .yd-trend-down{color:#dc2626;font-weight:600;}
#yatoon-dashboard-wrap .yd-trend-flat{color:#6b7280;}
#yatoon-dashboard-wrap .yd-section-grid{display:grid;grid-template-columns:1.3fr 1fr;gap:16px;margin-top:16px;}
#yatoon-dashboard-wrap .yd-panel{background:#fff;border:1px solid #e5e7eb;border-radius:10px;overflow:hidden;}
#yatoon-dashboard-wrap .yd-panel-head{padding:14px 18px;border-bottom:1px solid #f3f4f6;display:flex;align-items:center;justify-content:space-between;}
#yatoon-dashboard-wrap .yd-panel-head h2{margin:0;font-size:14px;font-weight:600;color:#111827;}
#yatoon-dashboard-wrap .yd-panel-head a{font-size:12px;color:#6b7280;text-decoration:none;}
#yatoon-dashboard-wrap .yd-panel-head a:hover{color:#111827;}
#yatoon-dashboard-wrap table{width:100%;border-collapse:collapse;}
#yatoon-dashboard-wrap th,#yatoon-dashboard-wrap td{padding:10px 18px;text-align:left;font-size:13px;border-bottom:1px solid #f9fafb;}
#yatoon-dashboard-wrap th{background:#f9fafb;color:#6b7280;font-size:11px;text-transform:uppercase;letter-spacing:.04em;font-weight:600;}
#yatoon-dashboard-wrap tr:last-child td{border-bottom:none;}
#yatoon-dashboard-wrap .yd-sub{font-size:11px;color:#9ca3af;}
#yatoon-dashboard-wrap .yd-status-confirmed,.yd-status-ACCEPTED{color:#16a34a;font-weight:600;}
#yatoon-dashboard-wrap .yd-status-pending{color:#d97706;font-weight:600;}
#yatoon-dashboard-wrap .yd-status-no_show{color:#dc2626;font-weight:600;}
#yatoon-dashboard-wrap .yd-status-cancelled{color:#9ca3af;}
#yatoon-dashboard-wrap .yd-actions-row{display:flex;gap:8px;flex-wrap:wrap;margin:12px 0 0;}
#yatoon-dashboard-wrap .yd-chart-wrap{padding:14px 18px 18px;}
#yatoon-dashboard-wrap .yd-bars{display:flex;align-items:flex-end;gap:6px;height:80px;}
#yatoon-dashboard-wrap .yd-bar-col{flex:1;display:flex;flex-direction:column;align-items:center;gap:4px;}
#yatoon-dashboard-wrap .yd-bar{width:100%;background:#e5e7eb;border-radius:4px 4px 0 0;transition:background .2s;}
#yatoon-dashboard-wrap .yd-bar.today{background:#111827;}
#yatoon-dashboard-wrap .yd-bar-day{font-size:10px;color:#9ca3af;text-align:center;}
#yatoon-dashboard-wrap .yd-bar-val{font-size:10px;color:#6b7280;font-weight:600;}
#yatoon-dashboard-wrap .yd-pro-gate{display:flex;align-items:center;gap:12px;background:#f5f3ff;border:1px dashed #c4b5fd;border-radius:10px;padding:16px 18px;margin-top:16px;}
#yatoon-dashboard-wrap .yd-pro-badge{background:#7c3aed;color:#fff;font-size:10px;font-weight:700;padding:2px 8px;border-radius:20px;letter-spacing:.04em;white-space:nowrap;}
#yatoon-dashboard-wrap .yd-pro-text{font-size:13px;color:#4c1d95;flex:1;line-height:1.5;}
#yatoon-dashboard-wrap .yd-pro-link{font-size:13px;font-weight:600;color:#7c3aed;text-decoration:none;white-space:nowrap;}
#yatoon-dashboard-wrap .yd-noshow-warn{background:#fef2f2;border:1px solid #fecaca;border-radius:8px;padding:10px 14px;font-size:13px;color:#991b1b;display:flex;align-items:center;gap:8px;}
#yatoon-dashboard-wrap .yd-readiness{background:#fff;border:1px solid #dbeafe;border-radius:10px;margin-top:16px;overflow:hidden;}
#yatoon-dashboard-wrap .yd-readiness-head{display:flex;align-items:center;justify-content:space-between;gap:16px;padding:14px 18px;background:#eff6ff;border-bottom:1px solid #dbeafe;}
#yatoon-dashboard-wrap .yd-readiness-head h2{margin:0;font-size:14px;color:#1e3a8a;}
#yatoon-dashboard-wrap .yd-readiness-score{font-size:12px;font-weight:700;color:#1d4ed8;white-space:nowrap;}
#yatoon-dashboard-wrap .yd-check-grid{display:grid;grid-template-columns:repeat(auto-fit,minmax(180px,1fr));gap:0;}
#yatoon-dashboard-wrap .yd-check{display:flex;align-items:flex-start;gap:10px;padding:14px 18px;border-right:1px solid #f3f4f6;border-bottom:1px solid #f9fafb;text-decoration:none;color:#111827;}
#yatoon-dashboard-wrap .yd-check:hover{background:#f8fafc;}
#yatoon-dashboard-wrap .yd-check-mark{width:22px;height:22px;border-radius:999px;display:flex;align-items:center;justify-content:center;font-size:13px;font-weight:800;flex:0 0 auto;}
#yatoon-dashboard-wrap .yd-check.is-ok .yd-check-mark{background:#dcfce7;color:#166534;}
#yatoon-dashboard-wrap .yd-check:not(.is-ok) .yd-check-mark{background:#fef3c7;color:#92400e;}
#yatoon-dashboard-wrap .yd-check-title{font-size:13px;font-weight:700;}
#yatoon-dashboard-wrap .yd-check-meta{font-size:11px;color:#6b7280;margin-top:2px;}
#yatoon-dashboard-wrap .yd-official{display:flex;align-items:center;justify-content:space-between;gap:14px;background:#f8fafc;border:1px solid #e5e7eb;border-radius:10px;padding:12px 16px;margin-top:14px;}
#yatoon-dashboard-wrap .yd-official strong{display:block;font-size:13px;color:#111827;}
#yatoon-dashboard-wrap .yd-official span{display:block;font-size:12px;color:#6b7280;margin-top:2px;}
#yatoon-dashboard-wrap .yd-official-actions{display:flex;gap:8px;flex-wrap:wrap;justify-content:flex-end;}
#yatoon-dashboard-wrap .yd-command{display:grid;grid-template-columns:1fr auto;gap:16px;align-items:center;background:#111827;color:#fff;border-radius:14px;padding:18px 20px;margin:14px 0 16px;box-shadow:0 14px 34px rgba(15,23,42,.16);}
#yatoon-dashboard-wrap .yd-command-kicker{font-size:11px;font-weight:800;text-transform:uppercase;letter-spacing:.08em;color:#f9a8d4;margin-bottom:4px;}
#yatoon-dashboard-wrap .yd-command-title{font-size:20px;font-weight:800;line-height:1.2;margin:0;color:#fff!important;}
#yatoon-dashboard-wrap .yd-command-sub{font-size:13px;color:#cbd5e1;margin-top:5px;}
#yatoon-dashboard-wrap .yd-command-actions{display:flex;gap:8px;flex-wrap:wrap;justify-content:flex-end;}
#yatoon-dashboard-wrap .yd-command-actions .button{border-radius:999px;min-height:36px;color:#fff!important;border-color:#64748b!important;background:transparent!important;}
#yatoon-dashboard-wrap .yd-command-actions .button-primary{background:#ec4899!important;border-color:#ec4899!important;color:#fff!important;}
#yatoon-dashboard-wrap .yd-readiness{margin-top:18px;}
#yatoon-dashboard-wrap .yd-funnel{background:#fff;border:1px solid #dbe2ea;border-radius:10px;margin:0 0 16px;padding:15px 18px;}
#yatoon-dashboard-wrap .yd-funnel-head{display:flex;align-items:center;justify-content:space-between;gap:12px;margin-bottom:12px;}
#yatoon-dashboard-wrap .yd-funnel-head h2{font-size:14px;margin:0;color:#111827;}
#yatoon-dashboard-wrap .yd-funnel-head span{font-size:11px;color:#64748b;}
#yatoon-dashboard-wrap .yd-funnel-steps{display:grid;grid-template-columns:repeat(5,minmax(0,1fr));gap:8px;}
#yatoon-dashboard-wrap .yd-funnel-step{border-top:3px solid #cbd5e1;padding-top:8px;min-width:0;}
#yatoon-dashboard-wrap .yd-funnel-step.is-done{border-color:#16a34a;}
#yatoon-dashboard-wrap .yd-funnel-label{font-size:12px;font-weight:700;color:#334155;line-height:1.3;}
#yatoon-dashboard-wrap .yd-funnel-time{font-size:10px;color:#94a3b8;margin-top:3px;white-space:nowrap;overflow:hidden;text-overflow:ellipsis;}
@media(max-width:980px){#yatoon-dashboard-wrap .yd-section-grid{grid-template-columns:1fr;}}
@media(max-width:760px){#yatoon-dashboard-wrap .yd-command{grid-template-columns:1fr;}#yatoon-dashboard-wrap .yd-command-actions{justify-content:flex-start;}#yatoon-dashboard-wrap .yd-funnel-steps{grid-template-columns:1fr 1fr;}}
</style>

<section class="yd-command">
    <div>
        <div class="yd-command-kicker">Today Command Center</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">Open the page you need, handle the day, and get back to the salon floor.</div>
    </div>
    <div class="yd-command-actions">
        <a class="button button-primary" href="<?php echo esc_url(admin_url('admin.php?page=sbs-bookings')); ?>">Bookings</a>
        <a class="button" href="<?php echo esc_url(admin_url('admin.php?page=yatoon-calendar')); ?>">Calendar</a>
        <a class="button" href="<?php echo esc_url(admin_url('admin.php?page=yatoon-clients')); ?>">Clients</a>
        <?php if (!empty($booking_preview_url)) : ?>
            <a class="button" href="<?php echo esc_url($booking_preview_url); ?>" target="_blank" rel="noopener noreferrer">Preview</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">Today's Appointments</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">Today's Revenue</div>
        <div class="yd-kpi-value">$<?php echo number_format(floatval($dashboard_summary->today_revenue ?? 0), 0); ?></div>
        <div class="yd-kpi-sub">Scheduled</div>
    </div>
    <div class="yd-kpi">
        <div class="yd-kpi-label">Month Revenue</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 $rev_diff; ?>% vs last month</span>
            <?php elseif ($rev_diff < 0) : ?>
                <span class="yd-trend-down"><?php echo $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">No-show Rate</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 $noshow_rate; ?>%</div>
        <div class="yd-kpi-sub"><?php echo $noshow_count; ?> of <?php echo $noshow_total; ?> this month</div>
    </div>
</div>

<?php if ($noshow_rate >= 15 && !$is_pro) : ?>
<div class="yd-noshow-warn">
    <strong>&#9888;</strong>
    <span><?php 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>Last 7 Days</h2>
        <a href="<?php echo esc_url(admin_url('admin.php?page=yatoon-reports')); ?>">Full reports &rarr;</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 ? $bk : ''; ?></div>
                <div class="yd-bar <?php echo $is_today ? 'today' : ''; ?>" style="height:<?php echo $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')); ?>">Open Bookings</a>
    <a class="button" href="<?php echo esc_url(admin_url('admin.php?page=yatoon-reports')); ?>">Reports</a>
    <a class="button" href="<?php echo esc_url(admin_url('admin.php?page=yatoon-clients')); ?>">Clients</a>
    <a class="button" href="<?php echo esc_url(admin_url('admin.php?page=sbs-notifications')); ?>">Notifications</a>
    <a class="button" href="<?php echo esc_url(admin_url('admin.php?page=yatoon-waitlist')); ?>">Waitlist</a>
</div>

<div class="yd-section-grid">
    <!-- Today's timeline -->
    <section class="yd-panel">
        <div class="yd-panel-head">
            <h2>Today's Timeline</h2>
            <a href="<?php echo esc_url(admin_url('admin.php?page=sbs-bookings')); ?>">All bookings &rarr;</a>
        </div>
        <table>
            <thead><tr><th>Time</th><th>Client</th><th>Service / Staff</th><th>Status</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(date('g:i A', strtotime($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>Needs Attention</h2></div>
        <table>
            <thead><tr><th>Type</th><th>Client</th><th>Action</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); ?>">Send request</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(date('M j', strtotime($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); ?>">Send recall</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>Deposit collection</strong> - Require upfront payment for high-value or repeat no-show clients.</span>
        <a class="yd-pro-link" href="<?php echo esc_url(admin_url('admin.php?page=yatoon-booking-system-pricing')); ?>">Upgrade &rarr;</a>
    </div>
    <div class="yd-pro-gate">
        <span class="yd-pro-badge">PRO</span>
        <span class="yd-pro-text"><strong>SMS reminders</strong> - Automatic text reminders slash no-show rates by up to 50%.</span>
        <a class="yd-pro-link" href="<?php echo esc_url(admin_url('admin.php?page=yatoon-booking-system-pricing')); ?>">Upgrade &rarr;</a>
    </div>
    <div class="yd-pro-gate">
        <span class="yd-pro-badge">PRO</span>
        <span class="yd-pro-text"><strong>Waitlist auto-notify</strong> - When a cancellation opens up, the next person in line is emailed instantly.</span>
        <a class="yd-pro-link" href="<?php echo esc_url(admin_url('admin.php?page=yatoon-booking-system-pricing')); ?>">Upgrade &rarr;</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 Yes' : '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 Yes' : 'Failed');
        }).fail(function(){ $btn.text('Failed'); });
    });
});
</script>
