<?php

Header('Access-Control-Allow-Origin: *'); //for allow any domain, insecure          
Header('Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, DELETE'); //method allowed
Header('Access-Control-Allow-Headers: X-Requested-With'); //for allow any headers, insecure

defined('BASEPATH') OR exit('No direct script access allowed');
  
class Api_Controller extends CI_Controller {

	 function __construct()
     {
          parent::__construct();
          $this->load->database();
          $this->load->model('api_model');
          $this->load->model('geofence_model');
          $this->load->model('customer_model');

	    
     }

	

    public function customer_singup_post(){
        $error = "";
        if (($this->input->post('c_mobile')) != "") {
            $data['c_mobile']= $this->input->post('c_mobile');
        } else {
            $error = "Mobile  No is required";
            goto end;
        }
        end :
        if($error !="")
        {
            $result = ["status"=>0,"message"=>$error];
        }
        else
        {
            //NEW CODE
           // $exist = $this->db->select('*')->from('customers')->where('c_mobile',$data['c_mobile'])->get()->result_array();
            $c_otp= rand (1000 , 9999);
            $result_data = $this->customer_model->add_customer($data); 
            if($result_data){
                $result = ["status"=>0,"message"=>"successfully created",'otp'=>$c_otp];
            }else{
                $result = ["status"=>0,"message"=>"Unable To Singup. Please login.."];
            }

          			
        }
	    echo json_encode($result);
	    exit(); 
    }

    public function login_post(){
        $error = "";
        if (($this->input->post('c_mobile')) != "") {
            $data['c_mobile']= $this->input->post('c_mobile');
        } else {
            $error = "Mobile  No is required";
            goto end;
        }
        /*
        if (($this->input->post('c_otp')) != "") {
            $data['c_otp']= $this->input->post('c_otp');
        } else {
            $error = "OTP is required";
            goto end;
        } */
        end :
        if($error !="")
        {
            $result = ["status"=>0,"message"=>$error];
        }
        else
        {
            $this->db->where('c_mobile', $this->input->post('c_mobile'));
            //$this->db->where('c_otp',$this->input->post('c_otp'));
             $query = $this->db->get("customers");
             if ($query->num_rows() >= 1) {
                $result = $query->row_array();
                
               if($result['c_isactive']==0) {
                  
                   $response = ["status"=>0,"message"=>"User not active.Please contact admin"];
               } else {
                   
                  $response = ["status"=>0,"message"=>"You got logged in successfully..","customer_info"=>$result];
              
               }
           } else {
               $response = ["status"=>0,"message"=>"Invalid Mobile or Otp"];
           }
        }

        echo json_encode($response);
	    exit(); 
    }

    public function  user_update_post(){
        $error = "";
        if (($this->input->post('c_id')) != "") {
            $c_id= $this->input->post('c_id');
        }else{
            $error = "c_id is required";
            goto end;
        } 
        if (($this->input->post('c_name')) != "") {
            $data['c_name']= $this->input->post('c_name');
        } 
        if (($this->input->post('c_email')) != "") {
            $data['c_email']= $this->input->post('c_email');
        } 
        if (($this->input->post('c_address')) != "") {
            $data['c_address']= $this->input->post('c_address');
        } 
        end :
        if($error !="")
        {
            $response = ["status"=>0,"message"=>$error];
        }
        else
        {
            $this->db->where('c_id', $c_id);
            $this->db->update('customers', $data);
            if($this->db->affected_rows() > 0){
                $response = ["status"=>1,"message"=>"profile successfully updated"];
            }else{
                $response = ["status"=>0,"message"=>"Unable to Update Profile"];
            }

        }
        echo json_encode($response);
	    exit(); 
    }

    
    
}