<?php
class ModelCatalogStoreRating extends Model {

  public function addRating($data){
    $sql = "DELETE FROM " . DB_PREFIX . "store_rating WHERE customer_id = " . (int)$this->customer->getId();
		$query = $this->db->query($sql);

    $sql = "INSERT INTO " . DB_PREFIX . "store_rating SET customer_id = " . (int)$this->customer->getId() . ", ".
    " author = '" . $this->db->escape($this->customer->getFirstName()) . ' ' . $this->db->escape($this->customer->getLastName()) . "', ".
    " text = '" . $this->db->escape($data['text']) . "', ".
    " rating = " . (int)$data['rating'] . ", ".
    " allow = " . (int)$data['allow'] . ", ".
    " date_added = NOW() ";

		$query = $this->db->query($sql);
	}

  public function editRating($store_rating_id, $data) {
    $this->db->query("UPDATE " . DB_PREFIX . "store_rating SET author = '" . $this->db->escape($data['author']) . "', text = '" . $this->db->escape(strip_tags($data['text'])) . "', rating = '" . (int)$data['rating'] . "', status = '" . (int)$data['status'] . "', date_added = NOW() WHERE store_rating_id = '" . (int)$store_rating_id . "'");
  }

  public function deleteRating($store_rating_id) {
    $this->db->query("DELETE FROM " . DB_PREFIX . "store_rating WHERE store_rating_id = " . (int)$store_rating_id);
  }

  public function getRating($store_rating_id) {
    $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "store_rating WHERE store_rating_id = " . (int)$store_rating_id);
    return $query->row;
  }

  public function getRatings($data = array()) {
    $sql = "SELECT * FROM " . DB_PREFIX . "store_rating WHERE date_added IS NOT NULL";

    if(isset($data['status'])){
      $sql .= " AND status = ".$data['status'];
    }

    if(isset($data['allow'])){
      $sql .= " AND allow = " . $data['allow'];
    }

		if(isset($data['notification_status'])){
			$sql .= " AND notification_status=".$this->db->escape($data['notification_status']);
		}

    $sort_data = array(
      'author',
      'rating',
      'status',
      'date_added'
    );

    if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
      $sql .= " ORDER BY " . $data['sort'];
    } else {
      $sql .= " ORDER BY date_added";
    }

    if (isset($data['order']) && ($data['order'] == 'DESC')) {
      $sql .= " DESC";
    } else {
      $sql .= " ASC";
    }

    if (isset($data['start']) || isset($data['limit'])) {
      if ($data['start'] < 0) {
        $data['start'] = 0;
      }

      if ($data['limit'] < 1) {
        $data['limit'] = 20;
      }

      $sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
    }

    $query = $this->db->query($sql);

    return $query->rows;
  }

  public function getTotalRatings() {
    $query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "store_rating");

    return $query->row['total'];
  }

  public function getTotalRatingsAwaitingApproval() {
    $query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "store_rating WHERE status = '0'");

    return $query->row['total'];
  }

    public function getTotalRatingsNotApproval() {
        $query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "store_rating WHERE status = '0' AND notification_status = '1' and allow = '1'");

        return $query->row['total'];
    }

    public function updateStatusNotificationRating($store_rating_id, $status) {
    $this->load->type('tipocomum');
    $this->db->query("UPDATE " . DB_PREFIX . "store_rating SET status = '" . (int)$status . "', notification_status = ".Type_Status::Tp_Desabilitado." WHERE store_rating_id = '" . (int)$store_rating_id . "'");
  }

  public function updateStatusRating($store_rating_id, $status) {
    $this->load->type('tipocomum');
    $this->db->query("UPDATE " . DB_PREFIX . "store_rating SET status = '" . (int)$status . "' WHERE store_rating_id = '" . (int)$store_rating_id . "'");
  }

}
