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

class Malamat extends CI_Model{

  /**

  * @author Fendrik Nurul Jadid <fendrik1311@gmail.com>

  * @since v.1.0

  **/


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

  public function get_data($id_user){
    return $this->db
    ->where("data_user_iddata_user",$id_user)
    ->get("alamat_user")->result();
  }

  public function get_data_edit($id){
    return $this->db
    ->where("idalamat_user",$id)
    ->get("alamat_user")->row();
  }

  function get_alamat_utama($id_user){
    return $this->db
    ->where("data_user_iddata_user",$id_user)
    ->where("status_default","1")
    ->get("alamat_user");
  }

  public function get_alamat_user($iduser = null)
  {
    $this->db->select("alamat_user.*, user.iduser, data_user.telp");
    $this->db->join("data_user","data_user.iddata_user = user.data_user_iddata_user","left");
    $this->db->join("alamat_user","alamat_user.data_user_iddata_user = data_user.iddata_user","left");
    if ($iduser != null || $iduser != '') {
      // code...
      $this->db->where("user.iduser",$iduser);
      // $this->db->where("alamat_user.status_default",1); //1 adalah untuk status alamat utama
    }
    return $this->db->get("user");
  }

  public function get_Provinsi($index=0, $end=0)
  {
    if ($index > 0) {
      $this->db->limit($index, $end);
    }
    $this->db->order_by("name");
    return $this->db->get("provinces");
  }

  public function get_Kabupaten($provinsi)
  {
    $this->db->select("regencie.*, provinces.name as provinsi");
    $this->db->join("provinces","provinces.id = regencie.province_id");
    $this->db->where("provinces.name", $provinsi);
    $this->db->order_by("regencie.name");
    return $this->db->get("regencie");
  }

  public function get_Kecamatan($kabupaten)
  {
    $this->db->select("districts.*, regencie.name as kabupaten");
    $this->db->join("regencie","regencie.id = districts.regency_id");
    $this->db->where("regencie.name", $kabupaten);
    $this->db->order_by("name");
    return $this->db->get("districts");
  }

  public function get_desa($kecamatan)
  {
    $this->db->select("villages.*, districts.name as kecamatan");
    $this->db->join("districts","districts.id = villages.district_id");
    $this->db->where("districts.name", $kecamatan);
    $this->db->order_by("name");
    return $this->db->get("villages");
  }

public function get_AlamatMitra($idmitra = null)
{
  if ($idmitra != null || $idmitra != '') {
    $this->db->where('idmitra',$idmitra);
  }
  return $this->db->get('mitra');
}

  public function get_tes()
  {
      return $this->db->get("provinces");
  }


}
