<?php

if (!defined('ABSPATH')) {
    exit;
}

/*----------------------------------------
メールフォーム：フォーム項目メタボックス登録
----------------------------------------*/
function immf_add_contact_form_fields_meta_box() {

    add_meta_box(
        'im_mail_form_fields',
        __(
            'Form Builder',
            'im-mail-form'
        ),
        'immf_fields_meta_box_callback',
        'im_mail_form',
        'normal',
        'high'
    );

}

add_action(
    'add_meta_boxes',
    'immf_add_contact_form_fields_meta_box'
);


/*----------------------------------------
メールフォーム：フォーム項目メタボックス表示
----------------------------------------*/
function immf_fields_meta_box_callback($post) {

    wp_nonce_field(
        'immf_save_contact_form_fields',
        'im_mail_form_fields_nonce'
    );

    $fields = get_post_meta(
        $post->ID,
        'immf_fields',
        true
    );

    $fields_transient_key =
        'immf_fields_input_'
        . get_current_user_id()
        . '_'
        . $post->ID;

    $submitted_fields = get_transient(
        $fields_transient_key
    );

    if (is_array($submitted_fields)) {

        $fields = $submitted_fields;

        delete_transient(
            $fields_transient_key
        );
    }

    if (!is_array($fields)) {
        $fields = [];
    }

    $invalid_fields_key =
        'immf_invalid_fields_'
        . get_current_user_id()
        . '_'
        . $post->ID;

    $invalid_fields = get_transient(
        $invalid_fields_key
    );

    if (!is_array($invalid_fields)) {
        $invalid_fields = [];
    }

    delete_transient(
        $invalid_fields_key
    );

    /*----------------------------------------
    必須項目の表記を取得
    ----------------------------------------*/
    $required_label = get_post_meta(
        $post->ID,
        'immf_required_label',
        true
    );

    if (
        !metadata_exists(
            'post',
            $post->ID,
            'immf_required_label'
        )
    ) {
        $required_label = _x(
            '*',
            'Default required field label',
            'im-mail-form'
        );
    }

    $use_confirm = get_post_meta(
        $post->ID,
        'immf_use_confirm',
        true
    );

    $confirm_button_label = get_post_meta(
        $post->ID,
        'immf_confirm_button_label',
        true
    );

    if (
        !metadata_exists(
            'post',
            $post->ID,
            'immf_confirm_button_label'
        )
    ) {
        $confirm_button_label = __(
            'Confirm',
            'im-mail-form'
        );
    }

    $submit_button_label = get_post_meta(
        $post->ID,
        'immf_submit_button_label',
        true
    );

    if (
        !metadata_exists(
            'post',
            $post->ID,
            'immf_submit_button_label'
        )
    ) {
        $submit_button_label = __(
            'Submit',
            'im-mail-form'
        );
    }

    ?>

    <div
        class="im-mail-form-admin"
        data-contact-form-builder
        data-required-label="<?php echo esc_attr($required_label); ?>"
    >

        <h3 class="im-mail-form-admin__group-title">
            <?php esc_html_e(
                'Frequently Used Fields',
                'im-mail-form'
            ); ?>
        </h3>
        <div class="im-mail-form-admin__presets">

            <button
                type="button"
                class="button button-primary"
                data-add-preset="name"
            >
                <?php esc_html_e(
                    'Name',
                    'im-mail-form'
                ); ?>
            </button>

            <button
                type="button"
                class="button button-primary"
                data-add-preset="gender"
            >
                <?php esc_html_e(
                    'Gender',
                    'im-mail-form'
                ); ?>
            </button>

            <button
                type="button"
                class="button button-primary"
                data-add-preset="email"
            >
                <?php esc_html_e(
                    'Email Address',
                    'im-mail-form'
                ); ?>
            </button>

            <button
                type="button"
                class="button button-primary"
                data-add-preset="postalCode"
            >
                <?php esc_html_e(
                    'Postal Code',
                    'im-mail-form'
                ); ?>
            </button>

            <button
                type="button"
                class="button button-primary"
                data-add-preset="address"
            >
                <?php esc_html_e(
                    'Address',
                    'im-mail-form'
                ); ?>
            </button>

            <button
                type="button"
                class="button button-primary"
                data-add-preset="tel"
            >
                <?php esc_html_e(
                    'Phone Number',
                    'im-mail-form'
                ); ?>
            </button>

            <button
                type="button"
                class="button button-primary"
                data-add-preset="date"
            >
                <?php esc_html_e(
                    'Preferred Date',
                    'im-mail-form'
                ); ?>
            </button>

            <button
                type="button"
                class="button button-primary"
                data-add-preset="message"
            >
                <?php esc_html_e(
                    'Message',
                    'im-mail-form'
                ); ?>
            </button>
        </div>

        <h3 class="im-mail-form-admin__group-title">
            <?php esc_html_e(
                'Input Types',
                'im-mail-form'
            ); ?>
        </h3>
        <div class="im-mail-form-admin__types">

            <button
                type="button"
                class="button button-secondary"
                data-add-custom-field
            >
                <?php esc_html_e(
                    'Text',
                    'im-mail-form'
                ); ?>
            </button>

            <button
                type="button"
                class="button button-secondary"
                data-add-custom-field="textarea"
            >
                <?php esc_html_e(
                    'Textarea',
                    'im-mail-form'
                ); ?>
            </button>

            <button
                type="button"
                class="button button-secondary"
                data-add-custom-field="select"
            >
                <?php esc_html_e(
                    'Select Box',
                    'im-mail-form'
                ); ?>
            </button>

            <button
                type="button"
                class="button button-secondary"
                data-add-custom-field="checkbox"
            >
                <?php esc_html_e(
                    'Checkbox',
                    'im-mail-form'
                ); ?>
            </button>

            <button
                type="button"
                class="button button-secondary"
                data-add-custom-field="radio"
            >
                <?php esc_html_e(
                    'Radio Button',
                    'im-mail-form'
                ); ?>
            </button>

            <button
                type="button"
                class="button button-secondary"
                data-add-custom-field="date"
            >
                <?php esc_html_e(
                    'Date',
                    'im-mail-form'
                ); ?>
            </button>

            <button
                type="button"
                class="button button-secondary"
                data-add-custom-field="file"
            >
                <?php esc_html_e(
                    'File Upload',
                    'im-mail-form'
                ); ?>
            </button>

            <button
                type="button"
                class="button button-secondary"
                data-add-custom-field="hidden"
            >
                <?php esc_html_e(
                    'Hidden Field',
                    'im-mail-form'
                ); ?>
            </button>

            <button
                type="button"
                class="button button-secondary"
                data-add-custom-field="html"
            >
                <?php esc_html_e(
                    'HTML',
                    'im-mail-form'
                ); ?>
            </button>
        </div>

        <div
            class="im-mail-form-admin__fields"
            data-fields-container
        >

            <?php foreach ($fields as $index => $field) : ?>

                <?php
                immf_render_admin_field(
                    $index,
                    $field,
                    $invalid_fields[$index] ?? [],
                    $required_label
                );
                ?>

            <?php endforeach; ?>

            <div
                class="im-mail-form-admin__preview-actions"
                data-field-preview-actions
                <?php echo empty($fields) ? 'hidden' : ''; ?>
            >
                <button
                    type="button"
                    class="im-mail-form-admin__preview-submit"
                    tabindex="-1"
                    aria-hidden="true"
                >
                    <?php
                    echo esc_html(
                        $use_confirm === '1'
                            ? $confirm_button_label
                            : $submit_button_label
                    );
                    ?>
                </button>
            </div>

        </div>

    </div>

    <?php
}


