<?php

use Joomla\CMS\Factory;
use Joomla\CMS\Language\Text;
use Joomla\CMS\Router\Route;

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

class guruAdminControllerguruReviews extends guruAdminController
{
    public function resolveReports()
    {
        $input = $this->input;
        $id = $input->getInt('id');

        if (!$this->checkToken('post', false)) {
            $errorMsg = Text::_('JINVALID_TOKEN_NOTICE');
            $this->setRedirect(
                Route::_('index.php?option=com_guru&controller=guruReviews&task=viewReport&tmpl=component&id=' . $id, false),
                $errorMsg,
                'warning'
            );
        }

        $db = Factory::getDbo();
        $query = $db->getQuery(true)
            ->update('#__guru_review_report')
            ->set('resolved = 1')
            ->where('review_id = ' . $db->q($id));

        $db->setQuery($query)->execute();
        ob_clean();
        echo Text::_('GURU_REVIEW_REPORT_RESOLVED_PLEASE_WAIT');
        echo '
        <script>
            setTimeout(() => {
                window.parent.location.reload();
            }, 1000)
        </script>
        ';
        die();
    }

    public function viewReport()
    {
        $view = $this->getView("guruReviews", "html");
        $view->viewReport();
    }

    public function viewContent()
    {
        $view = $this->getView("guruReviews", "html");
        $view->viewContent();
    }

    public function display($cachable = false, $urlparams = array())
    {
        $view = $this->getView("guruReviews", "html");
        $model = $this->getModel("guruReviews");
        $view->setModel($model, true);
        $view->display();
    }

    public function publish()
    {
        $this->checkToken();

        $input = $this->input;
        $ids = $input->get('cid', []);

        if (is_array($ids) && $ids) {
            $db = Factory::getDbo();
            $query = $db->getQuery(true)
                ->update('#__guru_review')
                ->set('state = 1')
                ->where('id IN (' . implode(',', $ids) . ')');

            $db->setQuery($query)->execute();
        }

        $this->setRedirect(Route::_('index.php?option=com_guru&controller=guruReviews', false), Text::_('JPUBLISHED'));
    }

    public function unpublish()
    {
        $this->checkToken();

        $input = $this->input;
        $ids = $input->get('cid', []);

        if (is_array($ids) && $ids) {
            $db = Factory::getDbo();
            $query = $db->getQuery(true)
                ->update('#__guru_review')
                ->set('state = 0')
                ->where('id IN (' . implode(',', $ids) . ')');

            $db->setQuery($query)->execute();
        }

        $this->setRedirect(Route::_('index.php?option=com_guru&controller=guruReviews', false), Text::_('JUNPUBLISHED'));
    }

    public function pending()
    {
        $this->checkToken();

        $input = $this->input;
        $ids = $input->get('cid', []);

        if (is_array($ids) && $ids) {
            $db = Factory::getDbo();
            $query = $db->getQuery(true)
                ->update('#__guru_review')
                ->set('state = 2')
                ->where('id IN (' . implode(',', $ids) . ')');

            $db->setQuery($query)->execute();
        }

        $this->setRedirect(Route::_('index.php?option=com_guru&controller=guruReviews', false), Text::_('JUNPUBLISHED'));
    }
}
