<?php
defined('BASEPATH') OR exit('No direct script access allowed');

require(APPPATH.'/libraries/RestController.php');

class Staffapp extends \chriskacerguis\RestServer\RestController
{
    public function __construct() {
        parent::__construct();
        $this->load->model('Staffapp_rest_model'); 
    }
    
     public function index(){
        $r = array(
            'key' => 'method issue'
        );
        $this->response($r); 
    }

    public function updateFCMToken_get(){
        $parmas=[];
        $params[0]['idStaff'] = $this->input->get('idStaff');
        $params[0]['fcmid'] = $this->input->get('fcmid');
        if(isset($params)){
            if(isset($params[0]['idStaff'])){
                $r = $this->Staffapp_rest_model->updateFCMToken($params[0]);
            }else{
                $json = json_decode($params[0],true);
                $r = $this->Staffapp_rest_model->updateFCMToken($json);
            }
            if($r){
                $this->response($r); 
            }else{
                $this->response("Invalid Access"); 
                
            }
        }
    }

    public function onlinestaff_get(){
        $r = $this->api_model->getOnlineStaff(1);
        $this->response($r); 
    }
    
    public function login_get(){

        //echo 'hello'; exit();
        $parmas=[];
        $params[0]['cellNumber'] = $this->input->get('cellNumber');
        $params[0]['username'] = $this->input->get('username');
        $params[0]['fcm_id'] = $this->input->get('fcm_id');
        if(isset($params[0]['cellNumber'])){
            $resp = null;
            $resp = $this->Staffapp_rest_model->login($params[0]);
        } else {
            $this->response("Missing Paramaters"); 
        }

        if($resp){
            $this->response($resp); 
        }else{
            $this->response("Invalid Access"); 
        }
    }
    
    public function checkMobileNumber_get(){
        $cellNumber = $this->input->get('cellNumber');
        if(isset($cellNumber)){
            $resp = null;
            if(isset($cellNumber)){
                $resp = $this->Staffapp_rest_model->checkMobileNumber($cellNumber);
            }
        }else{
            $row = json_decode($cellNumber,true);
            $resp = $this->Staffapp_rest_model->checkMobileNumber($cellNumber);
        }
        if($resp){
            $this->response($resp); 
        }else{
            $this->response("Invalid Access");
        }
    }
    
    public function getVisits_get(){
        $params=[];
        $params[0]['staffId'] = $this->input->get('staffId');
        $params[0]['startDate'] = $this->input->get('startDate');
        $params[0]['endDate'] = $this->input->get('endDate');
        $params[0]['businessId'] = $this->input->get('businessId');
        $resp = null;
        if(isset($params[0]['staffId']) && isset($params[0]['startDate']) && isset($params[0]['endDate']) && isset($params[0]['businessId'])){
            $resp = $this->Staffapp_rest_model->getVisits($params[0]['staffId'], $params[0]['startDate'], $params[0]['endDate'], $params[0]['businessId']);
        }else{
            $row = json_decode($params[0],true);
            $resp = $this->Staffapp_rest_model->getVisits($row['staffId'], $row['startDate'], $row['endDate'], $row['businessId']);
        }
        if($resp){
            $this->response($resp); 
        }else{
            $this->response("Invalid Access"); 
        }
    }

    
    public function staffservicereport_get(){
        $parmas=[];
        $params[0]['staffId'] = $this->input->get('staffId');
        $params[0]['startDate'] = $this->input->get('startDate');
        $params[0]['endDate'] = $this->input->get('endDate');
        $params[0]['businessId'] = $this->input->get('businessId');
        $params[0]['typeid'] = $this->input->get('typeid');
        $params[0]['categoryid'] = $this->input->get('categoryid');
        $resp = null;
        
        
            //$this->response('no parmas'); exit();
            //$row = json_decode($params[0],true);
            $resp = $this->Staffapp_rest_model->commission_details($this->input->get('staffId'), $this->input->get('startDate'),  $this->input->get('endDate'), $this->input->get('typeid'), $this->input->get('categoryid'), $this->input->get('businessId'));
       
        if($resp){
            $this->response($resp); 
        }else{
            $this->response("Invalid Access"); 
        } 
         
    }
    
