@php use Carbon\Carbon; @endphp
    <!-- CLIENT DASHBOARD -->

@extends('adminlte::page')

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

@section('content_header')

@stop

@section('content')
    <main class="pt-3 mx-2 mx-lg-5">
        <div class="row">
            <div class="col d-flex flex-column flex-sm-row align-items-center justify-content-sm-between">
                <div class="d-flex flex-row align-items-center mb-3 mb-sm-0">
                    <div class="d-flex justify-content-center align-items-center rounded-circle"
                         style="width: 44px; height: 44px; background-color: #F94A7A;">
                        <span class="h4 mb-0 text-white"><b>{{ Auth::user()->name[0] }}</b></span>
                    </div>
                    <div class="d-flex flex-column ml-3">
                        <p class="text-secondary mb-0">Welcome back!</p>
                        <h5>{{ Auth::user()->name }} | {{ Auth::user()->contract->client->name_fl }}</h5>
                    </div>
                </div>
                <div class="d-flex gap-2">
                    <div class="d-flex flex-column">
                        <span class="text-secondary mb-0">Search Token Balance</span>
                        <h5 class="text-left">123,942</h5>
                    </div>
                    <div class="mx-3 border-right border-secondary"></div>
                    <div class="d-flex flex-column">
                        <span class="text-secondary mb-0">Total Searches</span>
                        <h5 class="text-left">23,942</h5>
                    </div>
                </div>
            </div>
        </div>
        <div class="row mt-4">
            <div class="col d-flex flex-wrap justify-content-md-between p-5 bg-white rounded">
                <div class="d-flex">
                    <div class="d-flex justify-content-center align-items-center rounded-circle"
                         style="width: 64px; height: 64px; background-color: #E7FFF2;">
                        <i class="far fa-lg fa-folder-open text-success"></i>
                    </div>
                    <div class="d-flex flex-column ml-3">
                        <span class="text-secondary mb-0">Contracts</span>
                        <span class="text-left mb-0 fs-6"
                              style="font-size: 2rem;"><b>{{ Auth::user()->contract->client->contracts->count() }}</b></span>

                        <div class="d-flex align-items-center">
                            <i class="fas fa-arrow-up text-success"></i>
                            <span class="mb-0 ml-2 text-nowrap">Since 2012</span>
                        </div>
                    </div>
                </div>
                <div class="d-flex">
                    <div class="d-flex justify-content-center align-items-center rounded-circle"
                         style="width: 64px; height: 64px; background-color: #E7FFF2;">
                        <i class="fas fa-lg fa-user-check text-success"></i>
                    </div>
                    <div class="d-flex flex-column ml-3">
                        <span class="text-secondary mb-0">Account Users</span>
                        <span class="text-left mb-0"
                              style="font-size: 2rem;"><b>{{ Auth::user()->contract->users->count() }}</b></span>
                    </div>
                </div>
                <div class="d-flex mt-2 mt-md-0">
                    <div class="d-flex justify-content-center align-items-center rounded-circle"
                         style="width: 64px; height: 64px; background-color: #E7FFF2;">
                        <i class="fas fa-lg fa-desktop text-success"></i>
                    </div>
                    <div class="d-flex flex-column ml-3">
                        <span class="text-secondary mb-0">Active Now</span>
                        <span class="text-left mb-0" style="font-size: 2rem;"><b>1</b></span>
                    </div>
                </div>
            </div>
        </div>
        <div class="row mt-4">
            <div class="col d-flex flex-md-row flex-column">
                <div class="col col-md-7 d-flex flex-column mb-4 mb-md-0 p-3 bg-white rounded">
                    <span class="h4 mb-3">Contracts</span>
                    {{--                    <div class="my-2 border-bottom" style="border-color: gray;"></div>--}}
                    <div class="d-flex flex-column">
                        @php
                            $heads = [
                                'RID',
                                'Issue Date',
                                'Start Date',
                                'End Date',
                                'Tokens Bal.',
                                'Searches',
                                ['label' => 'Actions', 'no-export' => true, 'width' => 5, 'sort' => false],
                            ];

                            $config = [
                                'data' => Auth::user()->contract->client->contracts,
                                'order' => [[1, 'asc']],
                                'columns' => [null, null, null, null, null, null, ['orderable' => false]],
                            ];
                        @endphp

                        <x-adminlte-datatable id="contracts" bordered :heads="$heads" hoverable striped>
                            @foreach($config['data'] as $row)
                                <tr>
                                    <td>{{ $row->rid }}</td>
                                    <td>{{ Carbon::parse($contract->d_doc ?? '')->toDateString() }}</td>
                                    <td>{{ Carbon::parse($row->d_bgn ?? '')->toDateString() }}</td>
                                    <td>{{ Carbon::parse($row->d_end)->toDateString() }}</td>
                                    <td>{{ $row->balance ?? 0 }}</td>
                                    <td>0</td>
                                    <td>
                                        <a href="{{ route('sub-contracts.index') }}">
                                            <button class="btn btn-xs btn-default text-teal mx-1 shadow"
                                                    title="Details">
                                                <i class="fa fa-lg fa-fw fa-eye"></i>
                                            </button>
                                        </a>
                                    </td>
                                </tr>
                            @endforeach
                        </x-adminlte-datatable>
                    </div>
                </div>
                <div class="col d-flex flex-column p-3 bg-white rounded mt-4 mt-md-0 ml-md-4">
                    <span class="h4 mb-3">Billing History</span>
                    {{--                    <div class="my-2 border-bottom" style="border-color: gray;"></div>--}}
                    <div class="d-flex flex-column">
                        @php
                            function status_b(int $i) {
                            return '<span class="badge rounded-pill '. ($i == 1 ? "bg-success" : ($i == 2 ? "bg-warning" : "bg-danger")) .'">'. ($i == 1 ? "Paid" : ($i == 2 ? "Partial" : "Unpaid")) .'</span>';
                        }

                        $heads = [
                            'ID',
                            'Billing Period',
                            'Status',
                            ['label' => 'Actions', 'no-export' => true, 'width' => 5, 'sort' => false],
                        ];

                        $config = [
                            'data' => [
                                [228, '14-11-2021 - 14-11-2021', status_b(1)],
                                [22, '26-08-2022 - 26-08-2022', status_b(1)],
                                [754, '17-06-2019 - 17-06-2019', status_b(2)],
                                [650, '10-12-2016 - 10-12-2016', status_b(1)],
                                [541, '09-08-2019 - 09-08-2019', status_b(0)],
                            ],
                            'order' => [[1, 'asc']],
                            'columns' => [null, null, null, ['orderable' => false]],
                        ];
                        @endphp

                        <x-adminlte-datatable id="bills" bordered :heads="$heads" hoverable striped>
                            @foreach($config['data'] as $row)
                                <tr>
                                    @foreach($row as $cell)
                                        <td>{!! $cell !!}</td>
                                    @endforeach

                                    <td>
                                        <a href="#">
                                            <button class="btn btn-xs btn-default text-teal mx-1 shadow"
                                                    title="Details">
                                                <i class="fa fa-lg fa-fw fa-eye"></i>
                                            </button>
                                        </a>
                                    </td>
                                </tr>
                            @endforeach
                        </x-adminlte-datatable>
                    </div>
                </div>
            </div>
        </div>


        <div class="row mt-4">
            <div class="col d-flex">
                <div class="container-fluid d-flex flex-column p-3 bg-white rounded">
                    <div class="mb-3">
                        <span class="h4 mb-0">Account Users</span>
                        {{--<span class="d-block text-success mb-3">Contract #1156365622</span>--}}
                    </div>

                    <div class="accordion" id="contract-accordion">
                        @if(Auth::user()->contract->client->contracts->count() > 0)
                            @foreach(Auth::user()->contract->client->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">

                                            {{-- Setup data for datatables --}}
                                            @php
                                                $headings = [
                                                    'ID',
                                                    'Name',
                                                    'E-Mail',
                                                    'Role',
                                                    ['label' => 'Status'],
                                                    ['label' => 'Actions', 'no-export' => true, 'width' => 2],
                                                ];
                                            @endphp

                                            <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.index') }}"
                                                                           class="btn btn-xs btn-default text-teal mx-1 shadow" title="Details">
                                                                            <i class="fa fa-lg fa-fw fa-eye"></i>
                                                                        </a>
                                                                    </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>
            </div>
        </div>
    </main>
@stop

@section('css')

@stop

@section('js')

@stop
@section('plugins.Datatables', true)
