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

require_once(str_replace("\\","/",APPPATH).'helpers/Secure_area_adm.php');
class Setting extends Secure_area_adm {

	public function __construct()
	{
		parent::__construct();
		$this->load->model('M_general');
		$data['jenis_page'] = "front_office/";
		$data['controller_dir'] = 'ugd_dir/';
		$data['controller'] = 'ugd';
		$data['parent_controller'] = 'ugd';
		$data['active'] = 'ugd';
		//$data['smt_aktif'] = $this->db->get_where('pddikti_re_semester',array('a_smt_aktif'=>'1'))->row();
		$this->load->vars($data);
	}

	public function version()
	{
		
		$data['judul'] ='Pengaturan';
		$data['page']  ='setting/version';
		$data['desc_judul'] ='Versi Database';
		$data['active'] = 'version';
		$data['sub_menu'] = 'pengaturan';
	
		
		$data['breadcumb'][] = array('icon'=>'archive',
																 'link'=>'',
																 'judul'=>'Pengaturan');

		$this->load->view('template',$data);
	}


	public function db($untuk)
	{
		
		$data['judul'] ='Pengaturan';
		$data['page']  ='setting/master';
		$data['desc_judul'] ='Pengaturan Reference';
		$data['active'] = $untuk;

		if($untuk == 'bbmc_re_laboratorium_kecuali' || $untuk == 'bbmc_re_laboratorium_group_print'){
			$data['sub_menu'] = 'laboratorium';
			$data['active'] = 'setting_lab';
		}
		else if($untuk == 'bbmc_re_apotik_obat'){
			$data['sub_menu'] = 'apotek';
		}else if($untuk == 'bbmc_tr_amprahan_request'){
			$data['sub_menu'] = '';
			$data['active'] = 'request_amprahan';
			$data['judul'] ='Daftar Permohonan Amprahan';
			$data['desc_judul'] ='BBMC';
		}else{
			$data['sub_menu'] = 'pengaturan';
		}
		
		if($untuk != 'bbmc_re_staff'){
			$data['sub_sub_menu'] = 'reference';
		}
		
		$data['untuk'] = $untuk;
		
		$data['breadcumb'][] = array('icon'=>'archive',
																 'link'=>'',
																 'judul'=>'Pengaturan');

		$this->load->view('template',$data);
	}

	public function delete_setting($untuk, $id, $nav_menu=null, $sub_nav=null)
	{	
		$this->db->trans_start();
		if($nav_menu=='null'){$nav_menu=null;}
		if($sub_nav=='null'){$sub_nav=null;}
		$tabel = $untuk;
		$id_tabel = $this->M_general->get_kolom($untuk);
		$this->db->where($id_tabel[0], $id);
		$this->db->delete($untuk);
		if($this->db->trans_complete()){
			$this->session->set_flashdata('update_sukses_'.$tabel,true);
		}else{
			$this->session->set_flashdata('update_gagal_'.$tabel,true);
		}


		if($nav_menu!=null || $sub_nav!=null){
			redirect($_SERVER['HTTP_REFERER']."#$nav_menu", 'refresh');	
		}else{
			redirect($_SERVER['HTTP_REFERER'], 'refresh');	
		}
		
		
	}

	public function insert_entry($untuk, $tipe,  $id=null, $dari_menu=null, $nav_menu=null, $sub_nav=null)
	{
		

		if($id=='null'){$id=null;}
		if($dari_menu=='null'){$dari_menu=null;}
		if($nav_menu=='null'){$nav_menu=null;}
		if($sub_nav=='null'){$sub_nav=null;}


		$data['judul'] ='Pengaturan';
		$data['page']  ='setting/master';
		$data['desc_judul'] ='Pengaturan Reference';
		$data['active'] = 'setting';
		$data['active'] = $untuk;
		$data['sub'] 				= 'setting';
		$data['sub_menu'] = 'pengaturan';

		if($untuk == 'bbmc_re_laboratorium_kecuali'){
			$data['sub_menu'] = 'laboratorium';
			$data['active'] = 'setting_lab';
		}
		else if($untuk == 'bbmc_tr_amprahan_request'){
			$data['active'] = 'request_amprahan';
			$data['judul'] ='Daftar Permohonan Amprahan';
			$data['desc_judul'] ='BBMC';
			$data['sub'] = '';
			$data['sub_menu'] = '';
		}else if($untuk == 'bbmc_re_laboratorium_template'){
			$data['sub_menu'] = 'laboratorium';
			$data['active'] = 'setting_lab';
		}

		

		if($untuk != 'bbmc_re_staff'){
			$data['sub_sub_menu'] = 'reference';
		}

		

		$data['desc_judul'] 		= '';
		$data['page']     			= 'setting/setting_insert';
		$data['untuk']				= $untuk;
		$data['tgl_awal'] 			= null;
		$data['tgl_akhir'] 			= null;
		$data['tipe']				= $tipe;
		$data['id']					= $id;
		$data['dari_menu']			= $dari_menu;
		$data['nav_menu']			= $nav_menu;
		$data['sub_nav']			= $sub_nav;
		
		$data['breadcumb'][] = array('icon'=>'archive',
																 'link'=>'',
																 'judul'=>'Pengaturan');

	
		
		$this->load->view('template',$data);
	}

