<?php

if (!defined('ABSPATH')) { exit; }
global $wpdb;

$is_pro = class_exists('YATOON_License') && YATOON_License::is_pro();
$table_r = $wpdb->prefix . 'yatoon_recurring';
$table_b = $wpdb->prefix . 'yatoon_bookings';
$table_s = $wpdb->prefix . 'yatoon_services';

$series_list = $wpdb->get_results(
    "SELECT r.series_id,
            r.customer_name, r.customer_email, r.customer_phone,
            r.frequency_weeks, r.total_count,
            MIN(b.start_time) AS first_date,
            MAX(b.start_time) AS last_date,
            COUNT(r.id) AS occurrence_count,
            SUM(CASE WHEN r.status='active' THEN 1 ELSE 0 END) AS active_count,
            s.name AS service_name
     FROM $table_r r
     LEFT JOIN $table_b b ON b.id = r.booking_id
     LEFT JOIN $table_s s ON s.id = r.service_id
     GROUP BY r.series_id
     ORDER BY first_date DESC
     LIMIT 100"
);
?>
<div class="wrap" id="yatoon-recurring-wrap">
<h1 style="margin-bottom:4px;"><?php esc_html_e( 'Recurring Appointments', 'yatoon-booking-system' ); ?> <span style="font-size:18px;">*</span></h1>
<p style="color:#6b7280;margin-top:0;"><?php esc_html_e( 'Clients can set up automatic repeat bookings from the booking confirmation page. This is a', 'yatoon-booking-system' ); ?> <strong><?php esc_html_e( 'Pro', 'yatoon-booking-system' ); ?></strong> <?php esc_html_e( 'feature.', 'yatoon-booking-system' ); ?></p>

<style>
#yatoon-recurring-wrap *{box-sizing:border-box;}
#yatoon-recurring-wrap .yrr-panel{background:#fff;border:1px solid #e5e7eb;border-radius:10px;overflow:hidden;margin-top:16px;}
#yatoon-recurring-wrap table{width:100%;border-collapse:collapse;font-size:13px;}
#yatoon-recurring-wrap th{background:#f9fafb;padding:10px 16px;text-align:left;font-size:11px;font-weight:600;color:#6b7280;text-transform:uppercase;letter-spacing:.04em;border-bottom:1px solid #f3f4f6;}
#yatoon-recurring-wrap td{padding:12px 16px;border-bottom:1px solid #f9fafb;vertical-align:top;}
#yatoon-recurring-wrap tr:last-child td{border-bottom:none;}
#yatoon-recurring-wrap .yrr-sub{font-size:11px;color:#9ca3af;margin-top:2px;}
#yatoon-recurring-wrap .yrr-badge{display:inline-block;font-size:11px;padding:1px 8px;border-radius:20px;}
#yatoon-recurring-wrap .badge-active{background:#dcfce7;color:#166534;}
#yatoon-recurring-wrap .badge-done{background:#f3f4f6;color:#6b7280;}
#yatoon-recurring-wrap .yrr-pro-gate{background:#f5f3ff;border:1px dashed #c4b5fd;border-radius:10px;padding:28px;text-align:center;margin-top:16px;}
</style>

<?php if (!$is_pro): ?>
<div class="yrr-pro-gate">
    <div style="font-size:32px;margin-bottom:12px;">*</div>
    <h2 style="font-size:18px;font-weight:700;color:#111827;margin:0 0 8px;"><?php esc_html_e( 'Recurring Appointments - Pro Feature', 'yatoon-booking-system' ); ?></h2>
    <p style="color:#6b7280;font-size:14px;margin:0 0 20px;max-width:480px;margin-left:auto;margin-right:auto;">
        <?php esc_html_e( 'After booking, clients can choose to automatically repeat every 1, 2, 3, or 4 weeks. Future slots are created instantly and reminders are sent automatically.', 'yatoon-booking-system' ); ?>
    </p>
    <a href="<?php echo esc_url(admin_url('admin.php?page=yatoon-booking-system-pricing')); ?>" style="background:#7c3aed;color:#fff;padding:10px 24px;border-radius:8px;text-decoration:none;font-weight:600;font-size:14px;"><?php esc_html_e( 'Activate license', 'yatoon-booking-system' ); ?></a>
</div>
<?php else: ?>

<?php if (empty($series_list)): ?>
<div class="yrr-panel" style="padding:40px;text-align:center;color:#9ca3af;font-size:14px;">
    <?php esc_html_e( 'No recurring series yet. Clients can set up recurring bookings from the booking confirmation page.', 'yatoon-booking-system' ); ?>
</div>
<?php else: ?>
<div class="yrr-panel">
<table>
    <thead>
        <tr>
            <th><?php esc_html_e( 'Client', 'yatoon-booking-system' ); ?></th>
            <th><?php esc_html_e( 'Service', 'yatoon-booking-system' ); ?></th>
            <th><?php esc_html_e( 'Frequency', 'yatoon-booking-system' ); ?></th>
            <th><?php esc_html_e( 'Occurrences', 'yatoon-booking-system' ); ?></th>
            <th>First -> <?php esc_html_e( 'Last', 'yatoon-booking-system' ); ?></th>
            <th><?php esc_html_e( 'Status', 'yatoon-booking-system' ); ?></th>
        </tr>
    </thead>
    <tbody>
    <?php foreach ($series_list as $s):
        $active = intval($s->active_count);
        $total  = intval($s->occurrence_count);
        $status = $active > 0 ? 'active' : 'done';
    ?>
    <tr>
        <td>
            <strong><?php echo esc_html($s->customer_name ?: 'Guest'); ?></strong>
            <div class="yrr-sub"><?php echo esc_html($s->customer_email ?: $s->customer_phone); ?></div>
        </td>
        <td><?php echo esc_html($s->service_name ?: '-'); ?></td>
        <td>Every <?php echo intval($s->frequency_weeks); ?> week<?php echo $s->frequency_weeks > 1 ? 's' : ''; ?></td>
        <td><?php echo esc_html($total); ?> total · <?php echo esc_html($active); ?> upcoming</td>
        <td style="font-size:12px;">
            <?php echo $s->first_date ? esc_html(yatoon_format_local('M j, Y', $s->first_date)) : '-'; ?>
            <?php if ($s->last_date && $s->last_date !== $s->first_date): ?>
            <span style="color:#9ca3af;"> -> <?php echo esc_html(yatoon_format_local('M j, Y', $s->last_date)); ?></span>
            <?php endif; ?>
        </td>
        <td><span class="yrr-badge badge-<?php echo esc_attr($status); ?>"><?php echo $status === 'active' ? 'Active' : 'Completed'; ?></span></td>
    </tr>
    <?php endforeach; ?>
    </tbody>
</table>
</div>
<?php endif; ?>
<?php endif; ?>
</div>
