<?php
/**
 * Maho
 *
 * @category    design
 * @package     default_default
 * @copyright   Copyright (c) 2006-2020 Magento, Inc. (https://magento.com)
 * @copyright   Copyright (c) 2021-2024 The OpenMage Contributors (https://openmage.org)
 * @copyright   Copyright (c) 2024 Maho (https://mahocommerce.com)
 * @license     https://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
 */

/**
 * Template for Mage_Adminhtml_Block_Catalog_Product_Edit
 *
 * @var Mage_Adminhtml_Block_Catalog_Product_Edit $this
 */
?>
<div class="content-header">
    <h3><?= $this->getHeader() ?></h3>
    <p class="content-buttons form-buttons"><?= $this->getBackButtonHtml() ?>
    <?= $this->getCancelButtonHtml() ?>
    <?php if($this->getProductId()): ?>
        <?= $this->getDeleteButtonHtml() ?>
        <?php if($this->getProductSetId() && $this->getIsConfigured()): ?>
            <?= $this->getDuplicateButtonHtml() ?>
        <?php endif ?>
    <?php endif ?>
    <?php if($this->getProductSetId() && $this->getIsConfigured()): ?>
        <?= $this->getSaveButtonHtml() ?>
        <?= $this->getSaveAndEditButtonHtml() ?>
    <?php endif ?>
    </p>
</div>
<form action="<?= $this->getSaveUrl() ?>" method="post" id="product_edit_form" enctype="multipart/form-data">
    <?= $this->getBlockHtml('formkey') ?>
    <div style="display:none"></div>
</form>
<script type="text/javascript">
//<![CDATA[
    var productTemplateSyntax = /(^|.|\r|\n)({{(\w+)}})/;
    var productForm = new varienForm('product_edit_form', '<?= $this->getValidationUrl() ?>');
    productForm._processValidationResult = function(transport) {
        var response = transport.responseText.evalJSON();
        if (response.error){
            if (response.attribute && $(response.attribute)) {
                $(response.attribute).setHasError(true, productForm);
                Validation.ajaxError($(response.attribute), response.message);
                $(response.attribute).focus();
            }
            else if ($('messages')) {
                $('messages').innerHTML = '<ul class="messages"><li class="error-msg"><ul><li>' + response.message + '</li></ul></li></ul>';
            }
        }
        else{
            productForm._submit();
        }
    };
    function saveAndContinueEdit(urlTemplate) {
        var template = new Template(urlTemplate, productTemplateSyntax);
        var url = template.evaluate({tab_id:product_info_tabsJsTabs.activeTab.id});
        productForm.submit(url);
    }
    function setSettings(urlTemplate, setElement, typeElement) {
        var template = new Template(urlTemplate, productTemplateSyntax);
        setLocation(template.evaluate({attribute_set:$F(setElement),type:$F(typeElement)}));
    }

     function setSuperSettings(urlTemplate, attributesClass, validateField) {
        var attributesFields = $$('.' + attributesClass);
        var attributes = Form.serializeElements(attributesFields, true).attribute;
        if(typeof attributes == 'string') {
            attributes = [attributes];
        }

        if(!attributes) {
            $(validateField).value = 'no-attributes';
        } else {
            $(validateField).value = 'has-attributes';
        }

        if (productForm.validator.validate()) {
            var template = new Template(urlTemplate, productTemplateSyntax);
            var url = template.evaluate({
                attributes: btoa(attributes.join(',')).replace(new RegExp('/','g'),'%2F').replace(new RegExp('=','g'),'%3D')
            });
            setLocation(url);
        }
    }

    function checkMaxLength(Object, MaxLen)
    {
        if (Object.value.length > MaxLen-1) {
            Object.value = Object.value.substr(0, MaxLen);
        }
        return 1;
    }

Event.observe(window, 'load', function() {
    var objName = '<?= $this->getSelectedTabId() ?>';
    if (objName) {
        obj = $(objName);
        //IE fix (bubbling event model)
        product_info_tabsJsTabs.setSkipDisplayFirstTab();
        product_info_tabsJsTabs.showTabContent(obj);
    }
    Product.AttributesBridge.setTabsObject(product_info_tabsJsTabs);
});

Validation.addAllThese([
    ['validate-super-product-attributes', '<?= Mage::helper('core')->jsQuoteEscape(Mage::helper('catalog')->__('Please select one or more attributes.')) ?>', function(v) {
                return (v != "no-attributes");
            }]]);
//]]>
</script>
