<?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.
 */

/**
 * Description of User
 *
 * @author user
 */
require APPPATH . '/libraries/REST_Controller.php';

class User extends REST_Controller {

    function __construct() {
        parent::__construct();
        $this->load->database();
        $this->load->model('User_modal');
        $this->load->library('session');
    }

    public function login_post() {
        header('Content-type: application/json');
        $object = json_decode(file_get_contents('php://input'), true);
        
        $data_user=array("username"=>$object['LoginRequest']['SystemUser']['username'],"password"=> md5($object['LoginRequest']['SystemUser']['password']));
        $user = $this->User_modal->get_user($data_user);
        if ($user != null) {
            $token = $this->genarate_token();
            $this->User_modal->insert_user_token($object['LoginRequest']['SystemUser']['username'], $token);
            $message = array("Status" => "200",
                "Message" => "Login Successful!",
                "Token" => $token,
                "User" => $user);
            $response_message = array("LoginResponse" => $message);
            $this->response($response_message);
        } else {
            $message = array("Status" => "1001",
                "Message" => "Invalid username or password");
            $response_message = array("LoginResponse" => $message);
            $this->response($response_message);
        }
    }

    public function genarate_token() {
         return md5(uniqid(rand(), TRUE));
    }

}
