{% if customFieldDefinitions is not empty %}
    <div class="panel panel-default">
        <div class="panel-heading">
            <h3 class="panel-title">Deal Fields</h3>
        </div>
        <div class="panel-body">
            <div class="row">
                {% for field in customFieldDefinitions %}
                    {% set alias = field.getAlias() %}
                    {% set value = customFieldValues[alias] ?? null %}
                    {% set error = customFieldErrors[alias] ?? null %}
                    {% set isWide = field.getFieldType() == 'textarea' %}
                    <div class="{{ isWide ? 'col-md-12' : 'col-md-6' }}">
                        <div class="form-group{{ error ? ' has-error' : '' }}">
                            <label class="control-label" for="dealflow_custom_field_{{ alias }}">
                                {{ field.getLabel() }}
                                {% if field.isRequired() %}
                                    <span class="text-danger">*</span>
                                {% endif %}
                            </label>

                            {% if field.getFieldType() == 'textarea' %}
                                <textarea
                                    class="form-control"
                                    id="dealflow_custom_field_{{ alias }}"
                                    name="dealflow_custom_fields[{{ alias }}]"
                                    rows="4"
                                >{{ value is not null ? value : '' }}</textarea>
                            {% elseif field.getFieldType() == 'number' %}
                                <input
                                    class="form-control"
                                    id="dealflow_custom_field_{{ alias }}"
                                    type="number"
                                    step="any"
                                    name="dealflow_custom_fields[{{ alias }}]"
                                    value="{{ value is not null ? value : '' }}"
                                >
                            {% elseif field.getFieldType() == 'boolean' %}
                                <input type="hidden" name="dealflow_custom_fields[{{ alias }}]" value="0">
                                <div class="checkbox">
                                    <label>
                                        <input
                                            id="dealflow_custom_field_{{ alias }}"
                                            type="checkbox"
                                            name="dealflow_custom_fields[{{ alias }}]"
                                            value="1"
                                            {% if value %}checked{% endif %}
                                        >
                                        Yes
                                    </label>
                                </div>
                            {% elseif field.getFieldType() == 'select' %}
                                <select class="form-control" id="dealflow_custom_field_{{ alias }}" name="dealflow_custom_fields[{{ alias }}]">
                                    <option value="">Select {{ field.getLabel() }}</option>
                                    {% for option in field.getOptions() %}
                                        <option value="{{ option }}" {% if value is same as(option) %}selected{% endif %}>{{ option }}</option>
                                    {% endfor %}
                                </select>
                            {% elseif field.getFieldType() == 'date' %}
                                <input
                                    class="form-control"
                                    id="dealflow_custom_field_{{ alias }}"
                                    type="date"
                                    name="dealflow_custom_fields[{{ alias }}]"
                                    value="{{ value is not null ? value : '' }}"
                                >
                            {% else %}
                                <input
                                    class="form-control"
                                    id="dealflow_custom_field_{{ alias }}"
                                    type="text"
                                    name="dealflow_custom_fields[{{ alias }}]"
                                    value="{{ value is not null ? value : '' }}"
                                >
                            {% endif %}

                            {% if error %}
                                <span class="help-block">{{ error }}</span>
                            {% elseif field.supportsOptions() %}
                                <span class="help-block">Available options: {{ field.getOptions()|join(', ') }}</span>
                            {% endif %}
                        </div>
                    </div>
                {% endfor %}
            </div>
        </div>
    </div>
{% endif %}
