<?php

use Joomla\CMS\Factory;
use Joomla\CMS\HTML\HTMLHelper;
use Joomla\CMS\Language\Text;
use Joomla\CMS\Router\Route;
use Joomla\CMS\Uri\Uri;

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

$input = Factory::getApplication()->input;
$id = $input->getInt('id');

$db = Factory::getDbo();
$query = $db->getQuery(true)
    ->select([
        'a.user_id',
        'a.report_date',
        'a.report_type',
        'a.resolved',
        'CONCAT(b.firstname, " ", b.lastname) as reporter',
    ])
    ->from($db->qn('#__guru_review_report', 'a'))
    ->innerJoin($db->qn('#__guru_customer', 'b') . ' ON b.id = a.user_id')
    ->where('a.review_id = ' . $db->q($id))
    ->order('a.resolved ASC, a.report_date DESC');

$rows = $db->setQuery($query)->loadObjectList();
?>
<form action="<?php echo Route::_('index.php?option=com_guru&controller=guruReviews') ?>" method="post">
    <div>
        <button type="submit" class="btn btn-primary"><?php echo Text::_('GURU_REVIEW_RESOLVE_REPORTS') ?></button>
    </div>
    <input type="hidden" name="task" value="resolveReports">
    <input type="hidden" name="id" value="<?php echo $id ?>">
    <?php echo HTMLHelper::_('form.token') ?>
</form>
<br>
<table class="table table-striped">
    <thead>
        <tr>
            <th><?php echo Text::_('GURU_REVIEW_REPORTER') ?></th>
            <th><?php echo Text::_('GURU_REVIEW_REPORT_CONTENT') ?></th>
            <th><?php echo Text::_('GURU_REVIEW_REPORT_RESOLVED') ?></th>
        </tr>
    </thead>
    <tbody>
        <?php foreach ($rows as $row) : ?>
            <tr>
                <td>
                    <div>
                        <a href="<?php echo Route::_('index.php?option=com_guru&controller=guruCustomers&task=edit&cid[]=' . $row->user_id) ?>" target="_blank"><?php echo $row->reporter ?></a>
                    </div>
                    <div><small><?php echo Text::sprintf('GURU_REVIEW_REPORT_AT', $row->report_date) ?></small></div>
                </td>
                <td>
                    <?php
                    $reportText = '';
                    switch ($row->report_type) {
                        case '1':
                            $reportText = Text::_('GURU_REVIEW_REPORT_1');
                            break;
                        case '2':
                            $reportText = Text::_('GURU_REVIEW_REPORT_2');
                            break;

                        default:
                            $reportText = Text::_('GURU_REVIEW_REPORT_3');
                            break;
                    }

                    echo $reportText;
                    ?>
                </td>
                <td>
                <?php
                    switch ($row->resolved) {
                        case '1':
                            $icon = 'administrator/components/com_guru/images/tick.png';
                            break;

                        default:
                            $icon = 'administrator/components/com_guru/images/publish_x.png';
                            break;
                    }
                    ?>
                    <img src="<?php echo Uri::root() . $icon ?>" alt="">
                </td>
            </tr>
        <?php endforeach ?>
    </tbody>
</table>