<?php
/**
 * انبار — ویرایش / ایجاد محصول (چیدمان مشابه ویرایشگر ووکامرس + استایل Bootstrap زرتی).
 *
 * Expects $zerti_product_edit from zerti_render_inventory_product_edit().
 */
if (!defined('ABSPATH')) {
    exit;
}

if (!isset($zerti_product_edit) || !is_array($zerti_product_edit)) {
    return;
}

$product_id = (int) ($zerti_product_edit['product_id'] ?? 0);
$is_new = !empty($zerti_product_edit['is_new']);
$read_only = !empty($zerti_product_edit['read_only']);
$read_only_reason = (string) ($zerti_product_edit['read_only_reason'] ?? '');
$type_slug = (string) ($zerti_product_edit['type_slug'] ?? 'simple');
$back_url = (string) ($zerti_product_edit['back_url'] ?? (function_exists('zerti_admin_inventory_warehouse_list_return_url') ? zerti_admin_inventory_warehouse_list_return_url() : zerti_admin_route_url(['tab' => 'inventory', 'inv' => 'warehouse'])));
$wp_edit_link = (string) ($zerti_product_edit['wp_edit_link'] ?? '');

$pick = static function (string $key, $default = '') use ($zerti_product_edit) {
    return $zerti_product_edit[$key] ?? $default;
};

$type_choices = is_array($pick('type_choices', [])) ? $pick('type_choices', []) : [];
if ($type_choices === [] && function_exists('zerti_product_edit_type_choices')) {
    $type_choices = zerti_product_edit_type_choices();
}
$grouped_children = is_array($pick('grouped_children', [])) ? $pick('grouped_children', []) : [];

$date_input_type = function_exists('zerti_admin_date_input_type') ? zerti_admin_date_input_type() : 'text';
$date_input_data_attr = function_exists('zerti_admin_date_input_data_attr') ? zerti_admin_date_input_data_attr() : 'data-zerti-jalali-date';
$date_input_placeholder = function_exists('zerti_admin_date_placeholder') ? zerti_admin_date_placeholder() : '۱۴۰۳/۰۱/۰۱';
$date_input_dir = $date_input_type === 'date'
    ? 'ltr'
    : (function_exists('zerti_is_admin_rtl') && zerti_is_admin_rtl() ? 'rtl' : 'ltr');

$status_choices = is_array($pick('status_choices', [])) ? $pick('status_choices', []) : [];
$stock_status_choices = is_array($pick('stock_status_choices', [])) ? $pick('stock_status_choices', []) : [];
$catalog_visibility_choices = is_array($pick('catalog_visibility_choices', [])) ? $pick('catalog_visibility_choices', []) : [];
$tax_status_choices = is_array($pick('tax_status_choices', [])) ? $pick('tax_status_choices', []) : [];
$tax_class_choices = is_array($pick('tax_class_choices', [])) ? $pick('tax_class_choices', []) : [];
$backorder_choices = is_array($pick('backorder_choices', [])) ? $pick('backorder_choices', []) : [];
$shipping_class_choices = is_array($pick('shipping_class_choices', [])) ? $pick('shipping_class_choices', []) : [];
$category_tree = is_array($pick('category_tree', [])) ? $pick('category_tree', []) : [];
$selected_cats = is_array($pick('category_ids', [])) ? array_map('intval', $pick('category_ids', [])) : [];
$gallery_items = is_array($pick('gallery_items', [])) ? $pick('gallery_items', []) : [];
$attribute_rows = is_array($pick('attribute_rows', [])) ? $pick('attribute_rows', []) : [];
$variation_rows = is_array($pick('variation_rows', [])) ? $pick('variation_rows', []) : [];
$variation_attribute_defs = is_array($pick('variation_attribute_defs', [])) ? $pick('variation_attribute_defs', []) : [];
$global_attribute_choices = is_array($pick('global_attribute_choices', [])) ? $pick('global_attribute_choices', []) : [];
$download_rows = is_array($pick('download_rows', [])) ? $pick('download_rows', []) : [];
$upsell_products = is_array($pick('upsell_products', [])) ? $pick('upsell_products', []) : [];
$cross_sell_products = is_array($pick('cross_sell_products', [])) ? $pick('cross_sell_products', []) : [];

$image_url = (string) $pick('image_url', '');
$image_id = (int) $pick('image_id', 0);
$weight_unit = (string) $pick('weight_unit', 'kg');
$dimension_unit = (string) $pick('dimension_unit', 'cm');
$ro = $read_only ? ' readonly' : '';
$dis = $read_only ? ' disabled' : '';
$fc = 'form-control';
$currency_sym = function_exists('get_woocommerce_currency_symbol') ? get_woocommerce_currency_symbol() : '';

/**
 * Build <option> list HTML.
 *
 * @param array<string, string> $choices
 */
$render_options = static function (array $choices, string $current) use ($dis): void {
    foreach ($choices as $slug => $label) {
        printf(
            '<option value="%s"%s>%s</option>',
            esc_attr((string) $slug),
            selected($current, (string) $slug, false),
            esc_html($label)
        );
    }
};
?>
<div
    class="zerti-component zerti-component-inventory zerti-inventory-product-edit zerti-wc-product-editor zerti-inventory-sub-slot"
    data-zerti-inv-active="warehouse"
    data-zerti-inv-component="warehouse"
    data-zerti-product-id="<?php echo esc_attr((string) $product_id); ?>"
