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

class Master_data_rts extends CI_Controller {
	function __construct(){
		parent::__construct();
        $this->load->library('datatables');
        $this->master_m->HarusLogin();
	}
	
	public function index()
	{
		$data_field='';
		$data_head_field='';
		$this->session->unset_userdata(array('filter_pencarian','pilihan','isi_pencarian'));
		$q=$this->db->get_where('variable',array('tingkat'=>'Rumah Tangga'));
		$query='select 
				kode_wilayah
				,provinsi
				,kabupaten
				,kecamatan
				,kelurahan
				,';
		$tambahan=array('alamat'=>array('kecamatan'=>'Kecamatan','kelurahan'=>'Kelurahan'));
		$tambahan_filter=array('kecamatan'=>'isian','kelurahan'=>'isian');
		$list_pencarian=array();
		$jenis_pencarian=array();
		$in_array=array();
		$list_pencarian['-']='Tampilkan Semua';
		foreach($q->result() as $val){
			$list_pencarian[$val->id_variable]=$val->nama_variable;
			foreach($tambahan as $k=>$v){
				if($val->nama_field==$k){
					foreach($v as $k2=>$v2){
						$list_pencarian[$k2]=$v2;
						$jenis_pencarian[$k2]=$tambahan_filter[$k2];
						
					}
				}
			}
			
			$jenis_pencarian[$val->id_variable]=$val->jenis;
			$data_array=$this->getlist($val->id_variable);			
			if(!in_array($val->nama_field,$in_array)){
				$in_array[]=$val->nama_field;
				
				if($val->jenis=='pilihan'){
					if(count($data_array)>0){
						$query.=' CASE ';
						foreach($data_array as $k=>$ar){
							$query.=' WHEN '.$val->nama_field.'="'.$k.'" then "'.$ar.'"';
						}
						$query.=' ELSE '.$val->nama_field.' END AS "'.$val->nama_field.'",';
					}
				}else{
					if($val->nama_field!='')
					$query.=$val->nama_field.',';
				}
			}
			
		}
		
		$query.='id_data_rts as ID FROM data_rts order by nomor_urut_rumah_tangga asc';
		$this->db->query(' CREATE OR REPLACE VIEW view_data_rts AS '.$query);
		
		$fields = $this->db->field_data('view_data_rts');
		$fields_terdaftar=array();
		foreach ($fields as $field)
		{
		   $fields_terdaftar[]=$field->name;
		}
		$tambah=array("nama"=>"Nama");
		foreach($tambah as $k=>$v){
			$data_field.='{"data":"'.$k.'","orderable": false},';
			$data_head_field.='<th>'.$v.'</th>';
		}
		$kecuali=array();
		foreach($q->result() as $val){
			if(in_array($val->nama_field,$fields_terdaftar)){
				if(!in_array($val->nama_variable,$kecuali)){
					$data_field.='{"data":"'.$val->nama_field.'"},';
					$data_head_field.='<th>'.$val->nama_variable.'</th>';
					foreach($tambahan as $k=>$v){
						if($val->nama_field==$k){
							foreach($v as $k2=>$v2){
								$data_field.='{"data":"'.$k2.'"},';
								$data_head_field.='<th>'.$v2.'</th>';
							}
						}
					}
					
				}
			}
		}
		
		$data['data_field']=substr($data_field, 0, -1);
		$data['list_pencarian']=$list_pencarian;
		$data['jenis_pencarian']=json_encode($jenis_pencarian);
		$data['data_head_field']=$data_head_field;
		$data['konten']='master_data_rts_v';
		$this->load->view('index_v',$data);
	}
	
	
	function pilihan_detail_pencarian(){
		$id_variable=$this->input->post('id_variable');
		$data='';
		if($id_variable!='-'){
			$q=$this->db->get_where('variable_detail',array('id_variable'=>$id_variable));
			foreach($q->result() as $val){
				$data.='<option value="'.$val->nama_detail.'">'.$val->nama_detail.'</option>';
			}
		}
		echo $data;
	}	
	
	public function json()
	{
		$pilihan=@$this->session->pilihan;
		$filter_pencarian=@$this->session->filter_pencarian;
		$isi_pencarian=@$this->session->isi_pencarian;
		header('Content-Type: application/json');
		$this->datatables->select('*');
        $this->datatables->from('view_data_rts');
		if($filter_pencarian!=''){
			if($pilihan=='pilihan'){
				$this->datatables->where(array($filter_pencarian=>$isi_pencarian));
			}else if($pilihan=='isian'){
				$this->datatables->like(array($filter_pencarian=>$isi_pencarian));
			}
		}
		
		if($this->session->userdata('id_jenis_user')==2)
			$this->datatables->where(array('kelurahan'=>$this->session->userdata('detail_login')));
		
       // $this->datatables->add_column('nama', '<a href="world/edit/$2">edit</a> | <a href="world/delete/$1">delete</a>', 'ID');
        $data=json_decode($this->datatables->generate(), true);
		foreach($data['data'] as $key=>$val){
			$data['data'][$key]['nama']=$this->cari_nama($val['nomor_urut_rumah_tangga']);
		}
		echo json_encode($data);

		
	}
	
	
	public function simpan_cari()
	{
		$q=$this->db->get_where('variable',array('tingkat'=>'Rumah Tangga'));
		$list_pencarian=array('-'=>'');
		foreach($q->result() as $val){
			$list_pencarian[$val->id_variable]=$val->nama_field;
		}
		if($this->input->post('filter_pencarian')!='' && is_numeric($this->input->post('filter_pencarian')))
			$filter_pencarian=$list_pencarian[$this->input->post('filter_pencarian')];
		else{
			$filter_pencarian=$this->input->post('filter_pencarian');
		}
		
		$pilihan=$this->input->post('pilihan');
		if($pilihan=='pilihan'){
			$isi_pencarian=$this->input->post('pilihan_filter');
			$this->datatables->where(array($filter_pencarian=>$isi_pencarian));
			$data=array('filter_pencarian'=>$filter_pencarian,'pilihan'=>$pilihan,'isi_pencarian'=>$isi_pencarian);
			$this->session->set_userdata($data);		
		}else if($pilihan=='isian'){
			$isi_pencarian=$this->input->post('isian_filter');
			$this->datatables->like(array($filter_pencarian=>$isi_pencarian));
			$data=array('filter_pencarian'=>$filter_pencarian,'pilihan'=>$pilihan,'isi_pencarian'=>$isi_pencarian);
			$this->session->set_userdata($data);		
		}else if($this->input->post('filter_pencarian')=='-'){
			$this->session->unset_userdata(array('filter_pencarian','pilihan','isi_pencarian'));
		}
		echo 1;
	}
	
	function cari_nama($nomor_urut_rumah_tangga){
		$q=$this->db->get_where('data_individu',array('no_urut_rumah_tangga'=>$nomor_urut_rumah_tangga,'no_anggota_rumah_tangga'=>1));
		if($q->num_rows()==1){
			$data=$q->row();
			return $data->nama;
		}else{
			return '';
		}
	}
	
	function getlist($id_variable){
		$q=$this->db->get_where('variable_detail',array('id_variable'=>$id_variable));
		$array=array();
		foreach($q->result() as $val){
			$array[$val->no_detail]=$val->nama_detail;
		}
		return $array;
	}
}
