<?php

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

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

class Tuition extends CI_Controller {

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

	public function get_rate_sheet() {
		$mode = $this->input->get('mode');
		$data = $this->Tuition_model->get($mode);
		echo json_encode(['success' => true, 'data' => [
			'rate_sheet' => $data,
		]], JSON_UNESCAPED_UNICODE);
	}

	public function get() {
    	$tuition_id = $this->input->get('rate_sheet');
		$this->load->model('Tuition_amount_model');
    	$data = $this->Tuition_amount_model->get($tuition_id);
    	$this->load->model('Tuition_label_model');
    	$label = $this->Tuition_label_model->get($tuition_id);
    	echo json_encode(['success' => true, 'data' => [
    		'data' => $data,
			'label' => $label,
		]], JSON_UNESCAPED_UNICODE);
	}

    public function save(){
        $request = $this->input->post();
		$this->load->model('Tuition_label_model');
		$this->Tuition_label_model->saveVersion($request['label'], $request['rate_sheet']);
		$this->load->model('Tuition_amount_model');
		$this->Tuition_amount_model->saveVersion($request['tuition'], $request['rate_sheet']);
        echo json_encode(['success'=>true, 'data'=>[], JSON_UNESCAPED_UNICODE]);
    }

	public function insert(){
		$request = $this->input->post();
		$data = [
			'mode' => $request['mode'],
			'title' => $request['title'],
		];
		$tuition_id = $this->Tuition_model->insert($data);
		$this->load->model('Tuition_label_model');
		$this->Tuition_label_model->insert($request['label'], $tuition_id);
		$this->load->model('Tuition_amount_model');
		$this->Tuition_amount_model->insert($request['tuition'], $tuition_id);
//		$this->Tuition_model->saveVersion($request['tuition'], $request['mode']);
		echo json_encode(['success'=>true, 'data'=>[], JSON_UNESCAPED_UNICODE]);
	}

	public function rename() {
		$request = $this->input->post();
		$data = [
			'title' => $request['title'],
		];
		$this->Tuition_model->save($data, $request['id']);
		echo json_encode(['success'=>true, 'data'=>[], JSON_UNESCAPED_UNICODE]);
	}

}