>
    <div class="zerti-wc-product-editor__toolbar d-flex flex-wrap align-items-center gap-2 mb-3" role="toolbar" aria-label="<?php esc_attr_e('Product actions', 'zerti'); ?>">
        <a href="<?php echo esc_url($back_url); ?>" class="btn btn-outline-secondary btn-sm zerti-inventory-product-back d-inline-flex align-items-center gap-1">
            <i class="fa-solid fa-arrow-right" aria-hidden="true"></i>
            <?php esc_html_e('Back to list', 'zerti'); ?>
        </a>
        <?php if ($wp_edit_link !== '' && !$is_new) : ?>
            <a href="<?php echo esc_url($wp_edit_link); ?>" class="btn btn-outline-secondary btn-sm d-inline-flex align-items-center gap-1" target="_blank" rel="noopener noreferrer">
                <i class="fa-solid fa-up-right-from-square" aria-hidden="true"></i>
                <?php esc_html_e('Edit in WooCommerce', 'zerti'); ?>
            </a>
        <?php endif; ?>
    </div>

    <?php if ($read_only && $read_only_reason !== '') : ?>
        <p class="alert alert-info py-2" role="status"><?php echo esc_html($read_only_reason); ?></p>
    <?php endif; ?>

    <form
        class="zerti-inventory-product-edit__form zerti-wc-product-editor__form zerti-scrollbar-thin"
        method="post"
        action="#"
        novalidate
        <?php echo $read_only ? ' data-zerti-product-readonly="1"' : ''; ?>
    >
        <input type="hidden" name="product_id" value="<?php echo esc_attr((string) $product_id); ?>" />

        <div class="row g-3">
            <div class="col-12 col-xl-9 zerti-wc-product-editor__primary">
                <!-- عنوان محصول (مثل عنوان نوشته در ووکامرس) -->
                <div class="zerti-wc-postbox zerti-wc-postbox--title mb-3" id="zertiProductTitle">
                    <div class="zerti-wc-postbox__header py-2 px-3">
                        <h2 class="h6 mb-0">
                            <label class="mb-0" for="zertiProductName"><?php esc_html_e('Product name', 'zerti'); ?></label>
                        </h2>
                    </div>
                    <div class="zerti-wc-postbox__body">
                        <input
                            type="text"
                            class="zerti-wc-editor-title-input form-control"
                            id="zertiProductName"
                            name="name"
                            required
                            dir="rtl"
                            placeholder="<?php esc_attr_e('Enter product name', 'zerti'); ?>"
                            value="<?php echo esc_attr((string) $pick('name', '')); ?>"
                            <?php echo esc_attr($ro); ?>
                        />
                    </div>
                </div>

                <!-- دادهٔ محصول — Product data -->
                <div id="woocommerce-product-data" class="zerti-wc-product-data mb-3">
                    <div class="zerti-wc-product-data__head py-2 px-3 d-flex flex-wrap align-items-center gap-3">
                        <div class="zerti-wc-product-data__type-wrap d-flex flex-wrap align-items-center gap-2">
                            <span class="zerti-wc-product-data__type-label"><?php esc_html_e('Product data', 'zerti'); ?></span>
                            <span class="zerti-wc-product-data__type-sep text-body-secondary" aria-hidden="true">—</span>
                            <select
                                class="zerti-admin-select form-select form-select-sm zerti-wc-product-data__type-select"
                                id="zertiProductType"
                                name="product_type"
                                data-zerti-product-type
                                aria-label="<?php esc_attr_e('Product type', 'zerti'); ?>"
                                <?php echo esc_attr($dis); ?>
                            >
                                <optgroup label="<?php esc_attr_e('Product type', 'zerti'); ?>">
                                    <?php
                                    foreach ($type_choices as $slug => $label) {
                                        printf(
                                            '<option value="%s"%s>%s</option>',
                                            esc_attr((string) $slug),
                                            selected($type_slug, (string) $slug, false),
                                            esc_html((string) $label)
                                        );
                                    }
                                    ?>
                                </optgroup>
                            </select>
                        </div>
                        <span class="zerti-wc-product-data__flags d-flex flex-wrap gap-3 ms-auto" data-zerti-product-flags>
                            <label class="zerti-wc-product-data__flag form-check form-check-inline mb-0">
                                <input class="form-check-input" type="checkbox" id="zertiProductVirtual" name="virtual" value="1" data-zerti-product-virtual<?php checked(!empty($pick('virtual'))); echo esc_attr($dis); ?> />
                                <span class="form-check-label"><?php esc_html_e('Virtual', 'zerti'); ?></span>
                            </label>
                            <label class="zerti-wc-product-data__flag form-check form-check-inline mb-0">
                                <input class="form-check-input" type="checkbox" id="zertiProductDownloadable" name="downloadable" value="1" data-zerti-product-downloadable<?php checked(!empty($pick('downloadable'))); echo esc_attr($dis); ?> />
                                <span class="form-check-label"><?php esc_html_e('Downloadable', 'zerti'); ?></span>
                            </label>
                        </span>
                    </div>

                    <div class="zerti-wc-product-data__body d-flex flex-column flex-md-row">
                        <ul class="zerti-wc-tabs wc-tabs nav flex-md-column" role="tablist" aria-label="<?php esc_attr_e('Product data', 'zerti'); ?>">
                            <li class="zerti-wc-tabs__item general_options active" role="presentation">
                                <a href="#general_product_data" class="zerti-wc-tabs__link active" data-zerti-wc-tab="general" role="tab"><i class="fa-solid fa-sliders" aria-hidden="true"></i> <?php esc_html_e('Public', 'zerti'); ?></a>
                            </li>
                            <li class="zerti-wc-tabs__item inventory_options" role="presentation">
                                <a href="#inventory_product_data" class="zerti-wc-tabs__link" data-zerti-wc-tab="inventory" role="tab"><i class="fa-solid fa-boxes-stacked" aria-hidden="true"></i> <?php esc_html_e('Warehouse', 'zerti'); ?></a>
                            </li>
                            <li class="zerti-wc-tabs__item shipping_options" role="presentation" data-zerti-shipping-tab>
                                <a href="#shipping_product_data" class="zerti-wc-tabs__link" data-zerti-wc-tab="shipping" role="tab"><i class="fa-solid fa-truck" aria-hidden="true"></i> <?php esc_html_e('Shipping', 'zerti'); ?></a>
                            </li>
                            <li class="zerti-wc-tabs__item linked_product_options" role="presentation">
                                <a href="#linked_product_data" class="zerti-wc-tabs__link" data-zerti-wc-tab="linked" role="tab"><i class="fa-solid fa-link" aria-hidden="true"></i> <?php esc_html_e('Related products', 'zerti'); ?></a>
                            </li>
                            <li class="zerti-wc-tabs__item attribute_options" role="presentation">
                                <a href="#product_attributes" class="zerti-wc-tabs__link" data-zerti-wc-tab="attributes" role="tab"><i class="fa-solid fa-list" aria-hidden="true"></i> <?php esc_html_e('Attributes', 'zerti'); ?></a>
                            </li>
                            <li class="zerti-wc-tabs__item variation_options" role="presentation" data-zerti-variations-tab<?php echo $type_slug !== 'variable' ? ' hidden' : ''; ?>>
                                <a href="#variable_product_options" class="zerti-wc-tabs__link" data-zerti-wc-tab="variations" role="tab"><i class="fa-solid fa-layer-group" aria-hidden="true"></i> <?php esc_html_e('Variations', 'zerti'); ?></a>
                            </li>
                            <li class="zerti-wc-tabs__item advanced_options" role="presentation">
                                <a href="#advanced_product_data" class="zerti-wc-tabs__link" data-zerti-wc-tab="advanced" role="tab"><i class="fa-solid fa-wrench" aria-hidden="true"></i> <?php esc_html_e('Advanced', 'zerti'); ?></a>
                            </li>
                            <li class="zerti-wc-tabs__item downloadable_options" role="presentation" data-zerti-downloads-tab<?php echo empty($pick('downloadable')) ? ' hidden' : ''; ?>>
                                <a href="#downloadable_product_data" class="zerti-wc-tabs__link" data-zerti-wc-tab="downloads" role="tab"><i class="fa-solid fa-download" aria-hidden="true"></i> <?php esc_html_e('Downloads', 'zerti'); ?></a>
                            </li>
                        </ul>

                        <div class="zerti-wc-panels flex-grow-1">
                            <!-- عمومی -->
                            <div id="general_product_data" class="zerti-wc-panel woocommerce_options_panel active" data-zerti-wc-panel="general" role="tabpanel">
                                <?php zerti_wc_options_group_open(); ?>
                                <div data-zerti-general-external<?php echo $type_slug !== 'external' ? ' hidden' : ''; ?>>
                                    <?php
                                    ob_start(); ?>
