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

class Responden extends CI_Controller
{
    function __construct()
    {
        parent::__construct();
        $this->load->model('mo_responden');
		$config = array('module' => 'responden','roleid'=> $this->session->userdata('sRoleId'));
		$this->load->library('rolemodule',$config);
    }

	public function index()
    {
		$this->rolemodule->view_page();
		$data = array(
			'app_title'=>'Data Responden',
			'app_desc' =>'it all Responden'
		);
		
        $data['content']	= 'vw_responden';
        $this->load->view('webadmin/main', $data);
    }

	public function fnRespondenDataJson()
	{
		$button = '';
		$button .= '&nbsp;<a href="#" onClick="MyWindow=window.open(\''.site_url('responden/fnRespondenPrint').'/$1\', \'MyWindow\',\'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=1000,height=600,left=100,top=50\'); return false;" class="tip" title="View Detail"><i class="glyphicon glyphicon-fullscreen"></i></a>&nbsp;';
		if($this->rolemodule->check_button('edit'))
		{
			$button .= '&nbsp;&nbsp;<a href="#" onclick="fnEdit($1)" class="tip" title="Edit"><i class="glyphicon glyphicon-edit"></i></a>&nbsp;&nbsp;';
		}

		if($this->rolemodule->check_button('delete'))
		{
			$button .= '&nbsp;&nbsp;<a href="#" onclick="fnDelete($1)" class="tip" title="Hapus"><i class="text-red glyphicon glyphicon-trash"></i></a>&nbsp;&nbsp;';
		}

		$this->load->library('datatables');
		$this->datatables->select('a.f_id, f_customer_name, b.f_event_no,f_name,f_age,f_gender,f_email,f_hp,f_date,f_ipaddres');
		$this->datatables->join('t_event b','b.f_event_id=a.f_event_id');
		$this->datatables->join('t_customers c','c.f_customer_id=a.f_customer_id');
		
		if($this->input->get_post('id')) $this->datatables->where('a.f_customer_id',$this->input->get_post('id'));
		if($this->input->get_post('no')) $this->datatables->where('b.f_event_no',$this->input->get_post('no'));
		
		$this->datatables->from('trn_survey a');
		$this->datatables->add_column('Aksi',$button,'a.f_id');
		
		echo $this->datatables->generate();		
	}

	public function fnRespondenDataId()
	{
		$id 	= $this->input->get_post('id');
		$data	= $this->mo_responden->getDataId($id);
		echo json_encode($data);		
	}

	//Untuk Form Terpisah
	public function fnRespondenAdd()
    {
		$this->rolemodule->check_function('add');
		$data = array(
			'app_title'=>'Form Responden',
			'app_desc' =>'it all Form Responden',
			'form_header'	=>'<i class="fa fa-table"></i> Add&nbsp;&nbsp;',
			'fAct'			=>'Add',
		);
		
        $data['content']	= 'vw_responden_add';
        $this->load->view('webadmin/main', $data);
    }

	public function fnRespondenSave()
	{
		$this->rolemodule->check_function('add');
		if($this->mo_responden->saveData())
		{
			$this->session->set_userdata('SUCMSG','Sukses Tambah !!!');
			echo json_encode(array('msg'=>true));
		}
		else
		{
			echo json_encode(array('msg'=>false));
		}		
	}

	//Untuk Form Terpisah
	public function fnRespondenEdit($id)
    {
		$this->rolemodule->check_function('edit');
		$data = array(
			'app_title'=>'Form Responden',
			'app_desc' =>'it all Form Responden',
			'form_header'	=>'<i class="fa fa-edit"></i> Edit&nbsp;&nbsp;',
			'fAct'			=>'Edit',
			'fId'			=>$id,
		);
		
        $data['content']	= 'vw_responden_add';
        $this->load->view('webadmin/main', $data);
    }

