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

class C extends CI_Controller {

	var $data;
	function __construct()
	{
		parent::__construct();
		$this->load->model('mo_c','mc');
		$this->load->helper('survey');
	}
	
	public function index($short = null)
	{
		if($short)
		{
			$customer = $this->mc->alias_url($short);
			if($customer)
			{
				$data['namecompany'] = $customer->f_customer_name;
				$date = $this->mc->fnEventExp( $customer->f_customer_id );
				if($date)
				{
					if(date('Y-m-d') >= $date->ed and date('Y-m-d') <= $date->dt)
					{	
						if($this->session->userdata('lanjut'))
						{
							$company = array(
								"namecom" => $customer->f_customer_id,
								"eventcom" => $date->f_event_id,
							);
							//echo print_r($customer);die();
	
							$this->session->set_userdata($company);
							$account_plus = array(86,21); //,21
							
							if(in_array($customer->f_customer_id, $account_plus))
							{
								$this->load->view($customer->f_customer_id.'/form_survey',$data);
							} else {
								$this->load->view('form_survey',$data);
							}
							//$this->load->view('form_survey',$data);
						}
						else
						{
							$this->load->view('form_review',$short);
						}
						
					}
					else
					{
						show_404();
					}
				}
				else
				{
					show_404();
				}
			}
			else
			{
				show_404();
			}
		}
		else
		{
			show_404();
		}
	}
	
	function lanjut($short = null)
	{
		$this->session->set_userdata('lanjut',true);
		echo json_encode(array('msg'=> 'ok'));
	}

	function unlanjut($short = null)
	{
		$this->session->unset_userdata('lanjut',true);
		echo json_encode(array('msg'=> 'ok'));
	}

	function saveData($save = null)
	{
		$this->db->trans_start();
		//echo "<pre>data ";echo print_r($_POST)."</pre><br/>";

		$this->data = array(
			'f_name' 		=> $this->input->post('nama'),
			'f_age' 		=> $this->input->post('usia'),
			'f_gender' 		=> $this->input->post('gender'),
			'f_email'		=> $this->input->post('email'),
			//'f_hp' 			=> $this->input->post('hp'),
			'f_alamat' 			=> $this->input->post('f_address'),
			'f_sosmed' 			=> $this->input->post('f_sosmed'),
			'f_bisnis' 			=> $this->input->post('f_bisnis'),
			'f_perusahaan'		=> $this->input->post('perusahaan'),
			'f_jabatan'		=> $this->input->post('jabatan'),
			'f_sehat1'		=> ($this->input->post('f_sehat1')) ? 1:0,
			'f_ipaddres' 	=> $_SERVER['REMOTE_ADDR'],
			'f_date'  		=> date('Y-m-d'),
			'f_event_id'  	=> $this->session->userdata('eventcom'),
			'f_customer_id'	=> $this->session->userdata('namecom'),
		);

		$_hp = preg_replace( '/[^.0-9]/', '', $this->input->post('hp')); 
		if($_hp)
		{
			$_hp_prefix = substr($_hp,0,1);
			if($_hp_prefix == 0) { $this->data['f_hp'] = '62'.substr($_hp,1,15); }
			else if($_hp_prefix == 8) { $this->data['f_hp'] = '62'.substr($_hp,0,15); }
			else if($_hp_prefix == '+') { $this->data['f_hp'] = '62'.substr($_hp,1,15); }
			else { $this->data['f_hp'] = $_hp; }
		}

		$f_bisnis1 = $this->input->post('f_bisnis1');
		if($f_bisnis1)
		{
			$f_bisnis11 = $this->input->post('f_bisnis11');
			if($f_bisnis11) 
			{
				if($this->input->post('f_profesi'))	array_push( $f_bisnis1 ,$f_bisnis11.' '.$this->input->post('f_profesi') );
			}
			$f_bisnis12 = $this->input->post('f_bisnis12');
			if($f_bisnis12) 
			{
				if($this->input->post('f_lain')) array_push( $f_bisnis1 ,$f_bisnis12.' '.$this->input->post('f_lain') );
			}
			$this->data['f_bisnis_on'] = json_encode($f_bisnis1);
		}

		$f_bisnis2 = $this->input->post('f_bisnis2');
		if($f_bisnis2)
		{
			$f_bisnis21 = $this->input->post('f_bisnis21');
			if($f_bisnis21) 
			{
				if($this->input->post('f_profesi2')) array_push( $f_bisnis2 ,$f_bisnis21.' '.$this->input->post('f_profesi2') );
			}
			$f_bisnis22 = $this->input->post('f_bisnis22');
			if($f_bisnis22) 
			{
				if($this->input->post('f_lain2')) array_push( $f_bisnis2 ,$f_bisnis22.' '.$this->input->post('f_lain2') );
			}
			$this->data['f_bisnis_minat'] = json_encode($f_bisnis2);
		}

		$f_sehat2 = $this->input->post('f_sehat2');
		if($f_sehat2)
		{
			$f_sehat21 = $this->input->post('f_sehat21');
			if($f_sehat21) 
			{
				if($this->input->post('f_sehat_lain')) array_push( $f_sehat2 ,$f_sehat21.' '.$this->input->post('f_sehat_lain') );
			}
			$this->data['f_sehat2'] = json_encode($f_sehat2);
		}

		$this->db->where('f_soal_active',1);		
		$soal = $this->db->get('t_soal a')->result();
		$dataSoal = array();
		foreach($soal as $a)
		{
			$namepost = "no".$a->f_soal_id;
			$jawab = explode('_',$this->input->post($namepost));
			$dataSoal['kategori'][$a->f_kategori_id][$a->f_soal_id] = array('urut'=>$jawab[0],'jawab'=>$jawab[1]); 
			$dataSoal['subkategori'][$a->f_subkategori_id][$a->f_soal_id] = array('urut'=>$jawab[0],'jawab'=>$jawab[1]); 
			$dataSoal['all'][$a->f_kategori_id][$a->f_subkategori_id][$a->f_soal_id] = array('urut'=>$jawab[0],'jawab'=>$jawab[1]); 
		}
		
		//if($this->data['f_customer_id'] == 86)	
		$dataSoal['ukuran_kaos'] = $this->input->post('uk_kaos');
		if($this->input->post('uk_kaos_pasangan')) $dataSoal['ukuran_kaos_pasangan'] = $this->input->post('uk_kaos_pasangan');

		$this->data['f_survey'] = serialize($dataSoal);

		$this->db->insert('trn_survey',$this->data);
		
		$this->db->trans_complete();
		if($this->db->trans_status())
		{
			$this->session->unset_userdata('lanjut',true);
			echo json_encode(array('status'=>'ok'));
		}
		else
		{
			echo json_encode(array('status'=>'no'));
		}
	}
}