<input type="url" class="<?php echo esc_attr($fc); ?>" id="zertiProductExternalUrl" name="product_url" dir="ltr" placeholder="https://" value="<?php echo esc_attr((string) $pick('product_url', '')); ?>"<?php echo esc_attr($ro); ?> />

                                    <?php zerti_wc_form_field('zertiProductExternalUrl', __('Product URL', 'zerti'), ob_get_clean());

                                    ob_start(); ?>
<input type="text" class="<?php echo esc_attr($fc); ?>" id="zertiProductButtonText" name="button_text" value="<?php echo esc_attr((string) $pick('button_text', '')); ?>" placeholder="<?php esc_attr_e('Buy product', 'zerti'); ?>"<?php echo esc_attr($ro); ?> />


                                    <?php zerti_wc_form_field('zertiProductButtonText', __('Button text', 'zerti'), ob_get_clean());
                                    ?>
                                </div>
                                <div data-zerti-general-pricing<?php echo ($type_slug === 'grouped' || $type_slug === 'variable') ? ' hidden' : ''; ?>>
                                    <?php
                                    ob_start(); ?>
<input type="text" class="<?php echo esc_attr($fc); ?> wc_input_price" id="zertiProductRegularPrice" name="regular_price" dir="ltr" inputmode="decimal" value="<?php echo esc_attr((string) $pick('regular_price', '')); ?>"<?php echo esc_attr($ro); ?> />

                                    <?php zerti_wc_form_field('zertiProductRegularPrice',
                                        $currency_sym !== ''
                                            ? sprintf(/* translators: %s: currency symbol */ __('Regular price (%s)', 'zerti'), $currency_sym)
                                            : __('Regular price', 'zerti'), ob_get_clean());

                                    ob_start(); ?>
<input type="text" class="<?php echo esc_attr($fc); ?> wc_input_price" id="zertiProductSalePrice" name="sale_price" dir="ltr" inputmode="decimal" placeholder="<?php esc_attr_e('Sale price', 'zerti'); ?>" value="<?php echo esc_attr((string) $pick('sale_price', '')); ?>"<?php echo esc_attr($ro); ?> />


                                    <?php zerti_wc_form_field('zertiProductSalePrice',
                                        $currency_sym !== ''
                                            ? sprintf(/* translators: %s: currency symbol */ __('Sale price (%s)', 'zerti'), $currency_sym)
                                            : __('Sale price', 'zerti'), ob_get_clean());

                                    ob_start(); ?>
