@php use Carbon\Carbon;use Spatie\Permission\Models\Role; @endphp

<div>
    <fieldset>
        <legend>Contract Details</legend>
        <div class="row">
            <x-adminlte-select2 name="client_id" label="Select a Client" placeholder="Select a client"
                                fgroup-class="col-md-6" id="client-select" readonly="true">
                <option value="">Select a Client</option>
                <option selected value="{{ $client->rid ?? '' }}">
                    {{ $client->name_fl ?? '' }}
                </option>
            </x-adminlte-select2>

            <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="{{ $client->boss_surn ?? $contract->manager_surn ?? old('surname') }}"
                              fgroup-class="col-md-4"/>

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

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

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

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

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

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

    <fieldset>
        <legend>Contract Access</legend>
        <div class="row">
            <div class="col-md-6">
                <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-md-6">
                <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>
        <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 }}
                                <i class="fas fa-chevron-down float-right"></i>
                            </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-md-5 mr-3 border-right border-bottom">
                                        <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>
</div>

<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;
    }

    .content-header .breadcrumb {
        padding-left: 10px;
    }

</style>

@section('plugins.iCheck', true)
