@extends('adminlte::page')

@section('title', 'IPRS | Users')

@section('content_header')
    <div class="col-9 mx-auto d-flex justify-content-between align-items-center">
        <div class="row">
            <div class="col-sm-9">
                <ol class="breadcrumb">
                    <li class="breadcrumb-item"><a href="{{ route('dashboard') }}">Dashboard</a></li>
                    <li class="breadcrumb-item active">Organization Users</li>
                </ol>
            </div>
            <div class="col-sm-12">
                <h1 class="m-0">Organization Users</h1>
            </div>
        </div>
        <div>
            <div class="d-flex justify-content-end align-content-end">
                <a href="{{ route('sub-account.create') }}" class="btn btn-primary">New User</a>
            </div>
        </div>
    </div>
@stop

@section('plugins.Datatables', true)

@section('content')
    {{--<div class="col-9 mx-auto">
        --}}{{-- Setup data for datatables --}}{{--
        @php
            $headings = [
                'ID',
                'Name',
                'E-Mail',
                'Client',
                'Contract (RID)',
                'Role',
                ['label' => 'Status'],
                ['label' => 'Actions', 'no-export' => true, 'width' => 5],
            ];
        @endphp

        <x-adminlte-card>
            --}}{{-- Minimal example / fill data using the component slot --}}{{--
            <x-adminlte-datatable id="clients-table" :heads="$headings" striped hoverable bordered>
                @foreach($users as $user)
                    <tr>
                        <td>{{ $user->id }}</td>
                        <td>{{ ucfirst($user->name) }}</td>
                        <td>{{ $user->email }}</td>
                        <td class="text-primary" >
                            {{ $user->contract->client->name_fl }}
                        </td>
                        <td>
                            <span class="badge badge-light">{{ $user->contract_rid }}</span>
                        </td>
                        <td>
                            @foreach($roles as $role)
                                @if($user->hasRole($role->name))
                                    <span class="badge rounded-pill bg-info">{{ $role->name }}</span>
                                @endif
                            @endforeach
                        </td>
                        <td>
                            @if($user->status_l == 1)
                                <div class="badge badge-success">Active</div>
                            @else
                                <div class="badge badge-secondary">Locked</div>
                            @endif
                        </td>
                        <td>
                            <nobr>
                                <a href="{{ route('sub-account.show', $user->id) }}"
                                class="btn btn-xs btn-default text-teal mx-1 shadow" title="Details">
                                    <i class="fa fa-lg fa-fw fa-eye"></i>
                                </a>
                                <a href="{{ route('sub-account.edit', $user->id) }}"
                                class="btn btn-xs btn-default text-primary mx-1 shadow" title="Edit">
                                    <i class="fa fa-lg fa-fw fa-pen"></i>
                                </a>

                                <a href="#" class="btn btn-xs btn-default text-danger mx-1 shadow" title="Delete"
                                onclick="function submit() {
                                    if (confirm('Are you sure you want to delete?')) {
                                    document.getElementById('form{{$user->id}}').submit();
                                    }
                                    }
                                    submit()"><i class="fa fa-lg fa-fw fa-trash"></i></a>
                                <form
                                    id="form{{$user->id}}"
                                    action="{{route('sub-account.destroy',$user->id)}}"
                                    method="post">
                                    @csrf
                                    {{method_field('DELETE')}}
                                </form>

                            </nobr>

                        </td>
                    </tr>
                @endforeach
            </x-adminlte-datatable>
        </x-adminlte-card>
    </div>--}}
    <div class="col-9 mx-auto">
        <div class="accordion" id="contract-accordion">
            @if($contracts->count() > 0)
                {{-- Setup data for datatables --}}
                @php
                    $headings = [
                        'ID',
                        'Name',
                        'E-Mail',
                        'Role',
                        ['label' => 'Status'],
                        ['label' => 'Actions', 'no-export' => true, 'width' => 2],
                    ];
                @endphp
                @foreach($contracts as $c)
                    <div class="card">
                        <div class="card-header bg-light" id="heading-{{ $c->rid }}">
                            <div class="d-flex justify-content-between align-content-between">
                                <button class="btn btn-link text-left text-lg text-success" type="button"
                                        data-toggle="collapse" data-target="#collapse-{{ $c->rid }}"
                                        aria-expanded="true" aria-controls="collapse-{{ $c->rid }}">
                                    Contract No: #{{ $c->rid }}
                                </button>
                                {{--<a href="{{ route('contract.show', $c->rid) }}" class="btn btn-primary text-lg text-right">
                                    View <i class="fas fa-fw fa-file-alt"></i>
                                </a>--}}
                            </div>
                        </div>

                        <div id="collapse-{{ $c->rid }}" class="collapse @if($loop->first) show @endif"
                             aria-labelledby="heading-{{ $c->rid }}" data-parent="#contract-accordion">
                            <div class="card-body">

                                <x-adminlte-card>
                                    {{-- Minimal example / fill data using the component slot --}}
                                    <x-adminlte-datatable id="users-{{$loop->index}}" :heads="$headings" striped
                                                          hoverable bordered>
                                        @if($c->users->count())
                                            @foreach($c->users as $user)
                                                <tr>
                                                    <td>{{ $user->id }}</td>
                                                    <td>{{ ucfirst($user->name) }}</td>
                                                    <td>{{ $user->email }}</td>
                                                    <td>
                                                        @foreach($user->getRoleNames() as $role)
                                                            <span class="badge rounded-pill bg-info">{{ $role }}</span>
                                                        @endforeach
                                                    </td>
                                                    <td>
                                                        @if($user->status_l == 1)
                                                            <div class="badge badge-success">Active</div>
                                                        @else
                                                            <div class="badge badge-secondary">Locked</div>
                                                        @endif
                                                    </td>
                                                    <td>
                                                        <nobr>
                                                            {{--<a href="{{ route('sub-account.show', $user->id) }}"
                                                               class="btn btn-xs btn-default text-teal mx-1 shadow" title="Details">
                                                                <i class="fa fa-lg fa-fw fa-eye"></i>
                                                            </a>--}}
                                                            <a href="{{ route('sub-account.edit', $user->id) }}"
                                                               class="btn btn-xs btn-default text-primary mx-1 shadow"
                                                               title="Edit">
                                                                <i class="fa fa-lg fa-fw fa-pen"></i>
                                                            </a>

                                                            <a href="#"
                                                               class="btn btn-xs btn-default text-danger mx-1 shadow"
                                                               title="Delete"
                                                               onclick="function submit() {
                                                                if (confirm('Are you sure you want to delete?')) {
                                                                document.getElementById('form{{$user->id}}').submit();
                                                                }
                                                                }
                                                                submit()"><i class="fa fa-lg fa-fw fa-trash"></i></a>
                                                            <form
                                                                id="form{{$user->id}}"
                                                                action="{{route('sub-account.destroy',$user->id)}}"
                                                                method="post">
                                                                @csrf
                                                                {{method_field('DELETE')}}
                                                            </form>

                                                        </nobr>

                                                    </td>
                                                </tr>
                                            @endforeach
                                        @else
                                            <tr>
                                                <td colspan="6" class="text-center">
                                                    No users found under this contract!
                                                </td>
                                            </tr>
                                        @endif
                                    </x-adminlte-datatable>
                                </x-adminlte-card>
                            </div>
                        </div>
                    </div>
                @endforeach
            @else
                <div class="alert alert-secondary">No contracts available for this client.</div>
            @endif
        </div>
    </div>
@stop