<span class="zerti-wc-sale-dates d-inline-flex flex-wrap gap-2 align-items-center">
                                        <span class="zerti-wc-sale-date-group d-inline-flex align-items-center gap-1">
                                            <span class="zerti-wc-sale-date-label"><?php esc_html_e('Start', 'zerti'); ?></span>
                                            <input type="<?php echo esc_attr($date_input_type); ?>" class="<?php echo esc_attr($fc); ?> zerti-admin-date-input" id="zertiProductSaleFrom" name="sale_price_dates_from" <?php echo esc_attr($date_input_data_attr); ?> dir="<?php echo esc_attr($date_input_dir); ?>"<?php echo $date_input_type === 'text' ? ' inputmode="numeric" autocomplete="off" placeholder="' . esc_attr($date_input_placeholder) . '"' : ''; ?> value="<?php echo esc_attr((string) $pick('sale_price_dates_from', '')); ?>"<?php echo esc_attr($ro); ?> />
                                        </span>
                                        <span class="zerti-wc-sale-date-sep" aria-hidden="true">–</span>
                                        <span class="zerti-wc-sale-date-group d-inline-flex align-items-center gap-1">
                                            <span class="zerti-wc-sale-date-label"><?php esc_html_e('End', 'zerti'); ?></span>
                                            <input type="<?php echo esc_attr($date_input_type); ?>" class="<?php echo esc_attr($fc); ?> zerti-admin-date-input" id="zertiProductSaleTo" name="sale_price_dates_to" <?php echo esc_attr($date_input_data_attr); ?> dir="<?php echo esc_attr($date_input_dir); ?>"<?php echo $date_input_type === 'text' ? ' inputmode="numeric" autocomplete="off" placeholder="' . esc_attr($date_input_placeholder) . '"' : ''; ?> value="<?php echo esc_attr((string) $pick('sale_price_dates_to', '')); ?>"<?php echo esc_attr($ro); ?> />
                                        </span>
                                    </span>


                                    <?php zerti_wc_form_field('zertiProductSaleFrom', __('Sale schedule', 'zerti'), ob_get_clean(), '', 'zerti-wc-form-field--wide');
                                    ?>
                                </div>
                                <p class="zerti-wc-panel-hint text-body-secondary small mb-0" data-zerti-variable-notice<?php echo $type_slug !== 'variable' ? ' hidden' : ''; ?>><?php esc_html_e('Set variation attributes on the Attributes tab, then manage variations there.', 'zerti'); ?></p>
                                <?php
                                ob_start(); ?>
<select class="form-select" id="zertiProductTaxStatus" name="tax_status"<?php echo esc_attr($dis); ?>><?php $render_options($tax_status_choices, (string) $pick('tax_status', 'taxable')); ?></select>

                                <?php zerti_wc_form_field('zertiProductTaxStatus', __('Tax status', 'zerti'), ob_get_clean());

                                ob_start(); ?>
<select class="form-select" id="zertiProductTaxClass" name="tax_class"<?php echo esc_attr($dis); ?>><?php $render_options($tax_class_choices, (string) $pick('tax_class', '')); ?></select>


                                <?php zerti_wc_form_field('zertiProductTaxClass', __('Tax class', 'zerti'), ob_get_clean());
                                ?>
                                <?php zerti_wc_options_group_close(); ?>
                            </div>

                            <!-- انبار -->
                            <div id="inventory_product_data" class="zerti-wc-panel woocommerce_options_panel" data-zerti-wc-panel="inventory" role="tabpanel" hidden>
                                <?php zerti_wc_options_group_open(); ?>
                                <?php
                                ob_start(); ?>
<input type="text" class="<?php echo esc_attr($fc); ?>" id="zertiProductSku" name="sku" dir="ltr" autocomplete="off" value="<?php echo esc_attr((string) $pick('sku', '')); ?>"<?php echo esc_attr($ro); ?> />

                                <?php zerti_wc_form_field('zertiProductSku', __('Product ID (SKU)', 'zerti'), ob_get_clean());

                                ob_start(); ?>
<label class="form-check mb-0">
                                    <input class="form-check-input" type="checkbox" id="zertiProductManageStock" name="manage_stock" value="1" data-zerti-product-manage-stock<?php checked(!empty($pick('manage_stock'))); echo esc_attr($dis); ?> />
                                    <?php esc_html_e('Warehouse inventory management', 'zerti'); ?>
                                </label>


                                <?php zerti_wc_form_field('zertiProductManageStock', __('Manage stock?', 'zerti'), ob_get_clean());

                                ob_start(); ?>
<input type="text" class="<?php echo esc_attr($fc); ?>" id="zertiProductStockQty" name="stock_quantity" dir="ltr" inputmode="numeric" value="<?php echo esc_attr((string) $pick('stock_quantity', '')); ?>"<?php echo esc_attr($ro); ?> />


                                <?php zerti_wc_form_field('zertiProductStockQty', __('Stock quantity', 'zerti'), ob_get_clean(), '', 'zerti-wc-form-field--stock-qty');

                                ob_start(); ?>
<select class="form-select" id="zertiProductBackorders" name="backorders"<?php echo esc_attr($dis); ?>><?php $render_options($backorder_choices, (string) $pick('backorders', 'no')); ?></select>


                                <?php zerti_wc_form_field('zertiProductBackorders', __('Allow backorders?', 'zerti'), ob_get_clean());

                                ob_start(); ?>
<input type="text" class="<?php echo esc_attr($fc); ?>" id="zertiProductLowStock" name="low_stock_amount" dir="ltr" inputmode="numeric" placeholder="<?php esc_attr_e('Admin email', 'zerti'); ?>" value="<?php echo esc_attr((string) $pick('low_stock_amount', '')); ?>"<?php echo esc_attr($ro); ?> />


                                <?php zerti_wc_form_field('zertiProductLowStock', __('Low stock threshold', 'zerti'), ob_get_clean());

                                ob_start(); ?>
<select class="form-select" id="zertiProductStockStatus" name="stock_status"<?php echo esc_attr($dis); ?>><?php $render_options($stock_status_choices, (string) $pick('stock_status', 'instock')); ?></select>


                                <?php zerti_wc_form_field('zertiProductStockStatus', __('Stock status', 'zerti'), ob_get_clean(), '', 'zerti-wc-form-field--stock-status');

                                ob_start(); ?>
