<?php

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

//require APPPATH . '/libraries/MY_Controller.php';
//use Restserver\Libraries\MY_Controller;

class Leave_approval extends CI_Controller
{

    public function __construct()
    {
        parent::__construct();
        $this->load->model('Leave_approval_model');
    }

    public function listing()
    {
        $request = $this->input->get();
        $data = $this->Leave_approval_model->listing($request);
        echo json_encode($this->Leave_approval_model->build_datatables($request['draw'], $this->Leave_approval_model->totalCount(), $this->Leave_approval_model->filteredCount($request), $data), JSON_UNESCAPED_UNICODE);
    }

    public function save()
    {
        $params = ['leave_status' => $this->input->get('leave_status'), 'in_charge' => $this->input->get('admin_id')];
        $this->Leave_approval_model->save($params, $this->input->get('id'));
        //post json to remote url
        $url = "http://mastery.at-webs.hk/v1/api/apply/real_leave";
        $ch = curl_init($url);
        # Setup request to send json via POST.
        $payload = json_encode(array(
            "item_id" => $this->input->get('item_id'),
            "tenant_no" => $this->input->get('tenant_no'),
        ));
        //        print_r ($payload);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json"));
        # Return response instead of printing.
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        # Send request.
        $response = curl_exec($ch);
        curl_close($ch);
        # Print response.
        echo json_encode(['success' => true, 'data' => ['response' => $response], JSON_UNESCAPED_UNICODE]);
    }

    public function getfilteringdata()
    {
        $filteringdata = $this->Leave_approval_model->getFilteringData();
        $data['type'] = $filteringdata['type'];
        $data['leave_status'] = $filteringdata['leave_status'];
        echo json_encode(['success' => true, 'data' => $data, JSON_UNESCAPED_UNICODE]);
    }
}
