<?php
if ( ! defined( 'ABSPATH' ) ) { exit; }
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
$active_platform = get_option('yatoon_active_platform', 'local');
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
$is_square_mode  = ( $active_platform === 'square' );

// Portal sign-in state, surfaced inside the Edit Staff Member editor so the
// person managing an employee can see every way that employee can sign in
// without hunting through a second settings screen.
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
$yatoon_google_login_ids = array_map( 'absint', (array) get_option( 'yatoon_staff_google_login_ids', array() ) );
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
$yatoon_otp_login_ids    = array_map( 'absint', (array) get_option( 'yatoon_staff_otp_login_ids', array() ) );
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
$yatoon_google_ready     = class_exists( 'YATOON_Google_Identity' ) && YATOON_Google_Identity::google_enabled( 'staff' );
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
$yatoon_pin_length       = in_array( (int) get_option( 'yatoon_staff_pin_length', 6 ), array( 4, 6 ), true )
    ? (int) get_option( 'yatoon_staff_pin_length', 6 )
    : 6;
?>
<div class="wrap sbs-admin-page sbs-staff-page">
    <h1>
        <?php echo esc_html(get_admin_page_title()); ?>
        <?php if ($is_square_mode) : ?>
        <button type="button" id="sbs-sync-staff" class="page-title-action">
            <?php esc_html_e( 'Sync from Square', 'yatoon-booking-system' ); ?>
        </button>
        <button type="button" id="sbs-rebuild-staff-services" class="page-title-action" style="background:#f59e0b;border-color:#d97706;color:#fff;" title="<?php echo esc_attr__( 'Re-fetch staff-service assignments from Square booking profiles', 'yatoon-booking-system' ); ?>">
                <?php esc_html_e( 'Rebuild Service Assignments', 'yatoon-booking-system' ); ?>
        </button>
        <?php endif; ?>
        <?php if ( ! empty( $yatoon_staff_at_limit ) ) : ?>
        <button type="button" id="sbs-add-staff-btn" class="page-title-action" disabled style="opacity:.45;cursor:not-allowed;" title="<?php echo esc_attr__( 'You have reached the 2-staff limit on the free plan.', 'yatoon-booking-system' ); ?>">
            <?php esc_html_e( '+ Add Staff Member', 'yatoon-booking-system' ); ?>
        </button>
        <?php else : ?>
        <button type="button" id="sbs-add-staff-btn" class="page-title-action">
            <?php esc_html_e( '+ Add Staff Member', 'yatoon-booking-system' ); ?>
        </button>
        <?php endif; ?>
    </h1>

    <div id="sbs-rebuild-result" style="display:none;margin:12px 0;padding:12px 16px;border-radius:6px;font-size:14px;"></div>

    <?php if ( ! empty( $yatoon_staff_at_limit ) ) : ?>
    <div class="notice notice-warning" style="margin:12px 0 16px;">
        <p>
            <strong><?php esc_html_e( 'Staff limit reached.', 'yatoon-booking-system' ); ?></strong>
            The free plan supports up to <?php echo esc_html( $yatoon_staff_limit ); ?> staff members.
            <a href="<?php echo esc_url( class_exists( 'YATOON_License' ) ? YATOON_License::get_upgrade_url() : YBS_UPGRADE_URL ); ?>" target="_blank" style="font-weight:600;">
                <?php esc_html_e( 'Activate Pro license for unlimited staff &rarr;', 'yatoon-booking-system' ); ?>
            </a>
        </p>
    </div>
    <?php endif; ?>

    <p class="description" style="margin-bottom:4px;">
        <?php esc_html_e( 'Toggle', 'yatoon-booking-system' ); ?> <strong><?php esc_html_e( 'Show Online', 'yatoon-booking-system' ); ?></strong> to control which staff appear in the booking form.
        <?php if ($is_square_mode) : ?>
        Sync pulls the latest settings from Square's booking profiles, but you can override here.
        <?php else : ?>
        In Local mode, you can add staff, control who appears online, and assign which services each person can perform.
        <?php endif; ?>
    </p>
    <p class="description" style="margin-bottom:16px;">
        <?php esc_html_e( 'Pro supports', 'yatoon-booking-system' ); ?> <strong><?php esc_html_e( 'unlimited staff members', 'yatoon-booking-system' ); ?></strong>.
    </p>

    <?php if ( ! empty( $yatoon_staff_is_pro ) && is_dir( YATOON_PLUGIN_DIR . 'includes/pro' ) ) : ?>
    <div id="yatoon-any-staff-assignment" style="margin:0 0 18px;padding:16px 18px;background:#fff;border:1px solid #dcdcde;border-radius:10px;max-width:900px;scroll-margin-top:90px;">
        <h2 style="margin:0 0 8px;font-size:16px;"><?php esc_html_e( 'Any Staff assignment', 'yatoon-booking-system' ); ?></h2>
        <p class="description" style="margin:0 0 10px;"><?php esc_html_e( 'When several qualified employees are free, choose how Yatoon ranks them. Availability, service qualification, location, time off and booking conflicts are always checked first.', 'yatoon-booking-system' ); ?></p>
        <label for="sbs-any-staff-strategy" style="font-weight:600;margin-right:8px;"><?php esc_html_e( 'Assignment rule', 'yatoon-booking-system' ); ?></label>
        <select id="sbs-any-staff-strategy">
            <?php foreach ( array( 'service_order' => 'Staff priority order', 'round_robin' => 'Round robin', 'least_loaded' => 'Least loaded that day', 'preferred_staff' => 'Returning client preference', 'random' => 'Random' ) as $strategy_value => $strategy_label ) : ?>
                <option value="<?php echo esc_attr( $strategy_value ); ?>" <?php selected( get_option( 'yatoon_assignment_strategy', 'service_order' ), $strategy_value ); ?>><?php echo esc_html( $strategy_label ); ?></option>
            <?php endforeach; ?>
        </select>
        <button type="button" id="sbs-save-assignment-strategy" class="button button-primary" style="margin-left:8px;"><?php esc_html_e( 'Save assignment rule', 'yatoon-booking-system' ); ?></button>
        <span id="sbs-assignment-strategy-status" style="margin-left:8px;"></span>
        <p class="description" style="margin:10px 0 0;"><strong><?php esc_html_e( 'Staff priority order', 'yatoon-booking-system' ); ?></strong> <?php esc_html_e( 'uses the Priority number in each employee profile below; smaller numbers are chosen first, then name when numbers match.', 'yatoon-booking-system' ); ?></p>
    </div>
    <?php endif; ?>

    <?php if (empty($staff)) : ?>
        <div class="notice notice-warning">
            <?php if ($is_square_mode) : ?>
            <p><?php esc_html_e( 'No staff found. Please sync with Square to import your team members.', 'yatoon-booking-system' ); ?></p>
            <?php else : ?>
            <p><?php esc_html_e( 'No staff found yet. Add your first staff member to start taking bookings.', 'yatoon-booking-system' ); ?></p>
            <?php endif; ?>
        </div>
    <?php else : ?>
        <div class="sbs-table-toolbar">
            <div class="sbs-table-search">
                <input type="search" id="sbs-staff-search" class="regular-text" autocomplete="off" placeholder="<?php echo esc_attr__( 'Search staff, email, specialty, or ID', 'yatoon-booking-system' ); ?>" autocomplete="off">
                <select id="sbs-staff-status-filter">
                    <option value=""><?php esc_html_e( 'All statuses', 'yatoon-booking-system' ); ?></option>
                    <option value="active"><?php esc_html_e( 'Active', 'yatoon-booking-system' ); ?></option>
                    <option value="inactive"><?php esc_html_e( 'Inactive', 'yatoon-booking-system' ); ?></option>
                </select>
                <select id="sbs-staff-online-filter">
                    <option value=""><?php esc_html_e( 'All visibility', 'yatoon-booking-system' ); ?></option>
                    <option value="visible"><?php esc_html_e( 'Shown online', 'yatoon-booking-system' ); ?></option>
                    <option value="hidden"><?php esc_html_e( 'Hidden online', 'yatoon-booking-system' ); ?></option>
                </select>
                <button type="button" id="sbs-staff-clear-filters" class="button"><?php esc_html_e( 'Clear', 'yatoon-booking-system' ); ?></button>
            </div>
            <div class="sbs-table-actions">
                <button type="button" id="sbs-delete-selected-staff" class="button" disabled><?php esc_html_e( 'Delete Selected', 'yatoon-booking-system' ); ?></button>
            </div>
        </div>
        <div class="sbs-admin-table-wrap">
        <table class="wp-list-table widefat fixed striped" id="sbs-staff-table">
            <thead>
                <tr>
                    <th style="width:36px;"><input type="checkbox" id="sbs-select-all-staff" aria-label="<?php echo esc_attr__( 'Select all staff', 'yatoon-booking-system' ); ?>"></th>
                    <th style="width:50px;">ID</th>
                    <th style="width:70px;"><?php esc_html_e( 'Photo', 'yatoon-booking-system' ); ?></th>
                    <th><?php esc_html_e( 'Name', 'yatoon-booking-system' ); ?></th>
                    <th style="width:80px;"><?php esc_html_e( 'Priority', 'yatoon-booking-system' ); ?></th>
                    <th><?php esc_html_e( 'Email', 'yatoon-booking-system' ); ?></th>
                    <th style="width:90px;"><?php esc_html_e( 'Status', 'yatoon-booking-system' ); ?></th>
                    <th style="width:130px;"><?php esc_html_e( 'Show Online', 'yatoon-booking-system' ); ?></th>
                    <th style="width:150px;"><?php esc_html_e( 'Services', 'yatoon-booking-system' ); ?></th>
                    <th style="width:180px;"><?php esc_html_e( 'Time Off', 'yatoon-booking-system' ); ?></th>
                    <th style="width:160px;"><?php esc_html_e( 'Portal Access', 'yatoon-booking-system' ); ?></th>
                    <th style="width:110px;"><?php esc_html_e( 'Portal PIN', 'yatoon-booking-system' ); ?></th>
                    <th style="width:150px;"><?php esc_html_e( 'Actions', 'yatoon-booking-system' ); ?></th>
                </tr>
            </thead>
            <tbody>
                <?php // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound ?>
                <?php foreach ($staff as $member) : ?>
                <?php
                global $wpdb;
                // phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
                $time_off_rules = class_exists('YATOON_Business_Hours') ? YATOON_Business_Hours::get_staff_time_off_rules($member->id) : '';
                $time_off_blocks = class_exists('YATOON_Business_Hours') ? YATOON_Business_Hours::get_staff_time_off_blocks($member->id) : array();
                // phpcs:enable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
                ?>
                <tr id="sbs-staff-row-<?php echo esc_html( $member->id ); ?>"
                    data-id="<?php echo esc_attr($member->id); ?>"
                    data-name="<?php echo esc_attr($member->name); ?>"
                    data-email="<?php echo esc_attr($member->email); ?>"
                    data-bio="<?php echo esc_attr($member->bio); ?>"
                    data-image="<?php echo esc_url($member->image_url); ?>"
                    data-color="<?php echo esc_attr($member->calendar_color ?? ''); ?>"
                    data-active="<?php echo esc_attr($member->is_active); ?>"
                    data-bookable="<?php echo esc_attr($member->is_bookable); ?>"
                    data-role="<?php echo esc_attr($member->portal_role ?? 'staff'); ?>"
                    data-permissions="<?php echo esc_attr($member->portal_permissions ?? 'view_schedule'); ?>"
                    data-timeoff="<?php echo esc_attr($time_off_rules); ?>"
                    data-status="<?php echo esc_attr($member->is_active ? 'active' : 'inactive'); ?>"
                    data-online="<?php echo esc_attr($member->is_bookable ? 'visible' : 'hidden'); ?>"
                    data-sort-order="<?php echo esc_attr( intval( $member->sort_order ?? 0 ) ); ?>"
                    data-search="<?php echo esc_attr(strtolower($member->id . ' ' . $member->name . ' ' . $member->email . ' ' . $member->bio)); ?>">
                    <td><input type="checkbox" class="sbs-staff-row-checkbox" value="<?php echo esc_attr($member->id); ?>" aria-label="Select staff <?php echo esc_attr($member->name); ?>"></td>
                    <td><?php echo esc_html($member->id); ?></td>
                    <td>
                        <?php if ($member->image_url) : ?>
                            <button type="button" class="sbs-staff-open-profile sbs-staff-avatar-btn" title="<?php echo esc_attr__( 'Edit staff profile', 'yatoon-booking-system' ); ?>">
                                <img src="<?php echo esc_url($member->image_url); ?>"
                                     style="width:48px;height:48px;border-radius:50%;object-fit:cover;">
                            </button>
                        <?php else : ?>
                            <button type="button" class="sbs-staff-open-profile sbs-staff-avatar-btn" title="<?php echo esc_attr__( 'Add staff photo', 'yatoon-booking-system' ); ?>">
                            <div style="width:48px;height:48px;border-radius:50%;background:#e5e7eb;display:flex;align-items:center;justify-content:center;font-weight:700;color:#555;">
                                <?php echo esc_html( strtoupper(substr($member->name, 0, 1)) ); ?>
                            </div>
                            </button>
                        <?php endif; ?>
                    </td>
                    <td><button type="button" class="button-link sbs-staff-open-profile sbs-staff-name-link"><strong><?php echo esc_html($member->name); ?></strong></button></td>
                    <td><button type="button" class="button-link sbs-staff-open-profile" title="<?php echo esc_attr__( 'Edit staff priority', 'yatoon-booking-system' ); ?>"><?php echo esc_html( intval( $member->sort_order ?? 0 ) ); ?></button></td>
                    <td><?php echo esc_html($member->email ?: '-'); ?></td>
                    <td>
                        <?php if ($member->is_active) : ?>
                                <span style="color:#10B981;font-weight:600;"><?php esc_html_e( 'Active', 'yatoon-booking-system' ); ?></span>
                        <?php else : ?>
                                <span style="color:#10B981;font-weight:600;"><?php esc_html_e( 'Active', 'yatoon-booking-system' ); ?></span>
                        <?php endif; ?>
                    </td>
                    <td>
                        <label class="sbs-toggle" title="<?php echo esc_html( $member->is_bookable ? 'Click to hide from booking' : 'Click to show in booking' ); ?>">
                            <input type="checkbox"
                                   class="sbs-bookable-toggle"
                                   data-id="<?php echo esc_html( $member->id ); ?>"
                                   <?php checked($member->is_bookable, 1); ?>>
                            <span class="sbs-toggle-slider"></span>
                            <span class="sbs-toggle-label"><?php echo esc_html( $member->is_bookable ? 'Visible' : 'Hidden' ); ?></span>
                        </label>
                    </td>
                    <td>
                        <?php
                        // phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
                        $service_count = $wpdb->get_var( $wpdb->prepare(
                            "SELECT COUNT(*) FROM {$wpdb->prefix}yatoon_staff_services WHERE staff_id = %d",
                            $member->id
                        ) );
                        ?>
                        <span style="color:#666;"><?php echo esc_html( $service_count ); ?> assigned</span>
                        <button type="button" class="button button-small sbs-edit-services" data-staff-id="<?php echo esc_html( $member->id ); ?>" data-staff-name="<?php echo esc_attr($member->name); ?>" style="margin-left:8px;">
                            <?php esc_html_e( 'Edit', 'yatoon-booking-system' ); ?>
                        </button>
                    </td>
                    <td>
                        <?php if (!empty($time_off_blocks)) : ?>
                            <span style="display:block;color:#374151;font-weight:600;"><?php echo esc_html( count($time_off_blocks) ); ?> blocked</span>
                            <span style="display:block;color:#6b7280;font-size:12px;"><?php echo esc_html($time_off_blocks[0]['start']->format('M j g:i A')); ?></span>
                        <?php else : ?>
                            <span style="color:#9ca3af;"><?php esc_html_e( 'None', 'yatoon-booking-system' ); ?></span>
                        <?php endif; ?>
                    </td>
                    <td>
                        <?php
                        $portal_permissions = array_filter(array_map('trim', explode(',', (string) ( $member->portal_permissions ?? 'view_schedule' ))));
                        $permission_labels  = array();
                        if (in_array('view_all_schedules', $portal_permissions, true)) {
                            $permission_labels[] = 'All schedules';
                        }
                        if (in_array('update_status', $portal_permissions, true)) {
                            $permission_labels[] = 'Status updates';
                        }
                        if (in_array('create_booking', $portal_permissions, true)) {
                            $permission_labels[] = 'Create';
                        }
                        if (in_array('edit_booking', $portal_permissions, true)) {
                            $permission_labels[] = 'Edit';
                        }
                        if (in_array('cancel_booking', $portal_permissions, true)) {
                            $permission_labels[] = 'Cancel';
                        }
                        if (in_array('view_clients', $portal_permissions, true)) {
                            $permission_labels[] = 'Clients';
                        }
                        if (in_array('manage_clients', $portal_permissions, true)) {
                            $permission_labels[] = 'Manage clients';
                        }
                        if (in_array('send_messages', $portal_permissions, true)) {
                            $permission_labels[] = 'Messages';
                        }
                        if (in_array('checkout', $portal_permissions, true)) {
                            $permission_labels[] = 'Checkout';
                        }
                        if (in_array('request_time_off', $portal_permissions, true)) {
                            $permission_labels[] = 'Time off';
                        }
                        if (in_array('approve_time_off', $portal_permissions, true)) {
                            $permission_labels[] = 'Approvals';
                        }
                        ?>
                        <span style="display:block;color:#111827;font-weight:600;"><?php echo esc_html(ucfirst($member->portal_role ?? 'staff')); ?></span>
                        <span style="display:block;color:#6b7280;font-size:12px;">
                            <?php echo esc_html($permission_labels ? implode(', ', $permission_labels) : 'View only'); ?>
                        </span>
                    </td>
                    <td style="white-space:nowrap;">
                        <?php if ( ! empty( $member->portal_pin ) ) : ?>
                                <span style="color:#16a34a;font-weight:600;font-size:13px;"><?php esc_html_e( 'PIN set', 'yatoon-booking-system' ); ?></span>
                        <?php else : ?>
                        <span style="color:#9ca3af;font-size:13px;"><?php esc_html_e( 'No PIN', 'yatoon-booking-system' ); ?></span>
                        <?php endif; ?>
                        &nbsp;
                        <button type="button" class="button button-small sbs-set-pin-btn"
                                data-id="<?php echo esc_attr($member->id); ?>"
                                data-name="<?php echo esc_attr($member->name); ?>"
                                style="font-size:11px;">
                            <?php echo empty($member->portal_pin) ? 'Set PIN' : 'Change'; ?>
                        </button>
                    </td>
                    <td>
                        <button type="button"
                                class="button button-small sbs-edit-staff-btn"
                                data-id="<?php echo esc_attr($member->id); ?>"
                                data-name="<?php echo esc_attr($member->name); ?>"
                                data-email="<?php echo esc_attr($member->email); ?>"
                                data-bio="<?php echo esc_attr($member->bio); ?>"
                                data-image="<?php echo esc_url($member->image_url); ?>"
                                data-color="<?php echo esc_attr($member->calendar_color ?? ''); ?>"
                                data-active="<?php echo esc_attr($member->is_active); ?>"
                                data-bookable="<?php echo esc_attr($member->is_bookable); ?>"
                                data-role="<?php echo esc_attr($member->portal_role ?? 'staff'); ?>"
                                data-sort-order="<?php echo esc_attr( intval( $member->sort_order ?? 0 ) ); ?>"
                                data-permissions="<?php echo esc_attr($member->portal_permissions ?? 'view_schedule'); ?>"
                                data-phone="<?php echo esc_attr( $member->phone ?? '' ); ?>"
                                data-has-pin="<?php echo empty( $member->portal_pin ) ? '0' : '1'; ?>"
                                data-google-login="<?php echo in_array( (int) $member->id, $yatoon_google_login_ids, true ) ? '1' : '0'; ?>"
                                data-otp-login="<?php echo in_array( (int) $member->id, $yatoon_otp_login_ids, true ) ? '1' : '0'; ?>"
                                data-timeoff="<?php echo esc_attr($time_off_rules); ?>">
                            <?php esc_html_e( 'Edit', 'yatoon-booking-system' ); ?>
                        </button>
                        <button type="button"
                                class="button button-small sbs-delete-staff-btn"
                                data-id="<?php echo esc_attr($member->id); ?>"
                                style="color:#dc2626;margin-left:6px;">
                            <?php esc_html_e( 'Delete', 'yatoon-booking-system' ); ?>
                        </button>
                    </td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
        </div>
    <?php endif; ?>
