<?php
/*
** Copyright (C) 2001-2026 Zabbix SIA
**
** This program is free software: you can redistribute it and/or modify it under the terms of
** the GNU Affero General Public License as published by the Free Software Foundation, version 3.
**
** This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
** without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
** See the GNU Affero General Public License for more details.
**
** You should have received a copy of the GNU Affero General Public License along with this program.
** If not, see <https://www.gnu.org/licenses/>.
**/


/**
 * @var CPartial $this
 * @var array    $data
 */

if ($data['readonly'] && !$data['macros']) {
	$table = new CObject(_('No macros found.'));
}
else {
	$link = null;
	$is_hostprototype = array_key_exists('parent_hostid', $data);
	$inherited_width = $is_hostprototype ? ZBX_TEXTAREA_MACRO_INHERITED_WIDTH : ZBX_TEXTAREA_MACRO_VALUE_WIDTH;

	$table = (new CTable())
		->setId('tbl_macros')
		->addClass(ZBX_STYLE_TEXTAREA_FLEXIBLE_CONTAINER)
		->addClass('inherited-macros-table');

	if ($data['has_inline_validation']) {
		$table
			->setAttribute('data-field-type', 'set')
			->setAttribute('data-field-name', 'macros');
	}

	if (CWebUser::getType() == USER_TYPE_SUPER_ADMIN) {
		$link = (new CLink(_('configure'), (new CUrl('zabbix.php'))
				->setArgument('action', 'macros.edit')
				->getUrl()
			))
			->setTarget('_blank');
		$link = [' (', $link, ')'];
	}

	$table->setColumns([
		(new CTableColumn(_('Macro')))->addClass('table-col-macro'),
		(new CTableColumn(_('Effective value')))->addClass('table-col-value'),
		!$data['readonly'] ? (new CTableColumn())->addClass('table-col-action') : null,
		$is_hostprototype ? (new CTableColumn())->addClass('table-col-arrow') : null,
		$is_hostprototype ? (new CTableColumn(_('Parent host value')))->addClass('table-col-parent-value') : null,
		(new CTableColumn())->addClass('table-col-arrow'),
		(new CTableColumn(_('Template value')))->addClass('table-col-template-value'),
		(new CTableColumn())->addClass('table-col-arrow'),
		(new CTableColumn([_('Global value'), $link]))->addClass('table-col-global-value')
	]);

	foreach ($data['macros'] as $i => $macro) {
		$skip_from_submit = $data['has_inline_validation']
			&& !($macro['inherited_type'] & ZBX_PROPERTY_OWN) ? 1 : null;

		$value = array_key_exists('value', $macro) ? $macro['value'] : null;
		$macro_value = (new CMacroValue($macro['type'], 'macros['.$i.']', $value, false))
			->setReadonly($data['readonly']
				|| !($macro['discovery_state'] & CControllerHostMacrosList::DISCOVERY_STATE_CONVERTING)
				|| !($macro['inherited_type'] & ZBX_PROPERTY_OWN)
			)
			->setErrorContainer($data['has_inline_validation'] ? 'macros_'.$i.'_error_container' : null)
			->setErrorLabel($data['has_inline_validation'] ? _('Effective value') : null)
			->setAttribute('data-skip-from-submit', $skip_from_submit);

		$macro_cell = [
			(new CTextAreaFlexible('macros['.$i.'][macro]', $macro['macro']))
				->setReadonly($data['readonly']
					|| $macro['discovery_state'] != CControllerHostMacrosList::DISCOVERY_STATE_MANUAL
					|| $macro['inherited_type'] & ZBX_PROPERTY_INHERITED
				)
				->addClass('macro')
				->setWidth(ZBX_TEXTAREA_MACRO_WIDTH)
				->setAttribute('placeholder', '{$MACRO}')
				->setErrorContainer($data['has_inline_validation'] ? 'macros_'.$i.'_error_container' : null)
				->disableSpellcheck()
				->setErrorLabel($data['has_inline_validation'] ? _('Macro') : null)
				->setAttribute('data-skip-from-submit', $skip_from_submit)
		];

		$macro_cell[] = (new CInput('hidden', 'macros['.$i.'][discovery_state]', $macro['discovery_state']))
			->setAttribute('data-field-type', 'hidden')
			->setAttribute('data-skip-from-submit', $skip_from_submit);

		if (array_key_exists('hostmacroid', $macro)) {
			$macro_cell[] = (new CInput('hidden', 'macros['.$i.'][hostmacroid]', $macro['hostmacroid']))
				->setAttribute('data-field-type', 'hidden')
				->setAttribute('data-skip-from-submit', $skip_from_submit);
		}

		$macro_cell[] = (new CInput('hidden', 'macros['.$i.'][inherited_type]', $macro['inherited_type']))
			->setAttribute('data-field-type', 'hidden')
			->setAttribute('data-skip-from-submit', $skip_from_submit);

		if ($macro['inherited_type'] & ZBX_PROPERTY_INHERITED) {
			$inherited_macro = $macro[$macro['inherited_level']];
			$macro_cell[] = (new CInput('hidden', 'macros['.$i.'][inherited][value]', $inherited_macro['value']))
				->setAttribute('data-field-type', 'hidden')
				->setAttribute('data-skip-from-submit', $skip_from_submit);
			$macro_cell[] = (new CInput('hidden', 'macros['.$i.'][inherited][description]', $inherited_macro['description']))
				->setAttribute('data-field-type', 'hidden')
				->setAttribute('data-skip-from-submit', $skip_from_submit);
			$macro_cell[] = (new CInput('hidden', 'macros['.$i.'][inherited][macro_type]', $inherited_macro['type']))
				->setAttribute('data-field-type', 'hidden')
				->setAttribute('data-skip-from-submit', $skip_from_submit);
		}

		if ($macro['discovery_state'] != CControllerHostMacrosList::DISCOVERY_STATE_MANUAL) {
			$macro_cell[] = (new CInput('hidden', 'macros['.$i.'][original_value]', $macro['original']['value']))
				->setAttribute('data-field-type', 'hidden')
				->setAttribute('data-skip-from-submit', $skip_from_submit);
			$macro_cell[] = (new CInput('hidden', 'macros['.$i.'][original_description]', $macro['original']['description']))
				->setAttribute('data-field-type', 'hidden')
				->setAttribute('data-skip-from-submit', $skip_from_submit);
			$macro_cell[] = (new CInput('hidden', 'macros['.$i.'][original_macro_type]', $macro['original']['type']))
				->setAttribute('data-field-type', 'hidden')
				->setAttribute('data-skip-from-submit', $skip_from_submit);
		}

		if (array_key_exists('allow_revert', $macro)) {
			$macro_value->addRevertButton();
			$macro_value->setRevertButtonVisibility($macro['type'] != ZBX_MACRO_TYPE_SECRET
				|| array_key_exists('value', $macro)
			);

			$macro_cell[] = new CVar('macros['.$i.'][allow_revert]', '1');
		}

		if (!$data['readonly']) {
			// buttons
			$action_buttons = [];
			if ($macro['inherited_type'] & ZBX_PROPERTY_OWN) {
				if ($macro['discovery_state'] == CControllerHostMacrosList::DISCOVERY_STATE_CONVERTING) {
					$action_buttons[] = (new CButton('macros['.$i.'][change_state]', _('Revert')))
						->addClass(ZBX_STYLE_BTN_LINK)
						->addClass('element-table-set-manual');
				}
				elseif ($macro['discovery_state'] == CControllerHostMacrosList::DISCOVERY_STATE_AUTOMATIC) {
					$action_buttons[] = (new CButton('macros['.$i.'][change_state]', _x('Change', 'verb')))
						->addClass(ZBX_STYLE_BTN_LINK)
						->addClass('element-table-set-manual');
				}

				if (($macro['inherited_type'] & ZBX_PROPERTY_BOTH) == ZBX_PROPERTY_BOTH) {
					$action_buttons[] = (new CButton('macros['.$i.'][change_inheritance]', _x('Remove', 'verb')))
						->addClass(ZBX_STYLE_BTN_LINK)
						->addClass('element-table-change');
				}
				else {
					$action_buttons[] = (new CButton('macros['.$i.'][remove]', _x('Remove', 'verb')))
						->addClass(ZBX_STYLE_BTN_LINK)
						->addClass('element-table-remove');
				}
			}
			elseif ($macro['inherited_type'] & ZBX_PROPERTY_INHERITED) {
				$action_buttons[] = (new CButton('macros['.$i.'][change_inheritance]', _x('Change', 'verb')))
					->addClass(ZBX_STYLE_BTN_LINK)
					->addClass('element-table-change');
			}
		}

		$row = [
			(new CCol($macro_cell))->addClass(ZBX_STYLE_TEXTAREA_FLEXIBLE_PARENT),
			(new CCol($macro_value))
				->addClass(ZBX_STYLE_TEXTAREA_FLEXIBLE_PARENT)
				->addClass(ZBX_STYLE_NOWRAP),
			!$data['readonly'] ? (new CCol(new CHorList($action_buttons)))->addClass(ZBX_STYLE_NOWRAP) : null
		];

		// Parent host macro value.
		if ($is_hostprototype) {
			$row[] = array_key_exists('parent_host', $macro) ? LARR() : '';
			$row[] = (new CDiv(array_key_exists('parent_host', $macro) ? '"'.$macro['parent_host']['value'].'"' : null))
				->setAdaptiveWidth($inherited_width)
				->addClass(ZBX_STYLE_OVERFLOW_ELLIPSIS);
		}

		// Template macro value.
		$template_macro = null;

		if (array_key_exists('template', $macro)) {
			if ($macro['template']['rights'] == PERM_READ_WRITE) {
				$template_url = (new CUrl('zabbix.php'))
					->setArgument('action', 'popup')
					->setArgument('popup', 'template.edit')
					->setArgument('templateid', $macro['template']['templateid'])
					->getUrl();

				$link = new CLink($macro['template']['name'], $template_url);
			}
			else {
				$link = new CSpan($macro['template']['name']);
			}

			$template_macro = [$link, NAME_DELIMITER, '"'.$macro['template']['value'].'"'];
		}

		$row[] = array_key_exists('template', $macro) ? LARR() : '';
		$row[] = (new CDiv(array_key_exists('template', $macro) ? $template_macro : null))
			->setAdaptiveWidth($inherited_width)
			->addClass(ZBX_STYLE_OVERFLOW_ELLIPSIS);

		// Global macro value.
		$row[] = array_key_exists('global', $macro) ? LARR() : '';
		$row[] = (new CDiv(array_key_exists('global', $macro) ? '"'.$macro['global']['value'].'"' : null))
			->setAdaptiveWidth($inherited_width)
			->addClass(ZBX_STYLE_OVERFLOW_ELLIPSIS);

		$description_readonly = ($data['readonly']
			|| $macro['discovery_state'] == CControllerHostMacrosList::DISCOVERY_STATE_AUTOMATIC)
			|| !($macro['inherited_type'] & ZBX_PROPERTY_OWN);

		$table
			->addRow($row, 'form_row')
			->addRow((new CRow([
				(new CCol([
					(new CTextAreaFlexible('macros['.$i.'][description]', $macro['description']))
						->setErrorContainer('macros_'.$i.'_error_container')
						->setMaxlength(DB::getFieldLength('hostmacro', 'description'))
						->setAdaptiveWidth(ZBX_TEXTAREA_STANDARD_WIDTH)
						->setAttribute('placeholder', _('description'))
						->setReadonly($description_readonly)
						->setAttribute('data-skip-from-submit', $skip_from_submit),
					($macro['discovery_state'] != CControllerHostMacrosList::DISCOVERY_STATE_MANUAL)
						? (new CSpan(_('(created by host discovery)')))->addClass(ZBX_STYLE_GREY)
						: null
				]))
					->addClass(ZBX_STYLE_TEXTAREA_FLEXIBLE_PARENT)
					->addClass(CControllerHostMacrosList::MACRO_TEXTAREA_PARENT)
					->setColSpan(count($row))
				]))->addClass('form_row')
			)
			->addRow($data['has_inline_validation']
				? (new CCol())
					->addClass(ZBX_STYLE_ERROR_CONTAINER)
					->setAttribute('colspan', count($row))
					->setId('macros_'.$i.'_error_container')
				: null
			);
	}

	if (!$data['readonly']) {
		$table->setFooter(new CCol(
			(new CButton('macro_add', _('Add')))
				->addClass(ZBX_STYLE_BTN_LINK)
				->addClass('element-table-add')
		));
	}
}

$table->show();

// Initializing input secret and macro value init script separately.
(new CScriptTag("jQuery('.input-secret').inputSecret();"))->show();
(new CScriptTag("jQuery('.macro-input-group').macroValue();"))->show();
