<?php

if (!defined('INCLUDE_DIR')) die('403');

require_once INCLUDE_DIR.'class.company_area.php';

class CompanyAreaAjaxAPI extends AjaxController {

    function getChildren() {
        $parent = '';

        if (isset($_GET['parent']))
            $parent = $_GET['parent'];
        elseif (isset($_POST['parent']))
            $parent = $_POST['parent'];

        $parent = CompanyAreaDependency::normalizeValue($parent);
        $children = CompanyAreaDependency::getChildren($parent, true);

        $items = array();
        foreach ($children as $child) {
            $items[] = array(
                'id'    => $child,
                'value' => $child,
                'label' => $child,
            );
        }

        Http::response(200, $this->json_encode(array(
            'ok'     => true,
            'parent' => $parent,
            'items'  => $items,
        )), 'application/json');
    }
}