</div>

<div id="sbs-staff-modal" style="display:none;position:fixed;top:0;left:0;right:0;bottom:0;background:rgba(0,0,0,.5);z-index:100000;padding:clamp(12px,3vw,40px);overflow:auto;box-sizing:border-box;">
    <div class="sbs-staff-modal-card" style="width:min(960px,100%);margin:0 auto;background:#fff;border-radius:12px;max-height:90vh;overflow:auto;">
        <div style="padding:20px;border-bottom:1px solid #e5e7eb;display:flex;justify-content:space-between;align-items:center;">
            <h2 id="sbs-staff-modal-title" style="margin:0;"><?php esc_html_e( 'Add Staff Member', 'yatoon-booking-system' ); ?></h2>
            <button id="sbs-staff-modal-close" style="background:none;border:none;font-size:24px;cursor:pointer;color:#999;" type="button"><?php esc_html_e( '&times;', 'yatoon-booking-system' ); ?></button>
        </div>
        <div class="sbs-staff-modal-body" style="padding:24px;overflow-x:hidden;">
            <input type="hidden" id="sbs-staff-id" value="0">
            <table class="form-table">
                <tr><th><label for="sbs-staff-name"><?php esc_html_e( 'Name *', 'yatoon-booking-system' ); ?></label></th><td><input type="text" id="sbs-staff-name" class="regular-text"></td></tr>
                <tr><th><label for="sbs-staff-email"><?php esc_html_e( 'Email', 'yatoon-booking-system' ); ?></label></th><td><input type="email" id="sbs-staff-email" class="regular-text"></td></tr>
                <tr>
                    <th><?php esc_html_e( 'Photo', 'yatoon-booking-system' ); ?></th>
                    <td>
                        <p class="description" style="margin:0 0 8px;"><?php esc_html_e( 'Recommended: a clear square headshot, at least 600px wide. It appears in staff selection, Staff Portal identity, and client-facing booking screens.', 'yatoon-booking-system' ); ?></p>
                        <input type="hidden" id="sbs-staff-image-url" value="">
                        <div id="sbs-staff-image-preview" class="sbs-staff-photo-preview" style="display:none;">
                            <img src="" alt="" style="width:72px;height:72px;border-radius:50%;object-fit:cover;">
                        </div>
                        <button type="button" class="button" id="sbs-staff-image-upload"><?php esc_html_e( 'Upload / Choose Photo', 'yatoon-booking-system' ); ?></button>
                        <button type="button" class="button" id="sbs-staff-image-remove" style="display:none;"><?php esc_html_e( 'Remove Photo', 'yatoon-booking-system' ); ?></button>
                    </td>
                </tr>
                <tr>
                    <th><label for="sbs-staff-calendar-color"><?php esc_html_e( 'Calendar Color', 'yatoon-booking-system' ); ?></label></th>
                    <td>
                        <input type="color" id="sbs-staff-calendar-color" value="#84cc16" style="width:56px;height:36px;padding:2px;vertical-align:middle;">
                        <span class="description" style="margin-left:8px;"><?php esc_html_e( 'Used for this staff member\'s appointment blocks in Staff Portal.', 'yatoon-booking-system' ); ?></span>
                    </td>
                </tr>
                <tr><th><label for="sbs-staff-bio"><?php esc_html_e( 'Bio / Specialty', 'yatoon-booking-system' ); ?></label></th><td><textarea id="sbs-staff-bio" rows="3" class="large-text"></textarea></td></tr>
                <tr><th><label for="sbs-staff-sort-order"><?php esc_html_e( 'Priority', 'yatoon-booking-system' ); ?></label></th><td><input type="number" id="sbs-staff-sort-order" min="0" max="9999" step="1" value="" placeholder="<?php echo esc_attr__( 'Auto', 'yatoon-booking-system' ); ?>" style="width:110px;"><p class="description"><?php esc_html_e( 'Smaller numbers appear first and are preferred by the “Staff priority order” Any Staff rule. Leave blank for a new employee to place them last.', 'yatoon-booking-system' ); ?></p></td></tr>
                <tr><th><?php esc_html_e( 'Status', 'yatoon-booking-system' ); ?></th><td><label><input type="checkbox" id="sbs-staff-active" checked> <?php esc_html_e( 'Active', 'yatoon-booking-system' ); ?></label> <?php esc_html_e( '&nbsp;', 'yatoon-booking-system' ); ?> <label><input type="checkbox" id="sbs-staff-bookable" checked> <?php esc_html_e( 'Bookable', 'yatoon-booking-system' ); ?></label></td></tr>
                <tr>
                    <th><?php esc_html_e( 'Staff Portal Permissions', 'yatoon-booking-system' ); ?></th>
                    <td>
                        <label for="sbs-staff-portal-role" style="display:block;font-weight:600;margin-bottom:6px;"><?php esc_html_e( 'Role preset', 'yatoon-booking-system' ); ?></label>
                        <select id="sbs-staff-portal-role" style="min-width:220px;margin-bottom:12px;">
                            <option value="staff"><?php esc_html_e( 'Staff', 'yatoon-booking-system' ); ?></option>
                            <option value="manager"><?php esc_html_e( 'Manager', 'yatoon-booking-system' ); ?></option>
                            <option value="owner"><?php esc_html_e( 'Owner', 'yatoon-booking-system' ); ?></option>
                        </select>
                        <div class="sbs-permission-list">
                            <label><input type="checkbox" class="sbs-staff-permission" value="view_schedule" checked disabled> <?php esc_html_e( 'View own schedule', 'yatoon-booking-system' ); ?></label>
                            <label><input type="checkbox" class="sbs-staff-permission" value="view_all_schedules"> <?php esc_html_e( 'View all staff schedules', 'yatoon-booking-system' ); ?></label>
                            <label><input type="checkbox" class="sbs-staff-permission" value="update_status"> <?php esc_html_e( 'Mark arrived, no show, or completed', 'yatoon-booking-system' ); ?></label>
                            <label><input type="checkbox" class="sbs-staff-permission" value="create_booking"> <?php esc_html_e( 'Add appointments from Staff Portal', 'yatoon-booking-system' ); ?></label>
                            <label><input type="checkbox" class="sbs-staff-permission" value="edit_booking"> <?php esc_html_e( 'Edit appointment time, service, staff, and notes', 'yatoon-booking-system' ); ?></label>
                            <label><input type="checkbox" class="sbs-staff-permission" value="cancel_booking"> <?php esc_html_e( 'Cancel appointments from Staff Portal', 'yatoon-booking-system' ); ?></label>
                            <label><input type="checkbox" class="sbs-staff-permission" value="view_clients"> <?php esc_html_e( 'View client list and profiles', 'yatoon-booking-system' ); ?></label>
                            <label><input type="checkbox" class="sbs-staff-permission" value="manage_clients"> <?php esc_html_e( 'Add, edit, and delete clients', 'yatoon-booking-system' ); ?></label>
                            <label><input type="checkbox" class="sbs-staff-permission" value="send_messages"> <?php esc_html_e( 'Send SMS or WhatsApp messages', 'yatoon-booking-system' ); ?></label>
                            <label><input type="checkbox" class="sbs-staff-permission" value="checkout"> <?php esc_html_e( 'Checkout, send payment requests, and mark paid', 'yatoon-booking-system' ); ?></label>
                            <label><input type="checkbox" class="sbs-staff-permission" value="request_time_off"> <?php esc_html_e( 'Request time off', 'yatoon-booking-system' ); ?></label>
                            <label><input type="checkbox" class="sbs-staff-permission" value="approve_time_off"> <?php esc_html_e( 'Approve or reject staff time off', 'yatoon-booking-system' ); ?></label>
                        </div>
                        <p class="description"><?php esc_html_e( 'Choose a preset, then fine-tune individual permissions. Staff Portal cancel keeps appointment history; hard delete remains limited to WordPress admins.', 'yatoon-booking-system' ); ?></p>
                    </td>
                </tr>
                <tr>
                    <th><?php esc_html_e( 'Portal Sign-in', 'yatoon-booking-system' ); ?></th>
                    <td>
                        <div id="sbs-staff-portal-access" class="sbs-portal-access">
                            <div class="sbs-portal-access-row">
                                <div>
                                    <strong><?php esc_html_e( 'Portal PIN', 'yatoon-booking-system' ); ?></strong>
                                    <span id="sbs-staff-pin-state" class="sbs-pin-state"></span>
                                </div>
                                <div class="sbs-portal-access-actions">
                                    <button type="button" class="button button-small" id="sbs-staff-modal-set-pin"><?php esc_html_e( 'Set PIN', 'yatoon-booking-system' ); ?></button>
                                    <button type="button" class="button button-small sbs-danger-link" id="sbs-staff-modal-remove-pin" style="display:none;"><?php esc_html_e( 'Remove', 'yatoon-booking-system' ); ?></button>
                                </div>
                            </div>
                            <p class="description sbs-portal-hint" id="sbs-staff-pin-hint" style="display:none;"><?php esc_html_e( 'Save this employee first, then a PIN can be set.', 'yatoon-booking-system' ); ?></p>

                            <div class="sbs-portal-access-row">
                                <div>
                                    <strong><?php esc_html_e( 'PIN length', 'yatoon-booking-system' ); ?></strong>
                                    <span class="sbs-pin-state"><?php esc_html_e( 'Applies to every employee', 'yatoon-booking-system' ); ?></span>
                                </div>
                                <div class="sbs-portal-access-actions">
                                    <label class="sbs-pin-length-option"><input type="radio" name="sbs-staff-pin-length" value="4" <?php checked( $yatoon_pin_length, 4 ); ?>> <?php esc_html_e( '4 digits', 'yatoon-booking-system' ); ?></label>
                                    <label class="sbs-pin-length-option"><input type="radio" name="sbs-staff-pin-length" value="6" <?php checked( $yatoon_pin_length, 6 ); ?>> <?php esc_html_e( '6 digits', 'yatoon-booking-system' ); ?></label>
                                </div>
                            </div>
                            <p class="description" id="sbs-pin-length-warning" style="display:none;color:#b45309;"><?php esc_html_e( 'Changing the length means every existing PIN must be set again before those employees can sign in.', 'yatoon-booking-system' ); ?></p>

                            <label class="sbs-portal-toggle">
                                <input type="checkbox" id="sbs-staff-allow-google" <?php disabled( ! $yatoon_google_ready ); ?>>
                                <span>
                                    <strong><?php esc_html_e( 'Google sign-in', 'yatoon-booking-system' ); ?></strong>
                                    <span class="description sbs-portal-hint">
                                    <?php if ( $yatoon_google_ready ) : ?>
                                        <?php esc_html_e( 'Signs in with the Google account matching this employee\'s email address.', 'yatoon-booking-system' ); ?>
                                    <?php else : ?>
                                        <?php esc_html_e( 'Google sign-in is not configured yet. Set it up under Identity &amp; Sign-in first.', 'yatoon-booking-system' ); ?>
                                    <?php endif; ?>
                                    </span>
                                </span>
                            </label>

                            <label class="sbs-portal-toggle">
                                <input type="checkbox" id="sbs-staff-allow-otp">
                                <span>
                                    <strong><?php esc_html_e( 'Email or text-message code', 'yatoon-booking-system' ); ?></strong>
                                    <span class="description sbs-portal-hint" id="sbs-staff-otp-hint"><?php esc_html_e( 'Sends a one-time code to this employee\'s email address or phone number.', 'yatoon-booking-system' ); ?></span>
                                </span>
                            </label>

                            <p class="description" style="margin:10px 0 0;">
                                <a href="<?php echo esc_url( admin_url( 'admin.php?page=yatoon-identity' ) ); ?>" target="_blank" rel="noopener"><?php esc_html_e( 'Configure Google, Apple, and Facebook credentials', 'yatoon-booking-system' ); ?> &rarr;</a>
                            </p>
                        </div>
                    </td>
                </tr>
                <tr>
                    <th><?php esc_html_e( 'Blocked Times', 'yatoon-booking-system' ); ?></th>
                    <td>
                        <input type="hidden" id="sbs-staff-timeoff" value="">
                        <div id="sbs-staff-timeoff-rows" class="sbs-timeoff-editor"></div>
                        <button type="button" class="button" id="sbs-add-timeoff-row"><?php esc_html_e( '+ Add blocked time', 'yatoon-booking-system' ); ?></button>
                        <p class="description"><?php esc_html_e( 'Choose a date and time range when this staff member is unavailable. Use All day for vacations or full-day closures.', 'yatoon-booking-system' ); ?></p>
                    </td>
                </tr>
            </table>
        </div>
        <div style="padding:20px;border-top:1px solid #e5e7eb;text-align:right;">
            <button type="button" class="button" id="sbs-staff-modal-cancel"><?php esc_html_e( 'Cancel', 'yatoon-booking-system' ); ?></button>
            <button type="button" class="button button-primary" id="sbs-staff-modal-save" style="margin-left:8px;"><?php esc_html_e( 'Save', 'yatoon-booking-system' ); ?></button>
            <span id="sbs-staff-save-status" style="margin-left:10px;"></span>
        </div>
    </div>
