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

require_once __DIR__ . '/API_V1_Controller.php';

use EA\Engine\Api\V1\Request;
use EA\Engine\Api\V1\Response;
use EA\Engine\Types\NonEmptyText;

/**
 * Providers Controller
 *
 * @package Controllers
 */
class Checkin extends API_V1_Controller {
    /**
     * Providers Resource Parser
     *
     * @var \EA\Engine\Api\V1\Parsers\Providers
     */
    protected $parser;

    /**
     * Class Constructor
     */
    public function __construct()
    {
        parent::__construct();
		$this->load->model('checkin_model');
        $this->parser = new \EA\Engine\Api\V1\Parsers\Providers;
    }

    /**
     * GET API Method
     *
     *  returns all checkin values.
     */
    public function get()
    {
        try
        {			
			$checkins = $this->checkin_model->get_active_checkins();
			
			if(count($checkins) > 0) {
			$i=0;
				$responseArray['count']   = count($checkins);
				$responseArray['message'] = "success";
				foreach($checkins as $checkin) {
					$responseArray['checkins'][$i]['checkin_id']    = $checkin['checkin_id'];
					$responseArray['checkins'][$i]['surname']       = $checkin['surname'];
					$responseArray['checkins'][$i]['mobile_number'] = $checkin['mobile_number'];
					$i++;
				}
			} else {
				$responseArray['count']   = count($checkins);
				$responseArray['message'] = "No Records Found";
			}	
			
			$this->output
				 ->set_status_header('200')
				 ->set_content_type('application/json')
				 ->set_output(json_encode($responseArray));

        }
        catch (Exception $exception)
        {
            $this->handle_exception($exception);
        }
    }


    /**
     * POST API Method to update checkins
     *
     */
    public function post()
    {
        try
        {			
			// Get request body //
            $request   = new Request();
            $reqParam  = $request->get_body();	
			
			$i=0;
			foreach($reqParam as $res) {
				$checkinArray['fullname']   = $res['fullname'];
				$checkinArray['suburb']     = $res['suburb'];
				$checkinArray['email']      = $res['email'];
				$checkinArray['checkin_id'] = $res['checkin_id'];
				$checkinArray['bp_appointment_available'] = ($res['bp_appointment_available']=="yes") ? "1" : "2";
				
				if($checkinArray['bp_appointment_available'] == '2') {
					$checkinArray['status'] = '1';
				}	
				
				$checkinId = $this->checkin_model->update($checkinArray);
				
				$status = !empty($checkinId) ? "Updated Successfully." : "Update Failed.";
				$responseArray[$i]['status']     = $status;		
				$responseArray[$i]['checkin_id'] = $res['checkin_id'];		
				
			$i++;				
			}	
			
			$this->output
				 ->set_status_header('200')
				 ->set_content_type('application/json')
				 ->set_output(json_encode($responseArray));
	
		}
        catch (Exception $exception)
        {
            $this->handle_exception($exception);
        }
		
	}	

}