	public function upload_file($file_path, $var_name=null, $file_name=null, $insert_id=null)
	{
		$gas = true;
		$uuid = $this->db->query("select UUID() as id")->row()->id;
		$config['upload_path'] = './'.$this->Content_m->get_value('global_data','id_global_data','1','data_value');
        $config['allowed_types'] = 'jpg|png|jpeg|pdf';
        $config['max_size'] = 0;
        $config['max_width'] = 0;
        $config['max_height'] = 0;
        $config['overwrite'] = TRUE;
        $file_ext = pathinfo($_FILES[$var_name]["name"], PATHINFO_EXTENSION);
        if($file_name==null || $file_name == ''){
        	$config['file_name'] = $var_name.'-'.$insert_id.".".$file_ext;	
        }else{
        	$config['file_name'] = $file_name;	
        }
        
        $this->load->library('upload', $config);
        if ( ! $this->upload->do_upload($var_name)){
        	$gas = false;
          	echo $this->upload->display_errors();
          	echo "<a href='javascript:;' onclick='window.history.back();'>Back</a>";
        }
        $data[$var_name] = $config['file_name'];
        $data['gas'] = $gas;
        return $data;
	}

	public function submit_entry()
	{
		extract($_POST);

		$this->db->trans_start();
		//echo $tabel_id."<br>";
		//echo $tipe."<br>";
		//echo $tabel_id."<br>";
		$tabel_id_where = $tabel_id;
		//echo $tabel_id_where."<br>";
		$tabel_db = $tabel;
		$gas = true;
		$dari_menu = isset($dari_menu)?$dari_menu:'';
		$nav_menu = isset($nav_menu)?$nav_menu:'';
		$sub_nav = isset($sub_nav)?$sub_nav:'';

		if($tipe == 'insert'){
			foreach ($_POST as $key => $value)
				$data[$key]=$value!=''?$value:null;
			unset($data['tabel']);
			unset($data['tabel_id']);
			unset($data['id']);
			unset($data['tipe']);
			unset($data['id_pool_division']);
			unset($data['dari_menu']);
			unset($data['nav_menu']);
			unset($data['sub_nav']);
			unset($data['document_name']);

			//unset($data['charge_file']);
			//unset($data['certificate_file']);
			//unset($data['reference_file']);
			
			//unset($data['employee_id']);
			//var_dump($data)."<br>";
			//echo $data['tabel_id']."<br>";
			//echo $tabel_id_where."<br>";
			
			

			
	        if($tabel == 'bbmc_re_staff')
				$data['id_re_staff'] = $this->db->query("select UUID() as id")->row()->id;

			$this->db->insert($tabel_db,$data);
			//echo $this->db->last_query()."<br>";
			$last_insert_id = $this->db->insert_id();

			if(isset($_FILES["document_file"]["name"])){
				if($_FILES["document_file"]["name"] != ''){
					$doc_file = $this->upload_file($_FILES["document_file"]["name"], 'document_file', null, $last_insert_id);
					$data['document_file'] = $doc_file['document_file'];
					$gas = $doc_file['gas'];
				}
			}
			if(isset($_FILES["charge_file"]["name"])){
				if($_FILES["charge_file"]["name"] != ''){
					$doc_file = $this->upload_file($_FILES["charge_file"]["name"], 'charge_file', null, $last_insert_id);
					$data['charge_file'] = $doc_file['charge_file'];
					$gas = $doc_file['gas'];
				}
			}
			if(isset($_FILES["certificate_file"]["name"])){
				if($_FILES["certificate_file"]["name"] != ''){
					$doc_file = $this->upload_file($_FILES["certificate_file"]["name"], 'certificate_file', null, $last_insert_id);
					$data['certificate_file'] = $doc_file['certificate_file'];
					$gas = $doc_file['gas'];
				}
			}
			if(isset($_FILES["reference_file"]["name"])){
				if($_FILES["reference_file"]["name"] != ''){
					$doc_file = $this->upload_file($_FILES["reference_file"]["name"], 'reference_file', null, $last_insert_id);
					$data['reference_file'] = $doc_file['reference_file'];
					$gas = $doc_file['gas'];
				}
			}
			if(isset($_FILES["contract_file"]["name"])){
				if($_FILES["contract_file"]["name"] != ''){
					$doc_file = $this->upload_file($_FILES["contract_file"]["name"], 'contract_file', null, $last_insert_id);
					$data['contract_file'] = $doc_file['contract_file'];
					$gas = $doc_file['gas'];
				}
			}

			if(isset($_FILES["ticket_file"]["name"])){
				if($_FILES["ticket_file"]["name"] != ''){
					$doc_file = $this->upload_file($_FILES["ticket_file"]["name"], 'ticket_file', null, $last_insert_id);
					$data['ticket_file'] = $doc_file['ticket_file'];
					$gas = $doc_file['gas'];
				}
			}

			$this->db->where($tabel_id_where, $last_insert_id);
			$this->db->update($tabel_db,$data);

			//echo $this->db->last_query();
		}

		
		if($tipe == 'update'){
			
			
			$tabel_ids = $id;
			foreach ($_POST as $key => $value){
				$data[$key]=$value!=''?$value:null;
				
			}
			unset($data['tabel']);
			unset($data['tabel_id']);
			unset($data['id']);
			unset($data['tipe']);
			unset($data['id_pool_division']);
			unset($data['dari_menu']);
			unset($data['nav_menu']);
			unset($data['sub_nav']);
			//var_dump($data)."<br>";
			//unset($data['employee_id']);

			if(isset($_FILES["document_file"]["name"])){
				if($_FILES["document_file"]["name"] != ''){
					$doc_file = $this->upload_file($_FILES["document_file"]["name"], 'document_file', $data['document_name']);
					$data['document_file'] = $doc_file['document_file'];
					$gas = $doc_file['gas'];
				}
			}
			if(isset($_FILES["charge_file"]["name"])){
				if($_FILES["charge_file"]["name"] != ''){
					$doc_file = $this->upload_file($_FILES["charge_file"]["name"], 'charge_file', $data['document_name']);
					$data['charge_file'] = $doc_file['charge_file'];
					$gas = $doc_file['gas'];
				}			
			}
			if(isset($_FILES["certificate_file"]["name"])){
				if($_FILES["certificate_file"]["name"] != ''){
					$doc_file = $this->upload_file($_FILES["certificate_file"]["name"], 'certificate_file', $data['document_name']);
					$data['certificate_file'] = $doc_file['certificate_file'];
					$gas = $doc_file['gas'];
				}			
			}
			if(isset($_FILES["reference_file"]["name"])){
				if($_FILES["reference_file"]["name"] != ''){
					$doc_file = $this->upload_file($_FILES["reference_file"]["name"], 'reference_file', $data['document_name']);
					$data['reference_file'] = $doc_file['reference_file'];
					$gas = $doc_file['gas'];
					//echo "string";
				}			
			}
			if(isset($_FILES["contract_file"]["name"])){
				if($_FILES["contract_file"]["name"] != ''){
					$doc_file = $this->upload_file($_FILES["contract_file"]["name"], 'contract_file', $data['document_name']);
					$data['contract_file'] = $doc_file['contract_file'];
					$gas = $doc_file['gas'];
				}			
			}

			if(isset($_FILES["ticket_file"]["name"])){
				if($_FILES["ticket_file"]["name"] != ''){
					$doc_file = $this->upload_file($_FILES["ticket_file"]["name"], 'ticket_file', $data['document_name']);
					$data['ticket_file'] = $doc_file['ticket_file'];
					$gas = $doc_file['gas'];
				}
			}	

			

			unset($data['document_name']);
			//unset($data['charge_file']);
			//unset($data['certificate_file']);
			//unset($data['reference_file']);
			
			$this->db->where($tabel_id_where, $tabel_ids);
			$this->db->update($tabel_db,$data);
			//echo "<br><br>".$this->db->last_query();


		}

		//$this->db->trans_complete();
		
		//echo $tabel;
		
		
		if($gas == true){
			if($this->db->trans_complete()){
				$this->session->set_flashdata('update_sukses_'.$tabel,true);
				//echo "update_sukses_".$tabel;
			}else{
				$this->session->set_flashdata('update_gagal_'.$tabel,true);
				//echo "update_gagal_".$tabel;
			}

			
			
			
			//redirect($_SERVER['HTTP_REFERER'], 'refresh');	
			if($tabel_db == 'bbmc_re_laboratorium_template'){
				redirect('front_office/ugd_dir/ugd/laboratorium_setting_template');
			}else{
				redirect('setting/db/'.$tabel);
			}
			
			
			

		}
		else
		{
			echo "<br>Data tidak terupdate, silahkan hubungi TIM IT";
		}
		
		
	


	}