</div>

<div id="sbs-edit-services-modal" style="display:none; position:fixed; top:0; left:0; right:0; bottom:0; background:rgba(0,0,0,0.5); z-index:100000; padding:40px;">
    <div style="max-width:600px; margin:0 auto; background:#fff; border-radius:8px; max-height:90vh; overflow:auto;">
        <div style="padding:20px; border-bottom:1px solid #ddd;">
            <h2 style="margin:0;"><?php esc_html_e( 'Edit Services for', 'yatoon-booking-system' ); ?> <span id="sbs-modal-staff-name"></span></h2>
        </div>
        <div style="padding:20px;">
            <div style="display:flex; align-items:center; justify-content:space-between; margin-bottom:12px;">
                <p style="color:#666; margin:0;"><?php esc_html_e( 'Select which services this staff member can perform:', 'yatoon-booking-system' ); ?></p>
                <label style="display:flex; align-items:center; gap:6px; cursor:pointer; font-size:13px; font-weight:600; color:#374151; white-space:nowrap; user-select:none;">
                    <input type="checkbox" id="sbs-select-all-services" style="width:16px;height:16px;accent-color:#ec4899;cursor:pointer;">
                    <?php esc_html_e( 'Select All', 'yatoon-booking-system' ); ?>
                </label>
            </div>
            <div id="sbs-services-list"></div>
        </div>
        <div style="padding:20px; border-top:1px solid #ddd; text-align:right;">
            <button type="button" class="button" id="sbs-modal-cancel"><?php esc_html_e( 'Cancel', 'yatoon-booking-system' ); ?></button>
            <button type="button" class="button button-primary" id="sbs-modal-save" style="margin-left:8px;"><?php esc_html_e( 'Save', 'yatoon-booking-system' ); ?></button>
        </div>
    </div>
</div>



