<?php

namespace Hura8\Traits;

trait AdminEntityCategoryControllerTraits
{


    public function getDropBox($selectedId, $categoryParentId, $level=1){

        $all_categories = $this->getAllParent([]);

        $extra_space = "";
        for($i = 1; $i < $level; $i++){
            $extra_space .= "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
        }

        $result = "";

        if(isset($all_categories[$categoryParentId])) {
            foreach($all_categories[$categoryParentId] as $cat_info){
                $cat_id = $cat_info['id'];

                if($selectedId == $cat_id) {
                    $result .= "<option value='".$cat_id."' selected>". $extra_space ." - ".$cat_info['title']."</option>";
                }
                else {
                    $result .= "<option value='".$cat_id."'>". $extra_space ." - ".$cat_info['title']."</option>";
                }

                if($cat_info['is_parent']) {
                    $result .= $this->getDropBox($selectedId, $cat_id, $level+1);
                }
            }
        }

        return $result;
    }


    public function categorySelectBox(array $array_selected, $categoryParentId, $level=1){

        $all_categories = $this->getAllParent([]);

        if(!isset($all_categories[$categoryParentId])) return '';

        $extra_space = "";
        for($i = 1; $i < $level; $i++){
            $extra_space .= "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
        }

        $result = '';

        foreach ( $all_categories[$categoryParentId] as $item ) {

            $checked = (in_array($item['id'], $array_selected)) ? "checked" : "";

            if($item['is_parent']) {
                $result .= $extra_space ." <strong>". $item['title']."</strong><br/>";
                $result .= $this->categorySelectBox( $array_selected, $item['id'], $level+1);
            }else{
                if(!$checked) {
                    $result .= $extra_space . " <input type='checkbox' id='cat_" . $item['id'] . "' value=\"" . $item['id'] . "\" onchange=\"select_cat('cat_" . $item['id'] . "', " . $item['id'] . ")\" " . $checked . " /> <label for='cat_" . $item['id'] . "'>" . $item['title'] . "</label><br/>";
                }else{
                    $result .= $extra_space . " <input type='checkbox' id='cat_" . $item['id'] . "' value=\"" . $item['id'] . "\" onchange=\"select_cat('cat_" . $item['id'] . "', " . $item['id'] . ")\" " . $checked . " /> <label for='cat_" . $item['id'] . "' style='background-color:#FC0'>" . $item['title'] . "</label><br/>";
                }
            }

        }

        return $result;
    }

}
