<?php

defined('BASEPATH') or exit('No direct script access allowed');

class Ulasan_model extends CI_Model
{
    public function get_all()
    {
        $this->db->order_by('date_created', 'DESC');
        $this->db->join('produk', 'produk.id_produk=ulasan.id_produk',);
        $this->db->join('pembeli', 'pembeli.id_pembeli=ulasan.id_pembeli',);
        return $this->db->get('ulasan');
    }

    public function get_avg($id_produk)
    {
        $this->db->select_avg('rating', 'avg');
        $this->db->where('id_produk', $id_produk);
        return $this->db->get('ulasan');
    }

    public function get_rate($id_produk, $rating)
    {
        $this->db->where('id_produk', $id_produk);
        $this->db->where('rating', $rating);
        return $this->db->count_all_results('ulasan');
    }

    public function get_ulasan($id_produk)
    {
        $this->db->order_by('date_created', 'DESC');
        $this->db->where('id_produk', $id_produk);
        $this->db->join('pembeli', 'pembeli.id_pembeli=ulasan.id_pembeli',);
        return $this->db->get('ulasan');
    }

    public function add_ulasan($params)
    {
        $this->db->insert('ulasan', $params);
        return $this->db->insert_id();
    }

    public function add_reply($params)
    {
        $this->db->insert('reply', $params);
        return $this->db->insert_id();
    }
}