<?php

use Cake\Utility\Inflector;

/**
 * @var \{{ namespace }}\View\AppView $this
 * @var {{ modelObject.getEntityClass() }}|\Cake\Collection\CollectionInterface ${{ singularVar }}
 */
{% if 'add' not in action %}
     $this->assign('title', __('Edit {{ singularHumanName }}');
{% endif %}
{% if 'edit' not in action %}
    $this->assign('title', 'New {{ singularHumanName }}');
{% endif %}

$this->Breadcrumbs->add('{{ pluralHumanName }}', ['action' => 'index']);

{% if 'add' not in action %}
    $this->PageActions->group('top-right')->delete(${{ singularVar }});
{% endif %}

?>
{% set fields = Bake.filterFields(fields, schema, modelObject) %}

<?= $this->Form->create(${{ singularVar }}, ['floating' => true]) ?>
<div class="card">
    <div class="card-header">
        <h2 class="card-title h4 m-0">{{ singularHumanName }} Details</h2>
    </div>

    <div class="card-body">
        {% for field in fields %}
            {%- if keyFields[field] %}
                {%- set fieldData = Bake.columnData(field, schema) %}
                {%- if fieldData.null %}
                    <?= $this->Form->control('{{ field }}', ['options' => ${{ keyFields[field] }}, 'empty' => true]); ?>
                    {{- "\n" }}
                {%- else %}
                    <?= $this->Form->control('{{ field }}', ['options' => ${{ keyFields[field] }}]); ?>
                    {{- "\n" }}
                {%- endif %}
            {%- elseif field not in ['created', 'modified', 'updated'] %}
                {%- set fieldData = Bake.columnData(field, schema) %}
                {%- if fieldData.type in ['date', 'datetime', 'time'] and fieldData.null %}
                    <?= $this->Form->control('{{ field }}', ['empty' => true]); ?>
                    {{- "\n" }}
                {%- else %}
                    <?= $this->Form->control('{{ field }}'); ?>
                    {{- "\n" }}
                {%- endif %}
            {%- endif %}
        {%- endfor %}

        {%- if associations.BelongsToMany %}
            {%- for assocName, assocData in associations.BelongsToMany %}
                <?= $this->Form->control('{{ assocData.property }}._ids', ['options' => ${{ assocData.variable }}]); ?>
                {{- "\n" }}
            {%- endfor %}
        {% endif %}

    </div>
</div>
{%- if associations.HasMany is defined %}
<div class="card">

        {% for assocName, assocData in associations.HasMany %}

            <?php $row = function($index) { ?>
            <li
                    class="list-group-item"
                    data-has-many-item
            >
                <div class="row">
                    <div class="col">
                        {% for field in assocData.fields %}
                            {% if field != assocData.foreignKey %}
                                <?= $this->Form->control("{{ assocData.property }}.$index.{{ field }}"); ?>
                            {% endif %}
                        {% endfor %}
                    </div>
                    <div class="col-auto">
                        <button
                                type="button"
                                data-has-many-remove
                                class="btn btn-outline-danger"
                                onclick="this.closest('[data-has-many-item]').remove()"
                                data-bs-toggle="tooltip"
                                title="Remove <?= Inflector::humanize(Inflector::singularize('{{ assocData.alias }}')) ?>"
                        >
                            <i class="fa fa-minus"></i>
                        </button>
                    </div>
                </div>
            </li>
            <?php } ?>

            <div class="card-header">
                <h3 class="card-title"><?= Inflector::humanize('{{ assocData.alias }}') ?></h3>
            </div>

            <ul
                    class="list-group list-group-flush"
                    data-has-many
            >
                <template>
                    <?= $row('__NEW__') ?>
                </template>
                <?php if (!empty(${{ singularVar }}->{{ assocData.property }})): ?>
                <?php foreach (${{ singularVar }}->{{ assocData.property }} as $index => $entity): ?>
                <?= $row($index); ?>
                <?php endforeach ?>
                <?php endif ?>
                <li class="list-group-item text-end">
                    <button
                            type="button"
                            class="btn btn-outline-success"
                            data-has-many-add
                            data-bs-toggle="tooltip"
                            title="New <?= Inflector::humanize(Inflector::singularize('{{ assocData.alias }}')) ?>"
                    >
                        <i class="fa fa-plus"></i>
                    </button>
                </li>
            </ul>

        {% endfor %}
</div>
{%- endif %}

<?php
	$this->PageActions
->group('bottom-right')->submit()
->group('bottom-left')->cancel();
?>
<?= $this->Form->end() ?>

<?php

    $this->PageActions->appendLink(
'Cancel',
['action' => 'index'],
['class' => 'btn btn-secondary'],
'left'
);
?>
