<?php 
/*
Fungsi : Controller google map
*/

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

class map extends CI_Controller {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -  
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in 
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */
	
/*-------------Inisialisasi variabel--------------*/ 
	
	//Menu ID utk keperluan otorisasi
	
	var $menu_supplier_map  = 135;
	
	
/*-------------Inisialisasi variabel--------------*/ 	

/*------------Fungsi lokal------------------------*/
	


/*------------Fungsi local------------------------*/	
	function __construct()
	{
		parent::__construct();
		
		$this->load->model('branch/supp_map_model', '', TRUE);
		$this->fungsi->check_login();
	}
	
	
	public function index()
	{	
		$this->map_supp_report();
		
	}
	
/*==========================================SUPLIER MAP===================================*/
	function map_supp_report()
	{
		//Otorisasi menu
		$this->fungsi->menu_auth($this->menu_supplier_map);
		
		$data['menu_path'] = 'BRANCH > BRANCH SUPPLIER MAPS';
		
		if($this->session->userdata('idcab')==1)
		{
			$data['cab'] =   $this->supp_map_model->get_cabang()->result();
		}
		else
		{
			$data['cab'] =   $this->supp_map_model->get_cabang_byidcab($this->session->userdata('idcab'))->result();
		}
		
		$this->load->view('branch/branch_report_form_map', $data);	
	}
	
	function fetch_supp_data()
	{
		
	  $data = array();
	  $data2 = array();
	  $sql = $this->db->query("SELECT * FROM m_supp WHERE idcab = '".$this->input->post('idcab')."' AND lat <> '' AND suppname LIKE '%".$this->input->post('txt_search')."%'")->result();
	  
	  foreach($sql as $row)
			{
				
	  			$row_array["name"] = $row->suppname;
				$row_array["addr"] = $row->addr;
				$row_array["long"] = (float)$row->long;
				$row_array["lat"] = (float)$row->lat;
				
	  			array_push($data2, $row_array);
			}
			
		
		echo json_encode($data2);
	}
	
	function search_supp_data()
	{
		
	  $data = array();
	  $data2 = array();
	  $sql = $this->db->query("SELECT * FROM m_supp WHERE idcab = '".$this->input->post('idcab')."' AND lat <> '' AND suppname LIKE '%".$this->input->post('txt_search')."%'")->result();
	  
	  foreach($sql as $row)
			{
				
	  			$row_array["name"] = $row->suppname;
				$row_array["addr"] = $row->addr;
				$row_array["long"] = (float)$row->long;
				$row_array["lat"] = (float)$row->lat;
				
	  			array_push($data2, $row_array);
			}
			
		
		echo json_encode($data2);
	}
/*==========================================	SUPPLIER MAP end===================================*/

}