<?php
use Restserver\Libraries\REST_Controller;
defined('BASEPATH') OR exit('No direct script access allowed');

// This can be removed if you use __autoload() in config.php OR use Modular Extensions
/** @noinspection PhpIncludeInspection */
//To Solve File REST_Controller not found
// https://www.codexworld.com/codeigniter-rest-api-user-login-registration/
require APPPATH . 'libraries/REST_Controller.php';
require APPPATH . 'libraries/Format.php';

/**
* This is an example of a few basic user interaction methods you could use
* all done with a hardcoded array
*
* @package         CodeIgniter
* @subpackage      Rest Server
* @category        Controller
* @author          Phil Sturgeon, Chris Kacerguis
* @license         MIT
* @link            https://github.com/chriskacerguis/codeigniter-restserver
*/
class Api extends REST_Controller {

    function __construct()
    {
        // Construct the parent class
        parent::__construct();

        // Configure limits on our controller methods
        // Ensure you have created the 'limits' table and enabled 'limits' within application/config/rest.php
        //$this->methods['blog_get']['limit'] = 500; // 500 requests per hour per user/key
       // $this->methods['blog_post']['limit'] = 100; // 100 requests per hour per user/key
        //$this->methods['users_delete']['limit'] = 50; // 50 requests per hour per user/key
		$this->load->model('Api_mdl');
		$this->load->model('Cms_mdl');
    }
	
	function blog_get(){
	
		$result = $this->Cms_mdl->cms_details('about_us','main_content');
		
		/*if(isset($_GET['blog_id']) && $_GET['blog_id']!=''){
			$blog_id=$_GET['blog_id'];
		}else{
			$blog_id=0;
		}
		if(isset($_GET['category_id']) && $_GET['category_id']!=''){
			$category_id=$_GET['category_id'];
		}else{
			$category_id=0;
		}*/
       // $result = $this->Api_mdl->blog_details($blog_id,$category_id);

        if($result){

            $this->response($result, 200); 

        }else{

            //$this->response("No record found", 404);
			$this->response([
				'status' => True,
				'error' => 'No record found.'
			], REST_Controller::HTTP_BAD_REQUEST);

        }
    }
	
	function blogcomment_get(){
		
		if(isset($_GET['blog_id']) && $_GET['blog_id']!=''){
			$blog_id=$_GET['blog_id'];
		}else{
			$blog_id=0;
		}
        $result = $this->Api_mdl->blog_commnet_details($blog_id);

        if($result){

            $this->response($result, 200); 

        }else{

            //$this->response("No record found", 404);
			$this->response([
				'status' => True,
				'error' => 'No record found.'
			], REST_Controller::HTTP_BAD_REQUEST);

        }
    }
	
	public function signin_post(){
       
		// Get the post data
        $email = $this->post('email');
        $password = $this->post('password');
        
        // Validate the post data
        if(!empty($email) && !empty($password)){
            
            // Check if any user exists with the given credentials
            $con['returnType'] = 'single';
            $con['conditions'] = array(
                'email' => $email,
                'password' => md5($password),
                'status' => 0
            );
            $user = $this->Api_mdl->getRows($con);
            
            if($user){
                // Set the response and exit
                $this->response([
                    'status' => TRUE,
                    'message' => 'User login successful.',
                    'data' => $user
                ], REST_Controller::HTTP_OK);
            }else{
                // Set the response and exit
                //BAD_REQUEST (400) being the HTTP response code
                //$this->response("Wrong email or password.", REST_Controller::HTTP_BAD_REQUEST);
				$this->response([
                    'status' => FALSE,
                    'error' => 'Wrong email or password.'
                ], REST_Controller::HTTP_BAD_REQUEST);
            }
        }else{
            // Set the response and exit
            //$this->response("Provide email and password.", REST_Controller::HTTP_BAD_REQUEST);
			$this->response([
				'status' => FALSE,
				'error' => 'Provide email and password.'
			], REST_Controller::HTTP_BAD_REQUEST);
        }
		
    }	
	
	public function registration_post(){
        // Get the post data
        $name = strip_tags($this->post('name'));
        $email = strip_tags($this->post('email'));
        $password = $this->post('password');
        $mobile_no = strip_tags($this->post('mobile_no'));
        $address = strip_tags($this->post('address'));
        $dob = strip_tags($this->post('dob'));
        $gender = strip_tags($this->post('gender'));
        $reg_date = strtotime(date('Y-m-d'));
        
        // Validate the post data
        if(!empty($name) && !empty($email) && !empty($password)){
            
            // Check if the given email already exists
            $con['returnType'] = 'count';
            $con['conditions'] = array(
                'email' => $email,
            );
            $userCount = $this->Api_mdl->getRows($con);
            
            if($userCount > 0){
                // Set the response and exit
                //$this->response("The given email already exists.", REST_Controller::HTTP_BAD_REQUEST);
				$this->response([
					'status' => FALSE,
					'error' => 'The given email already exists.'
				], REST_Controller::HTTP_BAD_REQUEST);
            }else{
                // Insert user data
                $userData = array(
                    'name' => $name,
                    'email' => $email,
                    'password' => md5($password),
					'password_v' => $password,
                    'mobile_no' => $mobile_no,
                    'address' => $address,
                    'dob' => $dob,
                    'gender' => $gender,
                    'reg_date' => $reg_date
                );
				
                $insert = $this->Api_mdl->registration_insert($userData);
                
                // Check if the user data is inserted
                if($insert){
                    // Set the response and exit
                    $this->response([
                        'status' => TRUE,
                        'message' => 'The user has been added successfully.',
                        'data' => $insert
                    ], REST_Controller::HTTP_OK);
                }else{
                    // Set the response and exit
                    //$this->response("Some problems occurred, please try again.", REST_Controller::HTTP_BAD_REQUEST);
					$this->response([
						'status' => FALSE,
						'error' => 'Some problems occurred, please try again.'
					], REST_Controller::HTTP_BAD_REQUEST);
                }
            }
        }else{
            // Set the response and exit
            // $this->response("Provide complete user info to add.", REST_Controller::HTTP_BAD_REQUEST);
			$this->response([
				'status' => FALSE,
				'error' => 'Provide complete user info to add.'
			], REST_Controller::HTTP_BAD_REQUEST);
        }
    }
	
	


}
