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

class Api extends CI_Controller {
function __construct() {
        parent::__construct();
         $this->load->model('user_model');     
         $this->load->helper(array('url','upload'));    
    }
	
    			/**
     * CodeIgniter
     * @package         Survey
     * @author          Reshma Rajan
     * @since           Version 1.0
     * Date:            9-05-2017 
     * Description:     Login
     */ 
	function login()
     {
    
		 $data['username'] = $this->input->post('username');
		 $data['password'] = $this->input->post('password');
         $data['appVersion'] = $this->input->post('appVersion');
         if((isset($data['username']) && $data['username']!=NULL) && (isset($data['password']) && $data['password']!=NULL))
         {
         	$user_exist = $this->user_model->check_email($data);
         	if(isset($user_exist) && !empty($user_exist))
	        {
	        
	            $user_det = $this->user_model->login_data_app($data);
	            if(isset($user_det) && !empty($user_det))
	            {

					foreach ($user_det as $ud) 
					{	
						$userid=$ud['id'];
						$details = $this->user_model->get_taluk_details($userid);
						$panchayath_det = $this->user_model->get_panchayath_details($userid);

						if(isset($data['appVersion']) && $data['appVersion'] !=''){
						$where='id='.$userid;
	            		$this->user_model->update('login',array('app_version'=>$data['appVersion']),$where); 
						}
						
						if(isset($details['id']) && $details['id'] !=0){
							$taluk_name=$details['taluk_name'];
							$taluk_id=$details['id'];
						}else
						{
							$taluk_name='';
							$taluk_id='';
						}
						if(isset($panchayath_det['id']) && $panchayath_det['id'] !='0'){
						$panchayath_id=$panchayath_det['id'];
						$panchayath_name=$panchayath_det['panchayath_name'];
						$ward_det = $this->user_model->get_ward_details($panchayath_id); 

						}else
						{
							$panchayath_name='';
							$panchayath_id='';
							$ward_det=array();
						}
						
					}
					$count=$this->user_model->survey_count($userid);
					$result = array('status'=>ACTIVE,'userid'=>$userid,'taluk'=>$taluk_name,'panchayath'=>$panchayath_name,'taluk_id'=>$taluk_id,'panchayath_id'=>$panchayath_id,'survey_count'=>$count,'ward'=>$ward_det);
	                echo json_encode($result); 
	            }
	            else
	            {
					
	               $result = array('status'=>INACTIVE,'userid'=>'');
	               echo json_encode($result);   
	            }
	        }else
	        {
	        		$result = array('status'=>NOT_FOUND,'userid'=>'');
	            	echo json_encode($result);
	        
	        }
         }
         else 
         {
            $result = array();
            echo json_encode($result);
         }
         
     }
			/**
     * CodeIgniter
     * @package         Survey
     * @author          Reshma Rajan
     * @since           Version 1.0
     * Date:            9-05-2017 
     * Description:     Adding survey details
     */ 
	 function insert_survey()
     {
    		
			 $data1['jsonForm'] = $this->input->post('jsonForm');
			 $jsondata=json_decode($data1['jsonForm']);
			 //$jsondata=$data['jsonForm'];
            // print_r($jsondata);
           $res=array();
           if(isset($jsondata)&&!empty($jsondata))
		   {
		   		$i=1;
				foreach($jsondata as $row)
					{
		
					  $data['name'] = $row->name;
					  $data['mobile'] = $row->mobile;
					  $data['userId'] =  $row->userId;
					  $data['guardian_name'] =  $row->guardian_name;
					  $data['dob'] =  $row->dob;
					  $data['sex'] =  $row->sex;
					  $data['caste'] =  $row->caste;
					  $data['cast_other'] =  $row->caste_other;
					  $data['taluk_name'] =  $row->talukId;
					  $data['grama_panchayath'] =  $row->gramaPanchayathId;
					  $data['ward'] =  $row->ward;
					  if(isset($row->latitude) && $row->latitude !=''){
					  $data['latitude'] =  $row->latitude;
						}
					  if(isset($row->longitude) && $row->longitude !=''){
					  $data['longitude'] =  $row->longitude;
					  }

					  $data['cast_certificate'] =  $row->cast_certificate;
					  $data['toilet_avail'] =  $row->toilet_avail;
					  $data['aadhar'] =  $row->aadhar;
					  $data['aadhar_avail'] =  $row->aadhar_avail;
					  $data['ration_card'] =  $row->ration_card;
					  $data['rationcard_avail'] =  $row->rationcard_avail;
					  $data['jobcard'] =  $row->jobcard;
					  $data['jobcard_avail'] =  $row->jobcard_avail;
					  $data['name_base_line_survey'] =  $row->name_base_line_survey;
					  $data['electric_bill'] =  $row->electric_bill;
					  $data['electic_bill_avail'] =  $row->electic_bill_avail;
					  $data['back_acc'] =  $row->back_acc;
					  $data['bank_name'] =  $row->bank_name;
					  $data['branch_name'] =  $row->branch_name;
					  $data['bank_account_avail'] =  $row->bank_account_avail;
					  $data['ifc_code'] =  $row->ifc_code;
					  $data['cat_bpl_apl'] =  $row->cat_bpl_apl;
					  $data['cat_sc_st'] =  $row->cat_sc_st;
					  $data['cat_farmer'] =  $row->cat_farmer;
					  $data['handicapped'] =  $row->handicapped;
					  $data['landless_labour'] =  $row->landless_labour;
					  $data['women_centric_family'] =  $row->women_centric_family;
					  $data['open_place_avil_or_not'] =  $row->open_place_avil_or_not;
					  $data['toilet_construction'] =  $row->toilet_construction;
					  $data['bank_loan'] =  $row->bank_loan;
					  $data['material_for_construction'] =  $row->material_for_construction;
					  $data['readymade_toilet'] =  $row->readymade_toilet;
					  $data['status'] =  ACTIVE;
					  $data['created_at']=date('Y-m-d');
					  //$question_ans =  json_decode($row->question_ans);

					  
				  
				          if((isset($data['userId']) && $data['userId']!=NULL) && (isset($data['name']) && $data['name']!=NULL) && (isset($data['guardian_name']) && $data['guardian_name']!=NULL) && (isset($data['dob']) && $data['dob']!=NULL))
							 { 
							 		
									
									$data_add = $this->user_model->save('user_data',$data);
									if($data_add > 0)
									{	

										
										upload_api($data_add,$i);
										$res= array('status'=>ACTIVE,'userId'=>$data['userId']);
										
									}
									else 
									 {
									  $res= array('status'=>INACTIVE,'userId'=>'');
									 
									 }

									   
								//}
							     
							 }
							 
					$i++;
					}
					
					
		    }
		    echo json_encode($res);		
	 }

