// normal hosts
if (!$isDiscovered) {
    $cmbIPMIAuthtype = new CComboBox('ipmi_authtype', $ipmi_authtype);
    $cmbIPMIAuthtype->addItems(ipmiAuthTypes());
    $cmbIPMIAuthtype->addClass('openView');
    $cmbIPMIAuthtype->setAttribute('size', 7);
    $cmbIPMIAuthtype->addStyle('width: 170px;');
    $ipmiList->addRow(_('Authentication algorithm'), $cmbIPMIAuthtype);
    $cmbIPMIPrivilege = new CComboBox('ipmi_privilege', $ipmi_privilege);
    $cmbIPMIPrivilege->addItems(ipmiPrivileges());
    $cmbIPMIPrivilege->addClass('openView');
    $cmbIPMIPrivilege->setAttribute('size', 5);
    $cmbIPMIPrivilege->addStyle('width: 170px;');
    $ipmiList->addRow(_('Privilege level'), $cmbIPMIPrivilege);
} else {
    $cmbIPMIAuthtype = new CTextBox('ipmi_authtype_name', ipmiAuthTypes($dbHost['ipmi_authtype']), ZBX_TEXTBOX_SMALL_SIZE, true);
    $ipmiList->addRow(_('Authentication algorithm'), $cmbIPMIAuthtype);
    $cmbIPMIPrivilege = new CTextBox('ipmi_privilege_name', ipmiPrivileges($dbHost['ipmi_privilege']), ZBX_TEXTBOX_SMALL_SIZE, true);
    $ipmiList->addRow(_('Privilege level'), $cmbIPMIPrivilege);
}
$ipmiList->addRow(_('Username'), new CTextBox('ipmi_username', $ipmi_username, ZBX_TEXTBOX_SMALL_SIZE, $isDiscovered));
$ipmiList->addRow(_('Password'), new CTextBox('ipmi_password', $ipmi_password, ZBX_TEXTBOX_SMALL_SIZE, $isDiscovered));
$divTabs->addTab('ipmiTab', _('IPMI'), $ipmiList);
/*
 * Macros
 */