<label class="form-check mb-0">
                                    <input class="form-check-input" type="checkbox" id="zertiProductSoldIndividually" name="sold_individually" value="1"<?php checked(!empty($pick('sold_individually'))); echo esc_attr($dis); ?> />
                                    <?php esc_html_e('Individual sale', 'zerti'); ?>
                                </label>


                                <?php zerti_wc_form_field('zertiProductSoldIndividually', __('Sold individually?', 'zerti'), ob_get_clean(), __('Limit 1 per order', 'zerti'));
                                ?>
                                <?php zerti_wc_options_group_close(); ?>
                            </div>

                            <!-- حمل -->
                            <div id="shipping_product_data" class="zerti-wc-panel woocommerce_options_panel" data-zerti-wc-panel="shipping" data-zerti-shipping-panel role="tabpanel" hidden>
                                <p class="zerti-wc-panel-hint text-body-secondary small px-3 pt-2 mb-0" data-zerti-shipping-hint hidden><?php esc_html_e('Product is virtual — shipping settings are disabled.', 'zerti'); ?></p>
                                <div data-zerti-shipping-fields>
                                    <?php zerti_wc_options_group_open(); ?>
                                    <?php
                                    ob_start(); ?>
<input type="text" class="<?php echo esc_attr($fc); ?> wc_input_decimal" id="zertiProductWeight" name="weight" dir="ltr" inputmode="decimal" value="<?php echo esc_attr((string) $pick('weight', '')); ?>"<?php echo esc_attr($ro); ?> />

                                    <?php zerti_wc_form_field('zertiProductWeight',
                                        sprintf(/* translators: %s: weight unit */ __('Weight (%s)', 'zerti'), $weight_unit), ob_get_clean());

                                    ob_start(); ?>
<span class="zerti-wc-dimensions d-inline-flex flex-wrap gap-2">
                                        <input type="text" class="<?php echo esc_attr($fc); ?>" id="zertiProductLength" name="length" placeholder="<?php esc_attr_e('Length', 'zerti'); ?>" dir="ltr" value="<?php echo esc_attr((string) $pick('length', '')); ?>"<?php echo esc_attr($ro); ?> />
                                        <input type="text" class="<?php echo esc_attr($fc); ?>" id="zertiProductWidth" name="width" placeholder="<?php esc_attr_e('Width', 'zerti'); ?>" dir="ltr" value="<?php echo esc_attr((string) $pick('width', '')); ?>"<?php echo esc_attr($ro); ?> />
                                        <input type="text" class="<?php echo esc_attr($fc); ?>" id="zertiProductHeight" name="height" placeholder="<?php esc_attr_e('Height', 'zerti'); ?>" dir="ltr" value="<?php echo esc_attr((string) $pick('height', '')); ?>"<?php echo esc_attr($ro); ?> />
                                    </span>


                                    <?php zerti_wc_form_field('zertiProductLength',
                                        sprintf(/* translators: %s: dimension unit */ __('Dimensions (%s)', 'zerti'), $dimension_unit), ob_get_clean(), '', 'zerti-wc-form-field--wide');

                                    if ($shipping_class_choices !== []) {
                                        ob_start(); ?>
<select class="form-select" id="zertiProductShippingClass" name="shipping_class_id"<?php echo esc_attr($dis); ?>>
                                            <?php foreach ($shipping_class_choices as $term_id => $label) : ?>
                                                <option value="<?php echo esc_attr((string) $term_id); ?>"<?php selected((int) $pick('shipping_class_id', 0), (int) $term_id); ?>><?php echo esc_html($label); ?></option>
                                            <?php endforeach; ?>
                                        </select>

                                        <?php zerti_wc_form_field('zertiProductShippingClass', __('Shipping class', 'zerti'), ob_get_clean());
                                    }
                                    ?>
                                    <?php zerti_wc_options_group_close(); ?>
                                </div>
                            </div>

                            <!-- محصولات مرتبط -->
                            <div id="linked_product_data" class="zerti-wc-panel woocommerce_options_panel" data-zerti-wc-panel="linked" role="tabpanel" hidden>
                                <?php zerti_wc_options_group_open(); ?>
                                <div data-zerti-linked-grouped<?php echo $type_slug !== 'grouped' ? ' hidden' : ''; ?>>
                                    <?php
                                    zerti_product_edit_render_linked_picker(
                                        'children',
                                        __('Grouped products', 'zerti'),
                                        __('Products in this group.', 'zerti'),
                                        $grouped_children,
                                        $read_only
                                    );
                                    ?>
                                </div>
                                <div data-zerti-linked-standard<?php echo $type_slug === 'grouped' ? ' hidden' : ''; ?>>
                                    <?php
                                    zerti_product_edit_render_linked_picker(
                                        'upsell_ids',
                                        __('Upsells', 'zerti'),
                                        __('Suggested products on the product page.', 'zerti'),
                                        $upsell_products,
                                        $read_only
                                    );
                                    zerti_product_edit_render_linked_picker(
                                        'cross_sell_ids',
                                        __('Cross-sells', 'zerti'),
                                        __('Suggested on cart page.', 'zerti'),
                                        $cross_sell_products,
                                        $read_only
                                    );
                                    ?>
                                </div>
                                <?php zerti_wc_options_group_close(); ?>
                            </div>

                            <!-- ویژگی‌ها -->
                            <div id="product_attributes" class="zerti-wc-panel woocommerce_options_panel" data-zerti-wc-panel="attributes" role="tabpanel" hidden>
                                <div class="zerti-wc-panel-toolbar p-3">
                                    <?php if (!$read_only) : ?>
                                        <button type="button" class="btn btn-outline-secondary btn-sm" data-zerti-attribute-add><?php esc_html_e('Add attribute', 'zerti'); ?></button>
                                    <?php endif; ?>
                                </div>
                                <table class="table table-sm zerti-wc-attributes-table mb-0">
                                    <thead>
                                        <tr>
                                            <th><?php esc_html_e('Name', 'zerti'); ?></th>
                                            <th><?php esc_html_e('Value(s)', 'zerti'); ?></th>
                                            <th class="text-center"><?php esc_html_e('Display', 'zerti'); ?></th>
                                            <th class="text-center" data-zerti-attr-variation-col<?php echo $type_slug !== 'variable' ? ' hidden' : ''; ?>><?php esc_html_e('Used for variations', 'zerti'); ?></th>
                                            <?php if (!$read_only) : ?>
                                                <th></th>
                                            <?php endif; ?>
                                        </tr>
                                    </thead>
                                    <tbody data-zerti-attribute-rows>
                                        <?php
                                        foreach ($attribute_rows as $a_idx => $a_row) {
                                            if (is_array($a_row)) {
                                                zerti_product_edit_render_attribute_row($a_row, (int) $a_idx, $global_attribute_choices, $read_only);
                                            }
                                        }
                                        ?>
                                    </tbody>
                                </table>
                            </div>

                            <!-- تنوع‌ها -->
                            <div id="variable_product_options" class="zerti-wc-panel woocommerce_options_panel" data-zerti-wc-panel="variations" role="tabpanel"<?php echo $type_slug !== 'variable' ? ' hidden' : ''; ?>>
                                <div class="zerti-wc-panel-toolbar p-3 d-flex flex-wrap gap-2 align-items-center">
                                    <?php if (!$read_only) : ?>
                                        <button type="button" class="btn btn-outline-secondary btn-sm" data-zerti-variation-generate><?php esc_html_e('Generate variations', 'zerti'); ?></button>
                                        <button type="button" class="btn btn-outline-secondary btn-sm" data-zerti-variation-add><?php esc_html_e('Add variation', 'zerti'); ?></button>
                                    <?php endif; ?>
                                    <p class="zerti-wc-panel-hint text-body-secondary small mb-0 ms-md-auto" data-zerti-variations-hint><?php esc_html_e('Mark attributes as used for variations and add values on the Attributes tab first.', 'zerti'); ?></p>
                                </div>
                                <div class="zerti-variations-list" data-zerti-variation-rows>
                                    <?php
                                    foreach ($variation_rows as $v_idx => $v_row) {
                                        if (is_array($v_row)) {
                                            zerti_product_edit_render_variation_row($v_row, (int) $v_idx, $variation_attribute_defs, $read_only);
                                        }
                                    }
                                    ?>
                                </div>
                            </div>

                            <!-- پیشرفته -->
                            <div id="advanced_product_data" class="zerti-wc-panel woocommerce_options_panel" data-zerti-wc-panel="advanced" role="tabpanel" hidden>
                                <?php zerti_wc_options_group_open(); ?>
                                <?php
                                ob_start(); ?>