/*----------------------------------------
メールフォーム：管理画面用フィールド出力
----------------------------------------*/
function immf_render_admin_field(
    $index,
    $field = [],
    $invalid_field_names = [],
    $required_label = '*'
) {

    if (!is_array($invalid_field_names)) {
        $invalid_field_names = [];
    }

    $field = wp_parse_args(
        $field,
        [
            'label'        => '',
            'description'  => '',
            'name'         => '',
            'placeholder' => '',
            'type'         => 'text',
            'validation'   => '',
            'required'     => '0',
            'min_length'   => '',
            'max_length'   => '',
            'hidden_value' => '',
            'max_file_size'     => '',
            'allowed_file_types' => [],
            'options'      => '',
            'html_content' => '',
        ]
    );

    // フォームの種類
    $types = [
        'text'     => __(
            'Text',
            'im-mail-form'
        ),
        'email'    => __(
            'Email Address',
            'im-mail-form'
        ),
        'tel'      => __(
            'Phone Number',
            'im-mail-form'
        ),
        'textarea' => __(
            'Textarea',
            'im-mail-form'
        ),
        'select'   => __(
            'Select Box',
            'im-mail-form'
        ),
        'radio'    => __(
            'Radio Button',
            'im-mail-form'
        ),
        'checkbox' => __(
            'Checkbox',
            'im-mail-form'
        ),
        'date'     => __(
            'Date',
            'im-mail-form'
        ),
        'file'     => __(
            'File Upload',
            'im-mail-form'
        ),
        'hidden'   => __(
            'Hidden Field',
            'im-mail-form'
        ),
        'html'     => __(
            'HTML',
            'im-mail-form'
        ),
    ];

    // 許可するバリデーション設定
    $validations = [
        ''             => __(
            'None',
            'im-mail-form'
        ),
        'number'       => __(
            'Numbers only',
            'im-mail-form'
        ),
        'alphanumeric' => __(
            'Letters, numbers, and symbols (- _ . @)',
            'im-mail-form'
        ),
    ];

    //許可するファイル形式
    $file_types = [
        'jpeg' => 'JPG / JPEG',
        'png'  => 'PNG',
        'webp' => 'WebP',
        'pdf'  => 'PDF',
    ];
    ?>

    <div
        class="im-mail-form-admin__field<?php
        echo !empty($invalid_field_names)
            ? ' im-mail-form-admin__field--error'
            : '';
        ?>"
        data-field-item
        draggable="true"
    >

    <div class="im-mail-form-admin__field-header">

        <div
            class="im-mail-form-admin__field-title"
            data-field-title
        >
            <button
                type="button"
                class="im-mail-form-admin__toggle"
                aria-label="<?php esc_attr_e(
                    'Open field settings',
                    'im-mail-form'
                ); ?>"
                title="<?php esc_attr_e(
                    'Open field settings',
                    'im-mail-form'
                ); ?>"
                aria-expanded="false"
                data-toggle-field
            >
                <span class="im-mail-form-admin__field-title-label">
                    <span data-field-title-label>
                        <?php
                        echo esc_html(
                            match ($field['type']) {
                                'html' => __(
                                    'HTML Insert',
                                    'im-mail-form'
                                ),
                                'hidden' => __(
                                    'Hidden Field',
                                    'im-mail-form'
                                ),
                                default => $field['label'] ?: __(
                                    'Untitled Field',
                                    'im-mail-form'
                                ),
                            }
                        );
                        ?>
                    </span>

                    <span
                        class="im-mail-form-admin__required"
                        aria-label="<?php esc_attr_e(
                            'Required',
                            'im-mail-form'
                        ); ?>"
                        data-field-required-mark
                        <?php echo empty($field['required']) ? 'hidden' : ''; ?>
                    ><?php echo esc_html($required_label); ?></span>
                </span>
            </button>

            <?php
            /*----------------------------------------
            入力プレビュー（基本入力タイプのみ）
            公開フォームの初期表示を確認するための
            管理画面プレビュー専用の要素であり、
            name属性を持たせず送信対象にはしない。
            ----------------------------------------*/
            $preview_types = [
                'text',
                'email',
                'tel',
                'textarea',
                'select',
                'radio',
                'checkbox',
                'file',
                'date',
            ];

            $preview_option_labels = [];

            if (
                in_array(
                    $field['type'],
                    [
                        'select',
                        'radio',
                        'checkbox',
                    ],
                    true
                )
            ) {
                $preview_options = preg_split(
                    '/\r\n|\r|\n/',
                    $field['options'] ?? ''
                ) ?: [];

                foreach ($preview_options as $preview_option) {
                    $preview_option = trim($preview_option);

                    if ($preview_option === '') {
                        continue;
                    }

                    $preview_parts = array_map(
                        'trim',
                        explode(
                            '|',
                            $preview_option,
                            2
                        )
                    );

                    $preview_option_labels[] =
                        $preview_parts[1]
                        ?? $preview_parts[0];
                }
            }

            if (
                in_array(
                    $field['type'],
                    $preview_types,
                    true
                )
            ) :
            ?>

                <span
                    class="im-mail-form-admin__field-preview"
                    data-field-preview
                >

                    <?php if ($field['type'] === 'textarea') : ?>

                        <textarea
                            class="im-mail-form-admin__preview-input"
                            placeholder="<?php echo esc_attr($field['placeholder']); ?>"
                            rows="1"
                            tabindex="-1"
                            aria-hidden="true"
                        ></textarea>

                    <?php elseif ($field['type'] === 'select') : ?>

                        <select
                            class="im-mail-form-admin__preview-input"
                            tabindex="-1"
                            aria-hidden="true"
                        >
                            <option>
                                <?php
                                echo esc_html(
                                    $field['placeholder'] !== ''
                                        ? $field['placeholder']
                                        : __(
                                            'Please select',
                                            'im-mail-form'
                                        )
                                );
                                ?>
                            </option>

                            <?php foreach ($preview_option_labels as $preview_option_label) : ?>

                                <option>
                                    <?php echo esc_html($preview_option_label); ?>
                                </option>

                            <?php endforeach; ?>
                        </select>

                    <?php elseif ($field['type'] === 'radio') : ?>

                        <span
                            class="im-mail-form-admin__preview-choices"
                            aria-hidden="true"
                        >

                            <?php foreach ($preview_option_labels as $preview_option_label) : ?>

                                <label
                                    class="im-mail-form-admin__preview-choice"
                                >
                                    <input
                                        type="radio"
                                        tabindex="-1"
                                    >
                                    <span>
                                        <?php echo esc_html($preview_option_label); ?>
                                    </span>
                                </label>

                            <?php endforeach; ?>

                        </span>

                    <?php elseif ($field['type'] === 'checkbox') : ?>

                        <span
                            class="im-mail-form-admin__preview-choices"
                            aria-hidden="true"
                        >

                            <?php foreach ($preview_option_labels as $preview_option_label) : ?>

                                <label
                                    class="im-mail-form-admin__preview-choice"
                                >
                                    <input
                                        type="checkbox"
                                        tabindex="-1"
                                    >
                                    <span>
                                        <?php echo esc_html($preview_option_label); ?>
                                    </span>
                                </label>

                            <?php endforeach; ?>

                        </span>

                    <?php elseif ($field['type'] === 'file') : ?>

                        <input
                            type="file"
                            class="im-mail-form-admin__preview-file"
                            tabindex="-1"
                            aria-hidden="true"
                        >

                    <?php else : ?>

                        <input
                            type="<?php echo esc_attr($field['type']); ?>"
                            class="im-mail-form-admin__preview-input"
                            placeholder="<?php echo esc_attr($field['placeholder']); ?>"
                            tabindex="-1"
                            aria-hidden="true"
                        >

                    <?php endif; ?>

                </span>

                <?php if ($field['description'] !== '') : ?>

                    <p
                        class="im-mail-form-admin__preview-description"
                        data-field-preview-description
                    ><?php echo nl2br(esc_html($field['description'])); ?></p>

                <?php endif; ?>

            <?php endif; ?>
        </div>

        <div class="im-mail-form-admin__field-actions">

            <button
                type="button"
                class="im-mail-form-admin__drag-handle"
                aria-label="<?php esc_attr_e(
                    'Reorder field',
                    'im-mail-form'
                ); ?>"
                title="<?php esc_attr_e(
                    'Drag to reorder',
                    'im-mail-form'
                ); ?>"
                data-drag-handle
            ></button>

            <button
                type="button"
                class="im-mail-form-admin__edit"
                aria-label="<?php esc_attr_e(
                    'Open field settings',
                    'im-mail-form'
                ); ?>"
                title="<?php esc_attr_e(
                    'Open field settings',
                    'im-mail-form'
                ); ?>"
                aria-expanded="false"
                data-toggle-field
            >
                <span
                    class="dashicons dashicons-edit"
                    aria-hidden="true"
                ></span>
            </button>

            <button
                type="button"
                class="im-mail-form-admin__delete"
                aria-label="<?php esc_attr_e(
                    'Delete field',
                    'im-mail-form'
                ); ?>"
                title="<?php esc_attr_e(
                    'Delete field',
                    'im-mail-form'
                ); ?>"
                data-remove-field
            >
                <span
                    class="dashicons dashicons-trash"
                    aria-hidden="true"
                ></span>
            </button>

        </div>

    </div>

    <div
        class="im-mail-form-admin__field-body"
        data-field-body
        hidden
    >

    <div
        class="im-mail-form-admin__row"
        data-label-field
    >

        <label
            for="im-mail-form-field-label-<?php echo esc_attr($index); ?>"
            class="im-mail-form-admin__label"
        >
            <?php esc_html_e(
                'Field Label',
                'im-mail-form'
            ); ?>
            <span
                class="im-mail-form-admin__required"
                aria-label="<?php esc_attr_e(
                    'Required',
                    'im-mail-form'
                ); ?>"
            >*</span>
        </label>

        <input
            type="text"
            id="im-mail-form-field-label-<?php echo esc_attr($index); ?>"
            name="im_mail_form_fields[<?php echo esc_attr($index); ?>][label]"
            value="<?php echo esc_attr($field['label']); ?>"
            class="widefat<?php
            echo in_array(
                'label',
                $invalid_field_names,
                true
            )
                ? ' im-mail-form-admin__input--error'
                : '';
            ?>"
            data-field-label
            maxlength="50"
            required
        >

    </div>


    <div
        class="im-mail-form-admin__row"
        data-description-field
    >

        <label
            for="im-mail-form-field-description-<?php echo esc_attr($index); ?>"
            class="im-mail-form-admin__label"
        >
            <?php esc_html_e(
                'Description',
                'im-mail-form'
            ); ?>
        </label>

        <div>

            <textarea
                id="im-mail-form-field-description-<?php echo esc_attr($index); ?>"
                name="im_mail_form_fields[<?php echo esc_attr($index); ?>][description]"
                rows="3"
                class="widefat"
                maxlength="200"
                placeholder="<?php esc_attr_e(
                    'Optional instructions or notes',
                    'im-mail-form'
                ); ?>"
            ><?php echo esc_textarea($field['description']); ?></textarea>

        </div>

    </div>

    <div
        class="im-mail-form-admin__row"
        data-name-field
    >

        <label
            for="im-mail-form-field-name-<?php echo esc_attr($index); ?>"
            class="im-mail-form-admin__label"
        >
            <?php esc_html_e(
                'Field Name',
                'im-mail-form'
            ); ?>
            <span
                class="im-mail-form-admin__required"
                aria-label="<?php esc_attr_e(
                    'Required',
                    'im-mail-form'
                ); ?>"
            >*</span>
        </label>

        <input
            type="text"
            id="im-mail-form-field-name-<?php echo esc_attr($index); ?>"
            name="im_mail_form_fields[<?php echo esc_attr($index); ?>][name]"
            value="<?php echo esc_attr($field['name']); ?>"
            class="widefat<?php
            echo in_array(
                'name',
                $invalid_field_names,
                true
            )
                ? ' im-mail-form-admin__input--error'
                : '';
            ?>"
            placeholder="<?php esc_attr_e(
                'e.g. name',
                'im-mail-form'
            ); ?>"
            title="<?php esc_attr_e(
                'Use lowercase letters, numbers, hyphens, and underscores.',
                'im-mail-form'
            ); ?>"
            pattern="[a-z0-9_\-]+"
            maxlength="20"
            required
        >

    </div>


    <div class="im-mail-form-admin__row">

        <label
            for="im-mail-form-field-type-<?php echo esc_attr($index); ?>"
            class="im-mail-form-admin__label"
        >
            <?php esc_html_e(
                'Field Type',
                'im-mail-form'
            ); ?>
        </label>

        <select
            id="im-mail-form-field-type-<?php echo esc_attr($index); ?>"
            name="im_mail_form_fields[<?php echo esc_attr($index); ?>][type]"
            class="widefat"
        >

            <?php foreach ($types as $value => $label) : ?>

                <option
                    value="<?php echo esc_attr($value); ?>"
                    <?php selected($field['type'], $value); ?>
                >
                    <?php echo esc_html($label); ?>
                </option>

            <?php endforeach; ?>

        </select>

    </div>

    <div
        class="im-mail-form-admin__row"
        data-html-content-field
    >

        <label
            for="im-mail-form-field-html-content-<?php echo esc_attr($index); ?>"
            class="im-mail-form-admin__label"
        >
            <?php esc_html_e(
                'HTML',
                'im-mail-form'
            ); ?>
            <span
                class="im-mail-form-admin__required"
                aria-label="<?php esc_attr_e(
                    'Required',
                    'im-mail-form'
                ); ?>"
            >*</span>
        </label>

        <div>

            <textarea
                id="im-mail-form-field-html-content-<?php echo esc_attr($index); ?>"
                name="im_mail_form_fields[<?php echo esc_attr($index); ?>][html_content]"
                rows="6"
                class="widefat code<?php
                echo in_array(
                    'html_content',
                    $invalid_field_names,
                    true
                )
                    ? ' im-mail-form-admin__input--error'
                    : '';
                ?>"
                spellcheck="false"
            ><?php echo esc_textarea($field['html_content']); ?></textarea>

            <p class="description">
                <?php esc_html_e(
                    'Enter the HTML to display at this position in the form. Scripts and other unsafe HTML are not allowed.',
                    'im-mail-form'
                ); ?>
            </p>

        </div>

    </div>

    <div
        class="im-mail-form-admin__row"
        data-placeholder-field
    >

        <label
            for="im-mail-form-field-placeholder-<?php echo esc_attr($index); ?>"
            class="im-mail-form-admin__label"
            data-placeholder-label
        >
            <?php
            echo esc_html(
                $field['type'] === 'select'
                    ? __(
                        'Empty Option Label',
                        'im-mail-form'
                    )
                    : __(
                        'Placeholder',
                        'im-mail-form'
                    )
            );
            ?>
        </label>

        <div>

            <input
                type="text"
                id="im-mail-form-field-placeholder-<?php echo esc_attr($index); ?>"
                name="im_mail_form_fields[<?php echo esc_attr($index); ?>][placeholder]"
                value="<?php echo esc_attr($field['placeholder'] ?? ''); ?>"
                class="widefat"
                maxlength="200"
            >

        </div>

    </div>

    <div
        class="im-mail-form-admin__row"
        data-hidden-value-field
    >
        <label
            for="im-mail-form-field-hidden-value-<?php echo esc_attr($index); ?>"
            class="im-mail-form-admin__label"
        >
            <?php esc_html_e(
                'Hidden Value',
                'im-mail-form'
            ); ?>
        </label>

        <input
            type="text"
            id="im-mail-form-field-hidden-value-<?php echo esc_attr($index); ?>"
            name="im_mail_form_fields[<?php echo esc_attr($index); ?>][hidden_value]"
            value="<?php echo esc_attr($field['hidden_value']); ?>"
            class="widefat"
            placeholder="<?php esc_attr_e(
                'Enter the fixed value to submit',
                'im-mail-form'
            ); ?>"
        >
    </div>

    <div
        class="im-mail-form-admin__row"
        data-validation-field
    >

        <label
            for="im-mail-form-field-validation-<?php echo esc_attr($index); ?>"
            class="im-mail-form-admin__label"
        >
            <?php esc_html_e(
                'Validation',
                'im-mail-form'
            ); ?>
        </label>

        <select
            id="im-mail-form-field-validation-<?php echo esc_attr($index); ?>"
            name="im_mail_form_fields[<?php echo esc_attr($index); ?>][validation]"
            class="widefat"
        >

            <?php foreach ($validations as $value => $label) : ?>

                <option
                    value="<?php echo esc_attr($value); ?>"
                    <?php selected($field['validation'], $value); ?>
                >
                    <?php echo esc_html($label); ?>
                </option>

            <?php endforeach; ?>

        </select>

    </div>

    <div
        class="im-mail-form-admin__row"
        data-length-field
    >

        <span class="im-mail-form-admin__label">
            <?php esc_html_e(
                'Character Limits',
                'im-mail-form'
            ); ?>
        </span>

        <div class="im-mail-form-admin__length-fields">

            <label>
                <?php esc_html_e(
                    'Minimum',
                    'im-mail-form'
                ); ?>
                <input
                    type="number"
                    name="im_mail_form_fields[<?php echo esc_attr($index); ?>][min_length]"
                    value="<?php echo esc_attr($field['min_length']); ?>"
                    class="small-text"
                    min="1"
                    step="1"
                >
            </label>

            <label>
                <?php esc_html_e(
                    'Maximum',
                    'im-mail-form'
                ); ?>
                <input
                    type="number"
                    name="im_mail_form_fields[<?php echo esc_attr($index); ?>][max_length]"
                    value="<?php echo esc_attr($field['max_length']); ?>"
                    class="small-text<?php
                    echo in_array(
                        'max_length',
                        $invalid_field_names,
                        true
                    )
                        ? ' im-mail-form-admin__input--error'
                        : '';
                    ?>"
                    min="1"
                    step="1"
                >
            </label>

        </div>

    </div>

    <div
        class="im-mail-form-admin__row"
        data-max-file-size-field
    >

        <label
            for="im-mail-form-field-max-file-size-<?php echo esc_attr($index); ?>"
            class="im-mail-form-admin__label"
        >
            <?php esc_html_e(
                'Maximum File Size',
                'im-mail-form'
            ); ?>
        </label>

        <div>

            <?php
            $wordpress_max_file_size = max(
                1,
                (int) floor(
                    wp_max_upload_size() / MB_IN_BYTES
                )
            );
            ?>

            <input
                type="number"
                id="im-mail-form-field-max-file-size-<?php echo esc_attr($index); ?>"
                name="im_mail_form_fields[<?php echo esc_attr($index); ?>][max_file_size]"
                value="<?php echo esc_attr($field['max_file_size']); ?>"
                class="small-text"
                min="1"
                max="<?php echo esc_attr($wordpress_max_file_size); ?>"
                step="1"
            >

            MB&nbsp;&nbsp;&nbsp;
            <span class="description">
                <?php
                printf(
                    /* translators: %s: WordPress upload limit in megabytes. */
                    esc_html__(
                        'The value cannot exceed the WordPress upload limit (%s MB).',
                        'im-mail-form'
                    ),
                    esc_html($wordpress_max_file_size)
                );
                ?>
            </span>
        </div>

    </div>

    <div
        class="im-mail-form-admin__row"
        data-file-types-field
    >

        <span class="im-mail-form-admin__label">
            <?php esc_html_e(
                'Allowed File Types',
                'im-mail-form'
            ); ?>
        </span>

        <div class="im-mail-form-admin__file-types">

            <?php foreach ($file_types as $value => $label) : ?>

                <label>

                    <input
                        type="checkbox"
                        name="im_mail_form_fields[<?php echo esc_attr($index); ?>][allowed_file_types][]"
                        value="<?php echo esc_attr($value); ?>"
                        <?php checked(
                            in_array(
                                $value,
                                $field['allowed_file_types'],
                                true
                            )
                        ); ?>
                    >

                    <?php echo esc_html($label); ?>

                </label>

            <?php endforeach; ?>

        </div>

    </div>

    <div 
        class="im-mail-form-admin__row"
        data-required-field
    >

        <span class="im-mail-form-admin__label">
            <?php esc_html_e(
                'Required Setting',
                'im-mail-form'
            ); ?>
        </span>

        <label>

            <input
                type="checkbox"
                name="im_mail_form_fields[<?php echo esc_attr($index); ?>][required]"
                value="1"
                <?php checked($field['required'], '1'); ?>
            >

            <?php esc_html_e(
                'Make this field required',
                'im-mail-form'
            ); ?>

        </label>

    </div>


    <div
        class="im-mail-form-admin__row"
        data-options-field
    >

        <label
            for="im-mail-form-field-options-<?php echo esc_attr($index); ?>"
            class="im-mail-form-admin__label"
        >
            <?php esc_html_e(
                'Options',
                'im-mail-form'
            ); ?>
            <span
                class="im-mail-form-admin__required"
                aria-label="<?php esc_attr_e(
                    'Required',
                    'im-mail-form'
                ); ?>"
            >*</span>
        </label>

        <div>

            <textarea
                id="im-mail-form-field-options-<?php echo esc_attr($index); ?>"
                name="im_mail_form_fields[<?php echo esc_attr($index); ?>][options]"
                rows="4"
                class="widefat<?php
                echo in_array(
                    'options',
                    $invalid_field_names,
                    true
                )
                    ? ' im-mail-form-admin__input--error'
                    : '';
                ?>"
                placeholder="<?php esc_attr_e(
                    'Enter one option per line',
                    'im-mail-form'
                ); ?>"
            ><?php echo esc_textarea($field['options']); ?></textarea>

            <p class="description">
                <?php esc_html_e(
                    'Enter one option per line.',
                    'im-mail-form'
                ); ?>
                <?php esc_html_e(
                    'To use separate submitted values and labels, use this format:',
                    'im-mail-form'
                ); ?>
                <code class="im-mail-form-admin__option-format"><?php
                    esc_html_e(
                        'value | label',
                        'im-mail-form'
                    );
                ?></code>
            </p>

        </div>

    </div>

</div>

    </div>

    <?php
}
