<link href="/css/front/device_calculator.css" media="screen" rel="stylesheet" type="text/css" />
<script>
	function calculator(order_price = 0) {
		var cellDatas = Array.from(document.getElementsByClassName("vhost_cellDatas"));
		cellDatas.forEach(function(part) { //loop
			//console.log(part);
			order_price += parseInt((part.getAttribute('cost') - part.getAttribute('sale')) * part.options[part.selectedIndex].value);
			document.getElementById('price').value = order_price;
			document.getElementById('order_price').textContent = new Intl.NumberFormat().format(order_price);
		});
		var current = document.getElementById('paymentday');
		if (!current.selectedIndex) {
			alert("결제일을 선택해주세요");
			current.focus();
			return false
		}
	}
</script>
<div id="device_calculator">
	<?= form_open(URLS['addCart'], [
		'method' => 'post',
		"onsubmit" => 'return calculator()'
	]) ?>
	<input type="hidden" id="category" name="category" value="device">
	<input type="hidden" id="price" name="price">
	<table>
		<thead>
			<tr>
				<th scope="col" colspan="3">가상서버 견적 계산기</th>
			</tr>
		</thead>
		<tbody>
			<?php foreach ($viewDatas['cellDatas']['device']['categorys'] as $category) : ?>
				<?php
				$options = [];
				foreach ($viewDatas['cellDatas']['device']['options'][$category] as $entity) {
					$options[$entity->getPrimaryKey()] = $entity->getTitleWithPrice();
				}
				?>
				<tr>
					<td><?= lang("Device.CATEGORY.{$category}") ?></td>
					<td>
						<?= form_dropdown(
							$category,
							$options,
							old($category, 0),
							[
								'id' => $category,
								'class' => 'vhost_cellDatas',
								'onChange' => "calculator()"
							]
						) ?>
					</td>
				</tr>
			<?php endforeach ?>
			<tr>
				<th>결제일</th>
				<td class="column">
					<?php $paymentDayOptions = ['' => "결제일 선택"];
					for ($i = 1; $i <= 28; $i++) {
						$paymentDayOptions[$i] = "매월 {$i}일";
					}
					?>
					<?= form_dropdown('paymentday', $paymentDayOptions, old('paymentday', 25), ['id' => 'paymentday']);
					?>
				</td>
			</tr>
			<tr>
				<th class="none">주문금액</th>
				<td class="none" colspan="3">
					<span id="order_price">0</span>원
					<?= form_submit('', '신청', array("class" => "btn btn-outline btn-primary")); ?>
				</td>
			</tr>
		</tbody>
	</table>
	<?= form_close() ?>
	<script type="text/javascript">
		window.onload = calculator();
	</script>
	<?= $viewDatas['session']->getFlashdata('return_message') ? alert_CommonHelper($viewDatas['session']->getFlashdata('return_message')) : "" ?>
</div>