<div class="mcp-theme-1 popup-content-container p-3">
    @if(@$rows === 'error')
        <div class="text-secondary">
            <i class="fa fa-exclamation-triangle"></i>
            Query error / missing arg values!
        </div>
    @else
        @if(!count($columns))
            <div class="text-secondary">
                <i class="fa fa-exclamation-triangle"></i>
                Report columns not configured for this report!
            </div>
        @else
            <?php
            $userClauseArgs = [];
            ?>
            <div class="mb-2 d-flex flex-wrap">
                @foreach($line->lineClauses as $lineClause)
                    <div class="border rounded px-2 py-1 mr-2 bg-light clause-label-part">{{$lineClause->clause_label}}
                        @if(count($lineClause->lineClauseArgs))
                            <?php
                            $argsLabel = [];
                            foreach($lineClause->lineClauseArgs as $lineClauseArg) {
                                $argLabel = $lineClauseArg->clauseArg->arg_text . ': ' . $lineClauseArg->value;
                                $argsLabel[] = $argLabel;
                                if($lineClauseArg->access_level === 'USER') {
                                    $userClauseArgs[] = $lineClauseArg->clauseArg->arg_text;
                                }
                            }
                            ?>
                            [{{implode(', ', $argsLabel)}}]
                        @endif
                    </div>
                @endforeach
            </div>

            <!-- filters -->
            <a href="#" class="toggle-filters d-block mb-2" onclick="$('#view-data-filters-form').toggle(); return false;">Toggle Filter</a>
            <form action="" id="view-data-filters-form" onsubmit="return false;">

                @if(count($userClauseArgs))
                    <div class="px-2 pt-2 border bg-light">
                        <div class="font-weight-bold text-secondary mb-2">Override Arguments</div>
                        @foreach($userClauseArgs as $userClauseArg)
                            <div class="d-flex align-items-baseline mb-2">
                                <label class="m-0 width-70px">{{$userClauseArg}}</label>
                                <input type="text" class="form-control form-control-sm min-width-unset flex-grow-1" name="st_{{$userClauseArg}}" value="{{request()->input('st_' . $userClauseArg)}}">
                            </div>
                        @endforeach
                    </div>
                @endif

                <input type="hidden" name="sort_by" value="{{request()->input('sort_by')}}">
                <input type="hidden" name="sort_dir" value="{{request()->input('sort_dir')}}">
                <table class="table table-sm table-striped table-bordered mb-0 mt-2 bg-aliceblue">
                    <thead>
                        <?php $lastColumnRendered = false; ?>
                        @foreach($columns as $column)
                            <tr>
                                <th class="border-bottom-0 width-150px font-weight-normal align-middle text-nowrap {{request()->input($column['as'] . '_op') ? 'font-weight-bold text-info' : ''}}">{{$column['label']}}</th>

                                <th class="border-bottom-0 width-150px p-0 align-middle">
                                    <select class="form-control form-control-sm min-width-unset border-0 rounded-0 shadow-none" name="{{$column['as']}}_op">
                                        <option value="">-- select --</option>
                                        <?php
                                        switch($column['type']) {
                                            case 'integer':
                                            case 'bigint':
                                            case 'decimal':
                                                ?>
                                                <option {{request()->input($column['as'] . '_op') === '=' ? 'selected' : ''}} value="=">Equal</option>
                                                <option {{request()->input($column['as'] . '_op') === '!=' ? 'selected' : ''}} value="!=">Not equal</option>
                                                <option {{request()->input($column['as'] . '_op') === '<' ? 'selected' : ''}} value="<">Lesser than</option>
                                                <option {{request()->input($column['as'] . '_op') === '<=' ? 'selected' : ''}} value="<=">Lesser than or equal to</option>
                                                <option {{request()->input($column['as'] . '_op') === '>' ? 'selected' : ''}} value=">">Greater than</option>
                                                <option {{request()->input($column['as'] . '_op') === '>=' ? 'selected' : ''}} value=">=">Greater than or equal to</option>
                                                <?php
                                                break;
                                            case 'string':
                                            case 'text':
                                            case 'varchar':
                                                ?>
                                                <option {{request()->input($column['as'] . '_op') === '=' ? 'selected' : ''}} value="=">Is</option>
                                                <option {{request()->input($column['as'] . '_op') === '!=' ? 'selected' : ''}} value="!=">Is Not</option>
                                                <option {{request()->input($column['as'] . '_op') === 'ILIKE' ? 'selected' : ''}} value="ILIKE">Contains</option>
                                                <option {{request()->input($column['as'] . '_op') === 'NOT ILIKE' ? 'selected' : ''}} value="NOT ILIKE">Does not contain</option>
                                                <?php
                                                break;
                                            case 'bool':
                                            case 'boolean':
                                                ?>
                                                <option {{request()->input($column['as'] . '_op') === '=' ? 'selected' : ''}} value="=">Is</option>
                                                <?php
                                                break;
                                            case 'date':
                                            case 'datetime':
                                                ?>
                                                <option {{request()->input($column['as'] . '_op') === '=' ? 'selected' : ''}} value="=">Start/End</option>
                                                <?php
                                                break;
                                        }
                                        ?>
                                    </select>
                                </th>

                                <th class="border-bottom-0 p-0 align-middle">
                                    <div class="d-flex align-items-center">
                                    <?php
                                    switch($column['type']) {
                                        case 'integer':
                                        case 'bigint':
                                        case 'decimal':
                                            ?>
                                            <input type="number" name="{{$column['as']}}_value" value="{{request()->input($column['as'] . '_value')}}" class="min-width-unset shadow-none form-control form-control-sm border-0 rounded-0">
                                            <?php
                                            break;
                                        case 'string':
                                        case 'text':
                                        case 'varchar':
                                            ?>
                                            <input type="text" name="{{$column['as']}}_value" value="{{request()->input($column['as'] . '_value')}}" class="min-width-unset shadow-none form-control form-control-sm border-0 rounded-0">
                                            <?php
                                            break;
                                        case 'bool':
                                        case 'boolean':
                                            ?>
                                            <select class="form-control form-control-sm min-width-unset shadow-none border-0 rounded-0" name="{{$column['as']}}_value">
                                                <option {{request()->input($column['as'] . '_value') === 'TRUE' ? 'selected' : ''}} value="TRUE">True</option>
                                                <option {{request()->input($column['as'] . '_value') === 'FALSE' ? 'selected' : ''}} value="FALSE">False</option>
                                            </select>
                                            <?php
                                            break;
                                        case 'date':
                                        case 'datetime':
                                            ?>
                                            <input type="date" name="{{$column['as']}}_value_start" value="{{request()->input($column['as'] . '_value_start')}}" class="min-width-unset shadow-none width-200px form-control form-control-sm border-0 rounded-0" placeholder="Start">
                                            <input type="date" name="{{$column['as']}}_value_end" value="{{request()->input($column['as'] . '_value_end')}}" class="min-width-unset shadow-none width-200px form-control form-control-sm border-top-0 border-bottom-0 rounded-0" placeholder="End">
                                            <?php
                                            break;
                                    }
                                    ?>
                                    </div>
                                </th>

                                @if(!$lastColumnRendered)
                                    <th class="border-bottom-0 width-200px align-top bg-white" rowspan="{{count($columns)}}">
                                        <div class="d-flex align-items-center">
                                            <button class="btn btn-sm btn-primary mr-2"
                                                    onclick="return refreshDynamicStagPopup('{{route('practice-management.statTreeLines.view-data', compact('line'))}}?proUid={{request()->input('proUid')}}&' + $('#view-data-filters-form').serialize())">
                                                Filter
                                            </button>
                                            <button class="btn btn-sm btn-default border"
                                                    onclick="return refreshDynamicStagPopup('{{route('practice-management.statTreeLines.view-data', compact('line'))}}?proUid={{request()->input('proUid')}}')">
                                                Reset
                                            </button>
                                        </div>
                                    </th>
                                    <?php $lastColumnRendered = true; ?>
                                @endif
                            </tr>
                        @endforeach
                    </thead>
                </table>
            </form>

            <table class="table table-sm table-striped table-bordered mt-3">
                <thead>
                <tr>
                    @foreach($columns as $column)
                    <th class="border-bottom-0 text-left">
                        @include('app.stat-tree._sort_header', ['route' => route('practice-management.statTreeLines.view-data', compact('line')), 'label' => $column['label'], 'key' => $column['as']])
                    </th>
                    @endforeach
                </tr>
                </thead>
                <tbody>
                @foreach($rows as $row)
                    <tr>
                        @foreach($columns as $column)
                            <td>
                                @if($column['type'] === 'date')
                                    {{friendly_date($row->{$column['as']})}}
                                @elseif($column['type'] === 'datetime')
                                    {{friendly_date_time($row->{$column['as']})}}
                                @else
                                    {{ $row->{$column['as']} }}
                                @endif
                            </td>
                        @endforeach
                    </tr>
                @endforeach
                </tbody>
            </table>
            {!! $paginator->withQueryString()->links() !!}
        @endif
    @endif
</div>