			/**
     * CodeIgniter
     * @package         Survey
     * @author          Reshma Rajan
     * @since           Version 1.0
     * Date:            9-05-2017 
     * Description:     Survey count
     */ 
	function survey_count()
	{
		$data['userId'] = $this->input->post('userId');
		$data['appVersion'] = $this->input->post('appVersion');
		if(isset($data['userId']) && $data['userId'] !='')
		{
			if(isset($data['appVersion']) && $data['appVersion'] !=''){
			$where='id='.$data['userId'];
	        $this->user_model->update('login',array('app_version'=>$data['appVersion']),$where); 
	    	}
			$count=$this->user_model->survey_count($data['userId']);
			$res = array('status'=>ACTIVE,'survey_count'=>$count);
			echo json_encode($res);
		}else
		{
			$res = array('status'=>INACTIVE,'survey_count'=>'');
			echo json_encode($res);
		}

	}
	/*function get_api()
		 {
		
			 $data['userId'] = $this->input->post('userId');
			 $data['imei_number'] = $this->input->post('imei_number');
			 $data['app_version'] = $this->input->post('app_version');
		     $data['page'] = $this->input->post('page');
			 $data['paginationcount'] = $this->input->post('paginationcount');
			 
			 if(isset($data['userId']) && $data['userId']!=NULL)
			 {
				$user_get = $this->user_model->get_data($data);
				if(isset($user_get) && !empty($user_get))
				{
					foreach ($user_get as $get) 
								{
									$res[]= $get;
					            }
								 echo json_encode($res);
				}
				else
				{
				   	$res = array();
					echo json_encode($res);
				}
			 }
			 else
			 {
				$res = array();
				echo json_encode($res);
			 }
			
			 
		 }*/
		 
			 
		 
}
