<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

    class Login extends CI_Controller {

        function __construct() {

            parent::__construct();
            
        }

        
        public function validate() {
            
            $jsonStr = file_get_contents('php://input');
    $jsonObj = json_decode($jsonStr);
    
    if($jsonObj->pin)
    {
        $sql = "SELECT P.ID, P.NAME, R.NAME AS ROLENAME, R.PERMISSIONS FROM PEOPLE P JOIN ROLES R ON R.ID=P.ROLE WHERE P.APPPASSWORD LIKE 'sha1:".strtoupper(sha1($jsonObj->pin))."'";
    $arr = $this->db->query($sql)->result();
        if($arr)
        {
            $sql2 = "SELECT * FROM COMPANYINFO";
    $arr2 = $this->db->query($sql2)->result();
                $arr = array('status' => 1, 
                     'message' => "logged in", 
                     'token' => uniqid(), 
                     'waiter_id' => $arr[0]->ID,
                    'waiter_name' => $arr[0]->NAME,
                    'role_name' => $arr[0]->ROLENAME,
                    'permissions' => $arr[0]->PERMISSIONS,
                    'currency' => $arr2[0]->CURRENCY,
                    'decimals' => "2",
                    'restaurant' => array('id' => 1, 
                     'name' => $arr2[0]->COMPANYNAME,
                        ),
                    
                    );    

                  $this->output->set_status_header(200);
     header('Content-Type: application/json');
    echo json_encode( $arr );
        }
 else {
      $arr = array('status' => 0, 
                     'message' => "Incorrect PIN!", 
                    
                    );   
         $this->output->set_status_header(401);
     header('Content-Type: application/json');
    echo json_encode( $arr );
 }
  
    }
    
    else
    {
         $arr = array(
                     
                    );   
         $this->output->set_status_header(401);
     header('Content-Type: application/json');
    echo json_encode( $arr );
        
        
    }

  
        }

    }
