<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
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 */
require APPPATH . '/libraries/REST_Controller.php';

// use namespace
use Restserver\Libraries\REST_Controller;

/**
 * Description of Admin_car
 *
 * @author zmq163
 */
class User extends REST_Controller {

    //put your code here
    public function __construct($config = 'rest') {
        parent::__construct($config);
    }

    /**
     * 
     * 
     * 
     * 
     */
    public function index_get() {
        $this->set_response(array('status' => TRUE, 'message' => 'user data get success.'), REST_Controller::HTTP_OK);
    }

    public function index_post() {
        $this->set_response(array('status' => TRUE, 'message' => 'user data get success.', 'data' => json_decode(file_get_contents('php://input'), TRUE)), REST_Controller::HTTP_CREATED);
    }

    public function index_delete() {
        print_r(json_encode(array('status' => TRUE, 'message' => 'User data delete')));
    }

    public function index_put() {
        print_r(json_encode(array('status' => TRUE, 'message' => 'User data update')));
    }

    /**
     * 
     * 
     * 
     */
    public function userRegistration_post() {
        $getPostData = $this->getData();
    }
/**
     * 
     * 
     * 
     */
    public function userLogin_get() {
      //  $getPostData = $this->getData();
        $getPostData = array('password'=>'password','userId'=>'zmqtest');
        if (isset($getPostData['password']) and $getPostData['password'] != '' and isset($getPostData['userId']) and $getPostData['userId'] != ''and $this->returnBoolean($getPostData['userId'], $getPostData['password']) ) {
            $this->set_response(array('status' => TRUE, 'message' => 'user login success.', 'data' =>NULL), REST_Controller::HTTP_OK);
        } else {
            $this->set_response(array('status' => FALSE, 'message' => 'User id  or password incorrect'), REST_Controller::HTTP_BAD_REQUEST);
        }
    }
    /**
     * 
     * 
     * 
     */
    public function userLogin_post() {
        $getPostData = $this->getData();
        if (isset($getPostData['password']) and $getPostData['password'] != '' and isset($getPostData['userId']) and $getPostData['userId'] != ''and $this->returnBoolean($getPostData['userId'], $getPostData['password']) ) {
            $this->set_response(array('status' => TRUE, 'message' => 'user login success.', 'data' =>NULL), REST_Controller::HTTP_OK);
        } else {
            $this->set_response(array('status' => FALSE, 'message' => 'User id  or password incorrect'), REST_Controller::HTTP_BAD_REQUEST);
        }
    }
    private function returnBoolean($userId,$password){
        if($userId=='zmqtest' and $password='pass123'){
            return TRUE;    
        } else {
            return FALSE;
        }
    }

    /**
     * 
     * 
     * 
     */
    private function getData() {
        return json_decode(file_get_contents('php://input'), TRUE);
    }

}
