<?php
/**
 * Magento 2 Google Analytics 4 (GA4) GTM
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Anowave license that is
 * available through the world-wide-web at this URL:
 * https://www.anowave.com/license-agreement/
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade this extension to newer
 * version in the future.
 *
 * @category 	Anowave
 * @package 	Anowave_Ec
 * @copyright 	Copyright (c) 2026 Anowave (https://www.anowave.com/)
 * @license  	https://www.anowave.com/license-agreement/
 */
 
namespace Anowave\Ec\Block\Field;

use Magento\Framework\Data\Form\Element\AbstractElement;

use Magento\Config\Block\System\Config\Form\Field;

class Checkboxes extends Field
{
    const HIDDEN = 'NONE';

	/**
	 * Constructor 
	 * 
	 * @param \Magento\Backend\Block\Template\Context $context
	 * @param array $data
	 */
    public function __construct
    (
    	\Magento\Backend\Block\Template\Context $context,
    	array $data = [])
    {
    	parent::__construct($context, $data);
    }
	
	protected function _getElementHtml(AbstractElement $element)
    {
        $options = (array) $element->getValues();

        $values = explode(chr(44), (string) $element->getValue());

        $name = $element->getName();

        $html = '<div>';

        $html = sprintf('<input type="hidden" name="%s[]" value="%s" />', $name, static::HIDDEN);

        foreach ($options as $option) 
        {
            $value = $option['value'];
            $label = $option['label'];

            $checked = in_array($value, $values) ? 'checked="checked"' : '';

            $html .= sprintf('<div style="padding:2px 0px 2px 0px"><label><input type="checkbox" name="%s[]" value="%s" %s> %s</label></div>',$name,$value,$checked,$label);
        }

        $html .= '<div style="padding:10px 0px 10px 0px"><sup style="color:rgba(255, 0, 0, 1)">*</sup> <small>indicates that event is tracked via subsequent fetch() request to avoid caching issues.</small></div>';

        $html .= '</div>';

        return $html;
    }
}