<?php
/**
 * Maho
 *
 * @category    design
 * @package     base_default
 * @copyright   Copyright (c) 2006-2020 Magento, Inc. (https://magento.com)
 * @copyright   Copyright (c) 2021-2022 The OpenMage Contributors (https://openmage.org)
 * @copyright   Copyright (c) 2024 Maho (https://mahocommerce.com)
 * @license     https://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
 */
?>
<div class="form-add">
    <h2><?= $this->__('Write Your Own Review') ?></h2>
    <?php if ($this->getAllowWriteReviewFlag()): ?>
    <form action="<?= $this->getAction() ?>" method="post" id="review-form">
        <?= $this->getBlockHtml('formkey') ?>
        <fieldset>
            <?= $this->getChildHtml('form_fields_before') ?>
            <h3><?= $this->__("You're reviewing:") ?> <span><?= $this->escapeHtml($this->getProductInfo()->getName()) ?></span></h3>
            <?php if( $this->getRatings() && $this->getRatings()->getSize()): ?>
                <h4><?= $this->__('How do you rate this product?') ?> <em class="required">*</em></h4>
                <span id="input-message-box"></span>
                <table class="data-table" id="product-review-table">
                    <col />
                    <col width="1" />
                    <col width="1" />
                    <col width="1" />
                    <col width="1" />
                    <col width="1" />
                    <thead>
                        <tr>
                            <th>&nbsp;</th>
                            <th><span class="nobr"><?= $this->__('1 star') ?></span></th>
                            <th><span class="nobr"><?= $this->__('2 stars') ?></span></th>
                            <th><span class="nobr"><?= $this->__('3 stars') ?></span></th>
                            <th><span class="nobr"><?= $this->__('4 stars') ?></span></th>
                            <th><span class="nobr"><?= $this->__('5 stars') ?></span></th>
                        </tr>
                    </thead>
                    <tbody>
                    <?php foreach ($this->getRatings() as $_rating): ?>
                        <tr>
                            <th><?= $this->escapeHtml($_rating->getRatingCode()) ?></th>
                        <?php foreach ($_rating->getOptions() as $_option): ?>
                            <td class="value"><input type="radio" name="ratings[<?= $_rating->getId() ?>]" id="<?= $this->escapeHtml($_rating->getRatingCode()) ?>_<?= $_option->getValue() ?>" value="<?= $_option->getId() ?>" class="radio" /></td>
                        <?php endforeach ?>
                        </tr>
                    <?php endforeach ?>
                    </tbody>
                </table>
                <input type="hidden" name="validate_rating" class="validate-rating" value="" />
            <?php endif ?>
                <ul class="form-list">
                    <li>
                        <label for="nickname_field" class="required"><em>*</em><?= $this->__('Nickname') ?></label>
                        <div class="input-box">
                            <input type="text" name="nickname" id="nickname_field" class="input-text required-entry" value="<?= $this->escapeHtml($data->getNickname()) ?>" />
                        </div>
                    </li>
                    <li>
                        <label for="summary_field" class="required"><em>*</em><?= $this->__('Summary of Your Review') ?></label>
                        <div class="input-box">
                            <input type="text" name="title" id="summary_field" class="input-text required-entry" value="<?= $this->escapeHtml($data->getTitle()) ?>" />
                        </div>
                    </li>
                    <li>
                        <label for="review_field" class="required"><em>*</em><?= $this->__('Review') ?></label>
                        <div class="input-box">
                            <textarea name="detail" id="review_field" cols="5" rows="3" class="required-entry"><?= $this->escapeHtml($data->getDetail()) ?></textarea>
                        </div>
                    </li>
                </ul>
            </fieldset>
            <div class="buttons-set">
                <button type="submit" title="<?= $this->__('Submit Review') ?>" class="button"><?= $this->__('Submit Review') ?></button>
            </div>
    </form>
    <script type="text/javascript">
        var dataForm = new VarienForm('review-form');
        Validation.add('validate-rating',
            '<?= Mage::helper('core')->jsQuoteEscape($this->__('Please select one of each of the ratings above')) ?>',
            function(v) {
                const trs = document.getElementById('product-review-table').getElementsByTagName('tr');
                let error = 1;
                for (let j = 1; j < trs.length; j++) {
                    const inputs = trs[j].getElementsByTagName('input');
                    for (let i = 0; i < inputs.length; i++) {
                        if (inputs[i].checked === true) {
                            error = 0;
                        }
                    }
                    if (error === 1) {
                        return false;
                    } else {
                        error = 1;
                    }
                }
                return true;
            }
        );
    </script>
    <?php else: ?>
    <p class="review-nologged" id="review-form">
        <?= $this->__('Only registered users can write reviews. Please, <a href="%s">log in</a> or <a href="%s">register</a>', $this->getLoginLink(), Mage::helper('customer')->getRegisterUrl()) ?>
    </p>
    <?php endif ?>
</div>