    public function staffretailreport_get(){
        $parmas=[];
        $params[0]['staffId'] = $this->input->get('staffId');
        $params[0]['startDate'] = $this->input->get('startDate');
        $params[0]['endDate'] = $this->input->get('endDate');
        $params[0]['businessId'] = $this->input->get('businessId');
        $resp = null;
       
            $resp = $this->Staffapp_rest_model->retail_commission_details($this->input->get('staffId'), $this->input->get('startDate'), $this->input->get('endDate'), $this->input->get('businessId'));
        
        if($resp){
            $this->response($resp);
        }else{
            $this->response("Invalid Access");
        } 
    }
    
    public function servicetypes_get(){
        $parmas=[];
        $params[0]['businessId'] = $this->input->get('businessId');
        $resp = null;
        if(isset($params[0]['businessId'])){
            $resp = $this->Staffapp_rest_model->get_services_types($params[0]['businessId']);
        }else{
            $row = json_decode($params[0],true);
            $resp = $this->Staffapp_rest_model->get_services_types($row['businessId']);
        }
        if($resp){
            $this->response($resp);
        }else{
            $this->response("Invalid Access");
        } 
    }
    
    public function servicecategories_get(){
        $parmas=[];
        $params[0]['businessId'] = $this->input->get('businessId');
        $params[0]['servicetypeid'] = $this->input->get('servicetypeid');
        $params[0]['service_type_flag'] = $this->input->get('service_type_flag');

        
        $resp = null;
        if(isset($params[0]['servicetypeid']) && isset($params[0]['service_type_flag']) && isset($params[0]['businessId'])){
            $resp = $this->Staffapp_rest_model->get_services_categories($params[0]['servicetypeid'], $params[0]['service_type_flag'], $params[0]['businessId']);
        }else{
            $row = json_decode($params[0],true);
            $resp = $this->Staffapp_rest_model->get_services_categories($row['servicetypeid'], $row['service_type_flag'],  $row['businessId']);
        }
        
        if($resp){
            $this->response($resp);
        }else{
            $this->response("Invalid Access");
        } 
    }
    
    public function attendance_get(){
      
        $parmas=[];
        $params[0]['cellNumber'] = $this->input->get('cellNumber');
        $params[0]['fcm_id'] = $this->input->get('fcm_id');
        $params[0]['key'] = $this->input->get('key');
        $params[0]['date'] = $this->input->get('date');
        $params[0]['time'] = $this->input->get('time');
        $params[0]['businessId'] = $this->input->get('businessId');
        $params[0]['latitude'] = $this->input->get('latitude');
        $params[0]['longitude'] = $this->input->get('longitude');
        $params[0]['address'] = $this->input->get('address');
        $resp = null;
        if(isset($params[0]['cellNumber']) && isset($params[0]['fcm_id']) && isset($params[0]['key']) && isset($params[0]['date']) && isset($params[0]['time']) && isset($params[0]['businessId']) && isset($params[0]['address']) && isset($params[0]['longitude']) && isset($params[0]['latitude'])){
            $resp = $this->Staffapp_rest_model->mark_staff_attendance($params[0]['cellNumber'], $params[0]['fcm_id'], $params[0]['key'], $params[0]['date'], $params[0]['time'], $params[0]['businessId'], $params[0]['address'] , $params[0]['longitude'] , $params[0]['latitude']);
        }else{
            $row = json_decode($params[0],true);
            $resp = $this->Staffapp_rest_model->mark_staff_attendance($row['cellNumber'], $row['fcm_id'],  $row['key'],  $row['date'],  $row['time'],  $row['businessId'],  $row['longitude'],  $row['latitude']);
        }
        if($resp){
            $this->response($resp); 
        }else{
            $this->response("Invalid Access"); 
        } 
    }

