<?php

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

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

/**
 * @property Shift_approval_model $Shift_approval_model
 */
class Shift_approval extends CI_Controller
{

    public function __construct()
    {
        parent::__construct();
    }

    public function listing()
    {
        $this->load->model('Shift_approval_model');
        //        log_message('debug','here');
        $request = $this->input->get();
        $data = $this->Shift_approval_model->listing($request);
        echo json_encode($this->Shift_approval_model->build_datatables($request['draw'], $this->Shift_approval_model->totalCount(), $this->Shift_approval_model->filteredCount($request), $data), JSON_UNESCAPED_UNICODE);
    }

    public function save()
    {
        $id = $this->input->get('id');
        $status = $this->input->get('status');

        $this->load->model('Shift_approval_model');
        $makeup = $this->Shift_approval_model->detail($id);

        $this->Shift_approval_model->save([
            'status' => $status,
            'in_charge' => $this->session->userdata('userid')
        ], $id);

        $this->load->model('Event_enrollment_item_model');

        $fromEnrollmentItemId = $makeup['from_item_id'];
        $toTimeslotId = $makeup['to_item_id'];
        $studentId = $makeup['student_id'];

        $this->Event_enrollment_item_model->approved_makeup($studentId, $fromEnrollmentItemId, $toTimeslotId);

        //post json to remote url

        // $url="http://mastery.at-webs.hk/v1/api/apply/real_makeup";
        //        log_message('debug','@ci3 fromitemid:'.$fromitemId.';toitemid:'.$toitemId.';studentid:'.$studentId);//@ci3 fromitemid:2;toitemid:6;studentid:29
        // $ch = curl_init($url);
        # Setup request to send json via POST.
        // $payload = json_encode(array(
        //     "from_item_id" =>  $this->input->get('fromitemid'),
        //     "to_item_id" => $this->input->get('toitemid'),
        //     "student_id" => $this->input->get('studentid'),
        //     "tenant_no" => $this->input->get('tenant_no'),
        // ));
        // 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' => ['success' => true]], JSON_UNESCAPED_UNICODE]);
    }

    public function getalltypes()
    {
        // echo json_encode(['success' => true, 'data' => $this->Shift_approval_model->getAllTypes(), JSON_UNESCAPED_UNICODE]);
    }

    public function getallshiftstatus()
    {
        // echo json_encode(['success' => true, 'data' => $this->Shift_approval_model->getAllShiftStatus(), JSON_UNESCAPED_UNICODE]);
    }
}
