<?php

if (!defined('ABSPATH')) { exit; }
$rows          = (isset($rows) && is_array($rows)) ? $rows : array();
$block_enabled = !empty($block_enabled);
$count         = count($rows);
?>
<div class="aieo-dmm-card">
    <h3><?php esc_html_e('Multi-Attribute Variations', 'ai-eshop-optimizer'); ?></h3>
    <p class="description" style="max-width:820px;">
        <?php esc_html_e('Some variable products use MORE THAN ONE attribute for their variations (e.g. Colour × Size). That is correct for shops such as clothing, but for many catalogues it is an accident that multiplies into hundreds of combinations and leaves an “Any …” axis. Use the switch below to decide whether this store should block saving such products.', 'ai-eshop-optimizer'); ?>
    </p>

    <div style="margin:14px 0;padding:14px 16px;background:#f9fafb;border:1px solid #e5e7eb;border-radius:8px;max-width:820px;">
        <label style="display:flex;align-items:flex-start;gap:10px;cursor:pointer;">
            <input type="checkbox" id="aieo-mav-block" <?php checked($block_enabled); ?> style="margin-top:3px;">
            <span>
                <strong><?php esc_html_e('Block products that use more than one attribute for variations', 'ai-eshop-optimizer'); ?></strong><br>
                <span class="description"><?php esc_html_e('ON: the product editor prevents saving a variable product with more than one “Used for variations” attribute (with a non-destructive server-side backstop). Turn OFF for stores that legitimately sell on two axes — e.g. clothing (colour × size) — where the list below becomes informational only.', 'ai-eshop-optimizer'); ?></span>
            </span>
        </label>
        <p style="margin:12px 0 0;">
            <button type="button" class="button button-primary" id="aieo-mav-save"><?php esc_html_e('Save', 'ai-eshop-optimizer'); ?></button>
            <span id="aieo-mav-status" style="margin-left:10px;font-weight:600;"></span>
        </p>
    </div>

    <?php if (empty($rows)) : ?>
        <p style="padding:14px;background:#dcfce7;color:#166534;border-radius:6px;margin-top:12px;font-weight:600;">
            <?php esc_html_e('✓ Every variable product uses exactly one attribute for its variations.', 'ai-eshop-optimizer'); ?>
        </p>
    <?php else : ?>
        <p style="margin:10px 0 14px;">
            <span style="display:inline-block;border-radius:6px;padding:6px 12px;font-weight:600;<?php echo $block_enabled ? 'background:#fee2e2;color:#991b1b;' : 'background:#e0e7ff;color:#3730a3;'; ?>">
                <?php
                echo esc_html(number_format_i18n($count)) . ' ';
                echo $block_enabled
                    ? esc_html__('product(s) use multiple variation attributes — review / fix', 'ai-eshop-optimizer')
                    : esc_html__('product(s) use multiple variation attributes — allowed (informational)', 'ai-eshop-optimizer');
                ?>
            </span>
        </p>
        <table class="wp-list-table widefat striped">
            <thead><tr>
                <th><?php esc_html_e('Product', 'ai-eshop-optimizer'); ?></th>
                <th><?php esc_html_e('Attributes used for variations', 'ai-eshop-optimizer'); ?></th>
                <th style="width:60px;text-align:center;"><?php esc_html_e('Count', 'ai-eshop-optimizer'); ?></th>
                <th style="width:160px;"></th>
            </tr></thead>
            <tbody>
            <?php foreach ($rows as $r) :
                $pid  = (int) $r['id'];
                $edit = add_query_arg(array('post' => $pid, 'action' => 'edit', 'aieo_focus_attributes' => 1), admin_url('post.php'));
                $chip = $block_enabled ? 'background:#fee2e2;color:#991b1b;' : 'background:#e0e7ff;color:#3730a3;';
            ?>
                <tr>
                    <td>
                        <a href="<?php echo esc_url($edit); ?>" target="_blank" rel="noopener"><strong><?php echo esc_html($r['name']); ?></strong></a>
                        <?php if (!empty($r['sku'])) : ?><br><code style="font-size:11px;color:#6b7280;"><?php echo esc_html($r['sku']); ?></code><?php endif; ?>
                    </td>
                    <td>
                        <?php foreach ((array) $r['attrs'] as $an) : ?>
                            <span style="display:inline-block;border-radius:10px;padding:1px 9px;font-size:11px;margin:1px 3px 1px 0;<?php echo $chip; ?>"><?php echo esc_html($an); ?></span>
                        <?php endforeach; ?>
                    </td>
                    <td style="text-align:center;"><strong style="<?php echo $block_enabled ? 'color:#b91c1c;' : 'color:#4338ca;'; ?>"><?php echo esc_html((int) $r['count']); ?></strong></td>
                    <td style="white-space:nowrap;">
                        <a href="<?php echo esc_url($edit); ?>" target="_blank" rel="noopener" class="button button-small"><?php esc_html_e('Open attributes →', 'ai-eshop-optimizer'); ?></a>
                    </td>
                </tr>
            <?php endforeach; ?>
            </tbody>
        </table>
    <?php endif; ?>
</div>
<script>
(function ($) {
    $('#aieo-mav-save').on('click', function () {
        var $btn = $(this), $st = $('#aieo-mav-status'),
            block = $('#aieo-mav-block').is(':checked') ? 1 : 0;
        if (typeof aieoDmm === 'undefined' || !aieoDmm.ajaxurl) { $st.css('color', '#b91c1c').text('—'); return; }
        $btn.prop('disabled', true);
        $st.css('color', '#6b7280').text(<?php echo wp_json_encode(__('Saving…', 'ai-eshop-optimizer')); ?>);
        $.post(aieoDmm.ajaxurl, { action: 'aieo_dmm_mav_save', nonce: aieoDmm.nonce, block: block })
            .done(function (r) {
                $st.css('color', '#166534').text((r && r.data && r.data.message) ? r.data.message : <?php echo wp_json_encode(__('Saved.', 'ai-eshop-optimizer')); ?>);
            })
            .fail(function () { $st.css('color', '#b91c1c').text(<?php echo wp_json_encode(__('Save failed.', 'ai-eshop-optimizer')); ?>); })
            .always(function () { $btn.prop('disabled', false); });
    });
})(jQuery);
</script>