    public function attendance_request_get(){
      
        $parmas=[];
        $params[0]['cellNumber'] = $this->input->get('cellNumber');
        $params[0]['fcm_id'] = $this->input->get('fcm_id');
        $params[0]['key'] = $this->input->get('key');
        $params[0]['date'] = $this->input->get('date');
        $params[0]['time'] = $this->input->get('time');
        $params[0]['businessId'] = $this->input->get('businessId');
        $params[0]['latitude'] = $this->input->get('latitude');
        $params[0]['longitude'] = $this->input->get('longitude');
        $params[0]['address'] = $this->input->get('address');
        $resp = null;
        if(isset($params[0]['cellNumber']) && isset($params[0]['fcm_id']) && isset($params[0]['key']) && isset($params[0]['date']) && isset($params[0]['time']) && isset($params[0]['businessId']) && isset($params[0]['address']) && isset($params[0]['longitude']) && isset($params[0]['latitude'])){
        
            $resp = $this->Staffapp_rest_model->staff_attendance_request($params[0]['cellNumber'], $params[0]['fcm_id'], $params[0]['key'], $params[0]['date'], $params[0]['time'], $params[0]['businessId'], $params[0]['address'] , $params[0]['longitude'] , $params[0]['latitude']);
        
        }else{

            $resp =  "Missing Paramaters"; 
        }

        if($resp){
            $this->response($resp); 
        }else{
            $this->response("Invalid Access"); 
        } 
    }
    
   
    /***********HRM Work******************/ 
    public function getleavetypes_get(){
        $parmas=[];
        $params[0]['businessId'] = $this->input->get('businessId');
        
        $resp = null;
        if(isset($params[0]['businessId'])){
            $resp = $this->Staffapp_rest_model->get_leave_types($params[0]['businessId']);
        }else{
            $row = json_decode($params[0],true);
            $resp = $this->Staffapp_rest_model->get_leave_types($row['businessId']);
        }
        
        if($resp){
            $this->response($resp); 
        }else{
            $this->response("Invalid Access"); 
        } 
    }
    
    public function get_remaining_leaves_get() {
        $parmas=[];
        $params[0]['staff_id'] = $this->input->get('staff_id');
        $resp = null;
        if(isset($params[0]['staff_id'])){
            $resp = $this->Staffapp_rest_model->get_remaining_leaves($params[0]['staff_id']);
        }else{
            $row = json_decode($params[0],true);
            $resp = $this->Staffapp_rest_model->get_remaining_leaves($row['staff_id']);
        }
        if($resp){
           $this->response($resp); 
        }else{
            $this->response("Invalid Access"); 
        }
    }
    
    public function leave_application_get(){
        $parmas=[];
        $params[0]['staff_id'] = $this->input->get('staff_id');
        $params[0]['fcm_id'] = $this->input->get('fcm_id');
        $params[0]['required_from'] = $this->input->get('required_from');
        $params[0]['required_to'] = $this->input->get('required_to');
        $params[0]['business_id'] = $this->input->get('business_id');
        $params[0]['leave_type'] = $this->input->get('leave_type');
        $params[0]['comment'] = $this->input->get('comment');
        $resp = null;
        if(isset($params[0]['staff_id']) && isset($params[0]['fcm_id']) && isset($params[0]['required_from']) && isset($params[0]['required_to']) && isset($params[0]['business_id']) && isset($params[0]['leave_type']) && isset($params[0]['comment'])){
            $resp = $this->Staffapp_rest_model->leave_application($params[0]['staff_id'], $params[0]['fcm_id'], $params[0]['required_from'], $params[0]['required_to'], $params[0]['business_id'], $params[0]['leave_type'], $params[0]['comment']);
        }else{
            $row = json_decode($params[0],true);
            $resp = $this->Staffapp_rest_model->leave_application($row['staff_id'], $row['fcm_id'],  $row['required_from'],  $row['required_to'],  $row['business_id'], $row['leave_type'], $row['comment']);
        }
        
        if($resp){
            $this->response($resp); 
        }else{
            $this->response("Invalid Access");
        } 
    }
    
    public function adv_salary_application_get(){
        $parmas=[];
        $params[0]['staff_id'] = $this->input->get('staff_id');
        $params[0]['fcm_id'] = $this->input->get('fcm_id');
        $params[0]['amount'] = $this->input->get('amount');
        $params[0]['comment'] = $this->input->get('comment');
        $resp = null;
        if(isset($params[0]['staff_id']) && isset($params[0]['fcm_id']) && isset($params[0]['amount']) && isset($params[0]['comment'])){
            $resp = $this->Staffapp_rest_model->adv_salary_application($params[0]['staff_id'], $params[0]['fcm_id'], $params[0]['amount'], $params[0]['comment']);
        }else{
            $row = json_decode($params[0],true);
            $resp = $this->Staffapp_rest_model->adv_salary_application($row['staff_id'], $row['fcm_id'],  $row['amount'], $row['comment']);
        }
        if($resp){
            $this->response($resp); 
        }else{
            $this->response("Invalid Access");
        } 
    }
    