	public function userpanel()
	{
		

		$data['sub'] 				= 'setting';
		$data['judul']				= 'User SSO';
		$data['desc_judul'] 			= 'Single Sign On Sistem ';
		$data['page']     			= 'setting/userpanel';
		$data['tgl_awal'] 			= null;
		$data['tgl_akhir'] 			= null;
		$data['breadcumb'][] 		= array('icon'  => 'list',
											'link'  => '',
											'judul' => 'List Data');

		$data['active'] = 'hakakses';
		$data['sub_menu'] = 'pengaturan';

		$data['query'] 				= $this->M_general->user();
		
		$this->load->view('template',$data);
	}

	public function insert_userpanel($tipe, $id=null)
	{


		$data['sub'] 				= 'setting';
		$data['judul']				= 'User';
		$data['desc_judul'] 			= '';
		$data['page']     			= 'setting/userpanel_insert';
		$data['tipe']				= $tipe;
		$data['tgl_awal'] 			= null;
		$data['tgl_akhir'] 			= null;
		$data['id']					= $id;
		$data['breadcumb'][] 		= array('icon'  => 'list',
											'link'  => site_url('setting/userpanel/'),
											'judul' => 'List Data');

		$data['breadcumb'][] 		= array('icon'  => 'plus',
											'link'  => '',
											'judul' => $tipe.' Entry');

		$data['active'] = 'hakakses';
		$data['sub_menu'] = 'pengaturan';
		
		
		
		
		$this->load->view('template',$data);
	}

