<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Mbarang extends CI_Model{

  public function __construct()
  {
    parent::__construct();
    //Codeigniter : Write Less Do More
  }

  public function get_list()
  {
    $this->db->join('kategori','kategori.idkategori = barang.kategori_idkategori');
    $this->db->join('mitra','mitra.idmitra = barang.mitra_idmitra');
    $this->db->where("kategori.type","1");
    $this->db->order_by("idbarang","DESC");
    $this->db->limit("16");
    return $this->db->get("barang");
  }

  public function get_list_recommended()
  {
    $this->db->join('kategori','kategori.idkategori = barang.kategori_idkategori');
    $this->db->join('mitra','mitra.idmitra = barang.mitra_idmitra');
    $this->db->order_by("idbarang","DESC");
    $this->db->limit("16");
    return $this->db->get("barang");
  }

  public function get_list_barang($limit = 24, $page= 0, $kategori = null, $cari = null, $idmitra = null, $sorting=null){
    $row = $limit * $page;
    $this->db->join('kategori','kategori.idkategori = barang.kategori_idkategori');
    $this->db->join('mitra','mitra.idmitra = barang.mitra_idmitra');
    $this->db->limit($limit,$row);
    $this->db->where("kategori.type","1");
    if ($kategori != "" || $kategori != null) {
      $this->db->where('kategori.nama_kategori', $kategori);
    }
    if ($idmitra != "" || $idmitra != null) {
      $this->db->where('mitra.idmitra', $idmitra);
    }
    if ($cari != "" || $cari != null) {
      $this->db->like("barang.nama_barang", $cari);
    }
    if ($sorting == "az") {
      $this->db->order_by("nama_barang","ASC");
    }elseif ($sorting == "za") {
      $this->db->order_by("nama_barang","DESC");
    }elseif ($sorting == "termurah") {
      $this->db->order_by("harga","ASC");
    }elseif ($sorting == "termahal") {
      $this->db->order_by("harga","DESC");
    }
    return $this->db->get('barang');
  }

  public function get_list_mitra($idmitra)
  {
    $this->db->select("*, IFNULL(SUM(detail_pemesanan.jumlah), 0) as terjual");
    $this->db->join("barang","barang.idbarang = view_stok.idbarang");
    $this->db->join("detail_pemesanan","detail_pemesanan.barang_idbarang = view_stok.idbarang", "LEFT");
    $this->db->where("view_stok.mitra_idmitra", $idmitra);
    $this->db->group_by("view_stok.idbarang");
    return $this->db->get("view_stok");
  }

  public function get_search($cari = null)
  {
    $this->db->join("kategori", "kategori.idkategori = barang.kategori_idkategori");
    if ($cari != null || $cari != "") {
      $this->db->like("barang.nama_barang", $cari);
      $this->db->or_like("kategori.nama_kategori", $cari);
    }
    return $this->db->get("barang");
  }

  public function get_Data($id)
  {
    $this->db->join("kategori","kategori.idkategori = barang.kategori_idkategori");
    $this->db->join("mitra","mitra.idmitra = barang.mitra_idmitra");
    $this->db->where("idbarang", $id);
    return $this->db->get("barang");
  }

  public function get_konfood($cari_food=null, $idmitra=null, $idkabupaten=null)
  {
    $this->db->select("mitra.*, barang.*");
    $this->db->join("mitra","mitra.idmitra = barang.mitra_idmitra");
    $this->db->join("kategori","kategori.idkategori = barang.kategori_idkategori");
    $this->db->where("kategori.type","2");
    if ($idmitra!=null || $idmitra!="") {
      $this->db->where("mitra.idmitra", $idmitra);
    }
    // if ($idkabupaten!=null || $idkabupaten!="") {
    //   $this->db->where("mitra.idkabupaten", $idkabupaten);
    // }
    if ($cari_food!=null || $cari_food!="") {
      $this->db->like("barang.nama_barang", $cari_food);
    }
    return $this->db->get("barang");
  }

}
