<?php
/**
 * @package	HikaShop for Joomla!
 * @version	4.7.4
 * @author	hikashop.com
 * @copyright	(C) 2010-2023 HIKARI SOFTWARE. All rights reserved.
 * @license	GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
 */
defined('_JEXEC') or die('Restricted access');
?><legend><?php echo JText::_('HIKASHOP_'.strtoupper($this->type).'_ADDRESS'); ?></legend>
<?php

$name = $this->type.'_address';
$fields_type = $this->type.'_fields';
$show_url = 'order&task=show&subtask='.$name.'&cid='.$this->order->order_id;
$save_url = 'order&task=save&subtask='.$name.'&cid='.$this->order->order_id;
$update_url = 'order&task=edit&subtask='.$name.'&cid='.$this->order->order_id;

if(!isset($this->edit) || $this->edit !== true ) {
?>
	<div class="hika_edit"><a class="btn btn-primary" href="<?php echo hikashop_completeLink($update_url, true);?>" onclick="return window.hikashop.get(this,'hikashop_order_field_<?php echo $name; ?>');">
		<i class="fas fa-pen"></i> <?php echo JText::_('HIKA_EDIT'); ?>
	</a></div>
<?php
} else {
?>
	<div class="hika_edit">
		<a class="btn btn-success" href="<?php echo hikashop_completeLink($save_url, true);?>" onclick="return window.hikashop.form(this,'hikashop_order_field_<?php echo $name; ?>');">
			<i class="fa fa-save"></i> <?php echo JText::_('HIKA_SAVE'); ?>
		</a>
		<a class="btn btn-danger" href="<?php echo hikashop_completeLink($show_url, true);?>" onclick="return window.hikashop.get(this,'hikashop_order_field_<?php echo $name; ?>');">
			<i class="fa fa-times"></i> <?php echo JText::_('HIKA_CANCEL'); ?>
		</a>
	</div>
<?php
}
$address =& $this->order->$name;
$display = 'field_backend';
if(!empty($this->order->$fields_type)) {
?>
<table class="admintable table">
<?php
	if(isset($this->edit) && $this->edit === true ) {
		foreach($this->order->$fields_type as $field){
			if($field->$display){
				$fieldname = $field->field_namekey;
?>
	<tr class="hikashop_<?php echo $this->type;?>order_address_<?php echo $fieldname;?>">
		<td class="key"><label><?php echo $this->fieldsClass->trans($field->field_realname);?></label></td>
		<td><?php
				$onWhat = 'onchange';
				if($field->field_type == 'radio')
					$onWhat = 'onclick';

				$field->table_name = 'order';
				echo $this->fieldsClass->display(
						$field,
						@$address->$fieldname,
						'data['.$name.']['.$fieldname.']',
						false,
						'', // disable toggleField for now as it's normally not needed ' ' . $onWhat . '="window.hikashop.toggleField(this.value,\''.$fieldname.'\',\''.$name.'\',0);"',
						false,
						$this->order->$fields_type,
						$address
				);
		?></td>
	</tr>
<?php
			}
		}
?>
	<tr class="hikashop_<?php echo $this->type;?>_history">
		<td class="key"><label><?php echo JText::_('HISTORY'); ?></label></td>
		<td>
			<span><input onchange="window.orderMgr.<?php echo $this->type;?>_history_changed(this);" type="checkbox" id="hikashop_history_<?php echo $this->type;?>_store" name="data[history][store_data]" value="1"/><label for="hikashop_history_<?php echo $this->type;?>_store" style="display:inline-block"><?php echo JText::_('SET_HISTORY_MESSAGE');?></label></span><br/>
			<textarea id="hikashop_history_<?php echo $this->type;?>_msg" name="data[history][msg]" style="display:none;"></textarea>
		</td>
	</tr>
</table>
<script type="text/javascript">
window.orderMgr.<?php echo $this->type;?>_history_changed = function(el) {
	var fields = ['hikashop_history_<?php echo $this->type;?>_msg'], displayValue = '';
	if(!el.checked) displayValue = 'none';
	window.hikashop.setArrayDisplay(fields, displayValue);
}
</script>
<?php

		echo JHTML::_( 'form.token' );
	} else {
		foreach($this->order->$fields_type as $field){
			if($field->$display){
				$fieldname = $field->field_namekey;
?>
	<tr class="hikashop_<?php echo $this->type;?>order_address_<?php echo $fieldname;?>">
		<td class="key"><label><?php echo $this->fieldsClass->trans($field->field_realname);?></label></td>
		<td><span><?php echo $this->fieldsClass->show($field, @$address->$fieldname);?></span></td>
	</tr>
<?php
			}
		}
	}
?></table>
<?php
}
?>
<script type="text/javascript">
window.orderMgr.update<?php echo ucfirst($this->type);?> = function() {
	window.hikashop.xRequest('<?php echo hikashop_completeLink('order&task=show&subtask='.$this->type.'_address&cid='.$this->order->order_id, true, false, true); ?>',{update:'hikashop_order_field_<?php echo $this->type; ?>_address'});
}
</script>
