<?php

class ControllerProductProductReviews extends Controller {

    protected $referencePathFile = 'product/product_reviews';

    public function index($setting = array()) {
        $language = $this->language->load($this->referencePathFile);
        $this->data = array_merge($this->data ,$language);
        $this->loadData();
        $this->loadScript();

        $this->render($this->referencePathFile);
    }

    private function loadData() {
        $this->data['status'] = $this->config->get('avise_me_status');

        $this->language->load('product/product');

        if (isset($this->data['status']) && ($this->data['status'])) {
            $this->language->load('product/avise_me');
        }

        $limit = 10;

        $this->load->model('catalog/review');

        $this->data['text_on'] = $this->language->get('text_on');
        $this->data['text_no_reviews'] = $this->language->get('text_no_reviews');

        $this->data['total_reviews'] = $this->model_catalog_review->getTotalReviewsByProductId($this->request->get['product_id']);
        $this->data['reviews_per_page'] = $limit;

        $this->data['reviews'] = array();

        $results = $this->model_catalog_review->getReviewsByProductId($this->request->get['product_id'],0, $limit);
        $this->data['count_reviews'] = count($results);

        foreach ($results as $result) {
            $this->data['reviews'][] = array(
                'author' => $result['author'],
                'text' => $result['text'],
                'rating' => (int)$result['rating'],
                'date_added' => date($this->language->get('date_format_short'), strtotime($result['date_added']))
            );
        }
  
        $this->data['page_reviews'] = $this->render('product/review');
    }

    public function getReviews() {

        if (isset($this->request->get['page']) && ($this->request->get['page'])) {
            $page = $this->request->get['page'];
        } else {
            $page = 1;
        }

        if (isset($this->request->get['limit']) && ($this->request->get['limit'])) {
            $limit = $this->request->get['limit'];
        } else {
            $limit = 10;
        }

        $this->load->model('catalog/review');

        $results = $this->model_catalog_review->getReviewsByProductId($this->request->get['product_id'], ($page - 1), $limit);
        $this->data['count_reviews'] = count($results);

        foreach ($results as $result) {
            $this->data['reviews'][] = array(
                'author' => $result['author'],
                'text' => $result['text'],
                'rating' => (int)$result['rating'],
                'date_added' => date($this->language->get('date_format_short'), strtotime($result['date_added']))
            );
        }
        
        $this->response->setOutputJson(["html" => $this->render('product/review'), "total" => count($results)]);
    }

    protected function loadScript()
    {
        $this->document->addScript(array(
            'public/js/product/product_reviews.js'
        ));
    }
}