<?php
if (!function_exists('renderStatTreeLineNodeEdit')) {
    function renderStatTreeLineNodeEdit($line)
    {
        ?>
        <li class="jstree-open">
        <div class="d-inline-flex align-items-baseline st-node stat-tree-line" data-line-id="{{$line->id}}" data-line-uid="{{$line->uid}}">
            <textarea class="d-none" data-name="columns">{!! $line->columnsJSON() !!}</textarea>
            <textarea class="d-none" data-name="args">{!! $line->argsJSON() !!}</textarea>
            <span>{{$line->displayLabel()}}</span>
            <span class="ml-2 text-secondary">({{(is_null($line->last_refresh_count) ? '-' : $line->last_refresh_count)}})</span>
        </div>
        <?php
            if (count($line->children)) {
                echo '<ul>';
                foreach ($line->children as $child) {
                    renderStatTreeLineNodeEdit($child);
                }
                echo '</ul>';
            }
        ?>
        </li>
        <?php
    }
}
if (!function_exists('renderStatTreeLineNodeEdit_Moes')) {
    function renderStatTreeLineNodeEdit_Moes($line)
    {
        ?>
        <div class="d-flex align-items-baseline stat-tree-line">
            <div moe relative center class="remove-line-moe-{{$line->id}}">
                <a href="#" start show><i class="text-sm fa fa-trash-alt on-hover-opaque text-danger"></i></a>
                <form url="{{ route("practice-management.api.statTreeLine.remove") }}" center>
                    @csrf
                    <input type="hidden" name="uid" value="{{$line->uid}}">
                    <p>Are you sure?</p>
                    <div class="d-flex align-items-center">
                        <button class="btn btn-sm btn-danger mr-2" type="button" submit>Remove</button>
                        <button class="btn btn-sm btn-default mr-2 border" type="button" cancel>Cancel</button>
                    </div>
                </form>
            </div>
        </div>
        <?php
            if (count($line->children)) {
                foreach ($line->children as $child) {
                    renderStatTreeLineNodeEdit_Moes($child);
                }
            }
        ?>
        </li>
        <?php
    }
}
if(!@$statTree && !!@$slug) {
    $statTree = \App\Models\StatTree::where('slug', $slug)->first();
}
?>
@if(@$statTree)
    <div class="d-flex align-items-baseline mb-2">
        <h6 class="font-weight-bold m-0">{{$statTree->name}}</h6>
        <a href="#" id="refresh-counts" class="ml-3">Refresh Counts</a>
        <a href="#" class="tree-expand-all ml-3" title="Expand All"><i class="fa fa-angle-double-down text-secondary"></i></a>
        <a href="#" class="tree-collapse-all ml-2" title="Collapse All"><i class="fa fa-angle-double-up text-secondary"></i></a>
        <div class="d-none if-changed ml-auto mr-2">
            <a href="#" class="btn btn-sm btn-primary text-white" id="btn-save-tree">Save</a>
            <a href="#" class="ml-2 btn btn-sm btn-default border text-dark" onclick="return fastReload()">Reset</a>
        </div>
    </div>
    <div class="stat-tree-view mb-3 overflow-auto mr-2 min-height-300px" id="stat-tree-view-{{$statTree->id}}">
        <ul>
        @foreach($statTree->lines as $line)
            @if(!$line->parent_stat_tree_line_id)
                <?php renderStatTreeLineNodeEdit($line); ?>
            @endif
        @endforeach
        </ul>
    </div>
    @if(!$statTree->rootLines || !count($statTree->rootLines))
        <p class="text-sm text-secondary font-italic">Drag clauses from the left and drop it above to begin.</p>
    @endif
    <div class="border mb-3 p-2 position-absolute" style="left: -10000px; top: -10000px;">
    @foreach($statTree->lines as $line)
        @if(!$line->parent_stat_tree_line_id)
            <?php renderStatTreeLineNodeEdit_Moes($line); ?>
        @endif
    @endforeach
    </div>
@endif
