<?php
use \A7kz\Platform\Modules\Platform\Acl\Facades\Acl;
use A7kz\Platform\Models\UniModel;
?>
@if(Auth::check() && Acl::company_id())
        <?php
        $orgs = [];
        $isAdmin = Acl::isHasRole('admin');

        if ($isAdmin) {
            $companies = UniModel::model('core_companies')->get();
        } else {
            $companies = Acl::myCompanies();
        }

        $active = Acl::activeCompany();

        ?>
        <div class="card" id="companyListDiv" @if(!\Illuminate\Support\Facades\Request::has('filter')) style="display: none; min-width: 505px" @endif >
            <div class="card-header-custom row d-flex justify-content-start align-items-center" style="column-gap: 0.5rem">
                <div class="col-3 col-lg-1"><button class="btn btn-light filter-trigger">Фильтр</button></div><div class="card-title col-8 col-lg-10 d-flex justify-content-start align-items-center px-0">@lang("Мои организации") <button type="button" class="companylist-close" onclick="$('#companyListDiv').hide()"><i class="bi bi-x"></i></button></div>
            </div>
            <div class="card" id="filter_block" style="border: none; box-shadow: none; display: none">
                <div class="card-body">
                    <form method="get" id="company_list_filter">
                        <div class="row d-flex flex-row justify-content-between align-items-center" style="font-size: 0.8rem">
                                <div class="form-group col-3">
                                    <label>БИН/ИИН</label>
                                    <input type="text" class="form-control" name="filter[bin]" id="biniin" value="{{\Illuminate\Support\Facades\Request::get('filter')['bin'] ?? ''}}">
                                </div>
                                <div class="form-group col-3">
                                    <label>Код ГУ</label>
                                    <input type="text" class="form-control" name="filter[code_gu]" id="code_gu" value="{{\Illuminate\Support\Facades\Request::get('filter')['code_gu'] ?? ''}}">
                                </div>
                                <div class="form-group col-6">
                                    <label>Наименование</label>
                                    <input type="text" class="form-control" name="filter[name]" id="org_name" value="{{\Illuminate\Support\Facades\Request::get('filter')['name'] ?? ''}}">
                                </div>
                                <div class="form-group">
                                    <button type="submit" class="btn btn-success" id="filter_submit"><i class="bi bi-search"></i>Искать</button>
                                    <button type="submit" class="btn btn-danger" id="filter_reset" onclick="resetForm()"><i class="bi bi-x-circle"></i>Сброс</button>
                                </div>
                            </div>
                    </form>
                </div>
            </div>
            <div class="card-body">
                <table class="table table-striped">
                    @foreach($companies as $company)
                        @if((empty($active) || $active->id != $company->id))
                    <tr>
                        <td>{{$company->biniin}}</td>
                        <td>
                            <a href="{{lurl('/app/user.profile/changecompany')."?pk=".$company->id}}"
                               class="btn btn-success">@lang("Выбрать")</a>
                        </td>
                    </tr>
                        @endif
                    @endforeach
                </table>
            </div>
            <div class="card-footer">
                <a href="{{lurl('/register_company')}}" class="btn btn-light">@lang("Добавить новую")</a>
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">@lang("Отмена")</button>
            </div>
        </div>
@else
    <div class="card mb-4 mb-lg-0">
        <div class="card-body p-3">
            <div class="h5">@lang('Организация')</div>
            <div>
                <div class="d-flex mb-2">
                    <a class="btn btn-primary">@lang("Добавить организацию")</a>
                </div>
            </div>
        </div>
    </div>
@endif

<script>
    function resetForm() {
        $('#code_gu').val('');
        $('#biniin').val('');
        $('#org_name').val('');
    }
</script>
