@php use App\Models\Client;use Carbon\Carbon;use Spatie\Permission\Models\Role; @endphp
@extends('adminlte::page')

@section('title', 'IPRS | Edit a Contract')

@section('content_header')
    <div class="col-9 mx-auto">
        <div class="row mb-2">
            <div class="col-sm-6">
                <ol class="breadcrumb float-sm-left">
                    <li class="breadcrumb-item"><a href="{{ route('dashboard') }}">Dashboard</a></li>
                    <li class="breadcrumb-item"><a href="{{ route('contract.index') }}">Contracts</a></li>
                    <li class="breadcrumb-item active">{{ ucfirst($contract->client->name_fl) }}</li>
                </ol>
            </div>
        </div>
        <div class="row mb-2">
            <div class="col-sm-12">
                <h1 class="m-0">{{ ucfirst($contract->client->name_fl) }}</h1>
            </div>
        </div>
    </div>

@stop

@section('content')
    <!-- {{ $contract }} -->
    <div class="col-9 mx-auto">
        <form method="POST" action="{{ route('contract.update', ['contract'=>$contract->rid]) }}">
            @csrf
            @method('PUT')
            <fieldset>
                <legend>Contract Details</legend>
                <div class="row">
                    <x-adminlte-select name="client_id" label="Select a Client" placeholder="Select a client"
                                       fgroup-class="col-md-6" id="client-select">
                        <option value="">Select a Client</option>
                        @foreach(Client::all() as $client)
                            <option
                                @selected(isset($contract) && $client->rid == $contract->client_rid) value="{{ $client->rid }}">
                                {{ $client->name_fl }}
                            </option>
                        @endforeach
                    </x-adminlte-select>

                    <x-adminlte-input type="date" name="issue_date" label="Contract Issue Date" placeholder="Contract Issue Date"
                                      value="{{ Carbon::parse($contract->d_doc ?? '')->toDateString() ?? old('issue_date') }}"
                                      fgroup-class="col-md-6"/>

                </div>

                <div class="row">

                    <x-adminlte-input type="date" name="begin_date" label="Contract Start Date" placeholder="Contract Start Date"
                                      value="{{ Carbon::parse($contract->d_bgn ?? '')->toDateString() ?? old('begin_date') }}"
                                      fgroup-class="col-md-6"/>

                    <x-adminlte-input type="date" name="expire_date" label="Contract Expiry Date" placeholder="Contract Expiry Date"
                                      value="{{ Carbon::parse($contract->d_end ?? '')->toDateString() ?? old('expire_date') }}"
                                      fgroup-class="col-md-6"/>
                </div>

                <div class="row">
                    <x-adminlte-input name="contractNumber" label="Contract Number" placeholder="Contract Number"
                                      value=""
                                      fgroup-class="col-md-6 col-md-offset-3"/>

                </div>
            </fieldset>

            <fieldset>
                <legend>Company Representative</legend>
                <div class="row">
                    <x-adminlte-input name="surname" label="Surname" placeholder="Surname"
                                      value="{{ $contract->manager_surn ?? old('surname') }}"
                                      fgroup-class="col-md-4"/>

                    <x-adminlte-input name="name" label="First Name" placeholder="First Name"
                                      value="{{ $contract->manager_name ?? old('name') }}"
                                      fgroup-class="col-md-4"/>

                    <x-adminlte-input name="other_name" label="Other Name" placeholder="Other Name"
                                      value="{{ $contract->manager_othnam ?? old('other_name') }}"
                                      fgroup-class="col-md-4"/>

                    <x-adminlte-input name="post" label="Post in Organisation" placeholder="Post in Organisation"
                                      value="{{ $contract->manager_post ?? old('post') }}"
                                      fgroup-class="col-md-4"/>

                    <x-adminlte-input name="email" label="E-Mail Address" placeholder="E-Mail Address"
                                      value="{{ $contract->manager_email ?? old('email') }}"
                                      fgroup-class="col-md-4"/>

                    <x-adminlte-input name="phone" label="Phone Number" placeholder="Phone Number"
                                      value="{{ $contract->manager_phone ?? old('phone') }}"
                                      fgroup-class="col-md-4"/>

                    <x-adminlte-input name="cell" label="Cell Number (Tel)" placeholder="Cell Number (Tel)"
                                      value="{{ $contract->manager_cell ?? old('cell') }}"
                                      fgroup-class="col-md-4"/>
                </div>
            </fieldset>

            <fieldset>
                <legend>Contract Access</legend>
                <div class="row">
                    <div class="col">
                        <p class="font-weight-bold">Access type:</p>
                        <div class="form-check">
                            <input class="form-check-input" @checked(isset($contract) && ($contract->posted_by_internet == 1 || old("internet") == "on")) type="checkbox" name="internet" id="flexCheckDefault1">
                            <label class="form-check-label" for="flexCheckDefault1">
                                Internet
                            </label>
                        </div>
                        <div class="form-check">
                            <input class="form-check-input" @checked(isset($contract) && ($contract->posted_by_service == 1 || old("api") == "on")) type="checkbox" name="api" id="flexCheckDefault4">
                            <label class="form-check-label" for="flexCheckDefault4">
                                Web-Service (API)
                            </label>
                        </div>
                    </div>

                    <div class="col">
                        <p class="font-weight-bold">Payment type:</p>
                        <div class="form-check">
                            <input class="form-check-input" @checked(isset($contract) && ($contract->payment_type == 1 || old('payment_type') == 'Post-paid')) type="radio" name="payment_type" value="Post-paid" id="flexRadioDefault1">
                            <label class="form-check-label" for="flexRadioDefault1">
                                Post-paid
                            </label>
                        </div>
                        <div class="form-check">
                            <input class="form-check-input" @checked(isset($contract) && ($contract->payment_type == 2 || old('payment_type') == 'Pre-paid')) type="radio" name="payment_type" value="Pre-paid" id="flexRadioDefault2">
                            <label class="form-check-label" for="flexRadioDefault2">
                                Pre-paid
                            </label>
                        </div>
                        <div class="form-check">
                            <input class="form-check-input" @checked(isset($contract) && ($contract->payment_type == 3 || old('payment_type') == 'Free')) type="radio" name="payment_type" value="Free" id="flexRadioDefault3">
                            <label class="form-check-label" for="flexRadioDefault3">
                                Free
                            </label>
                        </div>
                    </div>

                </div>
                <!-- API Access Key Input (Initially Hidden) -->
                <div id="apiAccessKeyContainer" class="mt-2" style="display: none;">
                    <label for="apiAccessKey">API Tokens :</label>
                    <input type="number" class="form-control" name="api_tokens" id="apiAccessKey" placeholder="Number of tokens">
                </div>

                <x-adminlte-select id="status" name="in_use" label="Contract Status"
                                   placeholder="Select status">
                    <option value="">Select Status</option>
                    <option @selected(isset($contract) && $contract->in_use == "1") value="1">
                        Active (1)
                    </option>
                    <option @selected(isset($contract) && $contract->in_use == "0") value="0">
                        Inactive (0)
                    </option>
                </x-adminlte-select>
            </fieldset>

            <fieldset>
                <legend>Contract Access</legend>
                {{--<p class="font-weight-bold">Search Options</p>--}}
                @php
                    $roleName = isset($contract) ? 'CID-' . $contract->rid : null;
                    $role = Role::where('name', $roleName)->first();
                @endphp

                <div class="accordion" id="accordionExample">
                    @foreach($contractPermissions->where('section', 'page') as $gp)
                        @php
                            $headerGroup = match ($gp->group) {
                                "basic" => "Basic Search",
                                "advance" => "Advanced Search (Search in Docums)", // add more for phonetic
                            }
                        @endphp
                        <div class="card">
                            <div class="card-header" id="heading-{{$loop->index}}">
                                <h2 class="mb-0">
                                    <button class="btn btn-link btn-block text-left" type="button" data-toggle="collapse"
                                            data-target="#collapse-{{$loop->index }}" aria-expanded="true"
                                            aria-controls="collapse-{{$loop->index }}">
                                        {{ $headerGroup }}
                                    </button>
                                </h2>
                            </div>

                            <div id="collapse-{{$loop->index }}" class="collapse"
                                 aria-labelledby="heading-{{$loop->index}}"
                                 data-parent="#accordionExample">

                                <div class="card-body">
                                    <div class="icheck-primary" title="{{ $gp->name }}">
                                        <input type="checkbox" name="permissions[]" value="{{ $gp->name }}"
                                               @checked($role != null && $role->hasPermissionTo($gp->name))
                                               id="page-permissions-{{ $loop->index }}">

                                        <label for="page-permissions-{{ $loop->index }}" class="font-weight-normal">
                                            {{ $gp->name }}
                                        </label>
                                    </div>
                                    <hr>
                                    <p class="font-weight-bold">Data parameters</p>
                                    <div class="row">
                                        @foreach($contractPermissions->where('section', 'document')->where('group', $gp->group) as $doc)
                                            @php
                                                $document = match ($doc->name) {
                                                    "Basic Search ID card","Advance Search ID card" => "ID",
                                                    "Basic Search Passport card", "Advance Search Passport card" => "Passport",
                                                    "Basic Search Birth Certificate", "Advance Search Birth Certificate" => "Birth",
                                                    "Advance Search Refugees and Alien card" => "Refugee",
                                                    "Advance Search Death certificate" => "Death", // add more for phonetic
                                                }
                                            @endphp
                                            <div class="col-3 mr-3 border-right border-bottom mb-4">
                                                <div class="icheck-primary" title="{{ $doc->name }}">
                                                    <input type="checkbox"
                                                           @checked($role != null && $role->hasPermissionTo($doc->name)) name="permissions[]"
                                                           value="{{ $doc->name }}"
                                                           id="document-permissions-{{ $loop->index }}-{{ $doc->name }}">

                                                    <label for="document-permissions-{{ $loop->index }}-{{ $doc->name }}"
                                                           class="font-weight-normal">
                                                        {{ $doc->name }}
                                                    </label>
                                                </div>

                                                <p class="font-weight-bold">Search Input</p>
                                                @foreach($contractPermissions->where('section', 'input')->where('group', $gp->group)->where('document', $document) as $in)
                                                    <div class="icheck-primary" title="{{ $in->name }}">
                                                        <input type="checkbox"
                                                               @checked($role != null && $role->hasPermissionTo($in->name)) name="permissions[]"
                                                               value="{{ $in->name }}"
                                                               id="in-permissions-{{ $loop->index }}-{{ $doc->name }}">

                                                        <label for="in-permissions-{{ $loop->index }}-{{ $doc->name }}"
                                                               class="font-weight-normal">
                                                            {{ $in->name }}
                                                        </label>
                                                    </div>
                                                @endforeach
                                                <p class="font-weight-bold">Results Display</p>
                                                {{--<div class="icheck-primary" title="Personal details">
                                                    <input type="checkbox" checked disabled id="out-permissions">

                                                    <label for="out-permissions" class="font-weight-normal">
                                                        Personal details i.e., Pin, Surname, First Name, Other Name, ID Number, Gender, Date
                                                        of Birth and
                                                        Citizenship.
                                                    </label>
                                                </div>--}}
                                                {{--<p class="small text-danger">NB: This is already pre-selected for display.</p>--}}
                                                @foreach($contractPermissions->where('section', 'output')->where('group', $gp->group)->where('document', $document) as $out)
                                                    <div class="icheck-primary" title="{{ $out->name }}">
                                                        <input type="checkbox"
                                                               @checked($role != null && $role->hasPermissionTo($out->name)) name="permissions[]"
                                                               value="{{ $out->name }}"
                                                               id="out-permissions-{{ $loop->index }}-{{ $doc->name }}">

                                                        <label for="out-permissions-{{ $loop->index }}-{{ $doc->name }}" class="font-weight-normal">
                                                            {{ $out->name }}
                                                        </label>
                                                    </div>
                                                @endforeach
                                            </div>
                                        @endforeach
                                    </div>
                                </div>
                            </div>
                        </div>
                    @endforeach
                </div>
            </fieldset>
            <button type="submit" class="btn btn-success">Submit</button>
        </form>
    </div>
