<?php

use Joomla\CMS\Factory;
use Joomla\CMS\Language\Text;
use Joomla\CMS\Uri\Uri;

defined('_JEXEC') or die('Restricted access');

$input = Factory::getApplication()->input;
$user = Factory::getUser();
$courseId = $input->getInt('course_id', 0);
$rating = $input->getInt('rating');

$reviewSummary = guruHelper::getReviewSummary($courseId);
$countReview = $reviewSummary->get('count');
$reviewed = $this->currentUserReview->id;
?>
<div class="review-wrapper">
    <h2><?php echo Text::_('GURU_STUDENT_FEEDBACK') ?></h2>
    <div class="review-sumary" style="display: flex;">
        <div class="avg-rating-wrap">
            <div class="rating-number"><?php echo number_format($reviewSummary->get('score'), 1) ?></div>
            <?php echo guruHelper::renderRatingScore(['score' => floor($reviewSummary->get('score'))]) ?>
            <div class="rating-label"><?php echo Text::_('GURU_COURSE_RATING') ?></div>
            <div class="rating-label">
                <?php echo $countReview > 1 
                    ? Text::sprintf('GURU_NUM_REVIEWS', number_format($countReview)) 
                    : Text::sprintf('GURU_NUM_REVIEW', $countReview);
                ?>
            </div>
        </div>
        <div class="rating-list">
            <?php foreach ($reviewSummary->get('items') as $key => $value) : ?>
                <div class="rating-star-wrap">
                    <div class="rating-star"><?php echo guruHelper::renderRatingScore(['score' => $key]) ?></div>
                    <div class="avg-star">
                        <div class="bar-full">
                            <div class="bar-active" style="width: <?php echo $value ?>%;"></div>
                        </div>
                        <span><?php echo $value ?>%</span>
                    </div>
                </div>
            <?php endforeach ?>
        </div>
    </div>
    <div class="review-heading">
        <h2><?php echo Text::_('GURU_REVIEW') ?></h2>
        <div class="rating-filter">
            <select>
                <option value=""><?php echo Text::_('GURU_ALL_RATING') ?></option>
                <option value="5" <?php echo $rating == 5 ? 'selected' : '' ?>>5</option>
                <option value="4" <?php echo $rating == 4 ? 'selected' : '' ?>>4</option>
                <option value="3" <?php echo $rating == 3 ? 'selected' : '' ?>>3</option>
                <option value="2" <?php echo $rating == 2 ? 'selected' : '' ?>>2</option>
                <option value="1" <?php echo $rating == 1 ? 'selected' : '' ?>>1</option>
            </select>
        </div>
    </div>
    <div class="review-list">
        <?php foreach ($this->items as $item) : ?>
            <?php
            $avatar = $item->image && file_exists(JPATH_ROOT . $item->image) ? $item->image : '/components/com_guru/images/author_no_avatar.png';
            $avatar = Uri::root(true) . $avatar;
            ?>
            <div class="review-item <?php echo $item->user_id === $user->id ? 'is-yours' : '' ?>">
                <div class="review-content">
                    <div class="user-info">
                        <div class="user-avatar">
                            <img src="<?php echo $avatar ?>" alt="<?php echo htmlspecialchars($item->firstname . ' ' . $item->lastname) ?>">
                        </div>
                        <div class="user-name">
                            <h5>
                                <?php echo htmlspecialchars($item->firstname . ' ' . $item->lastname) ?>
                                <?php echo $item->user_id === $user->id ? '<span>Your review</span>' : '' ?>
                            </h5>
                        </div>
                        <div class="review-meta">
                            <div class="user-rating">
                                <?php echo guruHelper::renderRatingScore(['score' => $item->rating]) ?>
                            </div>
                            <div class="review-date">
                                <?php
                                $postedTime = Text::sprintf('GURU_REVIEW_TIME_TIPS', $item->review_time);
                                $timeTitle = $postedTime;
                                if ($item->modified_time) {
                                    $modifiedTime = Text::sprintf('GURU_REVIEW_UPDATE_TIME_TIPS', $item->modified_time);
                                    $timeTitle .= ' ' . $modifiedTime;
                                }
                                ?>
                                <span title="<?php echo $timeTitle ?>"><?php echo $this->renderReviewTime($item->review_time) ?></span>
                            </div>
                        </div>
                    </div>

                    <div class="review-text">
                        <?php echo nl2br(htmlspecialchars($item->content)) ?>
                    </div>
                    <?php if ($user->id) : ?>
                        <div class="review-report">
                            <a class="review-report-btn" href="javascript:;" data-review-id="<?php echo $item->id ?>">Report</a>
                            <div class="report-type-list">
                                <div class="report-type" data-type="1"><?php echo Text::_('GURU_REVIEW_REPORT_1') ?></div>
                                <div class="report-type" data-type="2"><?php echo Text::_('GURU_REVIEW_REPORT_2') ?></div>
                                <div class="report-type" data-type="3"><?php echo Text::_('GURU_REVIEW_REPORT_3') ?></div>
                            </div>
                        </div>
                    <?php endif ?>
                </div>
            </div>
        <?php endforeach ?>
        <?php if (!$this->items) : ?>
            <div class="review-empty"><?php echo Text::_('GUUR_REVIEW_EMPTY') ?></div>
        <?php endif ?>
    </div>
    <div class="pagination-review">
        <?php echo $this->pagination->getPagesLinks() ?>
    </div>
    <div class="current-user-review" style="<?php echo $reviewed ? 'display: block;' : 'display: none;' ?>">
        <?php
        $reviewState = $this->currentUserReview->state;
        $stateClass = '';
        $reviewNotice = '';
        if (!$reviewState) {
            $stateClass = 'review-unpublished';
            $reviewNotice = Text::_('GURU_YOUR_REVIEW_IS_UNPUBLISHED');
        } else if ($reviewState == 2) {
            $stateClass = 'review-pending';
            $reviewNotice = Text::_('GURU_YOUR_REVIEW_IS_PENDING');
        }
        ?>
        <?php if ($reviewNotice): ?>
            <div class="review-notice <?php echo $stateClass ?>">
                <?php echo $reviewNotice ?>
            </div>
        <?php endif ?>
        <h2><?php echo Text::_('GURU_YOUR_REVIEW') ?></h2>
        <div class="review-rating">
            <i class="fa fa-thumbs-up"></i>
            <span><?php echo Text::_('GURU_YOU_RATE') ?></span><?php echo guruHelper::renderRatingScore(['score' => $this->currentUserReview->rating]) ?>
        </div>
        <div class="review-time">
            <i class="fa fa-clock"></i>
            <span><?php echo Text::_('GURU_YOUR_TIME') ?></span><?php echo $this->currentUserReview->review_time ?>
        </div>
        <div class="review-content">
            <?php echo nl2br(htmlspecialchars($this->currentUserReview->content)) ?>
        </div>
        <div class="review-actions">
            <button class="review-edit" type="button"><?php echo Text::_('GURU_EDIT') ?></button>
            <button class="review-delete" type="button"><?php echo Text::_('GURU_DELETE') ?></button>
        </div>
    </div>
    <?php if ($user->id) : ?>
        <div class="review-composer" style="<?php echo $reviewed ? 'display: none;' : 'display: block;' ?>">
            <h2><?php echo Text::_('GURU_WRITE_YOUR_REVIEW') ?></h2>
            <div class="rating-selection">
                <div><?php echo Text::_('GURU_RATE_THIS_COURSE') ?></div>
                <div class="rating">
                    <input type="radio" id="guru-star5" name="rating" value="5" <?php echo $this->currentUserReview->rating == 5 ? 'checked' : '' ?> />
                    <label for="guru-star5" title="Awesome - 5 stars"></label>
                    <input type="radio" id="guru-star4" name="rating" value="4" <?php echo $this->currentUserReview->rating == 4 ? 'checked' : '' ?> />
                    <label for="guru-star4" title="Pretty good - 4 stars"></label>
                    <input type="radio" id="guru-star3" name="rating" value="3" <?php echo $this->currentUserReview->rating == 3 ? 'checked' : '' ?> />
                    <label for="guru-star3" title="Meh - 3 stars"></label>
                    <input type="radio" id="guru-star2" name="rating" value="2" <?php echo $this->currentUserReview->rating == 2 ? 'checked' : '' ?> />
                    <label for="guru-star2" title="Kinda bad - 2 stars"></label>
                    <input type="radio" id="guru-star1" name="rating" value="1" <?php echo $this->currentUserReview->rating == 1 ? 'checked' : '' ?> />
                    <label for="guru-star1" title="Sucks big time - 1 star"></label>
                </div>
            </div>
            <div class="content-composer">
                <textarea cols="30" rows="10"><?php echo htmlspecialchars($this->currentUserReview->content) ?></textarea>
            </div>
            <div class="review-actions">
                <button class="review-submit" type="button"><?php echo Text::_('GURU_SUBMIT') ?></button>
                <?php if ($this->currentUserReview->id) : ?>
                    <button class="review-cancel" type="button"><?php echo Text::_('GURU_CANCEL') ?></button>
                <?php endif ?>
            </div>
        </div>
    <?php endif ?>
</div>