    public function loan_application_get(){
        $parmas=[];
        $params[0]['staff_id'] = $this->input->get('staff_id');
        $params[0]['fcm_id'] = $this->input->get('fcm_id');
        $params[0]['num_of_months'] = $this->input->get('num_of_months');
        $params[0]['amount'] = $this->input->get('amount');
        $params[0]['comment'] = $this->input->get('comment');
        $resp = null;
        if(isset($params[0]['staff_id']) && isset($params[0]['fcm_id']) && isset($params[0]['amount']) && isset($params[0]['num_of_months']) && isset($params[0]['comment'])){
            $resp = $this->Staffapp_rest_model->loan_application($params[0]['staff_id'], $params[0]['fcm_id'], $params[0]['amount'], $params[0]['num_of_months'], $params[0]['comment']);
        }else{
            $row = json_decode($params[0],true);
            $resp = $this->Staffapp_rest_model->loan_application($row['staff_id'], $row['fcm_id'], $row['amount'], $row['num_of_months'], $row['comment']);
        }
        if($resp){
            $this->response($resp); 
        }else{
            $this->response("Invalid Access");
        } 
    }
    
    public function staff_attendance_report_get(){
        $params=[];
        $params[0]['staff_id'] = $this->input->get('staff_id');
        $params[0]['month'] = $this->input->get('month');
        $params[0]['year'] = $this->input->get('year');
        $resp = null;
        if(isset($params[0]['staff_id']) && isset($params[0]['month']) && isset($params[0]['year'])){
            $resp = $this->Staffapp_rest_model->staff_month_attendance_detail($params[0]['staff_id'], $params[0]['year'], $params[0]['month']);
        }else{
            $row = json_decode($params[0],true);
            $resp = $this->Staffapp_rest_model->staff_month_attendance_detail($row['staff_id'], $row['year'], $row['month']);
        }
        
        if($resp){
            $this->response($resp); 
        }else{
            $this->response("Invalid Access"); 
        } 
        
        
    }

    public function attendance_request_report_get(){
        $params=[];
        $params[0]['staff_id'] = $this->input->get('staff_id');
        $params[0]['month'] = $this->input->get('month');
        $params[0]['year'] = $this->input->get('year');
        $params[0]['business_id'] = $this->input->get('businessid');
        $resp = null;
        
        if(isset($params[0]['staff_id']) && isset($params[0]['month']) && isset($params[0]['year'])){
            $resp = $this->Staffapp_rest_model->attendance_request_report($params[0]['staff_id'], $params[0]['year'], $params[0]['month'], $params[0]['business_id']);       
        }
        
        if($resp){
            $this->response($resp); 
        }else{
            $this->response("Invalid Access"); 
        } 
        
        
    }
    
    public function leave_application_report_get(){
        $params=[];
        $params[0]['staff_id'] = $this->input->get('staff_id');
        $resp = null;
        if(isset($params[0]['staff_id'])){
            $resp = $this->Staffapp_rest_model->leave_application_report($params[0]['staff_id']);
        }else{
            $row = json_decode($params[0],true);
            $resp = $this->Staffapp_rest_model->leave_application_report($row['staff_id']);
        }
        if($resp){
            $this->response($resp); 
        }else{
            $this->response("Invalid Access"); 
        }
    }
    
    public function adv_salary_application_report_get(){
        $params=[];
        $params[0]['staff_id'] = $this->input->get('staff_id');
        $resp = null;
        if(isset($params[0]['staff_id'])){
            $resp = $this->Staffapp_rest_model->adv_salary_application_report($params[0]['staff_id']);
        }else{
            $row = json_decode($params[0],true);
            $resp = $this->Staffapp_rest_model->adv_salary_application_report($row['staff_id']);
        }
        
        if($resp){
            $this->response($resp); 
        }else{
            $this->response("Invalid Access"); 
        } 
    }
    
    public function loan_application_report_get(){
        $params=[];
        $params[0]['staff_id'] = $this->input->get('staff_id');
        $resp = null;
        if(isset($params[0]['staff_id'])){
            $resp = $this->Staffapp_rest_model->loan_application_report($params[0]['staff_id']);
        }else{
            $row = json_decode($params[0],true);
            $resp = $this->Staffapp_rest_model->loan_application_report($row['staff_id']);
        }
        
        if($resp){
            $this->response($resp); 
        }else{
            $this->response("Invalid Access"); 
        } 
    }
    
}
        