@stop

@section('css')
    <link href="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/css/select2.min.css" rel="stylesheet"/>
    <style>
        fieldset {
            background-color: white;
            padding-block-start: 0.35em;
            padding-inline-start: 1.25em;
            padding-inline-end: 1.25em;
            padding-block-end: 0.625em;
            margin: 0 0 1em 0;
            border: 1px groove rgb(192, 192, 192);
            border-radius: 5px;
        }

        legend {
            background-color: #356eac;
            color: white;
            width: auto;
            font-size: medium;
            padding: 5px 10px;
            border-radius: 5px;
        }

        button {
            background-color: #14b04c;
        }

        .btn-primary {
            background-color: #356eac;
        }

        .select2-container .select2-selection--single {
            height: unset;
        }

        .select2-container--default .select2-selection--single .select2-selection__arrow {
            height: 41px;
        }

        .btn-success {
            margin-bottom: 25px;
        }

        h3 {
            margin-top: 1.5rem !important;
        }

    </style>
@stop

@section('js')
    <script src="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/js/select2.min.js"></script>

    <script>
        $(document).ready(function () {
            $('#client-select').select2({
                placeholder: 'Select a client',
                allowClear: true
            });

            $('#flexCheckDefault4').change(function () {
                if ($(this).is(':checked')) {
                    $('#apiAccessKeyContainer').fadeIn();
                } else {
                    $('#apiAccessKeyContainer').fadeOut();
                }
            });

        });
    </script>

@stop

@section('plugins.iCheck', true)
