<?php
/**
 * انبارداری — ویژگی‌های سراسری محصول ووکامرس (مانند Products › Attributes).
 */
if (!defined('ABSPATH')) {
    exit;
}

$wc_ok = zerti_product_attributes_available();
$can_manage = zerti_user_can_manage_product_attributes();
$attr_types = zerti_product_attribute_type_choices();
$attr_orderby = zerti_product_attribute_orderby_choices();
$zerti_ui_dir = function_exists('zerti_get_text_direction') ? zerti_get_text_direction() : 'rtl';
?>
<div class="zerti-component zerti-component-inventory zerti-component-inventory-attributes zerti-inventory-sub-slot"
     data-zerti-inv-active="attributes"
     data-zerti-inv-component="attributes">
    <?php if (!$wc_ok) : ?>
        <p class="alert alert-warning mb-0" role="status">
            <?php esc_html_e('WooCommerce must be installed and active to manage attributes.', 'zerti'); ?>
        </p>
    <?php elseif (!$can_manage) : ?>
        <p class="alert alert-warning mb-0" role="status">
            <?php esc_html_e('You do not have permission to manage product attributes.', 'zerti'); ?>
        </p>
    <?php else : ?>
        <div class="zerti-inv-categories zerti-inv-categories--flat zerti-inv-categories--attributes" data-zerti-inv-attributes="1">
            <div class="zerti-inv-categories__toolbar">
                <label class="zerti-inv-categories__search-label form-label mb-0" for="zertiInvAttributesSearch">
                    <i class="fa-solid fa-magnifying-glass zerti-inv-categories__search-icon" aria-hidden="true"></i>
                    <input
                        type="search"
                        id="zertiInvAttributesSearch"
                        class="form-control form-control-sm zerti-inv-categories__search-input"
                        autocomplete="off"
                        dir="<?php echo esc_attr($zerti_ui_dir); ?>"
                        aria-label="<?php esc_attr_e('Search attributes', 'zerti'); ?>"
                        placeholder="<?php esc_attr_e('Search items', 'zerti'); ?>"
                    >
                </label>
            </div>

            <div class="zerti-inv-categories__layout">
                <aside class="zerti-inv-categories__tree-panel card" aria-label="<?php esc_attr_e('Attributes list', 'zerti'); ?>">
                    <div class="card-body p-0 d-flex flex-column min-h-0">
                        <div class="zerti-inv-categories__tree-scroll zerti-scrollbar-thin" dir="<?php echo esc_attr($zerti_ui_dir); ?>">
                            <div class="zerti-inv-categories__tree-scroll-inner">
                                <div class="zerti-inv-categories__tree-loading d-none" role="status">
                                    <span class="spinner-border spinner-border-sm" aria-hidden="true"></span>
                                    <span><?php esc_html_e('Loading…', 'zerti'); ?></span>
                                </div>
                                <div class="zerti-inv-categories__tree-empty text-body-secondary small p-3 d-none" role="status">
                                    <?php esc_html_e('Nothing found. Click «Add attribute» to create a new item.', 'zerti'); ?>
                                </div>
                                <ul class="zerti-inv-categories__tree list-unstyled mb-0" role="list"></ul>
                            </div>
                        </div>
                    </div>
                </aside>

                <section
                    class="zerti-inv-categories__editor card"
                    aria-label="<?php esc_attr_e('Edit attribute', 'zerti'); ?>"
                    data-zerti-editor-expanded="1"
                >
                    <div class="zerti-inv-categories__editor-bar">
                        <p class="mb-0 small text-body-secondary zerti-inv-categories__editor-bar-title">
                            <?php esc_html_e('Click to create a new attribute', 'zerti'); ?>
                        </p>
                        <button type="button" class="btn btn-sm btn-primary zerti-inv-categories__add-btn">
                            <i class="fa-solid fa-plus ms-1" aria-hidden="true"></i>
                            <?php esc_html_e('Add attribute', 'zerti'); ?>
                        </button>
                        <div class="zerti-inv-categories__editor-bar-actions d-flex flex-wrap align-items-center gap-2 ms-auto">
                            <button type="button" class="btn btn-sm btn-outline-secondary zerti-inv-categories__cancel-edit-btn d-none">
                                <?php esc_html_e('Cancel editing', 'zerti'); ?>
                            </button>
                        </div>
                    </div>

                    <div class="zerti-inv-categories__editor-body">
                        <div class="zerti-inv-categories__editor-scroll" dir="<?php echo esc_attr($zerti_ui_dir); ?>">
                        <div class="zerti-inv-categories__editor-scroll-inner">
                        <div class="card-body pt-0">
                        <form class="zerti-inv-categories__form d-none" novalidate>
                            <input type="hidden" name="id" value="0" data-zerti-field="id">

                            <div class="alert alert-danger py-2 small d-none zerti-inv-categories__form-error" role="alert"></div>
                            <div class="alert alert-success py-2 small d-none zerti-inv-categories__form-success" role="status"></div>

                            <div class="row g-3">
                                <div class="col-12 col-md-6">
                                    <label class="form-label" for="zertiAttrName"><?php esc_html_e('Name', 'zerti'); ?> <span class="text-danger">*</span></label>
                                    <input type="text" class="form-control" id="zertiAttrName" name="name" data-zerti-field="name" required>
                                </div>
                                <div class="col-12 col-md-6">
                                    <label class="form-label" for="zertiAttrSlug"><?php esc_html_e('Slug', 'zerti'); ?></label>
                                    <input type="text" class="form-control" id="zertiAttrSlug" name="slug" data-zerti-field="slug" dir="ltr">
                                </div>
                                <div class="col-12 col-md-6">
                                    <label class="form-label" for="zertiAttrType"><?php esc_html_e('Type', 'zerti'); ?></label>
                                    <select class="form-select zerti-admin-select" id="zertiAttrType" name="type" data-zerti-field="type">
                                        <?php foreach ($attr_types as $value => $label) : ?>
                                            <option value="<?php echo esc_attr($value); ?>"><?php echo esc_html($label); ?></option>
                                        <?php endforeach; ?>
                                    </select>
                                </div>
                                <div class="col-12 col-md-6">
                                    <label class="form-label" for="zertiAttrOrderBy"><?php esc_html_e('Default sort order', 'zerti'); ?></label>
                                    <select class="form-select zerti-admin-select" id="zertiAttrOrderBy" name="order_by" data-zerti-field="order_by">
                                        <?php foreach ($attr_orderby as $value => $label) : ?>
                                            <option value="<?php echo esc_attr($value); ?>"><?php echo esc_html($label); ?></option>
                                        <?php endforeach; ?>
                                    </select>
                                </div>
                                <div class="col-12">
                                    <div class="form-check">
                                        <input class="form-check-input" type="checkbox" id="zertiAttrArchives" name="has_archives" data-zerti-field="has_archives" value="1">
                                        <label class="form-check-label" for="zertiAttrArchives">
                                            <?php esc_html_e('Enable archives?', 'zerti'); ?>
                                        </label>
                                    </div>
                                </div>
                            </div>

                            <div class="zerti-inv-categories__terms-panel d-none mt-4 pt-4 border-top">
                            <div class="d-flex flex-wrap align-items-center justify-content-between gap-2 mb-3">
                                <h6 class="mb-0 small fw-semibold"><?php esc_html_e('Terms', 'zerti'); ?></h6>
                                <label class="zerti-inv-categories__search-label form-label mb-0">
                                    <i class="fa-solid fa-magnifying-glass zerti-inv-categories__search-icon" aria-hidden="true"></i>
                                    <input
                                        type="search"
                                        class="form-control zerti-inv-categories__term-search-input"
                                        autocomplete="off"
                                        dir="<?php echo esc_attr($zerti_ui_dir); ?>"
                                        placeholder="<?php esc_attr_e('Search terms…', 'zerti'); ?>"
                                    >
                                </label>
                            </div>

                            <div class="alert alert-danger py-2 small d-none zerti-inv-categories__term-form-error" role="alert"></div>

                            <ul class="zerti-inv-categories__terms-list list-group list-group-flush mb-3 border rounded"></ul>
                            <p class="zerti-inv-categories__terms-empty text-body-secondary small d-none mb-3" role="status"></p>

                            <div class="zerti-inv-categories__term-form border rounded p-3" role="group">
                                <input type="hidden" data-zerti-term-field="id" value="0">
                                <p class="mb-2 zerti-inv-categories__term-form-title fw-semibold"><?php esc_html_e('New term', 'zerti'); ?></p>
                                <div class="row g-3">
                                    <div class="col-12 col-md-6">
                                        <label class="form-label"><?php esc_html_e('Name', 'zerti'); ?> <span class="text-danger">*</span></label>
                                        <input type="text" class="form-control" data-zerti-term-field="name" required>
                                    </div>
                                    <div class="col-12 col-md-6">
                                        <label class="form-label"><?php esc_html_e('Slug', 'zerti'); ?></label>
                                        <input type="text" class="form-control" data-zerti-term-field="slug" dir="ltr">
                                    </div>
                                    <div class="col-12">
                                        <label class="form-label"><?php esc_html_e('Description', 'zerti'); ?></label>
                                        <textarea class="form-control" rows="2" data-zerti-term-field="description"></textarea>
                                    </div>
                                </div>
                                <div class="d-flex flex-wrap gap-2 mt-3 justify-content-end">
                                    <button type="button" class="btn btn-sm btn-outline-secondary zerti-inv-categories__term-cancel-btn d-none">
                                        <?php esc_html_e('Cancel', 'zerti'); ?>
                                    </button>
                                    <button type="button" class="btn btn-sm btn-outline-danger zerti-inv-categories__term-delete-btn d-none">
                                        <?php esc_html_e('Delete', 'zerti'); ?>
                                    </button>
                                    <button type="button" class="btn btn-sm btn-primary zerti-inv-categories__term-save-btn">
                                        <?php esc_html_e('Save term', 'zerti'); ?>
                                    </button>
                                </div>
                            </div>
                            </div>

                            <div class="zerti-inv-categories__form-footer d-flex flex-wrap justify-content-between align-items-center gap-2 mt-4 pt-3 border-top">
                                <p class="text-body-secondary small mb-0 zerti-inv-categories__meta-line d-none"></p>
                                <div class="d-flex flex-wrap gap-2 ms-auto">
                                    <button type="button" class="btn btn-sm btn-outline-secondary zerti-inv-categories__cancel-btn">
                                        <?php esc_html_e('Cancel', 'zerti'); ?>
                                    </button>
                                    <button type="button" class="btn btn-sm btn-outline-danger zerti-inv-categories__delete-btn d-none">
                                        <?php esc_html_e('Delete', 'zerti'); ?>
                                    </button>
                                    <button type="submit" class="btn btn-sm btn-primary zerti-inv-categories__save-btn">
                                        <?php esc_html_e('Save', 'zerti'); ?>
                                    </button>
                                </div>
                            </div>
                        </form>
                        </div>
                        </div>
                        </div>
                    </div>
                </section>
            </div>
        </div>
    <?php endif; ?>
</div>