	public function fnRespondenUpdate()
	{
		$this->rolemodule->check_function('edit');
		$id 	= $this->input->get_post('id');
		if($this->mo_responden->updateData($id))
		{
			$this->session->set_userdata('SUCMSG','Sukses Edit !!!');
			echo json_encode(array('msg'=>true));
		}
		else
		{
			echo json_encode(array('msg'=>false));
		}		
	}

	public function fnRespondenDelete()
	{
		$this->rolemodule->check_function('delete');
		$id 	= $this->input->get_post('id');
		if($this->mo_responden->deleteData($id))
		{
			echo 'TRUE';
		}
		else
		{
			echo 'FALSE';
		}		
	}

	public function fnRespondenComboData()
	{
		$json['result'] = $this->mo_responden->remoteComboData();
		echo json_encode($json);
	}

	public function fnRespondenExcel()
    {
        $this->load->helper('exportexcel');
        $namaFile = "trn_survey.xls";
        $judul = "trn_survey";
        $tablehead = 0;
        $tablebody = 1;
        $nourut = 1;
        //penulisan header
        header("Pragma: public");
        header("Expires: 0");
        header("Cache-Control: must-revalidate, post-check=0,pre-check=0");
        header("Content-Type: application/force-download");
        header("Content-Type: application/octet-stream");
        header("Content-Type: application/download");
        header("Content-Disposition: attachment;filename=" . $namaFile . "");
        header("Content-Transfer-Encoding: binary ");

        xlsBOF();

        $kolomhead = 0;
        xlsWriteLabel($tablehead, $kolomhead++, "No");
		xlsWriteLabel($tablehead, $kolomhead++, "F Id");
		xlsWriteLabel($tablehead, $kolomhead++, "F Event Id");
		xlsWriteLabel($tablehead, $kolomhead++, "F Customer Id");
		xlsWriteLabel($tablehead, $kolomhead++, "F Name");
		xlsWriteLabel($tablehead, $kolomhead++, "F Age");
		xlsWriteLabel($tablehead, $kolomhead++, "F Gender");
		xlsWriteLabel($tablehead, $kolomhead++, "F Date");
		xlsWriteLabel($tablehead, $kolomhead++, "F Email");
		xlsWriteLabel($tablehead, $kolomhead++, "F Hp");
		xlsWriteLabel($tablehead, $kolomhead++, "F Survey");
		xlsWriteLabel($tablehead, $kolomhead++, "F Ipaddres");

		foreach ($this->mo_responden->fnGetData() as $data) {
            $kolombody = 0;

            //ubah xlsWriteLabel menjadi xlsWriteNumber untuk kolom numeric
            xlsWriteNumber($tablebody, $kolombody++, $nourut);
			xlsWriteNumber($tablebody, $kolombody++, $data->f_id);
			xlsWriteNumber($tablebody, $kolombody++, $data->f_event_id);
			xlsWriteNumber($tablebody, $kolombody++, $data->f_customer_id);
			xlsWriteLabel($tablebody, $kolombody++, $data->f_name);
			xlsWriteNumber($tablebody, $kolombody++, $data->f_age);
			xlsWriteLabel($tablebody, $kolombody++, $data->f_gender);
			xlsWriteLabel($tablebody, $kolombody++, $data->f_date);
			xlsWriteLabel($tablebody, $kolombody++, $data->f_email);
			xlsWriteLabel($tablebody, $kolombody++, $data->f_hp);
			xlsWriteLabel($tablebody, $kolombody++, $data->f_survey);
			xlsWriteLabel($tablebody, $kolombody++, $data->f_ipaddres);

			$tablebody++;
			$nourut++;
        }

        xlsEOF();
        exit();
	}

	public function fnRespondenWord()
    {
        header("Content-type: application/vnd.ms-word");
        header("Content-Disposition: attachment;Filename=trn_survey.doc");

        $data = array(
            'trn_survey_data' => $this->mo_responden->fnGetData(),
            'start' => 0
        );
        
        $this->load->view('vw_responden_doc',$data);
    }

