<?= $this->extend('layouts/front') ?>
<?= $this->section('content') ?>
<link href="/css/front/content.css" media="screen" rel="stylesheet" type="text/css" />
<div id="content">
  <div><?= html_entity_decode($viewDatas['currentCategory']->head) ?></div>
  <table class="form table table-bordered table-striped">
    <tbody>
      <?php foreach ($viewDatas['fields'] as $field) : ?>
        <tr>
          <td class="label"><?= getFieldLabel_ProductHelper($field, $viewDatas) ?></td>
          <td class="column">
            <?= getFieldView_ProductHelper($field, $viewDatas['entity'], $viewDatas) ?>
            <?= validation_show_error($field); ?>
          </td>
        </tr>
      <?php endforeach; ?>
    </tbody>
  </table>
  <?php if ($viewDatas['entity']->status == DEFAULTS['STATUS']) : ?>
    <?= form_open(URLS['addCart'], ['method' => 'post']) ?>
    <?= form_hidden("product_uid", $viewDatas['entity']->getPrimaryKey()) ?>
    <input type="hidden" id="price" name="price" value="<?= ($viewDatas['entity']->price) * 1 ?>">
    <?php
    $quantityOptions = [DEFAULTS['EMPTY'] => "구매수량 선택"];
    for ($i = 1; $i <= $viewDatas['entity']->stock; $i++) {
      $quantityOptions[$i] = $i . "개";
    }
    ?>
    <table class="form table table-bordered table-striped" style="width:400px;">
      <tr>
        <td class=" label" nowrap>구매금액</td>
        <td class="column">
          <span id="order_price"><?= number_format($viewDatas['entity']->price * 1) ?></span>원
        </td>
        <td rowspan="3">
          <?= form_submit('', '구매하기', array("class" => "btn btn-outline btn-primary")); ?>
        </td>
      </tr>
      <tr>
        <td class="label" nowrap>구매수량</td>
        <td class="column">
          <?= form_dropdown('quantity', $quantityOptions, old('quantity', 1), ['onChange' => "cal_price(this.options[this.selectedIndex].value)"]); ?>
          <?= validation_show_error('quantity'); ?>
        </td>
      </tr>
      <?php if ($viewDatas['entity']->type == 'rental') : ?>
        <?php $paymentDayOptions = [DEFAULTS['EMPTY'] => "결제일 선택"];
        for ($i = 1; $i <= 28; $i++) {
          $paymentDayOptions[$i] = "매월 {$i}일";
        }
        ?>
        <tr>
          <td class="label" nowrap>결제일</td>
          <td class="column">
            <?= form_dropdown('paymentday', $paymentDayOptions, old('paymentday', 25)) ?>
            <?= validation_show_error('paymentday'); ?>
          </td>
        </tr>
      <?php endif ?>
    </table>
    <?= form_close(); ?>
    <script>
      function cal_price(quantity) {
        var price = 0;
        price = <?= $viewDatas['entity']->price ?> * quantity;;
        document.getElementById('price').value = price;
        document.getElementById('order_price').textContent = new Intl.NumberFormat().format(price);
      }
    </script>
  <?php else : ?>
    이제품은 현재[<?= lang('Product.STATUS.' . $viewDatas['entity']->status) ?>]입니다.
  <?php endif ?>
  <div><?= html_entity_decode($viewDatas['currentCategory']->tail) ?></div>
</div>
<?= $this->endSection() ?>