<input type="number" class="<?php echo esc_attr($fc); ?>" id="zertiProductMenuOrder" name="menu_order" dir="ltr" value="<?php echo esc_attr((string) (int) $pick('menu_order', 0)); ?>"<?php echo esc_attr($ro); ?> />

                                <?php zerti_wc_form_field('zertiProductMenuOrder', __('Menu order', 'zerti'), ob_get_clean());

                                ob_start(); ?>
<label class="form-check mb-0">
                                    <input class="form-check-input" type="checkbox" id="zertiProductReviews" name="reviews_allowed" value="1"<?php checked(!empty($pick('reviews_allowed', true))); echo esc_attr($dis); ?> />
                                    <?php esc_html_e('Active', 'zerti'); ?>
                                </label>


                                <?php zerti_wc_form_field('zertiProductReviews', __('Reviews', 'zerti'), ob_get_clean(), __('Enable reviews', 'zerti'));
                                ?>
                                <?php zerti_wc_options_group_close(); ?>

                                <?php zerti_wc_options_group_open(); ?>
                                <?php
                                ob_start(); ?>
<textarea class="<?php echo esc_attr($fc); ?>" id="zertiProductPurchaseNote" name="purchase_note" rows="3" dir="rtl"<?php echo esc_attr($ro); ?>><?php echo esc_textarea((string) $pick('purchase_note', '')); ?></textarea>

                                <?php zerti_wc_form_field('zertiProductPurchaseNote', __('Purchase note', 'zerti'), ob_get_clean(), __('Note sent to customer after purchase.', 'zerti'), 'zerti-wc-form-field--wide');
                                ?>
                                <?php zerti_wc_options_group_close(); ?>
                            </div>

                            <!-- دانلودها -->
                            <div id="downloadable_product_data" class="zerti-wc-panel woocommerce_options_panel" data-zerti-wc-panel="downloads" data-zerti-downloads-panel role="tabpanel" hidden>
                                <?php zerti_wc_options_group_open(); ?>
                                <table class="table table-sm zerti-wc-downloads-table mb-2">
                                    <thead>
                                        <tr>
                                            <th><?php esc_html_e('Name', 'zerti'); ?></th>
                                            <th><?php esc_html_e('File URL', 'zerti'); ?></th>
                                            <?php if (!$read_only) : ?>
                                                <th></th>
                                            <?php endif; ?>
                                        </tr>
                                    </thead>
                                    <tbody data-zerti-download-rows>
                                        <?php
                                        foreach ($download_rows as $d_idx => $d_row) {
                                            if (is_array($d_row)) {
                                                zerti_product_edit_render_download_row($d_row, (int) $d_idx, $read_only);
                                            }
                                        }
                                        ?>
                                    </tbody>
                                </table>
                                <?php if (!$read_only) : ?>
                                    <button type="button" class="btn btn-outline-secondary btn-sm mb-3" data-zerti-download-add><?php esc_html_e('Add file', 'zerti'); ?></button>
                                <?php endif; ?>
                                <?php
                                ob_start(); ?>
