{extends file=$smarty.const.SMARTY_LAYOUT_DASHBOARD_WIDGET_DEFAULT}

{block name="content"}
	{if $widget['data']['currenciesList'] and $widget['data']['currencyTypesList']}
		<table style="min-width:300px">
			<tr>
				<td class="inputs">
					{$widget['data']['form']->render()}
				</td>
			</tr>
		</table>
		<table class="simpleList w100p currencyExchangeRates">
			<thead>
				<tr>
					<th>{$widgetLang['currencyTh']}</th>
					{foreach $widget['data']['currencyTypesList'] as $currencyType => $currencyName}
						<th>{$currencyName}</th>
					{/foreach}
				</tr>
			</thead>

			<tbody>
				{foreach $widget['data']['currenciesList'] as $currency}
					<tr class="{cycle values="odd,even"}" data-currency="{$currency}">
						<td class="bold">{$currency}</td>
						{foreach $widget['data']['rates'] as $data}
							{if $data['currency'] == $currency}
								<td data-currency="{$data['currency']}" data-currencyType="{$data['currencyType']}" style="text-align:right;">{$data['rate']}</td>
							{/if}
						{/foreach}
					</tr>
				{/foreach}
			</tbody>
		</table>
	{else}

		{if !$widget['data']['currenciesList']}
			<div class="messageDivContainer layout-inline">
				{message type="info" message=$widgetLang['messages']['noCurrList']}
			</div>
		{else}
			<div class="messageDivContainer layout-inline">
				{message type="info" message=$widgetLang['messages']['noCurrTypesList']}
			</div>
		{/if}
	{/if}
{/block}

{block name="js"}
	<script type="text/javascript">
		window.currencyExchangeRates_callMethod_callMethod = function(parameters, response) {
			if (response['status'] != 'OK') {
				alert('The request failed due to a technical problem.');
				return false;
			}

			// the actual result
			for (var i = 0; i < response['rates'].length; i++) {
				var searchingAttribute = '[data-currency=' + response['rates'][i]['currency'] + '][data-currencyType=' + response['rates'][i]['currencyType'] + ']' ;
				var td = $(parameters['src']).down('table.currencyExchangeRates >tbody td' + searchingAttribute);
				if (td) {
					td.update(response['rates'][i]['rate']);
				}
				else {
					throw new Error("Invalid table td");
				}
			}
		}
	</script>
{/block}
