<?php

namespace App\Http\Controllers\api\v1;

use App\Http\Controllers\BaseController;

use App\Modules\Bookings\Onsite;

class jophielHolidayControllerRESTv1 extends BaseController
{
    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function index()
    {
            $ch = curl_init();
            $id = Auth::user()->id;
            curl_setopt($ch, CURLOPT_URL, config('jophiel.url').'/api/holiday?staff='.$id);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

            $header = array();
            $header[] = 'Content-length: 0';
            $header[] = 'Content-type: application/json';
            $header[] = 'Authorization: Bearer '. config('jophiel.token');
            curl_setopt($ch, CURLOPT_HTTPHEADER,$header);

            
            $head = curl_exec($ch);
            $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
            curl_close($ch);
            // if ( $httpCode != 200) {
            //     return 'error';
            // }   
            return $head;
    }
    public function store(Request $request)
    {

        $ch = curl_init();
        $id = Auth::user()->id;
        $fields = $request::all();
        $fields['staff_id'] = $id;
        $fields['isApproved'] = false;
        $fields['title'] = $fields['description'];
        $fields_string = json_encode($fields); 

        curl_setopt($ch, CURLOPT_URL, config('jophiel.url').'/api/holiday?staff='.$id);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");  

        $header = array();
        $header[] = 'Content-length: ' . strlen($fields_string);
        $header[] = 'Content-type: application/json';
        $header[] = 'Authorization: Bearer '. config('jophiel.token');
        curl_setopt($ch, CURLOPT_HTTPHEADER,$header);

        
        $head = curl_exec($ch);
        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        curl_close($ch);
        // if ( $httpCode != 200) {
        //     return 'error';
        // }   
        return $head;
    }

    public function daysAllowed(Request $request){
        $ch = curl_init();
        $id = Auth::user()->id;
        curl_setopt($ch, CURLOPT_URL, config('jophiel.url').'/api/daysLeft?staff='.$id);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

        $header = array();
        $header[] = 'Content-length: 0';
        $header[] = 'Content-type: application/json';
        $header[] = 'Authorization: Bearer '. config('jophiel.token');
        curl_setopt($ch, CURLOPT_HTTPHEADER,$header);

        
        $head = curl_exec($ch);
        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        curl_close($ch);
        // if ( $httpCode != 200) {
        //     return 'error';
        // }   
        return $head;
}

}