<input type="number" class="<?php echo esc_attr($fc); ?>" id="zertiDownloadLimit" name="download_limit" min="0" dir="ltr" placeholder="∞" value="<?php echo esc_attr((string) $pick('download_limit', '')); ?>"<?php echo esc_attr($ro); ?> />

                                <?php zerti_wc_form_field('zertiDownloadLimit', __('Download limit', 'zerti'), ob_get_clean(), __('Empty = unlimited', 'zerti'));

                                ob_start(); ?>
<input type="number" class="<?php echo esc_attr($fc); ?>" id="zertiDownloadExpiry" name="download_expiry" min="0" dir="ltr" placeholder="∞" value="<?php echo esc_attr((string) $pick('download_expiry', '')); ?>"<?php echo esc_attr($ro); ?> />


                                <?php zerti_wc_form_field('zertiDownloadExpiry', __('Download expiry (days)', 'zerti'), ob_get_clean(), __('Empty = unlimited', 'zerti'));
                                ?>
                                <?php zerti_wc_options_group_close(); ?>
                            </div>
                        </div>
                    </div>
                </div>

                <!-- توضیح کوتاه -->
                <div class="zerti-wc-postbox zerti-wc-postbox--editor mb-3" id="postexcerpt">
                    <div class="zerti-wc-postbox__header py-2 px-3">
                        <h2 class="h6 mb-0"><?php esc_html_e('Product short description', 'zerti'); ?></h2>
                    </div>
                    <div class="zerti-wc-postbox__body">
                        <div
                            class="zerti-rich-editor zerti-rich-editor--short"
                            data-zerti-rich-editor
                            data-zerti-rich-editor-min-height="224"
                            data-zerti-rich-editor-placeholder="<?php esc_attr_e('Product short description…', 'zerti'); ?>"
                            <?php echo $read_only ? ' data-zerti-rich-editor-readonly="1"' : ''; ?>
                        >
                            <textarea class="zerti-rich-editor__source <?php echo esc_attr($fc); ?>" id="zertiProductShortDesc" name="short_description" dir="rtl"<?php echo esc_attr($ro); ?>><?php echo esc_textarea((string) $pick('short_description', '')); ?></textarea>
                        </div>
                    </div>
                </div>

                <!-- توضیحات محصول -->
                <div class="zerti-wc-postbox zerti-wc-postbox--editor mb-3" id="postdivrich">
                    <div class="zerti-wc-postbox__header py-2 px-3">
                        <h2 class="h6 mb-0"><?php esc_html_e('Product description', 'zerti'); ?></h2>
                    </div>
                    <div class="zerti-wc-postbox__body">
                        <div
                            class="zerti-rich-editor zerti-rich-editor--long"
                            data-zerti-rich-editor
                            data-zerti-rich-editor-min-height="224"
                            data-zerti-rich-editor-placeholder="<?php esc_attr_e('Full product description…', 'zerti'); ?>"
                            <?php echo $read_only ? ' data-zerti-rich-editor-readonly="1"' : ''; ?>
                        >
                            <textarea class="zerti-rich-editor__source <?php echo esc_attr($fc); ?>" id="zertiProductDesc" name="description" dir="rtl"<?php echo esc_attr($ro); ?>><?php echo esc_textarea((string) $pick('description', '')); ?></textarea>
                        </div>
                    </div>
                </div>
            </div>

            <!-- سایدبار (متاباکس‌های ووکامرس) -->
            <aside class="col-12 col-xl-3 zerti-wc-product-editor__sidebar">
                <?php zerti_wc_postbox_open(__('Publish', 'zerti'), 'submitdiv'); ?>
                <div class="zerti-wc-publish-fields d-grid gap-2">
                    <div>
                        <label class="form-label small mb-1" for="zertiProductStatus"><?php esc_html_e('Status', 'zerti'); ?></label>
                        <select class="form-select" id="zertiProductStatus" name="status"<?php echo esc_attr($dis); ?>><?php $render_options($status_choices, (string) $pick('status', 'publish')); ?></select>
                    </div>
                    <div>
                        <label class="form-label small mb-1" for="zertiProductCatalogVis"><?php esc_html_e('Visible in catalog', 'zerti'); ?></label>
                        <select class="form-select" id="zertiProductCatalogVis" name="catalog_visibility"<?php echo esc_attr($dis); ?>><?php $render_options($catalog_visibility_choices, (string) $pick('catalog_visibility', 'visible')); ?></select>
                    </div>
                    <div class="form-check">
                        <input class="form-check-input" type="checkbox" id="zertiProductFeatured" name="featured" value="1"<?php checked(!empty($pick('featured'))); echo esc_attr($dis); ?> />
                        <label class="form-check-label" for="zertiProductFeatured"><?php esc_html_e('Featured product', 'zerti'); ?></label>
                    </div>
                </div>
                <?php if (!$read_only) : ?>
                    <div class="zerti-wc-publish-actions d-grid gap-2 mt-3 pt-3 border-top">
                        <button type="submit" class="btn btn-primary btn-sm zerti-inventory-product-save">
                            <?php echo $is_new ? esc_html__('Publish', 'zerti') : esc_html__('Update', 'zerti'); ?>
                        </button>
                        <a href="<?php echo esc_url($back_url); ?>" class="btn btn-outline-secondary btn-sm zerti-inventory-product-back"><?php esc_html_e('Cancel', 'zerti'); ?></a>
                    </div>
                <?php endif; ?>
                <?php zerti_wc_postbox_close(); ?>

                <?php zerti_wc_postbox_open(__('Product image', 'zerti'), 'postimagediv'); ?>
                <div class="zerti-wc-product-image text-center">
                    <div class="zerti-inventory-product-edit__image-preview mb-2<?php echo $image_url === '' ? ' zerti-inventory-product-edit__image-preview--empty' : ''; ?>">
                        <?php if ($image_url !== '') : ?>
                            <img src="<?php echo esc_url($image_url); ?>" alt="" class="zerti-inventory-product-edit__image-img" decoding="async" />
                        <?php else : ?>
                            <i class="fa-solid fa-image" aria-hidden="true"></i>
                        <?php endif; ?>
                    </div>
                    <input type="hidden" name="image_id" value="<?php echo esc_attr((string) $image_id); ?>" data-zerti-product-image-id />
                    <?php if (!$read_only) : ?>
                        <div class="d-flex flex-wrap gap-2 justify-content-center">
                            <button type="button" class="btn btn-outline-secondary btn-sm" data-zerti-product-pick-image><?php esc_html_e('Set product image', 'zerti'); ?></button>
                            <button type="button" class="btn btn-outline-secondary btn-sm" data-zerti-product-remove-image<?php echo $image_id < 1 ? ' hidden' : ''; ?>><?php esc_html_e('Remove image', 'zerti'); ?></button>
                        </div>
                    <?php endif; ?>
                </div>
                <?php zerti_wc_postbox_close(); ?>

                <?php zerti_wc_postbox_open(__('Image gallery', 'zerti')); ?>
                <ul class="zerti-inventory-product-edit__gallery list-unstyled mb-2" data-zerti-product-gallery>
                    <?php foreach ($gallery_items as $item) : ?>
                        <?php
                        $gid = (int) ($item['id'] ?? 0);
                        $gurl = (string) ($item['url'] ?? '');
                        if ($gid < 1 || $gurl === '') {
                            continue;
                        }
                        ?>
                        <li class="zerti-inventory-product-edit__gallery-item" data-id="<?php echo esc_attr((string) $gid); ?>">
                            <img src="<?php echo esc_url($gurl); ?>" alt="" decoding="async" />
                            <?php if (!$read_only) : ?>
                                <button type="button" class="btn btn-sm btn-outline-danger" data-zerti-gallery-remove aria-label="<?php esc_attr_e('Delete', 'zerti'); ?>">&times;</button>
                            <?php endif; ?>
                            <input type="hidden" name="gallery_ids[]" value="<?php echo esc_attr((string) $gid); ?>" />
                        </li>
                    <?php endforeach; ?>
                </ul>
                <?php if (!$read_only) : ?>
                    <button type="button" class="btn btn-outline-secondary btn-sm w-100" data-zerti-product-pick-gallery><?php esc_html_e('Add gallery images', 'zerti'); ?></button>
                <?php endif; ?>
                <?php zerti_wc_postbox_close(); ?>

                <?php if ($category_tree !== []) : ?>
                    <?php zerti_wc_postbox_open(__('Product categories', 'zerti'), 'product_catdiv'); ?>
                    <div class="zerti-inventory-product-edit__cat-tree">
                        <?php zerti_product_edit_render_category_tree($category_tree, $selected_cats, $read_only); ?>
                    </div>
                    <?php zerti_wc_postbox_close(); ?>
                <?php endif; ?>

                <?php zerti_wc_postbox_open(__('Product tags', 'zerti'), 'tagsdiv-product_tag'); ?>
                <div class="zerti-product-tags-input position-relative" data-zerti-product-tags>
                    <input
                        type="text"
                        class="<?php echo esc_attr($fc); ?>"
                        name="product_tags"
                        data-zerti-product-tags-input
                        dir="rtl"
                        autocomplete="off"
                        placeholder="<?php esc_attr_e('Separate tags with commas', 'zerti'); ?>"
                        value="<?php echo esc_attr((string) $pick('product_tags', '')); ?>"
                        <?php echo esc_attr($ro); ?>
                    />
                    <ul class="zerti-product-tags-input__results list-unstyled mb-0" data-zerti-product-tags-results hidden></ul>
                </div>
                <p class="form-text mb-0 mt-2"><?php esc_html_e('Separate tags with commas. Existing tags are suggested as you type.', 'zerti'); ?></p>
                <?php zerti_wc_postbox_close(); ?>

                <?php if (!empty($pick('brands_available')) && zerti_product_brands_available()) : ?>
                    <?php
                    $brand_postbox_id = 'brandsdiv-' . sanitize_html_class(zerti_product_brand_taxonomy());
                    zerti_wc_postbox_open(__('Brands', 'zerti'), $brand_postbox_id);
                    ?>
                    <div class="zerti-product-tags-input position-relative" data-zerti-product-brands>
                        <input
                            type="text"
                            class="<?php echo esc_attr($fc); ?>"
                            name="product_brands"
                            data-zerti-product-brands-input
                            dir="rtl"
                            autocomplete="off"
                            placeholder="<?php esc_attr_e('Separate brands with commas', 'zerti'); ?>"
                            value="<?php echo esc_attr((string) $pick('product_brands', '')); ?>"
                            <?php echo esc_attr($ro); ?>
                        />
                        <ul class="zerti-product-tags-input__results list-unstyled mb-0" data-zerti-product-brands-results hidden></ul>
                    </div>
                    <p class="form-text mb-0 mt-2"><?php esc_html_e('Separate brands with commas. Existing brands are suggested as you type.', 'zerti'); ?></p>
                    <?php zerti_wc_postbox_close(); ?>
                <?php endif; ?>
            </aside>
        </div>

        <div class="zerti-inventory-product-edit__feedback mt-2" role="status" aria-live="polite" hidden></div>
    </form>
</div>