if (empty($macros)) {
    $macros = array(array('macro' => '', 'value' => ''));
}
$macrosView = new CView('common.macros', array('macros' => $macros, 'readonly' => $isDiscovered));
$divTabs->addTab('macroTab', _('Macros'), $macrosView->render());
$templatesTable = new CTable(_('No templates defined.'), 'formElementTable');
$templatesTable->setAttribute('style', 'min-width: 500px;');
$templatesTable->setAttribute('id', 'template_table');
$templatesTable->setHeader(array(_('Name'), _('Action')));
foreach ($this->data['templates'] as $templateid => $templateName) {
    $hostForm->addVar('templates[' . $templateid . ']', $templateName);
    $row = new CRow(array($templateName, new CButton('remove', _('Remove'), 'javascript: removeTemplate("' . $templateid . '");', 'link_menu')));
    $row->setAttribute('id', 'template_row_' . $templateid);
    $templatesTable->addRow($row);
}
$templatesDiv = new CDiv(array($templatesTable, new CButton('btn1', _('Add'), 'return PopUp("popup.php?srctbl=templates&srcfld1=hostid&srcfld2=host' . '&dstfrm=' . $hostForm->getName() . '&dstfld1=new_template&templated_hosts=1' . url_param($this->data['templates'], false, 'existed_templates') . '", 450, 450)', 'link_menu'), BR(), BR(), new CCheckBox('mass_replace_tpls', $this->data['mass_replace_tpls']), SPACE, _('Replace'), BR(), new CCheckBox('mass_clear_tpls', $this->data['mass_clear_tpls']), SPACE, _('Clear when unlinking')), 'objectgroup inlineblock border_dotted ui-corner-all');
$templatesDiv->setAttribute('id', 'templates_div');
$hostFormList->addRow(array(_('Link templates'), SPACE, new CVisibilityBox('visible[template_table]', !empty($this->data['visible']['template_table']) ? 'yes' : 'no', 'templates_div', _('Original'))), $templatesDiv);
// append ipmi to form list
$ipmiAuthtypeComboBox = new CComboBox('ipmi_authtype', $this->data['ipmi_authtype']);
$ipmiAuthtypeComboBox->addItems(ipmiAuthTypes());
$hostFormList->addRow(array(_('IPMI authentication algorithm'), SPACE, new CVisibilityBox('visible[ipmi_authtype]', isset($this->data['visible']['ipmi_authtype']), 'ipmi_authtype', _('Original'))), $ipmiAuthtypeComboBox);
$ipmiPrivilegeComboBox = new CComboBox('ipmi_privilege', $this->data['ipmi_privilege']);
$ipmiPrivilegeComboBox->addItems(ipmiPrivileges());
$hostFormList->addRow(array(_('IPMI privilege level'), SPACE, new CVisibilityBox('visible[ipmi_privilege]', isset($this->data['visible']['ipmi_privilege']), 'ipmi_privilege', _('Original'))), $ipmiPrivilegeComboBox);
$hostFormList->addRow(array(_('IPMI username'), SPACE, new CVisibilityBox('visible[ipmi_username]', isset($this->data['visible']['ipmi_username']), 'ipmi_username', _('Original'))), new CTextBox('ipmi_username', $this->data['ipmi_username'], ZBX_TEXTBOX_SMALL_SIZE));
$hostFormList->addRow(array(_('IPMI password'), SPACE, new CVisibilityBox('visible[ipmi_password]', isset($this->data['visible']['ipmi_password']), 'ipmi_password', _('Original'))), new CTextBox('ipmi_password', $this->data['ipmi_password'], ZBX_TEXTBOX_SMALL_SIZE));
// append inventories to form list
$inventoryModesComboBox = new CComboBox('inventory_mode', $this->data['inventory_mode'], 'submit()');
$inventoryModesComboBox->addItem(HOST_INVENTORY_DISABLED, _('Disabled'));
$inventoryModesComboBox->addItem(HOST_INVENTORY_MANUAL, _('Manual'));
$inventoryModesComboBox->addItem(HOST_INVENTORY_AUTOMATIC, _('Automatic'));
$hostFormList->addRow(array(_('Inventory mode'), SPACE, new CVisibilityBox('visible[inventory_mode]', isset($this->data['visible']['inventory_mode']), 'inventory_mode', _('Original'))), $inventoryModesComboBox);
$hostInventoryTable = DB::getSchema('host_inventory');
if ($this->data['inventory_mode'] != HOST_INVENTORY_DISABLED) {
    foreach ($this->data['inventories'] as $field => $fieldInfo) {
    foreach ($data['linked_templates'] as $template) {
        $tmplList->addVar('templates[]', $template['templateid']);
        $templateLink = (new CLink($template['name'], 'templates.php?form=update&templateid=' . $template['templateid']))->setTarget('_blank');
        $linkedTemplateTable->addRow($templateLink, null, 'conditions_' . $template['templateid']);
    }
    $tmplList->addRow(_('Linked templates'), (new CDiv($linkedTemplateTable))->addClass(ZBX_STYLE_TABLE_FORMS_SEPARATOR)->setAttribute('style', 'min-width: ' . ZBX_TEXTAREA_BIG_WIDTH . 'px;'));
}
$divTabs->addTab('templateTab', _('Templates'), $tmplList);
/*
 * IPMI
 */
if ($data['flags'] != ZBX_FLAG_DISCOVERY_CREATED) {
    $cmbIPMIAuthtype = new CListBox('ipmi_authtype', $data['ipmi_authtype'], 7, null, ipmiAuthTypes());
    $cmbIPMIPrivilege = new CListBox('ipmi_privilege', $data['ipmi_privilege'], 5, null, ipmiPrivileges());
} else {
    $cmbIPMIAuthtype = [(new CTextBox('ipmi_authtype_name', ipmiAuthTypes($data['ipmi_authtype']), true))->setWidth(ZBX_TEXTAREA_SMALL_WIDTH), new CVar('ipmi_authtype', $data['ipmi_authtype'])];
    $cmbIPMIPrivilege = [(new CTextBox('ipmi_privilege_name', ipmiPrivileges($data['ipmi_privilege']), true))->setWidth(ZBX_TEXTAREA_SMALL_WIDTH), new CVar('ipmi_privilege', $data['ipmi_privilege'])];
}
$divTabs->addTab('ipmiTab', _('IPMI'), (new CFormList())->addRow(_('Authentication algorithm'), $cmbIPMIAuthtype)->addRow(_('Privilege level'), $cmbIPMIPrivilege)->addRow(_('Username'), (new CTextBox('ipmi_username', $data['ipmi_username'], $data['flags'] == ZBX_FLAG_DISCOVERY_CREATED))->setWidth(ZBX_TEXTAREA_SMALL_WIDTH))->addRow(_('Password'), (new CTextBox('ipmi_password', $data['ipmi_password'], $data['flags'] == ZBX_FLAG_DISCOVERY_CREATED))->setWidth(ZBX_TEXTAREA_SMALL_WIDTH)));
/*
 * Macros
 */
$macrosView = new CView('hostmacros', ['macros' => $data['macros'], 'show_inherited_macros' => $data['show_inherited_macros'], 'is_template' => false, 'readonly' => $data['flags'] == ZBX_FLAG_DISCOVERY_CREATED]);
$divTabs->addTab('macroTab', _('Macros'), $macrosView->render());
$inventoryFormList = new CFormList('inventorylist');
$inventoryFormList->addRow(null, (new CRadioButtonList('inventory_mode', (int) $data['inventory_mode']))->addValue(_('Disabled'), HOST_INVENTORY_DISABLED)->addValue(_('Manual'), HOST_INVENTORY_MANUAL)->addValue(_('Automatic'), HOST_INVENTORY_AUTOMATIC)->setEnabled($data['flags'] != ZBX_FLAG_DISCOVERY_CREATED)->setModern(true));
if ($data['flags'] == ZBX_FLAG_DISCOVERY_CREATED) {
    $inventoryFormList->addVar('inventory_mode', $data['inventory_mode']);
}
$hostInventoryTable = DB::getSchema('host_inventory');
$hostInventoryFields = getHostInventories();
$divTabs->addTab('hostTab', _('Host'), $hostList);
// templates
$tmplList = new CFormList('tmpllist');
foreach ($templates as $tid => $temp_name) {
    $frmHost->addVar('templates[' . $tid . ']', $temp_name);
    $tmplList->addRow($temp_name, array(new CSubmit('unlink[' . $tid . ']', _('Unlink'), null, 'link_menu'), SPACE, SPACE, isset($original_templates[$tid]) ? new CSubmit('unlink_and_clear[' . $tid . ']', _('Unlink and clear'), null, 'link_menu') : SPACE));
}
$tmplAdd = new CButton('add', _('Add'), 'return PopUp("popup.php?srctbl=templates&srcfld1=hostid&srcfld2=host' . '&dstfrm=' . $frmHost->getName() . '&dstfld1=new_template&templated_hosts=1' . url_param($templates, false, 'existed_templates') . '", 450, 450)', 'link_menu');
$tmplList->addRow($tmplAdd, SPACE);
$divTabs->addTab('templateTab', _('Templates'), $tmplList);
/*
 * IPMI
 */
$ipmiList = new CFormList('ipmilist');
$cmbIPMIAuthtype = new CComboBox('ipmi_authtype', $ipmi_authtype);
$cmbIPMIAuthtype->addItems(ipmiAuthTypes());
$cmbIPMIAuthtype->setAttribute('size', 7);
$cmbIPMIAuthtype->addStyle('width: 170px;');
$ipmiList->addRow(_('Authentication algorithm'), $cmbIPMIAuthtype);
$cmbIPMIPrivilege = new CComboBox('ipmi_privilege', $ipmi_privilege);
$cmbIPMIPrivilege->addItems(ipmiPrivileges());
$cmbIPMIPrivilege->setAttribute('size', 5);
$cmbIPMIPrivilege->addStyle('width: 170px;');
$ipmiList->addRow(_('Privilege level'), $cmbIPMIPrivilege);
$ipmiList->addRow(_('Username'), new CTextBox('ipmi_username', $ipmi_username, ZBX_TEXTBOX_SMALL_SIZE));
$ipmiList->addRow(_('Password'), new CTextBox('ipmi_password', $ipmi_password, ZBX_TEXTBOX_SMALL_SIZE));
$divTabs->addTab('ipmiTab', _('IPMI'), $ipmiList);
/*
 * Macros
 */
if (empty($macros)) {
        $tmplList->addVar('templates[' . $template['templateid'] . ']', $template['templateid']);
        $templateLink = (new CLink($template['name'], 'templates.php?form=update&templateid=' . $template['templateid']))->setTarget('_blank');
        $linkedTemplateTable->addRow([$templateLink, (new CCol((new CSubmit('unlink[' . $template['templateid'] . ']', _('Unlink')))->addClass(ZBX_STYLE_BTN_LINK)))->addClass(ZBX_STYLE_NOWRAP)]);
        $ignoreTemplates[$template['templateid']] = $template['name'];
    }
    $tmplList->addRow(_('Linked templates'), (new CDiv($linkedTemplateTable))->addClass(ZBX_STYLE_TABLE_FORMS_SEPARATOR)->setAttribute('style', 'min-width: ' . ZBX_TEXTAREA_BIG_WIDTH . 'px;'));
    // create new linked template table
    $newTemplateTable = (new CTable())->addRow([(new CMultiSelect(['name' => 'add_templates[]', 'objectName' => 'templates', 'ignored' => $ignoreTemplates, 'popup' => ['parameters' => 'srctbl=templates&srcfld1=hostid&srcfld2=host&dstfrm=' . $frmHost->getName() . '&dstfld1=add_templates_&templated_hosts=1&multiselect=1']]))->setWidth(ZBX_TEXTAREA_STANDARD_WIDTH)])->addRow([(new CSubmit('add_template', _('Add')))->addClass(ZBX_STYLE_BTN_LINK)]);
    $tmplList->addRow(_('Link new templates'), (new CDiv($newTemplateTable))->addClass(ZBX_STYLE_TABLE_FORMS_SEPARATOR)->setAttribute('style', 'min-width: ' . ZBX_TEXTAREA_BIG_WIDTH . 'px;'));
}
$divTabs->addTab('templateTab', _('Templates'), $tmplList);
// display inherited parameters only for hosts prototypes on hosts
if ($parentHost['status'] != HOST_STATUS_TEMPLATE) {
    // IPMI
    $ipmiList = new CFormList();
    $ipmiList->addRow(_('Authentication algorithm'), (new CTextBox('ipmi_authtype', ipmiAuthTypes($parentHost['ipmi_authtype']), true))->setWidth(ZBX_TEXTAREA_SMALL_WIDTH));
    $ipmiList->addRow(_('Privilege level'), (new CTextBox('ipmi_privilege', ipmiPrivileges($parentHost['ipmi_privilege']), true))->setWidth(ZBX_TEXTAREA_SMALL_WIDTH));
    $ipmiList->addRow(_('Username'), (new CTextBox('ipmi_username', $parentHost['ipmi_username'], true))->setWidth(ZBX_TEXTAREA_SMALL_WIDTH));
    $ipmiList->addRow(_('Password'), (new CTextBox('ipmi_password', $parentHost['ipmi_password'], true))->setWidth(ZBX_TEXTAREA_SMALL_WIDTH));
    $divTabs->addTab('ipmiTab', _('IPMI'), $ipmiList);
    // macros
    $macros = $parentHost['macros'];
    if ($data['show_inherited_macros']) {
        $macros = mergeInheritedMacros($macros, getInheritedMacros(zbx_objectValues($hostPrototype['templates'], 'templateid')));
    }
    $macros = array_values(order_macros($macros, 'macro'));
    $macrosView = new CView('hostmacros', ['macros' => $macros, 'show_inherited_macros' => $data['show_inherited_macros'], 'is_template' => false, 'readonly' => true]);
    $divTabs->addTab('macroTab', _('Macros'), $macrosView->render());
}
$inventoryFormList = (new CFormList('inventorylist'))->addRow(null, (new CRadioButtonList('inventory_mode', (int) $hostPrototype['inventory']['inventory_mode']))->addValue(_('Disabled'), HOST_INVENTORY_DISABLED)->addValue(_('Manual'), HOST_INVENTORY_MANUAL)->addValue(_('Automatic'), HOST_INVENTORY_AUTOMATIC)->setEnabled($hostPrototype['templateid'] == 0)->setModern(true));
$divTabs->addTab('inventoryTab', _('Host inventory'), $inventoryFormList);
// append proxy to form list
$proxyComboBox = new CComboBox('proxy_hostid', $data['proxy_hostid']);
$proxyComboBox->addItem(0, _('(no proxy)'));
foreach ($data['proxies'] as $proxie) {
    $proxyComboBox->addItem($proxie['hostid'], $proxie['host']);
}
$hostFormList->addRow([_('Monitored by proxy'), SPACE, (new CVisibilityBox('visible[proxy_hostid]', 'proxy_hostid', _('Original')))->setChecked(isset($data['visible']['proxy_hostid']))], $proxyComboBox);
// append status to form list
$hostFormList->addRow([_('Status'), SPACE, (new CVisibilityBox('visible[status]', 'status', _('Original')))->setChecked(isset($data['visible']['status']))], new CComboBox('status', $data['status'], null, [HOST_STATUS_MONITORED => _('Enabled'), HOST_STATUS_NOT_MONITORED => _('Disabled')]));
$templatesFormList = new CFormList('templatesFormList');
// append templates table to from list
$newTemplateTable = (new CTable())->addRow([(new CMultiSelect(['name' => 'templates[]', 'objectName' => 'templates', 'data' => $data['linkedTemplates'], 'popup' => ['parameters' => 'srctbl=templates&srcfld1=hostid&srcfld2=host&dstfrm=' . $hostView->getName() . '&dstfld1=templates_&templated_hosts=1&multiselect=1']]))->setWidth(ZBX_TEXTAREA_STANDARD_WIDTH)])->addRow([(new CDiv([new CLabel([(new CCheckBox('mass_replace_tpls'))->setChecked($data['mass_replace_tpls'] == 1), _('Replace')], 'mass_replace_tpls'), BR(), new CLabel([(new CCheckBox('mass_clear_tpls'))->setChecked($data['mass_clear_tpls'] == 1), _('Clear when unlinking')], 'mass_clear_tpls')]))->addClass('floatleft')]);
$templatesFormList->addRow([_('Link templates'), SPACE, (new CVisibilityBox('visible[templates]', 'templateDiv', _('Original')))->setChecked(isset($data['visible']['templates']))], (new CDiv($newTemplateTable))->setId('templateDiv')->addClass(ZBX_STYLE_TABLE_FORMS_SEPARATOR)->setAttribute('style', 'min-width: ' . ZBX_TEXTAREA_BIG_WIDTH . 'px;'));
$ipmiFormList = new CFormList('ipmiFormList');
// append ipmi to form list
$ipmiFormList->addRow([_('IPMI authentication algorithm'), SPACE, (new CVisibilityBox('visible[ipmi_authtype]', 'ipmi_authtype', _('Original')))->setChecked(isset($data['visible']['ipmi_authtype']))], new CComboBox('ipmi_authtype', $data['ipmi_authtype'], null, ipmiAuthTypes()));
$ipmiFormList->addRow([_('IPMI privilege level'), SPACE, (new CVisibilityBox('visible[ipmi_privilege]', 'ipmi_privilege', _('Original')))->setChecked(isset($data['visible']['ipmi_privilege']))], new CComboBox('ipmi_privilege', $data['ipmi_privilege'], null, ipmiPrivileges()));
$ipmiFormList->addRow([_('IPMI username'), SPACE, (new CVisibilityBox('visible[ipmi_username]', 'ipmi_username', _('Original')))->setChecked(isset($data['visible']['ipmi_username']))], (new CTextBox('ipmi_username', $data['ipmi_username']))->setWidth(ZBX_TEXTAREA_SMALL_WIDTH));
$ipmiFormList->addRow([_('IPMI password'), SPACE, (new CVisibilityBox('visible[ipmi_password]', 'ipmi_password', _('Original')))->setChecked(isset($data['visible']['ipmi_password']))], (new CTextBox('ipmi_password', $data['ipmi_password']))->setWidth(ZBX_TEXTAREA_SMALL_WIDTH));
$inventoryFormList = new CFormList('inventoryFormList');
// append inventories to form list
$inventoryFormList->addRow([_('Inventory mode'), SPACE, (new CVisibilityBox('visible[inventory_mode]', 'inventory_mode_div', _('Original')))->setChecked(isset($data['visible']['inventory_mode']))], (new CDiv((new CRadioButtonList('inventory_mode', (int) $data['inventory_mode']))->addValue(_('Disabled'), HOST_INVENTORY_DISABLED)->addValue(_('Manual'), HOST_INVENTORY_MANUAL)->addValue(_('Automatic'), HOST_INVENTORY_AUTOMATIC)->setModern(true)))->setId('inventory_mode_div'));
$hostInventoryTable = DB::getSchema('host_inventory');
foreach ($data['inventories'] as $field => $fieldInfo) {
    if (!isset($data['host_inventory'][$field])) {
        $data['host_inventory'][$field] = '';
    }
    if ($hostInventoryTable['fields'][$field]['type'] == DB::FIELD_TYPE_TEXT) {
        $fieldInput = (new CTextArea('host_inventory[' . $field . ']', $data['host_inventory'][$field]))->setWidth(ZBX_TEXTAREA_BIG_WIDTH);
    } else {
        $field_length = $hostInventoryTable['fields'][$field]['length'];