<?php

/* 
 * Author       : Jai K
 * Purpose      : Main file for API
 * Created On   : 2016-12-12 11:23
 */


define('CMD'            , strtoupper($_REQUEST['cmd']));
define('TYPE'           , strtoupper($_REQUEST['type']));
define('MODULE'         , strtolower($_REQUEST['module']));



/*  cmd = get_list & type = masters & module = DAILY & data = {"from_date": "2016-09-04", "offset" : 900}  

     - from_date

    - to_date

    - offset
    
    - per_page (100)

    - No. of records

    - */
#error_reporting( 1 );
header('Content-type: application/json; charset=utf-8');

class API {
    
    function __construct(){
        
        $class  = TYPE;
        $fn     = MODULE;
        $json   = file_get_contents('php://input');
        $post   = !empty($json) ? json_decode($json) : $_REQUEST;
        $post   = !is_array($post) ? get_object_vars($post) : $post;
        
        require_once './config.php';    
        require_once './includes/'. $class . '.php';
        
        try {
            
            if (empty(CMD)) :
            throw new Exception('Command cannot be empty!');
            endif;
            
            $class = new $class();
            $class->post = $post;
            $results = $class->$fn();
            
        } catch (Exception $exception) {

            $results['header'] = array(
                'status' => 'F',
                'message' => $exception->getMessage()
            );

            $results['data'] = '';
        }
        
        if( isset( $post['p'] ) ) :
            print_r( $results );
        else :
            //echo json_encode( $results );
            echo json_encode($results, JSON_UNESCAPED_SLASHES);
        endif;
        
        #echo json_encode( $results );
    }

        
}

$api = new API();


 #http://192.168.1.164/thiruapp/api/index.php?cmd=GET_LIST&type=masters&module=services&data={"from_date":"2016-09-04","offset":"900"}