<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Range
 *
 * @author mahed
 */
require APPPATH . '/libraries/REST_Controller.php';

class Range extends REST_Controller {

    function __construct() {
        parent::__construct();
        $this->load->database();
        $this->load->model('Range_modal');
        $this->load->model('Token_modal');
    }

    public function editrange_post() {
        $headers = $this->input->request_headers();
        $valid = $this->Token_modal->exit_token($headers['Token']);
        if ($valid != null) {
            header('Content-type: application/json');
            $object = json_decode(file_get_contents('php://input'), true);
            $data = $object['UpdateRangeListRequest']['RangeList']['Range'];
            foreach ($data as $row) {
                $executive = $row['executive'];
                break;
            }
            $range = $this->Range_modal->exit_range($executive);
            if ($range != null) {
                $data_executive = array("executive" => $executive);
                $this->Range_modal->delete_range($data_executive);
                foreach ($data as $row) {
                    $this->Range_modal->add_range($row);
                }
                $message = array("Status" => "200",
                    "Message" => "Updated successfully.");
                $response_message = array("UpdateRangeListResponse" => $message);
                $this->response($response_message);
            } else {
                foreach ($data as $row) {
                    $this->Range_modal->add_range($row);
                }
                $message = array("Status" => "1004",
                    "Message" => "Updated successfully.");
                $response_message = array("UpdateRangeListResponse" => $message);
                $this->response($response_message);
            }
        } else {
            $message = array("Status" => "1002",
                "Message" => "Invalid token");
            $response_message = array("UpdateRangeListResponse" => $message);
            $this->response($response_message);
        }
    }

    //put your code here
}
