<?php
if (!defined('BASEPATH'))
    exit('No direct script access allowed');
class Apilogin extends CI_Controller
{
    var $main_menu_name = "";
    var $sub_menu_name = "";
    public function __construct()
    {
        parent::__construct();
        $this->load->model('User_Model');
        $this->load->model('Common_Model');
        ini_set('display_errors', 1); 
        ini_set('display_startup_errors', 1); 
        error_reporting(E_ALL);
    }
    public function index()
    {
        $data = array(
            "status" => 0,
            "msg" => "Error",
            "err_code" => "error-200-a"
        );
        echo json_encode($data);
    }

    public function login()
	{
	    if (isset($_SERVER['HTTP_ORIGIN'])) {
      // Decide if the origin in $_SERVER['HTTP_ORIGIN'] is one
      // you want to allow, and if so:
      header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}");
      header('Access-Control-Allow-Credentials: true');
      header('Access-Control-Max-Age: 86400');    // cache for 1 day
    }
    // Access-Control headers are received during OPTIONS requests
    if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {

      if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD']))
        // may also be using PUT, PATCH, HEAD etc
        header("Access-Control-Allow-Methods: GET, POST, OPTIONS");

      if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']))
        header("Access-Control-Allow-Headers: {$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}");

      exit(0);
    }
        
        $status = 0;
        $msg = '';
		$user_username=$this->input->get('user_username');
			//$password=$this->input->post('password');
			//get user details by id
		$user_id=$this->User_Model->remote_login($user_username);
			//echo "<br/>test:$user_id";
			
			if($user_id){
				$data['user_details']=$this->User_Model->get_user_info($user_id);
				//create sessions
				$ss_user_username=$data['user_details']['user_username'];
				$ss_user_id=$data['user_details']['user_id'];
				$ss_group_id=$data['user_details']['group_id'];
				$ss_warehouse_id=$data['user_details']['warehouse_id'];	
				$ss_dept_id=$data['user_details']['dep_id'];	
				$ss_user_first_name=$data['user_details']['user_first_name'];
				$ss_user_last_name=$data['user_details']['user_last_name'];
				$ss_user_group_name=$data['user_details']['user_group_name'];
			    $check_chashier_float=$this->User_Model->get_user_chashier_float($user_id);
			    $float_status=0;
			    if($check_chashier_float>0){
			       $float_status=1; 
			    }
				$sesdata = array(
                   'ss_user_username'  => $ss_user_username,
                   'ss_user_id'     => $ss_user_id,
                   'ss_group_id' => $ss_group_id,
                   'ss_dept_id'     => $ss_dept_id,
				   'ss_warehouse_id' => $ss_warehouse_id,
				   'ss_user_first_name' => $ss_user_first_name,
				   'ss_user_last_name' => $ss_user_last_name,
				   'ss_user_group_name' => $ss_user_group_name,
				   'ss_cashier_float_id' => $check_chashier_float,
				    'ss_cashier_float_status'=>$float_status
               );
			$this->User_Model->create_user_sessions($sesdata);
				//insert user activity
				$this->Common_Model->add_user_activitie("Log User");
                //$status = 1;
                //$msg .= "successfully added."; 
                redirect('dashboard');
			}
			else {
				$msg .= "Failed. (Error code: error-407-i). \n";
				redirect('login');
			}

        echo json_encode(array(
            "status" => $status,
            "msg" => $msg,
            'data' => array()
        ));
	}

}