	public function hakakses_userpanel($tipe, $id=null)
	{
		$data['sub'] 				= 'setting';
		$data['judul']				= 'User';
		$data['desc_judul'] 			= '';
		$data['page']     			= 'setting/userpanel_hakakses';
		$data['tipe']				= $tipe;
		$data['tgl_awal'] 			= null;
		$data['tgl_akhir'] 			= null;
		$data['id']					= $id;
		$data['breadcumb'][] 		= array('icon'  => 'list',
											'link'  => site_url('setting/userpanel/'),
											'judul' => 'List Data');

		$data['breadcumb'][] 		= array('icon'  => 'plus',
											'link'  => '',
											'judul' => $tipe.' Entry');

	
		
		$data['active'] = 'hakakses';
		$data['sub_menu'] = 'pengaturan';
		$this->load->view('template',$data);
	}

	public function submit_userpanel()
	{
		extract($_POST);
		
		if(isset($_POST))
		{
			
			if($password == '' && $confirm_password == '')
			{
				$this->session->set_flashdata('pass_tidak_boleh_kosong',TRUE);
				redirect($_SERVER['HTTP_REFERER'],'refresh');
			}
			else
			{
				
				$cek=$this->db->where('username',$username)->get('global_user');
				//echo $this->db->last_query()."<br>";
				if($cek->num_rows()==0)
				{
					if($confirm_password == $password)
					{

						if(isset($password) && $password != ''){

							$this->db->where('id_user',$id_sdm);
							$this->db->delete('global_user');
							$data2=array(
								'id_global_user'=>$this->db->query("select UUID() as id")->row()->id,
								'id_user'=>$id_sdm,
								'username'=>$username,
								'password'=>md5($password),
							);
							$this->db->insert('global_user', $data2);
							//echo $this->db->last_query();
							$this->session->set_flashdata('sukses',TRUE);
							
							//redirect('/administrator/pengaturan/atur_hakakses/edit/'.$id_sdm);
							//redirect($_SERVER['HTTP_REFERER'],'refresh');
							redirect('setting/userpanel/'.$id_pool_division);
							
						}	
					}
					else
					{
						$this->session->set_flashdata('pass_tidak_sama',TRUE);
						//redirect('/administrator/pengaturan/atur_hakakses/edit/'.$id_sdm);
						//redirect($_SERVER['HTTP_REFERER'],'refresh');
						redirect('setting/userpanel/'.$id_pool_division);
					}

				}
				else if($cek->num_rows()==1)
				{
					$get_username_id_user = $this->Content_m->get_row_custom('global_user','username', $username)->id_user;
					if($get_username_id_user == $id_sdm)
					{
						if($confirm_password == $password)
						{
							if(isset($password) && $password != ''){
								$data2=array(
									'username'=>$username,
									'password'=>md5($password),
								);
								$this->db->where('id_user',$id_sdm);
								$this->db->update('global_user', $data2);
								
								$this->session->set_flashdata('sukses',TRUE);
								//redirect('/administrator/pengaturan/atur_hakakses/edit/'.$id_sdm);
								//redirect($_SERVER['HTTP_REFERER'],'refresh');
								redirect('setting/userpanel/'.$id_pool_division);
							}
							else
							{
								//echo "password tidak boleh kosong.";
								$this->session->set_flashdata('pass_tidak_boleh_kosong',TRUE);
								//redirect('/administrator/pengaturan/atur_hakakses/edit/'.$id_sdm);
								//redirect($_SERVER['HTTP_REFERER'],'refresh');
								redirect('setting/userpanel/'.$id_pool_division);
							}	
						}
						else
						{
							//echo "password dan confirm password tidak sama.";
							$this->session->set_flashdata('pass_tidak_sama',TRUE);
							//redirect('/administrator/pengaturan/atur_hakakses/edit/'.$id_sdm);
							//redirect($_SERVER['HTTP_REFERER'],'refresh');
							redirect('setting/userpanel/'.$id_pool_division);
						}
					}
					else
					{
						//echo "username sudah ada. silahkan ulangi kembali ...";
						$this->session->set_flashdata('username_sudah_ada',TRUE);
						//redirect('/administrator/pengaturan/atur_hakakses/edit/'.$id_sdm);
						//redirect($_SERVER['HTTP_REFERER'],'refresh');
						redirect('setting/userpanel/'.$id_pool_division);
					}
				}
				else{
					//echo "username sudah ada. silahkan ulangi kembali.";
					$this->session->set_flashdata('username_sudah_ada',TRUE);
					//redirect('/administrator/pengaturan/atur_hakakses/edit/'.$id_sdm);
					//redirect($_SERVER['HTTP_REFERER'],'refresh');
					redirect('setting/userpanel/'.$id_pool_division);
				}
			}
			



			
			//echo $password;

			
			
		}
		else{
			//echo "terjadi error dalam proses update data, silahkan ulangi kembali";
			$this->session->set_flashdata('error',TRUE);
			//redirect('/administrator/pengaturan/atur_hakakses/edit/'.$id_sdm);
			redirect($_SERVER['HTTP_REFERER'],'refresh');
			//redirect('admin/daftar_online');
		}

		//echo $this->db->last_query();	
		
	}

	public function submit_userpanel_hakakses()
	{
		extract($_POST);

		$this->db->where('id_user',$id_sdm);
		$this->db->delete('global_user_hakakses_bbmc');

		foreach ($hakakses as $value ) {
			$data['id_user'] = $id_sdm;
			$data['id_hakakses'] = $value;
			$this->db->insert('global_user_hakakses_bbmc',$data);
		}

		$this->session->set_flashdata('sukses',TRUE);
		redirect($_SERVER['HTTP_REFERER'],'refresh');
	}

  
}