	function fnRespondenPrint($id = false)
	{
		if($id)
		{
			$data['kategori']		= $this->mo_responden->comboDataKategori();
			$data['subkategori']	= $this->mo_responden->comboDataSubKategori();
			$data['sql']			= $this->mo_responden->getDataId($id);
			$data['view']			= 'print';
			$this->load->view('vw_responden_print',$data);
		}
		else
		{
			return FALSE;
		}
	}
	
	public function fnListCustomers()
	{
		$this->db->select('f_customer_id,f_customer_name',false);
		$this->db->like('f_customer_name',$this->input->get_post('query'), 'both');
		$this->db->limit(15);
		$sql = $this->db->get('t_customers');
		$data = array();
		if($sql->num_rows()>0)
		{
			foreach($sql->result() as $a)
			{
				$data[] = array(
					'id' => $a->f_customer_id,
					'label' => $a->f_customer_name,
				);
			}
			echo json_encode($data);
		}
		else
		{
			$data = array(
					'id' => 0,
					'label' => 'Belum Terdaftar',
				);
			echo json_encode(array($data));
		}
	}

	function simulasi()
	{
		// kategori
		$kategori = $this->mo_responden->comboDataKategori();
		$subkategori = $this->mo_responden->comboDataSubKategori();
		$get_data = $this->mo_responden->getDataResponden(4,4);
		
		//echo "<pre>";echo print_r($kategori)."</pre><br/>";
		//echo "<pre>";echo print_r($subkategori)."</pre><br/>";
		//echo "<pre>";echo print_r($get_data)."</pre><br/>";

        $namaFile = "trn_survey.xls";
        $judul = "trn_survey";
        header("Pragma: public");
        header("Expires: 0");
        header("Cache-Control: must-revalidate, post-check=0,pre-check=0");
        header("Content-Type: application/force-download");
        header("Content-Type: application/octet-stream");
        header("Content-Type: application/download");
        header("Content-Disposition: attachment;filename=" . $namaFile . "");
        header("Content-Transfer-Encoding: binary ");

		echo "<table border=\"1\" width=\"100%\">";
		echo "<tr>";
		echo "<th rowspan=\"2\">No</th>";
		echo "<th rowspan=\"2\">Nama</th>";
		echo "<th colspan=\"10\">".$kategori[1]['desc']."</th>";
		echo "<th rowspan=\"2\">Total</th>";
		echo "<th rowspan=\"2\">Kesiapan Pensiun</th>";
		echo "<th rowspan=\"2\">Modus</th>";
		echo "<th rowspan=\"2\">Tipe Pensiun</th>";
		echo "</tr>";
		// tr 2
		echo "<tr>";
		foreach($subkategori[1] as $a)
		{
			echo "<th colspan=\"2\">".$a['name']."</th>";
		}
		echo "</tr>";
		// body
		$i=1;
		foreach($get_data as $a)
		{
			echo "<tr>";
			echo "<td>".$i."</td>";
			echo "<td>".$a['f_name']."</td>";
			$h=0;$j=0; $modus = array();
			foreach($subkategori[1] as $b => $c)
			{
				$d = $a['f_survey']['subkategori'][$b];
				foreach($a['f_survey']['subkategori'][$b] as $e => $f)
				{
					if($e == 6 && $f['urut'] == $d[1]['urut'] || $e == 7 && $f['urut'] == $d[2]['urut'] || $e == 8 && $f['urut'] == $d[3]['urut'] || $e == 9 && $f['urut'] == $d[4]['urut'] || $e == 10 && $f['urut'] == $d[5]['urut'])
					{
						$hasil_akhir = 0;
					}
					else
					{
						$hasil_akhir = $f['jawab'];
					}
					
					//echo "<td>urut : ".$f['urut']."<br/>jawab : ".$f['jawab']."<br/>hasil : ".$cek."</td>";
					echo "<td>".$hasil_akhir."</td>";
					$modus[$h] = $hasil_akhir;
					$j += $hasil_akhir;
					$h++;
				}
			}
			// Total Kesiapan dan Hasilnya
			echo "<td>".$j."</td>";
			echo "<td>".kesiapan($j)."</td>";

			//echo "<pre>";echo print_r($modus)."</pre><br/>";
			// Modus dan Hasilnya
			//echo "<pre>";echo print_r($modus)."</pre><br/>";
			$values = array_count_values($modus);
			asort($values);
			//echo "<pre>";echo print_r($values)."</pre><br/>";
			$hmodus = array_search(max($values), $values); 
			echo "<td>".$hmodus."</td>";
			echo "<td>".tipe_pensiun($hmodus)."</td>";
			echo "</tr>";
			$i++;
		}		
		echo "</table>";
		echo "<br/>\n\n";

		echo "<table border=\"1\" width=\"100%\">";
		echo "<tr>";
		echo "<th rowspan=\"2\">No</th>";
		echo "<th rowspan=\"2\">Nama</th>";
		echo "<th colspan=\"10\">".$kategori[2]['desc']."</th>";
		echo "<th rowspan=\"2\">Total</th>";
		echo "<th rowspan=\"2\">Tingkat Kecemasan</th>";
		echo "<th rowspan=\"2\">Tipe Kecemasan karena kehilangan</th>";
		echo "</tr>";
		// tr 2
		echo "<tr>";
		foreach($subkategori[2] as $a)
		{
			echo "<th colspan=\"2\">".$a['name']."</th>";
		}
		echo "</tr>";
		// body
		$i=1;
		foreach($get_data as $a)
		{
			echo "<tr>";
			echo "<td>".$i."</td>";
			echo "<td>".$a['f_name']."</td>";
			$h=0;$j=0; $modus = array();
			foreach($subkategori[2] as $b => $c)
			{
				$d = $a['f_survey']['subkategori'][$b];
				foreach($a['f_survey']['subkategori'][$b] as $e => $f)
				{
					if($e == 16 && $f['urut'] == $d[11]['urut'] || $e == 17 && $f['urut'] == $d[12]['urut'] || $e == 18 && $f['urut'] == $d[13]['urut'] || $e == 19 && $f['urut'] == $d[14]['urut'] || $e == 20 && $f['urut'] == $d[15]['urut'])
					{
						$hasil_akhir = 0;
					}
					else
					{
						$hasil_akhir = $f['jawab'];
					}
					
					$cemas[$i][$hasil_akhir] .= $c['desc'].',';
					//echo "<td>urut : ".$f['urut']."<br/>jawab : ".$f['jawab']."<br/>hasil : ".$cek."</td>";
					echo "<td>".$hasil_akhir."</td>";
					$modus[$h] = $hasil_akhir;
					$j += $hasil_akhir;
					$h++;
				}
			}
			// Total Kesiapan dan Hasilnya
			echo "<td>".$j."</td>";
			echo "<td>".kecemasan($j)."</td>";

			//echo "<pre>";echo print_r($modus)."</pre><br/>";
			// Modus dan Hasilnya
			//echo "<pre>";echo print_r($modus)."</pre><br/>";
			$values = array_count_values($modus);
			ksort($values);
			//echo "<pre>";echo print_r($values)."</pre><br/>";
			//echo "<pre>";echo print_r($cemas)."</pre><br/>";
			//$hmodus = array_search(min($values), $values); 
			$hmodus = array_search(key($values), $values); 
			$trim = trim($cemas[$i][key($values)],',');
			//echo "<td>".trim($cemas[$i][$hmodus],',')."</td>";
			echo "<td>".$trim."</td>";
			//echo "<td>".tipe_pensiun($hmodus)."</td>";
			echo "</tr>";
			$i++;
		}		
		echo "</table>";
		
		//echo menu_list(1);
		
		
	}
}

/* End of file Responden.php */
/* Location: ./application/modules/Responden/controllers/Responden.php */ 
