<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Claims extends CI_Controller {

	function __construct() {
		parent::__construct();
		$this->load->model('My_model');
		$this->load->library('admin_layout');	
	}

	function index() {
		// if(!$username=$this->session->userdata('logged_in')) redirect(base_url().'sign_in');
		// $user_id = $this->session->userdata('user_id');
			
		// $header_data = array(
		//   'username'  		=> $username,
		//   'user_id'    		=> $user_id
		// );
		
		$this->admin_layout->set_title("Claim");
		$this->admin_layout->set_description("");

		
		$content = "admin/view_claims";
		$data['temp']='';
		$this->admin_layout->view($content,'',$data,true);
	
	}


	
	private function get_data($param) {	
		$rows = $this->subscriber_model->get_rows($param);
		return json_data($rows ? $rows : []);			
	}
	

  
 	/************************************************
	* Access Data Source via AJAX call
	************************************************/	

	public function data_source(){
		if(!$username=$this->session->userdata('logged_in')) redirect(base_url().'sign_in');
		$user_id = $this->session->userdata('user_id');
				
		if(!$this->input->is_ajax_request()){
			echo "<h1>Invalid access.</h1>";
		}		
		foreach($_POST as $key=>$value) {
			if($value != '') $$key = $value;
			else $$key = false;
		}
		
		//Check agent_id if it belongs to admins.		
		$where = array(
			'admin_id' => $user_id,
			'agent_id' => $agent
		);
		
		if(!$agent_id = $this->my_model->get_value('admin_agents','agent_id',$where)) return false;
				
		//$where_param = " t0.agent_id = "  .$agent_id;
		//$where_param .= " AND t1.client_id > 1";		

		if($search !== false) $this->param['search'] = $search;
		//getcycleid
		$today = date('Y-m-d',strtotime($date_to));
		$select = 't0.*';		
	    $tables = array('cycles','company_cycle_category');
	    $ids = array('category');
	    $where = "t1.company_id = " .$parent_id ." AND DATE('".$today."') BETWEEN date_start AND date_end";
		$orderby = ""; 	
	    $limit = "";
	    $get_cycle_id = $this->my_model->get_row_join($select,$tables,$ids,$where,$orderby,$limit);
		$this->param['cycle_id'] = $get_cycle_id->cycle_id;
		//$this->param['cycle_id'] = 7;
		//
		$this->param['agent_id'] = $agent;
		$this->param['date_from'] = date('Y-m-d',strtotime($date_from));		
		$this->param['date_to'] = date('Y-m-d',strtotime($date_to));			
		
		if(!$result = $this->get_data($this->param)) {
			echo json_encode(array("SQL" => $this->db->last_query()));
		}
		else echo $result;
		
				
	}



	
	/*******************************
	* Top Bar definition 
	*******************************/	
	private function topbar($user_id) {
		$parent_id = $this->session->userdata('parent_id');
		$daterange = array(
			'type' 		=> 'daterange',	
			'col_width'	=> 'col-md-4',	
			'date_from'	=> date('m/01/Y'),
			'date_to'	=> date('m/d/Y')
		);
	   
		/*******************************************************
		* Get list of agents for Select Option 
		*******************************************************/		
		$this->load->model('agent_model');
		$this->load->helper('arraydata_helper');
		// $result = $this->agent_model->get_agents_name($user_id);
		$select = 't1.*';		
	    $tables = array('admin_agents','agents');
	    $ids = array('agent_id');
	    $where = "t0.admin_id = " .$user_id ." AND t0.status = 1 AND t1.status = 1";
		$orderby = "t1.display_name ASC"; 	
	    $limit = "";
	    $result = $this->my_model->get_rows_join($select,$tables,$ids,$where,$orderby,$limit);
		//
		$agents = obj_to_array_values($result);
		   
		$agent = array(
			'type' 		=> 'select',	
			'col_width'	=> 'col-md-3',	
			'id'		=> 'agent',
			'label'		=> 'Agent',
			'options'	=> $agents			
		);
		$agents = obj_to_array_values($result);
		   
		
	   
	   	/*******************************************************
	   	* Get list of company cycles for Select Option 
	   	*******************************************************/
		$where = array('admin_id' => $user_id);
	 	$company_id = $this->my_model->get_value('admins','company_id',$where);
		
		// $this->load->model('cycle_model');		  
	    // $result = $this->cycle_model->get_cycles($company_id);
	    $select = 'cycle_id,CONCAT("Cycle #",LPAD(t1.cycle,2,"0")," (",DATE_FORMAT(date_start,"%Y/%m/%d"),"-",DATE_FORMAT(date_end,"%Y/%m/%d"),")") as cycle';		
	    $tables = array('company_cycle_category','cycles');
	    $ids = array('category');
	    $where = "t0.company_id = " .$company_id;
		$orderby = "t1.cycle_id ASC"; 	
	    $limit = "";
	    $result = $this->my_model->get_rows_join($select,$tables,$ids,$where,$orderby,$limit);
	    $cycles = obj_to_array_values($result);
		
		// $current_cycle_id = $this->cycle_model->get_cycle_id_by_company_id($company_id);
		// getcycleid
		// $today = date('Y-m-d');
		// $select = 't0.*';		
	 //    $tables = array('cycles','company_cycle_category');
	 //    $ids = array('category');
	 //    $where = "t1.company_id = " .$user_id ." AND DATE('".$today."') BETWEEN date_start AND date_end";
		// $orderby = ""; 	
	 //    $limit = "";
	 //    $get_cycle_id = $this->my_model->get_row_join($select,$tables,$ids,$where,$orderby,$limit);
		// //
		// $cycle = array(
	 //   		'type' 		=> 'select',	
		// 	'col_width'	=> 'col-md-4',	
		// 	'id'			=> 'cycle',
		// 	'label'		=> 'Cycle',
		// 	'options'	=> $cycles,
		// 	'selected'	=> $get_cycle_id->cycle_id
	 //   );

	      if($parent_id == 2339){
			   $agent = array(
					'type' 		=> 'select',	
					'col_width'	=> 'col-md-3',	
					'id'			=> 'agent',
					'label'		=> 'DSR NAME ',
					'options'	=> $agents			
				);
	      }else{
			   $agent = array(
					'type' 		=> 'select',	
					'col_width'	=> 'col-md-3',	
					'id'			=> 'agent',
					'label'		=> 'Agent ',
					'options'	=> $agents			
				);
	      } 
	   

		
		
		$search = array(
			'type'		 	=> 'input',	
			'col_width'  	=> 'col-md-3',
			'id'		 		=> 'search',
			'placeholder'	=> 'Search'
		);
		
		$button = array(
			'type'		=> 'button',	
			'id'			=> 'topbar-submit',
			'label'		=> 'Go!',
			'icon'		=> '<i class="fa fa-refresh" aria-hidden="true"></i>',
			'attach_to'	=> 'search',  //Attach to input element
			'style'		=> 'btn-warning'
		);
		
		$export = array(
			'type'		=> 'button',
			'id'		=> 'topbar-export',
			'label'		=> 'Export to Excel',
			'icon'		=> '<i class="fa fa-file-excel-o" aria-hidden="true"></i>',			
			'style'		=> 'btn-success'
		);
		$print = array(
			'type'		=> 'button',			
			'id'		=> 'topbar-print',
			'label'		=> 'Print',
			'icon'		=> '<i class="fa fa-print" aria-hidden="true"></i>',
			'style'		=> 'btn-default'
		);
		
		
		return json_encode(array(
			$daterange,
			$agent,		   		
			$search,
			$button,
			$export,
			$print	
		));	   
	}
}
